Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Platform-independent Serial Port code

by bikeNomad (Priest)
on Jun 16, 2001 at 02:41 UTC ( #88966=sourcecode: print w/ replies, xml ) Need Help??

Category: Miscellaneous
Author/Contact Info bikeNomad, Ned Konz,,
Description: This module makes it possible to use Perl programs that were written to use Win32::SerialPort or Device::SerialPort on the other kind of operating system without changing the source text. Just include the module and run your existing code:
perl -MAnySerialPort

It aliases the symbol table and provides a translating constructor.

Save it as somewhere on your Perl @INC path.

# Lets serial port programs written for either Windows or Unix
# work on the other kind of system without modification.
# By Ned Konz,,
# This script must have only LF line endings to work cross-platform.
# usage:
#  perl -MAnySerialPort
# This will map port names between Linux and Windows; if your system d
# use the same mappings, you can call
# Device::SerialPort::mapPorts
# or
# Win32::SerialPort::mapPorts
# to change it:
# Device::SerialPort->mapPorts('COM1:' => '/dev/magicSerial0',
#         'COM2' => '/dev/magicSerial1');
use strict;
package AnySerialPort;
use vars '@ISA';

 my %portMap;
 my $oldNew;
 my $onWindows = 0;

 if ($^O eq 'MSWin32') # running on Win32
  $onWindows = 1;
  eval "use Win32::SerialPort";
  *main::Device::SerialPort:: = *main::Win32::SerialPort::;
  $oldNew = \&Win32::SerialPort::new;
  $INC{'Device/'} = $INC{'Win32/'};
  %portMap = ('/dev/ttyS0' => 'COM1:',
     '/dev/ttyS1' => 'COM2:',
     '/dev/ttyS2' => 'COM3:',
     '/dev/ttyS3' => 'COM4:',
 else # running on Unix
  eval "use Device::SerialPort";
  *main::Win32::SerialPort:: = *main::Device::SerialPort::;
  $oldNew = \&Device::SerialPort::new;
  $INC{'Win32/'} = $INC{'Device/'};
  %portMap = ('COM1:'=> '/dev/ttyS0',
     'COM2:'=> '/dev/ttyS1',
     'COM3:'=> '/dev/ttyS2',
     'COM4:'=> '/dev/ttyS3',

 die "$@\n" if $@;
 @ISA = 'Device::SerialPort';

 # Hook the constructor so we can map the port names
 # and class if needed
 *main::Device::SerialPort::new = sub {
  my $class = shift;
  my $portName = shift;
  if ($onWindows != ($class eq 'Win32::SerialPort'))
   $portName = $portMap{$portName} || $portName;
   $class = $onWindows ? 'Win32::SerialPort' : 'Device::SerialPort';
  $oldNew->($class, $portName, @_);

 # Gets and/or modifies the port mapping
 # Returns a hash
 sub Device::SerialPort::mapPorts
  my $self = shift;
  %portMap = (%portMap, @_);


Comment on Platform-independent Serial Port code
Download Code

Back to Code Catacombs

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://88966]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2015-11-30 18:35 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (778 votes), past polls