parsing - How to Calculate LRC in Java -



parsing - How to Calculate LRC in Java -

my message protocol follows:

stx: 0x02 etx: 0x03

dle: 0x10 (delimiter used in front end of 0x2, 0x3 or 0x10 info bytes , not stx, etx or dle).

data: values 0x02, 0x03 or 0x10 delimited avoid confusion stx, etx , dle

lrc: calculates 'xor' , excludes dles , stx includes etx. also, lrc values not delimited if 0x2, 0x3 or 0x10.

here info test message testing:

byte[] testmessage1 = { 0x02, // stx 0x10,0x2,0xa,0x10,0x10,0x7,0x8, // info 02, a, 10, 7, 8 0x03, // etx 0x2^0xa^0x10^0x7^0x8^0x03 // lrc calculated info (with dle removed) plus etx };

here lrc calculation have:

public static byte calculatelrc(byte[] bytes) { byte lrc = 0; (int = 1; < bytes.length; i++) { lrc ^= bytes[i]; } homecoming lrc; }

how lrc calculation of test byte message according protocol check if valid message, , info before not corrupted?

very broadly speaking, parsing works state variable, , depending on next symbol read state changed (error beingness possible state).

in protocol, validating message, if think logically start, can work out states.

the first byte must stx, or not valid message. after that, calculate lrc while looking etx. 1 time find etx, next byte must lrc. conditional states, escaping via dle can expressed different states, too:

state 0: if symbol stx -> state 1 else -> error state 1: if symbol = etx -> state 3 else if symbol = dle -> state 2 else update lrc state 2: update lrc -> state 1 state 3: if symbol == lrc -> ok else error

once have state transition table, putting code shouldn't terribly difficult.

java parsing bytearray

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -