How to parse this data from external devices

Novice, please,
How to parse the data from the external device? Use the analog serial port to receive the serial data from the STM32. The data content is FA E0 10 00 67 02 A8 0A 18 19 F3 FF A2 F3 AE 16 25 0E 10 EC AF D1 AD 9B

FA E0 10 00 will never change, but the data of the fifth and sixth bytes will change. How to capture the contents of the two byte positions 67 02? And convert it to a decimal value. (For example, 67 02 is converted, the two bytes need to be exchanged to become 02 67, and the final decimal result is 615)

The code I currently use is

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
  Serial.begin(115200);
  mySerial.begin(115200);
}
 while (mySerial.available() > 0)
 {
   Serial.write(mySerial.read());
 }

Good background information in this Serial Input Basics tutorial.

If you store all those data bytes in a character array, with the 0xFA byte in element zero, then the data of interest can be rearranged as an integer thusly:

int data = (array[5]<<8) | array[4];

consider the following which simulates receiving a byte stream and synchronizes to the 4 header bytes

#define BUF_SIZE    10
byte      buf [10];
unsigned  bufIdx = 0;

const byte Hdr [] = { 0xFA, 0xE0, 0x10, 0x00 };

void loop  ()
{
    if (available ())  {
        byte c = read();

        Serial.println (c, HEX);

        if (sizeof(Hdr) > bufIdx && Hdr [bufIdx] != c)
            return;

        buf [bufIdx++] = c;
        if (BUF_SIZE == bufIdx)  {
            bufIdx = 0;
            processBuf ();
        }
    }
}

// -------------------------------------
void
processBuf ()
{
    int val = (buf [5] << 8) + buf [4];

    Serial.print   ("processBuf: ");
    Serial.print   (val);
    Serial.print   (", Ox");
    Serial.println (val, HEX);
}

// -----------------------------------------------------------------------------

const byte Data [] = {
    0xA2, 0xF3, 0xAE, 0x16, 0x25, 0x0E, 0x10,
    0xFA, 0xE0, 0x10, 0x00, 0x67, 0x02, 0xA8, 0x0A, 0x18, 0x19, 0xF3, 0xFF,
    0xA2, 0xF3, 0xAE, 0x16, 0x25, 0x0E, 0x10, 0xEC, 0xAF, 0xD1, 0xAD, 0x9B
};

unsigned dataIdx = 0;

// -------------------------------------
int
available (void)
{
    return dataIdx < sizeof(Data);
}

// -------------------------------------
byte
read (void)
{
    return Data [dataIdx++];
}

// -----------------------------------------------------------------------------
void setup  ()
{
    Serial.begin  (9600);
}

@gcjr That is a really neat solution. My only suggestion is

if (sizeof(Hdr) > bufIdx && Hdr [bufIdx] != c) {
    bufIdx = 0;
    return;
}

Just for interest I tried to do a String version, since Strings can store 0x00,
but it turns out that Stream.cpp has a lot of bugs in its methods that prevent it handling char > 0x80

read() is fine, but readBytes(), timedRead, the parse methods and find methods and readString methods all fail on char > 0x80

One of the consequences of this is that readString will not work with UTF-8 input

If anyone wants a corrected version of Stream.cpp it is available from my Taming Arduino Strings tutorial.
Edit -- My mistake, String is fine reading >0x80 and reading UTF-8

On Mega, Uno and some other boards SoftwareSerial is limited to 19200.
I did not read any clear spec about the board you are using, but beware of this.
I have not seen any references of SoftwareSerial working properly on 115200.

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