SerialPort Read missing data on ESP32

Hi everybody,

I am using ESP32-wroom-32e with Arduino IDE.

I have 2 sensors connected on serial port which send data as char array.

Sample Output of Sensor
0x68 0x1F 0x00 0x84 0x10 0x50 0x25 0x01 0x60 0x00 0x11 0x60 0x00 0x00 0x23 0x04 0x10 0x23 0x04 0x01 0x23 0x02 0x22 0x68 0x12 0x68 0x14 0x11 0x12 0x15 0x30

0x68 is the start char of message. and checksum is calculated without start of message.

I read all the values if serial available on port.


#define RX1 27
#define TX1 16
#define RX2 25
#define TX2 17
char c, c2;
char inChar1;
char inChar11;
char inChar2;
char inChar22;
String content1;
String content2;
byte msg1[31];
byte msg2[31];
char and1 = 0x0F;
char and2 = 0xF0;
char and3 = 0x10;
char Clearing[] = {0x68, 0x04, 0x00, 0x28, 0x2c};
float sensor1 = 0;
float sensor2 = 0, sensorvalue = 0;
byte Sumcrc1, Sumcrc2;






void setup() {
  Serial.begin(115200);
  Serial1.begin(115200, SERIAL_8N1, RX1, TX1);
  Serial2.begin(115200, SERIAL_8N1, RX2, TX2);
}

void loop() {



getser11();
getser22();


 
  Serial.print(sensor1);
  Serial.print("*****");
  Serial.print(sensor2);
  Serial.println("*****");

  if (Serial.available())
  {
    c = Serial.read();
    if (c == 'a')
    {
      ClearS1();
      ClearS2();
    }
  }
  delay(10);
}


float convertingvalue(char h1, char h2, char h3)
{
  unsigned long number;

  number = h1 & 0x0F;
  number = (number * 10) + (h2 >> 4);
  number = (number * 10) + (h2 & 0x0F);
  number = (number * 10) + (h3 >> 4);
  number = (number * 10) + (h3 & 0x0F);


  float value = number * 0.01;
  if ((h1 >> 4) == 1)
  {
    value = -value;
  }
  return value;

}

byte checksumcalc( byte c_data[])
{
  byte sum = 0;
  for (int i = 0; i < 31; i++)
  {
    sum = sum + c_data[i];

  }
  return sum;
}

void ClearS1()
{
  Serial1.write(Clearing[0]);
  Serial1.write(Clearing[1]);
  Serial1.write(Clearing[2]);
  Serial1.write(Clearing[3]);
  Serial1.write(Clearing[4]);

}
void ClearS2()
{
  Serial2.write(Clearing[0]);
  Serial2.write(Clearing[1]);
  Serial2.write(Clearing[2]);
  Serial2.write(Clearing[3]);
  Serial2.write(Clearing[4]);
}


void getser11()
{
  if (Serial1.available() > 0)
  {
    inChar1 = Serial1.read();

    inChar11 = Serial1.read();
    if (inChar1 == 0x68 && inChar11 == 0x1F) // start of frame
    {
      msg1[0] = inChar11;

      for (int inCharno = 1; inCharno < 31; inCharno++)
      {
        inChar1 = Serial1.read();
        //Serial.print(inChar1,HEX);
        msg1[inCharno] = inChar1;
      }

      //Serial.println();


    }

  }
  Sumcrc1 = checksumcalc(msg1);
  if (msg1[30] == Sumcrc1)
  {
    sensor1 = convertingvalue(msg1[9], msg1[10], msg1[11]);
  }
  else
  {

    Serial.println("sumcrc error1");

  }
}


void getser22()
{
  if (Serial2.available() > 0)
  {
    inChar2 = Serial2.read();

    inChar22 = Serial2.read();
    if (inChar2 == 0x68 && inChar22 == 0x1F) // start of frame
    {
      msg2[0] = inChar22;

      for (int inCharno1 = 1; inCharno1 < 31; inCharno1++)
      {
        inChar2 = Serial2.read();
        //Serial.print(inChar2,HEX);
        msg2[inCharno1] = inChar2;
      }

      //Serial.println();


    }

  }
  Sumcrc2 = checksumcalc(msg2);
  if (msg2[30] == Sumcrc2)
  {
    sensor2 = convertingvalue(msg2[9], msg2[10], msg2[11]);
  }
  else
  {

    Serial.println("sumcrc error2");

  }
}

After running code, sometimes, I got crc check error on my code.

I have also created another function to read sensor data without a loop

