Serial Port data received Error

Hello everybody,

I have a motor driver which can be controlled over RS232 and using ESP32-wroom to control driver.

I have a command to control motor and a query command to check driver status.

I have to write "AB EC 10 00 00 00 E8 93" this is a command to control motor speed and position and after sending this command driver sends back a acknowledge data as 11 byte. After that I send data to query the motor position with a query command " AC 08 00 00 00 00 00 00" and this return the rotor position data with 6 byte data.

Here is the code


void loop()
{
  currentTime = millis();

  if (currentTime - lastTime >= 100)
  {
    lastTime = currentTime;

      ads.setMux(ADS1115_REG_CONFIG_MUX_SINGLE_0);
      ads.triggerConversion();
      AdcValue = ads.getConversion();



    Serial2.write(0xAB);
    Serial2.write(0xEC);
    Serial2.write(0x10);
    Serial2.write(0x00);
    Serial2.write(0x00);
    Serial2.write(0x00);
    Serial2.write(0xE8);
    Serial2.write(0x93);
    delay(5);
    if (Serial2.available() > 9)
    {
      for (int i = 0; i < 11; i++)
      {
        myinput = Serial2.read();
        MotorDatainput[i] = myinput;
      }

    }
    
    delay(25);
    Serial2.write(0xAC);
    Serial2.write(0x08);
    Serial2.write(0x00);
    Serial2.write(0x00);
    Serial2.write(0x00);
    Serial2.write(0x00);
    Serial2.write(0x00);
    Serial2.write(0x00);
    
    delay(25);
    if (Serial2.available() > 5)
    {
      Serial.print("****");
      MotorPos[0] =Serial2.read();
      MotorPos[1] =Serial2.read();
      MotorPos[2] =Serial2.read();
      MotorPos[3] =Serial2.read();
      MotorPos[4] =Serial2.read();
      MotorPos[5] =Serial2.read();
    }
    Serial.print("<");
    Serial.print(AdcValue);
    Serial.print("<");
    Serial.printf("%02x",MotorDatainput[0]);
    Serial.printf("%02x",MotorDatainput[1]);
    Serial.printf("%02x",MotorDatainput[2]);
    Serial.printf("%02x",MotorDatainput[3]);
    Serial.printf("%02x",MotorDatainput[4]);
    Serial.printf("%02x",MotorDatainput[5]);
    Serial.printf("%02x",MotorDatainput[6]);
    Serial.printf("%02x",MotorDatainput[7]);
    Serial.printf("%02x",MotorDatainput[8]);
    Serial.printf("%02x",MotorDatainput[9]);
    Serial.printf("%02x",MotorDatainput[10]);
    Serial.print("<");
    Serial.printf("%02x",MotorPos[0]);
    Serial.printf("%02x",MotorPos[1]);
    Serial.printf("%02x",MotorPos[2]);
    Serial.printf("%02x",MotorPos[3]);
    Serial.printf("%02x",MotorPos[4]);
    Serial.printf("%02x",MotorPos[5]);
    Serial.println();




  } 
}


the output should be

****<adcvalue<ABEB000001010000009C01<AC080000003B

but output is below:

****<adcvalue<AC080000003B0000009C01<ABEB00000101

what am I doing wrong? I could not figure out the error. I have to send data as wanted over udp and still stuck on getting data correctly.

Thanks

you check if at least 10 bytes are available but you read 11

    if (Serial2.available() > 9)
    {
      for (int i = 0; i < 11; i++)
      {
        myinput = Serial2.read();
        MotorDatainput[i] = myinput;
      }
    }

if you are unlucky the 11th byte did not arrive yet and as you empty the buffer very quickly and you might get 0xFF in MotorDatainput[10] (if there is nothing to read, read() will return -1)

if you are very unlucky the bytes are not all there and you skip the reading

Which means the missing byte is now is still unread in the incoming buffer which you'll get when you try to read the position

You should not second guess the timing of an asynchronous protocol. Just read the data when it comes and when you have the right count or meet an end marker, proceed with the next step

I would suggest to study Serial Input Basics to understand the fundamentals

For you next edit, code like that would be more aesthetically pleasing if you put those values in an array and called Serial.write() on the whole thing at one time.