Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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!

Replies are listed 'Best First'.
Re: Serial Port I/O
by Anonymous Monk on May 15, 2013 at 17:32 UTC
    Sorry, the code block is incorrect and has a close($serial) that was added later when troubleshooting. The code actually looks like this:
    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"; $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";
Re: Serial Port I/O
by jmlynesjr (Deacon) on May 15, 2013 at 19:41 UTC

    Try looking at/borrowing/replicating the low level code from Win32::SerialPort.

    James

    There's never enough time to do it right, but always enough time to do it over...

Re: Serial Port I/O
by bulk88 (Priest) on May 16, 2013 at 00:37 UTC
    On windows select only works on sockets. WaitForMultipleObjects/WaitForSingleObject is the basic (but not the only) event loop of Windows. WFMO/WFSO are used to implement select() under the hood.