Johnathan Au
Johnathan Au

Reputation: 5362

Is [0x02, 0x03, 0x00] valid data?

I am trying to write a program that returns valid data according to a message protocol within a byte[] array.

I have:

STX: 0x02
ETX: 0X03
DLE: 0x10 //Delimiter

Valid data is when the byte[] array contains STX, ETX, data and a correctly calculated LRC, for example:

byte[] validMsg1 = {0x02, // STX
        0x10,0x2,0xA,0x10,0x10,0x7,0x8, // Data 2 A 10 7 8
        0x03, // ETX
        0x2^0xA^0x10^0x7^0x8^0x03}; // LRC calculated from the data (with the DLE removed) plus the ETX 

Invalid data example:

byte[] invalidMsg1 = {0x02, // STX
        0x10,0x2,0xA, // Data 2 A
        0x03, // ETX
        0x2^0xA^0x10^0x7^0x8^0x03,
        0x02, //STX
        0x05,0x44};

The byte might also contain random values around the valid data:

byte[] validMsgWithRandomData1 = {0x32,0x32,0x32, //Random data
        0x02, // STX
        0x31,0x32,0x10,0x02,0x33, // Data 31 32 02 33
        0x03, // ETX
        0x31^0x32^0x02^0x33^0x03,// LRC calculated from the data (with the DLE removed) plus the ETX
        0x2,0x3,0x00,0x02 //Random data
        }; 

My problems is that when I use the message with random data in and have 0x2,0x3,0x00,0x02 it breaks because it sees 0x02 and 0x03 as the STX and ETX and then it calculates LRC which results in 0x03 which results in this being returned: 0x2,0x3,0x00 with the last 0x03 seens as the LRC.

Question is this valid data:

byte[] data = {
    0x02, // STX
    0x03, // ETX
    0x00, // LRC
}; 

I am supposed to return the most recent valid data which is that but there is better data within this which is:

byte[] validData = {
    0x02, // STX
    0x31,0x32,0x10,0x02,0x33, // Data 31 32 02 33
    0x03, // ETX
    0x31^0x32^0x02^0x33^0x03,// LRC calculated from the data (with the DLE removed) plus the ETX
}; 

Upvotes: 0

Views: 2005

Answers (0)

Related Questions