| |
# USAGE: require 'start_port.pl';
# $port_object = open_port ($port);
#
# $port defaults to "COM1" on Win32 and "/dev/ttyS0" otherwise
# you will need to specify which port to use on any OS other than linux
use vars qw($OS_win);
# We start with some cross-platform black magic
BEGIN { # Decide which module to use based on the operating system
$| = 1;
$OS_win = ($^O eq "MSWin32") ? 1 : 0;
if ($OS_win) { eval "use Win32::SerialPort 0.19" }
else { eval "use Device::SerialPort 0.07" }
die "$@\n" if $@;
}
use strict;
# open_port() takes a port_name as parameter
# provides plausible defaults if none specified (Win32 and linux)
sub open_port {
my $port = shift;
my $serial_port;
if ($OS_win) {
$port = "COM1" unless ($port);
$serial_port = Win32::SerialPort->new ($port);
} else {
$port = "/dev/ttyS0" unless ($port);
$serial_port = Device::SerialPort->new ($port);
print "\n=== Bypassing ioctls ===\n\n"
unless $serial_port->can_ioctl;
}
die "Can't open serial port $port: $^E\n" unless ($serial_port);
$serial_port->handshake("none");
# CM17 doesn't care about other parameters unless
# pass-through port is used.
# The CM11 doesn't need ioctls -- but it does have
# to set the traditional
# serial parameters like baud, parity, stopbits, etc.
$serial_port->error_msg(1); # use built-in error messages
$serial_port->user_msg(0);
$serial_port->databits(8);
$serial_port->baudrate(4800);
$serial_port->parity("none");
$serial_port->stopbits(1);
$serial_port->dtr_active(1);
$serial_port->write_settings
|| die "Could not set up port\n";
return $serial_port;
}
1;
|