0

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
}; 
Johnathan Au
  • 5,244
  • 18
  • 70
  • 128
  • all data is valid if you don't tell us what protocol you're talking about – bcrist Feb 02 '14 at 16:06
  • In general, in a message protocol. Are you allowed to have a message with just the STX,ETX and the LRC? – Johnathan Au Feb 02 '14 at 16:35
  • there is no "in general". C0 control characters other than `BS` (and this one is a stretch), `HT`, `LF`, and `CR` are essentially obsolete (`NUL` is almost universally used to terminate a string due to the prevalence of C APIs, but this wasn't its original purpose). Characters like `STX` and `ETX` have no meaning except when talking about a specific protocol like ANPA-1312. – bcrist Feb 02 '14 at 17:39

0 Answers0