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

In my previous project, I always set my baud rate at 9600, and for my ongoing project BAUD_RATE = 9600 also works. The thing is, when I switch the BAUD_RATE = 19200 or bigger value, it start to behave weird.

In this project, I use a MLX90393(Hall sensor) module collects data and sends the data to Arduino Mega via I2C connection, then Arduino will write out the received data to Serial Port which connect with PC. Finally PC will re-encode and analysis the data by Python (Software I use is Spyder in Anaconda, ver 5.1.5 ).

The data form I was expecting to receive is E???????S (not yet re-encode).

  • Those 7 ?marks are the data I need.
  • 'E' and 'S' are start and end signal for PC respectively.

The thing is, if I change the baud rate at 19200. Sometimes I can only get a 'ES' reply in Serial Port in the vary first loop, and there's no any data included in. Furthermore, whenever I try to change the baud rate over 19600, I get no reply in the Serial Port, and 'ES' didn’t even show up. I've checked it on both PC's COM Port and Arduino's Serial Monitor.

Is this the sensor's hardware limitations? Can any body help with me?
Here's my code:

#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);
      }
    }
  }
}

Have you changed the baud rate in the Serial monitor to match the value used in Serial.begin() ?

I did change it with different baud rate settings