Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I'm not sure I understand your script, but let me see if I at least understand your task. What follows is actually a different plan from what you seem to be trying to do in the OP code, but (if I understand what you said about your sample data strings) I think it might be a useful approach (and might be easier to implement, relative to what you've tried so far):

  1. You want to read bytes continuously from a serial port
  2. Every time you receive STX (0x02), you want start capturing/storing input
  3. Having started capture/storage, you want to stop it when you receive ETX (0x03) and do something with the stored byte sequence, clear the capture buffer, and return to step 2.
  4. But if you get NAK (0x15) or if more than 79 (80?) bytes get stored before receiving ETX, you want to ditch the capture buffer without further ado and return to step 2.
  5. Something you didn't mention (but should be considered): if a second STX arrives before an ETX, NAK or 80 bytes have appeared, you should either treat this 2nd STX as data, or else ditch the capture buffer and start a fresh capture.

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:

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; } } } }
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.

(update: made a minor fix to the 2nd "printf" format, to put a space between consecutive byte values)


In reply to Re: Buffer in Serial read routine seemingly not working?? by graff
in thread Buffer in Serial read routine seemingly not working?? by jagexCoder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (7)
    As of 2014-12-29 08:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (185 votes), past polls