An example using Win32::Serialport to chat with a paging service via a modem. You will need to change phone numbers and responses. The example consists of two simple scripts: 1) pager_setup.pl - A one-time initialization that saves the setup in a configuration file for easy reuse. Each port can have a separate setup. 2) pager.pl - Dials out and sends the message The error handling is "bare bones". The dial will retry 5 times on BUSY. You probably want to leave $debug=1 until you see how it works. USAGE: perl pager_setup.pl COM1 perl pager.pl --port COM1 --pager 9876543210 --message "Test Message" ==================== pager_setup.pl ============================ #!perl -w use Win32::SerialPort 0.14; use strict; my $VERSION = sprintf("%d.%02d", '$Revision: 1.4 $ ' =~ /(\d+)\.(\d+)/); my $port; if (@ARGV) { $port = shift @ARGV; if (! ($port =~ /^COM\d{1}$/i)) { die "Option port to be specified as ``COMx''.\n"; } } else { print "\nUsage: $0 \nVersion: $VERSION\n"; exit; } my $port_obj = new Win32::SerialPort ($port) || die "Can't open $port: $^E\n"; # Open port # Modify port settings $port_obj->error_msg(1); $port_obj->user_msg(1); $port_obj->databits(7); $port_obj->baudrate(2400); $port_obj->parity("even"); $port_obj->stopbits(2); ## $port_obj->handshake("dtr"); ## my modem wanted "rts" $port_obj->handshake("rts"); # default timeouts $port_obj->read_char_time(0); $port_obj->read_const_time(5000); $port_obj->read_interval(0); $port_obj->write_char_time(0); $port_obj->write_const_time(3000); # other parameters $port_obj->stty_echo(0); ## so lookfor doesn't thrash with modem ## This is the essential step $port_obj->write_settings || die "Cannot write settings"; $port_obj->save("pager_$port.cfg") || die "Can't save pager_$port.cfg: $^E\n"; # Configuration file $port_obj->close; # Close port __END__ =head1 NAME pager_setup.pl. Perl script to create pager_COMx.cfg files. =head1 SYNOPSIS perl pager_setup.pl COM1 =head1 DESCRIPTION The script pager_setup.pl creates a configuration file for use by pager.pl. It permits different ports to use different characteristics and simplifies maintaining pager.pl. =head1 AUTHORS Ceri E. Jones Bill Birthisel (wcbirthisel@alum.mit.edu> =head1 SEE ALSO perl(1). pager.pl. =head1 HISTORY $Log: pager_setup.pl,v $ Revision 1.4 1999/03/06 19:44:44 BILLB Extracted from pager.pl as separate script. Revision 1.3 1999/02/27 09:04:15 JONESC Added a couple more command line options. Revision 1.2 1999/02/26 20:45:11 JONESC Added POD. =cut =============================== pager.pl ============================ #!perl -w use Getopt::Long; use Win32::SerialPort 0.14; use strict; my $VERSION = sprintf("%d.%02d", '$Revision: 1.4 $ ' =~ /(\d+)\.(\d+)/); my ($line, $pager, $message, $port, $port_obj, $string, $timeout); my $paging_service = "5551212"; # Supply real number here my $debug = 1; process_options(); # Sets $message, $pager, $port $port_obj = start Win32::SerialPort ("pager_$port.cfg") || die "Can't open pager_$port.cfg: $^E\n"; # Open port with previously saved configuration # Connect to paging service and send Page print STDERR "DEBUG: Paging Service: \"$paging_service\"\n" if $debug; print STDERR "DEBUG: Pager Number: \"$pager\"\n" if $debug; print STDERR "DEBUG: Message: \"$message\"\n\n" if $debug; # Your responses may vary - modem dependent # You probably need at least BUSY and CONNECT $port_obj->are_match("BUSY","CONNECT","OK","NO DIALTONE","ERROR","RING", "NO CARRIER","NO ANSWER"); # my modem resets to give verbose responses $port_obj->write("ATZ\r") || die "Could Not Reset\n"; # Check Modem responding to reset # 5 second timeout from config file waitfor() || die "Modem Did Not Reset\n"; # Timeouts will need adjustment for dfferent services and locations. # Must give the receiving modem a few seconds to pickup and negotiate. $port_obj->read_const_time(30000); my $retries = 0; for (;;) { $port_obj->write("ATDT$paging_service\r") || die "Could Not Dial\n"; # Dial Paging service my $diallog = waitfor(); die "Dial timed out or failed\n" unless (defined $diallog); last if ($diallog eq "CONNECT"); if ((++$retries < 5) && ($diallog eq "BUSY")) { sleep 1; # adjust as required next; } die "Dial did not connect properly: $diallog\n" } $port_obj->write("\r\r"); # Hit enter when connected $port_obj->read_const_time(10000); waitfor("Please enter paging number") || die "Missing prompt for pager\n"; $port_obj->write($pager . "\r"); # Send pager number. $port_obj->read_const_time(5000); waitfor("Please enter message") || die "Missing prompt for message\n"; $port_obj->write($message . "\r"); # Send message waitfor(">") || die "Missing prompt after message\n"; $port_obj->write("N\r"); # End session ## waitfor("Goodbye\r") || die "Missing signoff from service\n"; waitfor("Goodbye") || die "Missing signoff from service\n"; $port_obj->close; # Close port sub process_options { my ($opt_help, $opt_message, $opt_port, $opt_pager, $opt_version); if (@ARGV > 0) { GetOptions("message|msg=s" => \$opt_message, "pager=s" => \$opt_pager, "port=s" => \$opt_port, "help|h|?" => \$opt_help, "version|v" => \$opt_version ) or die "$!\n"; } else { $opt_help = 1; } if (defined $opt_help) { print < -pager -message|msg "" | [-help|h|?] | [-version|v] EOF exit; } if (defined $opt_version) { print <lookclear; # clear buffers my $gotit = ""; my $response = shift; if ($response) { $port_obj->are_match($response); print "DEBUG: Waiting for \"$response\".\n" if $debug; } else { print "DEBUG: Waiting for \"are_match()\".\n" if $debug; } for (;;) { return unless (defined ($gotit = $port_obj->lookfor(1))); if ($gotit ne "") { my ($match, $after, $pattern) = $port_obj->lastlook; print "DEBUG: Got .\"$gotit$match$after\".\n" if $debug; return $match; } return if ($port_obj->reset_error); } } __END__ =head1 NAME pager.pl. Perl script for generating pager messages. =head1 SYNOPSIS perl pager.pl --port COM1 --pager 9876543210 --message "Test Message" =head1 DESCRIPTION pager.pl is a perl script designed to call the BT paging service for the purpose of generating a pager message, utilising the information supplied on the command line. =head1 AUTHORS Ceri E. Jones Bill Birthisel (wcbirthisel@alum.mit.edu> =head1 SEE ALSO perl(1). pager_setup.pl. =head1 HISTORY $Log: pager_setup.pl,v $ Revision 1.4 1999/03/07 19:55:16 BILLB Split initialization into pager_setup.pl and rewrote waitfor. Revision 1.3 1999/02/27 09:04:15 JONESC Added a couple more command line options. Revision 1.2 1999/02/26 20:45:11 JONESC Added POD. =cut