Modem Check routines

The file http://www.theory.physics.ubc.ca/chk-modem.tar contains the following two programs which will search through all of the /dev/ttyS? files to determine if any of them act like a modem (ie respond to an AT command). The first, reset-serial, is a program to reset the serial port. Some programs (including pppd ) can leave the serial port in a funny state. This program attempts to reset the serial port into a form it can be written to or read from. The useage of this program is
reset-serial /dev/ttyS1
where ttyS1 must be replaced by the serial port of interest.
reset-serial

#!/usr/local/bin/perl 

use POSIX qw(:termios_h :fcntl_h);

if ( "$ARGV[0]" eq "" ){ die "Useage: reset-serial \n";}

$modem = "$ARGV[0]";

sysopen ( MODEM, $modem, O_RDWR | O_NONBLOCK ) || die "reset-serial:$modem: $!\n";  
sleep 1;
$mo = POSIX::Termios->new;
$mo->getattr(fileno(MODEM)) or die "reset-serial:getattr: $!\n";
$mo->setlflag($mo->getlflag & ~ECHO);
$mo->setcflag(($mo->getcflag | CREAD | CLOCAL) & ~HUPCL);
$mo->setattr(fileno(MODEM), TCSANOW) or die "reset-serial:setattr: $!\n";

close MODEM

(This was adapted from a program posted to comp.protocols.ppp by James Carlson)

To run a check of a certain serial line to see if there is a modem attached and that modem is answering, try

/usr/sbin/chat -v -t 5 '' AT OK < /dev/ttyS1 > /dev/ttyS1

If this finishes immediately, there is a modem there. If it hangs for about 5 seconds and then returns, there is not. If you look at the end of /var/log/messages, there will be a line stating that chat timed out waiting for the OK. Here is a script to run the test on all of the serial ports on your system Use it by nameing it chk-modem, making it executable, placing it in root's path, and running
chk-modem
as root. (Note this script assumes that both chk-modem and reset-serial are in the same directory.)


#!/bin/sh
DIR="`dirname $0`/"
# Assume both this script and reset-serial in same directory

for i in `ls /dev/ttyS*`
#check all the ttyS serial ports
do
  echo
  echo Testing $i
  if [ -r $i -a -w $i ] ; then
#Make sure port is both readable and writable.     
   if  $DIR/reset-serial $i 2>/dev/null ; then
    if /usr/sbin/chat -v -t 2 '' AT OK ; then
       echo $i seems to be attached to a modem
    else
       echo $i does not respond to modem commands
    fi
   else
    echo Serial port $i cannot be reset-- either in use or not a serial port
   fi
  else
    echo $i must be both readable and writable
  fi
done