| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# This tool is used to check how Device::SerialPort is behaving on |
| 4 |
# your machine. It will list all the possible values for each function |
| 5 |
# as it runs. Edit this tool to test various settings. |
| 6 |
# |
| 7 |
# $Id: modemtest 281 2004-02-24 05:27:24Z nemies $ |
| 8 |
# |
| 9 |
# Copyright (C) 2000-2003 Kees Cook |
| 10 |
# [email protected], http://outflux.net/ |
| 11 |
# |
| 12 |
# This program is free software; you can redistribute it and/or |
| 13 |
# modify it under the terms of the GNU General Public License |
| 14 |
# as published by the Free Software Foundation; either version 2 |
| 15 |
# of the License, or (at your option) any later version. |
| 16 |
# |
| 17 |
# This program is distributed in the hope that it will be useful, |
| 18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
# GNU General Public License for more details. |
| 21 |
# |
| 22 |
# You should have received a copy of the GNU General Public License |
| 23 |
# along with this program; if not, write to the Free Software |
| 24 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 |
# http://www.gnu.org/copyleft/gpl.html |
| 26 |
|
| 27 |
use Device::SerialPort qw (:STAT); |
| 28 |
use strict; |
| 29 |
use warnings; |
| 30 |
|
| 31 |
=head1 NAME |
| 32 |
|
| 33 |
modemtest - Tool to examining your modem through Perl's Device::SerialPort |
| 34 |
|
| 35 |
=head1 SYNOPSIS |
| 36 |
|
| 37 |
modemtest [OPTS] [DEVICE [BAUD [DATA [PARITY [STOP [FLOW]]]]]] |
| 38 |
|
| 39 |
DEVICE Device to use as a serial port (default: "/dev/modem") |
| 40 |
BAUD Serial speed to use (default: 9600) |
| 41 |
DATA Number of databits to use (default: 8) |
| 42 |
PARITY Type of parity to use (default: "none") |
| 43 |
STOP Number of stop bits to use (default: 1) |
| 44 |
FLOW Kind of flow control to use (default: "none") |
| 45 |
|
| 46 |
-h, --help Help report |
| 47 |
--skip-status Skip modem status bit tests |
| 48 |
--hide-possible Don't show all possible settings |
| 49 |
|
| 50 |
=head1 DESCRIPTION |
| 51 |
|
| 52 |
Some systems, serial ports, and modem behave in strange ways. To test |
| 53 |
the capabilities of Perl's Device::SerialPort, this tool queries the |
| 54 |
system settings for the given DEVICE, and attempts to set up the port |
| 55 |
and send the initialization string "ATE1" to the modem, reporting the |
| 56 |
results seen. |
| 57 |
|
| 58 |
=head1 SEE ALSO |
| 59 |
|
| 60 |
L<Device::SerialPort(3)> |
| 61 |
|
| 62 |
L<perl(1)> |
| 63 |
|
| 64 |
=head1 AUTHOR |
| 65 |
|
| 66 |
Kees Cook <[email protected]>. |
| 67 |
|
| 68 |
=head1 COPYRIGHT AND LICENSE |
| 69 |
|
| 70 |
Copyright 2000-2004 by Kees Cook <[email protected]>. |
| 71 |
|
| 72 |
This program is free software; you may redistribute it and/or modify |
| 73 |
it under the same terms ans Perl itself. |
| 74 |
|
| 75 |
=cut |
| 76 |
|
| 77 |
printf "Device::SerialPort v%d.%d.%d loaded.\n", |
| 78 |
int(${Device::SerialPort::VERSION}), |
| 79 |
(int(${Device::SerialPort::VERSION}*1000)=~/(\d{3})$/), |
| 80 |
(int(${Device::SerialPort::VERSION}*1000000)=~/(\d{3})$/); |
| 81 |
|
| 82 |
my $opt_skip_status=0; |
| 83 |
my $opt_hide_possible=0; |
| 84 |
|
| 85 |
# quick params |
| 86 |
if ($ARGV[0] eq "-h" || |
| 87 |
$ARGV[0] eq "--help") { |
| 88 |
die "Usage: $0 [DEVICE [BAUD [DATABITS [PARITY [STOPBITS [FLOW]]]]]] |
| 89 |
-h, --help Help report |
| 90 |
--skip-status Skip modem status bit tests |
| 91 |
--hide-possible Don't show all possible settings |
| 92 |
"; |
| 93 |
} |
| 94 |
|
| 95 |
while ($ARGV[0]=~/^--(.*)/) { |
| 96 |
if ($1 eq "skip-status") { |
| 97 |
$opt_skip_status=1; |
| 98 |
} |
| 99 |
elsif ($1 eq "hide-possible") { |
| 100 |
$opt_hide_possible=1; |
| 101 |
} |
| 102 |
else { |
| 103 |
die "Unknown option '--$1'. Try '--help'.\n"; |
| 104 |
} |
| 105 |
shift @ARGV; |
| 106 |
} |
| 107 |
|
| 108 |
# your serial port. |
| 109 |
my ($device,$baudrate,$databits,$parity,$stopbits,$handshake)=@ARGV; |
| 110 |
$device ||= "/dev/modem"; |
| 111 |
$baudrate ||= "9600"; |
| 112 |
$databits ||= "8"; |
| 113 |
$parity ||= "none"; |
| 114 |
$stopbits ||= "1"; |
| 115 |
$handshake ||= "none"; |
| 116 |
|
| 117 |
my $port=new Device::SerialPort($device) || die "new($device): $!\n"; |
| 118 |
print "Port '$device' open\n"; |
| 119 |
|
| 120 |
# Are the ioctls loaded? |
| 121 |
my $bool=$port->can_ioctl(); |
| 122 |
print "can ioctl: ",($bool ? "Yes" : "No"),"\n"; |
| 123 |
if (!$bool) { |
| 124 |
die "The rest of this test is useless without ioctl methods.\n"; |
| 125 |
} |
| 126 |
|
| 127 |
# Handshaking |
| 128 |
if (!$opt_hide_possible) { |
| 129 |
my @handshakes=$port->handshake; |
| 130 |
print "Handshakes:\n"; |
| 131 |
grep(print("\t$_\n"),sort(@handshakes)); |
| 132 |
} |
| 133 |
$handshake=$port->handshake($handshake); |
| 134 |
print "Got handshake: $handshake\n"; |
| 135 |
|
| 136 |
# Baud rate |
| 137 |
if (!$opt_hide_possible) { |
| 138 |
my @bauds=$port->baudrate; |
| 139 |
print "Bauds:\n"; |
| 140 |
grep(print("\t$_\n"),sort { $b <=> $a } @bauds); |
| 141 |
} |
| 142 |
$baudrate=$port->baudrate($baudrate); |
| 143 |
print "Got baud: $baudrate\n"; |
| 144 |
|
| 145 |
# Databits |
| 146 |
if (!$opt_hide_possible) { |
| 147 |
my @databits=$port->databits; |
| 148 |
print "Databits:\n"; |
| 149 |
grep(print("\t$_\n"),sort { $b <=> $a } @databits); |
| 150 |
} |
| 151 |
$databits=$port->databits($databits); |
| 152 |
print "Got databits: $databits\n"; |
| 153 |
|
| 154 |
# Parity |
| 155 |
if (!$opt_hide_possible) { |
| 156 |
my @parity=$port->parity; |
| 157 |
print "Parity:\n"; |
| 158 |
grep(print("\t$_\n"),sort @parity); |
| 159 |
} |
| 160 |
$parity=$port->parity($parity); |
| 161 |
print "Got parity: $parity\n"; |
| 162 |
|
| 163 |
# Stopbits |
| 164 |
if (!$opt_hide_possible) { |
| 165 |
my @stopbits=$port->stopbits; |
| 166 |
print "Stopbits:\n"; |
| 167 |
grep(print("\t$_\n"),sort { $b <=> $a } @stopbits); |
| 168 |
} |
| 169 |
$stopbits=$port->stopbits($stopbits); |
| 170 |
print "Got stopbits: $stopbits\n"; |
| 171 |
|
| 172 |
if (!$opt_skip_status) { |
| 173 |
linestatus(); |
| 174 |
print "\n"; |
| 175 |
|
| 176 |
my $delay=3; |
| 177 |
|
| 178 |
# Flip on DTR and RTS |
| 179 |
my $dtr=$port->dtr_active(1) ? "okay" : "failed"; |
| 180 |
my $rts=$port->rts_active(1) ? "okay" : "failed"; |
| 181 |
print "Activated DTR($dtr) and RTS($rts) ... pausing for $delay seconds\n"; |
| 182 |
linestatus(); |
| 183 |
print "\n"; |
| 184 |
sleep $delay; |
| 185 |
|
| 186 |
$dtr=$port->dtr_active(0) ? "okay" : "failed"; |
| 187 |
$rts=$port->rts_active(0) ? "okay" : "failed"; |
| 188 |
print "Deactivated DTR($dtr) and RTS($rts) ... pausing for $delay seconds\n"; |
| 189 |
linestatus(); |
| 190 |
print "\n"; |
| 191 |
sleep $delay; |
| 192 |
|
| 193 |
$dtr=$port->dtr_active(1) ? "okay" : "failed"; |
| 194 |
print "Activated DTR($dtr) ... pausing for $delay seconds\n"; |
| 195 |
linestatus(); |
| 196 |
print "\n"; |
| 197 |
sleep $delay; |
| 198 |
|
| 199 |
$rts=$port->rts_active(1) ? "okay" : "failed"; |
| 200 |
print "Activated RTS($rts) ... pausing for $delay seconds\n"; |
| 201 |
linestatus(); |
| 202 |
print "\n"; |
| 203 |
sleep $delay; |
| 204 |
} |
| 205 |
|
| 206 |
# Just in case: reset our timing and buffers |
| 207 |
$port->lookclear(); |
| 208 |
$port->read_const_time(100); |
| 209 |
$port->read_char_time(5); |
| 210 |
|
| 211 |
# Turn on parity checking: |
| 212 |
#$port->stty_inpck(1); |
| 213 |
#$port->stty_istrip(1); |
| 214 |
|
| 215 |
# Read a chunk |
| 216 |
my ($count,$str,$got,$cnt); |
| 217 |
readchunk(); |
| 218 |
|
| 219 |
# Write some AT commands to the modem |
| 220 |
writechunk("ATE1\r"); |
| 221 |
|
| 222 |
# Read a few chunks |
| 223 |
readchunk(); |
| 224 |
readchunk(); |
| 225 |
|
| 226 |
print "\n"; |
| 227 |
linestatus(); |
| 228 |
|
| 229 |
# close the port |
| 230 |
undef $port; |
| 231 |
print "Port closed\n"; |
| 232 |
|
| 233 |
sub writechunk |
| 234 |
{ |
| 235 |
my $str=shift; |
| 236 |
|
| 237 |
my $count = $port->write($str); |
| 238 |
print "wrote: $count\n"; |
| 239 |
$str=~s/([^\040-\176])/sprintf("{0x%02X}",ord($1))/ge; |
| 240 |
print "written ->$str<-\n"; |
| 241 |
} |
| 242 |
|
| 243 |
sub readchunk |
| 244 |
{ |
| 245 |
# read a chunk of data |
| 246 |
sleep 1; |
| 247 |
my ($count,$str)=$port->read(1); |
| 248 |
my $got; |
| 249 |
$cnt=$count; |
| 250 |
while ($count>0) { |
| 251 |
($count,$got)=$port->read(1); |
| 252 |
$str.=$got; |
| 253 |
$cnt+=$count; |
| 254 |
} |
| 255 |
print "read: $cnt\n"; |
| 256 |
$str=~s/([^\040-\176])/sprintf("{0x%02X}",ord($1))/ge; |
| 257 |
print "saw ->$str<-\n"; |
| 258 |
} |
| 259 |
|
| 260 |
sub linestatus |
| 261 |
{ |
| 262 |
my $status = $port->modemlines; |
| 263 |
printf("Modem status = 0x%04X (DTR=%s CTS=%s RTS=%s DSR=%s RNG=%s CD=%s)\n", |
| 264 |
$status, |
| 265 |
($status & MS_DTR_ON) ? "ON " : "off", |
| 266 |
($status & MS_CTS_ON) ? "ON " : "off", |
| 267 |
($status & MS_RTS_ON) ? "ON " : "off", |
| 268 |
($status & MS_DSR_ON) ? "ON " : "off", |
| 269 |
($status & MS_RING_ON) ? "ON " : "off", |
| 270 |
($status & MS_RLSD_ON) ? "ON " : "off", |
| 271 |
); |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
# /* vi:set ai ts=4 sw=4 expandtab: */ |