0

I'am deconding one electronic device serial protocol and using ESP8266 for this task. Device is sending 202 byte data constantly and I figured out begining and the end.

Whole packet looks like:

71 c8 01 10 56 55 62 49 00 05 00 00 00 00 00 00 00 00 00 00 19 15 11 55 16 5e 55 05 09 00 00 00 00 00 00 00 00 00 80 8f 80 8a b2 71 71 97 99 00 00 00 00 00 00 00 00 00 00 00 80 85 15 8a 85 85 d0 7b 78 1f 7e 1f 1f 79 79 8d 8d 9e 96 71 8f b7 a3 7b 8f 8e 85 80 8f 8a 94 9e 8a 8a 94 9e 82 90 8b 05 65 78 c1 0b 00 00 00 00 00 00 00 00 55 56 55 21 53 15 5a 05 12 12 19 00 00 00 00 00 00 00 00 e2 ce 0d 71 81 72 ce 0c 92 81 b0 00 aa 7c ab b0 32 32 9c b6 32 32 32 80 b7 af cd 9a ac 79 80 77 80 ff 91 01 29 59 00 00 3b 0b 1c 51 59 01 36 79 01 01 c3 02 00 dd 02 00 05 00 00 01 00 00 06 01 01 01 01 01 0a 14 00 00 00 77

Here start "header" is 0x71 and 0xc8 and the last 202 byte is a Checksum value of hole 201 bytes.

Goal is to get valid 202 byte data by checking if start contains 0x71 and 0xc8

My readserial looks like, how it to improve in best way?

void readSerial()
{
  if (Serial.available() > 0) {
    data_length = Serial.readBytes(data, 202);
       while (Serial.available()) {
      delay(2);
      Serial.read();
    }
  }
  else data_length = 0;
}

0

First, I think the protocol is slightly different probably. The first byte is 71. The next byte seems to be the number of bytes that follow (C8 hex is 200). The last byte is the checksum.

So you need to check for 71, when that is received you can read the next byte (number of bytes), the data, and the checksum.

So the code should be something like:

void readSerial()
{
   if (Serial.available() > 0) 
   {
      uint8_t readByte = Serial.read(); // uint8_t = 8 bit unsigned integer
      if (readByte == 71)
      {
         // Read message
         data_length = Serial.read();
         data_length = Serial.readBytes(data, data_length);
         checksum = Serial.read();
         // Check checksum
      }
      else 
      {
         data_length = 0;
      }
   }
}

(note: code is untested)

About blocking prevention, you will have to read them one by one and doing other tasks meanwhile.

Update

Assuming reading starts while sender is already sending possibly, and assuming a message always starts with 71 C8.

Create an enumeration that shows how many 'start bytes' have passed (71 C8 are start bytes).

enum EStartByte
{
   WaitForMessage,
   FirstStartBytePassed,
   ReadMessage
};

enum EStartByte startByte = WaitForMessage;

void readSerial()
{
   while (Serial.available())
   {
      switch (startByte)
      {
      case WaitForMessage: 
          // Expect first start byte
          if (Serial.readByte() == 0x71)
          {
               startByte = FirstStartBytePassed; 
          }
          break;

      case FirstStartBytePassed: 
          // Expect second start byte
          if (Serial.readByte() == 0xC8)
          {
               startByte = ReadMessage; // Start reading
          }
          else
          {
               startByte = WaitForMessage;
          }
          break;

      case ReadMessage:
          // Read data
          data_length = Serial.readBytes(data, 200);
          checksum = Serial.read();
          // Check checksum

          startByte = WaitForMessage; // Start over
          break;
      }
   }
}
1
  • Comments are not for extended discussion; this conversation has been moved to chat. – VE7JRO Jan 5 '20 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.