I can't receive anything in Serial Port at different baud rate

我之前寫程式的習慣都會將鮑率設置為 9600,在我目前正在進行的項目中,將鮑率設定為9600 也能夠正常運作。問題是當我把鮑率換成19200 或是更大的值時,它開始變得很奇怪。

我目前進行中這個項目,我用一個 MLX90393模組(霍爾傳感器)收集數據,並將數據以I2C口發送到 Arduino Mega板,然後 Arduino Mega將接收到的數據藉由USB COM Port 傳送給電腦。最後電腦將用 Python 對數據解碼和分析(我使用 Anaconda 中的 Spyder,ver 5.1.5)。

我預期在Serial視窗接收到的數據形式是 E???????S(尚未解碼)。

  • 這中間7 個"?"是感測器所讀取到的數據,為解碼時通常以問號或其他亂碼呈現。
  • 'E' 和 'S' 分別是給電腦端Python讀取用的的開始與結束信號。

問題是,如果我將鮑率更改為 19200,有時會發生在第一次void loop循環中,Serial Port只會收到'ES', 'E' 和 'S' 之間原本應該包含的數據則不翼而飛。令外,若是我想將鮑率設在19200以上 ,Serial Port,甚至變得完全空白,收不到任何東西。我已經在 PC 的 COM Port和 Arduino 的序列埠監控視窗上檢查過,也有記得將電腦與序列埠監控視窗的鮑率改成和Arduino的相同。

這是 MLX90393傳感測器的硬體限制嗎?任何大師都可以幫助我嗎?

[EN ver: I can't receive replies in Serial Port at different baud rate]
這是我的程式碼:

#include <Wire.h>
const int baud_rate = 19200;
byte val = 0;
int reading = 0;
bool setup_flag = 0;
char str;
#define address1 0x0C //(00)

void setup()
{
  Wire.begin();
  Serial.begin(baud_rate);
  setup_flag = 0;
  delay(50);
  Wire.beginTransmission(address1);
  Wire.write(byte(0x1F));

  Wire.endTransmission();     // stop transmitting
  Wire.requestFrom(address1, 1);    // request 1 bytes from slave device 0001100
  delay(50);

  while (Wire.available()) {
    reading = Wire.read();
  }
  setup_flag = 1;
  // Serial.println("Set up ready.");
  delay(300);
}

void loop()
{
  if (Serial.available()) {
    // python sends value "2" to start the program.
    // Serial.println("\n2 get");
    str = Serial.read();
    if (str == '2') { // as start signal
      if (setup_flag == 1) {
        Serial.write(byte(0x45));  //for python, 0x45=69=E
        Wire.beginTransmission(address1);
        Wire.write(byte(0x4E));      // 01001110RM  sets register pointer to the command register (0x00)
        Wire.endTransmission();     // stop transmitting
        delay(5);
        Wire.requestFrom(address1, 7);    // request 7 bytes from slave device #112 status+x+y+z
        delay(5);
        // Send to PC
        while (Wire.available()) {
          reading = Wire.read();  // receive high byte (overwrites previous reading)
          Serial.write(reading);   // print the reading
        }
        Serial.write(byte(0x53)); //0x53=83=S
        delay(5);
      }
    }
  }
}


while (Wire.available()) {
  reading = Wire.read();  // receive high byte (overwrites previous reading)
  Serial.print(reading, HEX);   // print the reading in HEX format
  Serial.write(' ');
}

解碼這部分的程式碼我放在python那邊,改成在Arduino這裡先解碼我還是會出現一樣的問題

你能用 115200 波特的串行监视器试试这个吗?
(我不会说中文)

#include <Wire.h>
const byte deviceAddress = 0x0C; //(00)

void request(byte command, byte count) {
  Serial.print("Sending command 0x"); Serial.print(command); Serial.print(" --> ");
  Wire.beginTransmission(deviceAddress);
  Wire.write(command);
  Wire.endTransmission();     // stop transmitting
  delay(5);
  byte n = Wire.requestFrom(deviceAddress, count);    // request count bytes from slave device
  if (n != count) {
    Serial.print(n);
    Serial.println(" bytes response --> Bad answer from device");
  } else {
    while (Wire.available()) {
      Serial.print(Wire.read(), HEX); Serial.write(' ');
    }
    Serial.println();
  }
}

void setup() {
  Wire.begin();
  Serial.begin(115200);  // <=== NOTE SERIAL CONSOLE AT 115200 BAUDS
  request(0x1F, 1);
  request(0x4E, 7);
}

void loop() {}

不要使用python,只使用串行监视器

At first few tries it comes out like this:

Sending command 0x31 --> 90 
Sending command 0x78 --> 90 FF FF FF FF FF FF 

But if I press the RESET button on the Arduino board too many times, it'll become like this:

Sending command 0x31 --> 90 
Sending command 0x78 --> 82 B9 1E B 64 A6 B5 
Sending command 0x31 --> 92 
Sending command 0x78 --> 0 bytes response --> Bad answer from device
Sending command 0x31 --> 92 
Sending command 0x78 --> 0 bytes response --> Bad answer from device
...

Once the first request returns '92', no matter how many time I hit the RESET button, it can only return '92' and 'Bad answers'. The only way to reboot is to unplug the USB and retry, but it will soon fall back to '92' again.

double check your I2C connection, pull-ups if needed and of course the spec of the MLX90393's datasheet to ensure you connected in the right way and are sending appropriate commands.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.