I'm trying to write a simple program to communicate with the serial port. I am able to send commands, but getting what is sent back has proven difficult. I cannot use Win32::SerialPort, or Device::SerialPort.

I had limited success with:

my $COM_PORT = "com1"; open DEV,"+<", "$COM_PORT" or die "failed to open com port\n"; print DEV "\n"; $input = <DEV>;
But it can hang on $input = <DEV> when there is no incoming data.

I have played around with sysopen and IO::Select, but I can't get any of the select functions to recognize the Serial port as ready to read/write.
use IO::Select; use IO::Handle; $COM_PORT = "COM1"; my $Serial; #system("mode com1 baud=9600 stop=1 data=8 parity=n rts=on dtr=on"); #open $Serial,"+<", "$COM_PORT" or die "failed to open com port\n"; sysopen $Serial,"$COM_PORT", O_RDWR|O_NDELAY|O_NCOTTY or die "failed t +o open com port\n"; my $fd = fileno($Serial); close ($Serial); print "File Descriptor is: $fd \n"; $sel = IO::Select->new; $sel -> add($Serial); if($sel -> exists($Serial)) { print "Handle exists \n"; } print $Serial "\n"; my $timeout = 0; if (my @ready = $sel -> can_read(0)) { print "Ready to read! \n"; } if (my @ready = $sel -> can_write(0)) { print "Ready to write! \n"; } if(my @ready = $sel -> has_error(0)) { print "Exceptions found! \n"; } close ($Serial); print "Finished... \n";
But when run, this simply outputs:
Handle Exists
Finished...

What am I doing wrong? Does anyone know how to simply check if there is any incoming data on the serial port without Win32::SerialPort?

Thanks!


In reply to Serial Port I/O by Anonymous Monk

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!
  • 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, details, 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, summary, 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.