Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Win32 Serial Communication

by Anonymous Monk
on Jan 03, 2002 at 06:33 UTC ( #135894=perlquestion: print w/replies, xml ) Need Help??

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

As part of my project, I need to interface a PC (Windows machine) with a hardware component that I created. The communication is done via a standard db-9 socket (AKA COM port). However, due to the restrictions of my hardware component, the communication process needs to follow an XYZ-modem type protocol, that I have adapted for various reasons. Thus, I need to have a program that runs this protocol. I am rather proficient in Perl, but have never dealt with serial communication. I saw that there are several modules, but there are almost no examples. Also, the fact that they are in v0.19 is kind of shying me away. Has anyone heard/seen/used these modules and make any recomendations? Or can someone suggest a beter way?

Replies are listed 'Best First'.
Re: Win32 Serial Communication
by rob_au (Abbot) on Jan 03, 2002 at 06:50 UTC
    The tried and tested serial communications module for Windows-based systems is Win32::SerialPort - This module, despite its 0.19 versioning, is fairly widely used and quite robust in nature, making direct use of Win32 System API calls. The documentation for this module is quite complete and there is very little that I suspect that you cannot do with this module - If you find this module insufficient, you may want to try a little Google searching before you commit yourself too heavily to this project to find other examples of Win32::SerialPort usage.

    The *NIX equivalent of this module is Device::SerialPort which makes use of the termios library and is exceptionally good for serial communications.


    perl -e 's&&[@.]/&&s&.com.&_&&&print'

      I decided to try out this module. The following is pretty much the code from the examples
      use strict; use Win32::SerialPort qw( :STAT 0.19 ); my $PortName = 'COM1'; my $ob = new Win32::SerialPort($PortName) || die "Can't open $PortName +: $^E\n"; Win32::SerialPort->set_test_mode_active(1); $ob->handshake("none"); $ob->baudrate(57600); #may need to be 56000 $ob->parity("none"); $ob->databits(8); $ob->stopbits(1); $ob->binary(1); $ob->parity_enable(0); $ob->read_const_time(5000); $ob->read_interval(0); $ob->write_const_time(3000); $ob->debug(1); $ob->error_msg(1); # prints hardware messages like "Framing Error" $ob->user_msg(1); # prints function messages like "Waiting for CTS +" $ob->buffers(16,16); # read, write $ob->write_settings || die "Cannot write settings"; $ob->status; print "Starting 1 character background read\n"; my $in = $ob->read_bg(1); my $done = 0; my $blk; my $err; my $out; my $result; for (;;) { ($done, $in, $result) = $ob->read_done(0); $ob->status; sleep(1); last if $done; } print "got = $in\nresult = ",ord($result),"\n"; undef $ob;
      The output says I receive a character, but it's ascii value is 0 which is wrong. I know that my hardware works because I have tried talking to it with HyperTerm. Where is my flaw?
        I've done a lot of work with Device::SerialPort, which is the unix implementation of Win32::SerialPort. I get just a single "0" value when I have something in my settings wrong. Usually it's a baud rate. (My hardware at the other end doesn't always honour it's DIP settings for the baud rate) The other condition that I've had generate that was an error in flow control settings.

        Maybe try a test at 2400 or something.

        In any case, do please x 1000 use Win32::SerialPort, because it is what is normally used, it works _very_ well, and people expect to see it. Also, it's interface is portable to other platforms. (for example, the native platform of Perl...)
        Snazzy tagline here

(jcwren) Re: Win32 Serial Communication
by jcwren (Prior) on Jan 03, 2002 at 10:45 UTC

    I've used this module of a number of projects myself. However, I've always use the 'input' and 'write' methods, typically with select() for timeouts.

    For some code I have written to drive a maniufacturing test platform, I decided to use the tied methods. What a disaster! I resorted to writing my own line buffering routines, and using the 'input' and 'write' methods that had served me so well in the past.

    Admittedly, the documentation (which is very strong in some areas, and more than very weak in others) seems to indicate that the tied methods use some code that is less than heavily tested. But still, I'm not sure how the tied methods escaped from the lab, because even for a basic send/expect type protocol, I was having major problems. Which is a shame, because that would be very handy.

    The worst is that something like @a = <SERIALPORT> will return empty, even with stty_icanon(1). I think this area needs a lot of help yet. Luckily, it's easy to write your own buffering routines that only return whole complete lines. I'd really like to see it behave more like a real file, and be able to use select() directly on the handle. As near as I could tell from the observed behavior (and not digging into the code because of time constraints), it doesn't actually give access to the file handles of the port object. Hence, select() can't be applied to it.


    e-mail jcwren

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://135894]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2022-06-30 23:11 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (98 votes). Check out past polls.