SPUG: serial I/O from Perl (W32 & Red Hat)

David Dyck david.dyck at fluke.com
Tue Apr 27 23:16:42 CDT 2004

On Tue, 27 Apr 2004 at 17:36 -0700, Michael R. Wolf <MichaelRWolf at att.net>...:

> I'm connecting two systems together for the purpose of testing.
> It's been a long time since I twiddled bits across a serial port. Last
> time I did so it was in assembly and C. I'd like to do much better
> this time -- Perl.

Here's a start that allows us to run code on Unix and Windows,
although we've found Unix more reliable.

use vars qw($OS_win);
    $OS_win = ($^O eq "MSWin32") ? 1 : 0;

    if ($OS_win) {
        eval "use Win32::SerialPort 0.11 qw(:ALL)";
        die "$@\n" if ($@);
    else {
        eval "use Device::SerialPort qw(:ALL)";
        die "$@\n" if ($@);

Then when you want to allocate a port

    $name = '/dev/' . $name if not $OS_win;
    $port = $OS_win ? Win32::SerialPort->new($name) : Device::SerialPort->new($name), 1;

After that, the access through $port is very much the same.

More information about the spug-list mailing list