`void getser1()
{
  if (Serial1.available() > 0)
  {
    for (int ii = 0; ii < 31; ii++)
    {
      msg1[ii] = 0x00;
    }
    inChar1 = Serial1.read();
    if (inChar1 == 0x68) // start of frame
    {
      inChar1 = Serial1.read(); // Data Length
      if (inChar1 == 0x1F)
      {
        msg1[0] = inChar1; // Data length
        inChar1 = Serial1.read(); // Address
        msg1[1] = inChar1; // Adress
        inChar1 = Serial1.read(); // Command means data
        if (inChar1 == 0x84)
        {
          msg1[2] = inChar1; // command
          msg1[3] = Serial1.read(); // roll byte 1
          msg1[4] = Serial1.read(); // roll byte 2
          msg1[5] = Serial1.read(); // roll byte 3
          msg1[6] = Serial1.read();//pitch byte 1
          msg1[7] = Serial1.read();//pitch byte 2
          msg1[8] = Serial1.read();//pitch byte 3
          msg1[9] = Serial1.read();//heading byte 1
          msg1[10] = Serial1.read();//heading byte 2
          msg1[11] = Serial1.read();//heading byte 3
          msg1[12] = Serial1.read();//x acc byte 1
          msg1[13] = Serial1.read();// x acc byte 2
          msg1[14] = Serial1.read();// x acc byte 3
          msg1[15] = Serial1.read();// y acc byte 1
          msg1[16] = Serial1.read();// y acc byte 2
          msg1[17] = Serial1.read();// y acc byte 3
          msg1[18] = Serial1.read();// z acc byte 1
          msg1[19] = Serial1.read();// z acc byte 2
          msg1[20] = Serial1.read();// z acc byte 3
          msg1[21] = Serial1.read();// gyro x byte 1
          msg1[22] = Serial1.read();// gyro x byte 2
          msg1[23] = Serial1.read();// gyro x byte 3
          msg1[24] = Serial1.read();// gyro y byte 1
          msg1[25] = Serial1.read();// gyro y byte 2
          msg1[26] = Serial1.read();// gyro y byte 3
          msg1[27] = Serial1.read();// gyro z byte 1
          msg1[28] = Serial1.read();// gyro z byte 2
          msg1[29] = Serial1.read();// gyro z byte 3
          msg1[30] = Serial1.read();// checksum

        }

      }
      Sumcrc1 = checksumcalc(msg1);
      if (msg1[30] == Sumcrc1)
      {
        sensor1 = convertingvalue(msg1[9], msg1[10], msg1[11]);
      }
      else
      {

        Serial.println("sumcrc error1");

      }

    }

  }

}

void getser2()
{

  if (Serial2.available() > 0)
  {
    for (int ii = 0; ii < 31; ii++)
    {
      msg2[ii] = 0x00;
    }
    c2 = Serial2.read();
    if (c2 == 0x68) // start of frame
    {
      c2 = Serial2.read(); // Data Length
      if (c2 == 0x1F)
      {
        msg2[0] = c2; // Data length
        c2 = Serial2.read(); // Address
        msg2[1] = c2; // Adress
        c2 = Serial2.read(); // Command means data
        if (c2 == 0x84)
        {
          msg2[2] = c2; // command
          msg2[3] = Serial2.read(); // roll byte 1
          msg2[4] = Serial2.read(); // roll byte 2
          msg2[5] = Serial2.read(); // roll byte 3
          msg2[6] = Serial2.read();//pitch byte 1
          msg2[7] = Serial2.read();//pitch byte 2
          msg2[8] = Serial2.read();//pitch byte 3
          msg2[9] = Serial2.read();//heading byte 1
          msg2[10] = Serial2.read();//heading byte 2
          msg2[11] = Serial2.read();//heading byte 3
          msg2[12] = Serial2.read();//x acc byte 1
          msg2[13] = Serial2.read();// x acc byte 2
          msg2[14] = Serial2.read();// x acc byte 3
          msg2[15] = Serial2.read();// y acc byte 1
          msg2[16] = Serial2.read();// y acc byte 2
          msg2[17] = Serial2.read();// y acc byte 3
          msg2[18] = Serial2.read();// z acc byte 1
          msg2[19] = Serial2.read();// z acc byte 2
          msg2[20] = Serial2.read();// z acc byte 3
          msg2[21] = Serial2.read();// gyro x byte 1
          msg2[22] = Serial2.read();// gyro x byte 2
          msg2[23] = Serial2.read();// gyro x byte 3
          msg2[24] = Serial2.read();// gyro y byte 1
          msg2[25] = Serial2.read();// gyro y byte 2
          msg2[26] = Serial2.read();// gyro y byte 3
          msg2[27] = Serial2.read();// gyro z byte 1
          msg2[28] = Serial2.read();// gyro z byte 2
          msg2[29] = Serial2.read();// gyro z byte 3
          msg2[30] = Serial2.read();// checksum

        }

      }
      Sumcrc2 = checksumcalc(msg2);
      if (msg2[30] == Sumcrc2)
      {
        sensor2 = convertingvalue(msg2[9], msg2[10], msg2[11]);
      }
      else
      {
        Serial.println("sumcrc error2");

      }


    }

  }


}`

I couldn't figure out what is the problem. Am I doing a mistake on serial read or any other thing.

You check if any bytes are received but then you read 32 bytes without checking if the buffer already holds that amount of bytes.

With this code you may have as little as 1 byte available but you are trying to read 32 bytes.

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