in reply to Buffer in Serial read routine seemingly not working??
In other words, a simple 2-state machine would do what you want. The initial state stores nothing and just watches for STX. Once STX is found, you switch to storing input and looking for ETX. You'll stop storing when either ETX or NAK is found or when 80 bytes are stored, whichever comes first, and return to the initial state; if ETX was found first, you'll do something with the stored data, otherwise you'll ditch it.
If that isn't what you meant to describe, please try again to see if you can explain the task more clearly. If that is what you meant, then that description should lead you to a fairly simple script, along the lines of:
If you want a 2nd STX in the input to be treated as data, just comment out the @buffer = (); line in the first "if" clause.my $capture_state = 0; # looking for STX my @buffer; while (1) { $byte = $port->read(1); if ( $byte == 0x02 ) { # got STX $capture_state = 1; @buffer = (); # (if we were already capturing for a prev STX, + ditch that) } if ( $capture_state ) { if ( $byte == 0x15 or @buffer > 79 ) { # got NAK or too many +bytes @buffer = (); $capture_state = 0; } else { push @buffer, $byte; if ( $byte == 0x03 ) { # got ETX printf "Received complete message of %d bytes:", scala +r @buffer; printf " %02x" for ( @buffer ); print "\n"; @buffer = (); $capture_state = 0; } } } }
(update: made a minor fix to the 2nd "printf" format, to put a space between consecutive byte values)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Buffer in Serial read routine seemingly not working??
by Anonymous Monk on Dec 03, 2012 at 05:26 UTC | |
|
Re^2: Buffer in Serial read routine seemingly not working??
by Anonymous Monk on Dec 03, 2012 at 00:39 UTC | |
|
Re^2: Buffer in Serial read routine seemingly not working??
by Anonymous Monk on Dec 03, 2012 at 02:56 UTC |