Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Accessing serial ports under Windows

by Odud (Pilgrim)
on Jun 17, 2000 at 22:53 UTC ( #18663=perlquestion: print w/replies, xml ) Need Help??
Odud has asked for the wisdom of the Perl Monks concerning the following question:

Has anyone experience of using the serial ports under Windows? I'm trying to send commands to the modem and read the response. I thought that I'd read all the relevant bits in the open tutorial and I have the following code
use warnings; use Fcntl; my $tty = "COM3"; my $text = shift || "ATH0"; sysopen(COM,$tty,O_RDWR) || die "Can't open $tty $^E"; transmit("$text"); receive(); sub transmit { my $data = shift; $data .= "\r"; @ascii = unpack("C*",$data); print "transmit: [@ascii]\n"; syswrite(COM,$data); } sub receive { sysread(COM,$buf,4); @ascii = unpack("C*",$buf); print "receive: [@ascii]\n"; return $buf; }
However when I run the script it hangs on the sysread instead of returning O.K. as expected. I think the port is opening correctly, because when something else is using the port the open fails with an "Access denied" message.

Replies are listed 'Best First'.
Re: Accessing serial ports under Windows
by chromatic (Archbishop) on Jun 17, 2000 at 23:43 UTC
    You probably ought to check the return values of sysread and syswrite. They both return undef on error:
    $result = syswrite(COM, $data); if (defined $result) { print "$result bytes written\n"; } else { die "Couldn't write to port.\n"; }
    Of ourse, there's always the CPAN module Win32::SerialPort.
      I supposedly searched CPAN this afternoon and found nothing! - in my defence my twin daughters were climbing on me at the time. But you are quite correct I should not assume that the syswrite and sysread are working.
(jcwren) RE: Accessing serial ports under Windows
by jcwren (Prior) on Jun 17, 2000 at 23:50 UTC
    As chromatic mentioned, there is Win32::SerialPort. I've used this rather extensively with some Perl modules I wrote to control several amateur radio transceivers, my weather station, and some equipment I designed to interface general purpose I/O to a serial port. It works quite well, and better yet, you can port it to Linux using Device::SerialPort.

    You get a lot of control over the port, and unless you don't get too bizarre, all you need is a BEGIN code snipped to select Win32::SerialPort or Device::SerialPort based on the OS reported by $^O.

    --Chris (who wonders why the first link isn't right, and second is)
Re: Accessing serial ports under Windows
by Anonymous Monk on Jun 16, 2008 at 21:08 UTC
    I took your code, replaced $^E with $!, and it ran fine... maybe some strange quirk on your modem? Maybe for some reason there aren't 4 bytes from the modem for the script to read, so the call to sysread is blocking?
      If only I could remember what I was trying to do - but it was eight years ago...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://18663]
Approved by root
[stevieb]: I am thankful that I fell into Perl years ago in the manner I did. I don't believe there is another language that people care about so much, that they're willing to give everything. Langs come and go, but in Perl, it seems, things remain consistent.

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2017-02-25 01:18 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (364 votes). Check out past polls.