Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
When you say "both loops are not being entered at all", I would guess that it's because you have asked the "read" or "sysread" to return at the point where the serial-port device has sent 2048 or 10000 bytes worth of content, and maybe the device isn't sending that much data. (How much data are you expecting to get back after you send a given "AT" command to the device?)

Have you tried it this way?

my $buffer; while (1) { read( PORT, $byte, 1 ); $buffer .= $byte; if ( length( $buffer )) { printf( "Buffer now has %d bytes; last byte read was 0x%02x\n" +, length( $buffer ), $byte ); } }
You can kill that process (i.e. ^C or some other method) when you feel that it has run long enough so you know what's going on. Then you can add logic inside the while loop so that when the input buffer attains some particular state (or the last byte was some particular value), you break out of the loop and do something else.

The term "discipline" applied to reading from a file handle covers things like: (a) how to delimit input records (i.e. either by number of bytes, or by watching for a particular byte value or byte sequence; and (b) whether or not your process should wait for data when it tries to read (i.e. using a "blocking" vs. "non-blocking" read - "blocking" is the default).

It's been a while since I've tried to play with non-blocking reads, but you should be able to look that up. The basic idea is that when "read" is called, it always returns pretty much right away; if the device has data to be read, your input buffer gets the data, otherwise your input buffer remains empty and your process continues to execute. This is how people handle devices where input data arrives at unpredictable intervals, but other things have to be done while waiting for input.

The example above assumes blocking input (read doesn't return until the device has delivered a byte to be read). If you were to convert it to use a non-blocking input, you'd want to set $byte to empty string or undef before the read call, then check if it contains a value after the read; if it does, then append to the buffer and print the report. (When using non-blocking input, it can also a good idea in many cases to include a "sleep()" call, to keep the loop from consuming too many cpu cycles.)

In reply to Re^3: Trying to read from serial port - not getting a response by graff
in thread Trying to read from serial port - not getting a response by adiuva

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (4)
    As of 2018-06-21 23:45 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (120 votes). Check out past polls.