daniel-gr-andersson has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
I'm creating a script to dial a phone number through a GSM modem.
I'm using Device::Modem (is there an alternative?)

In the CPAN active.pl example file (included below) "connect" is done first, and after that "is_active".
Does anyone who knows when "connect" would be successful and "is_active" would fail?
I'm not able to find any in-depth information about this.

Thanks Daniel


http://search.cpan.org/~cosimo/Device-Modem-1.56/lib/Device/Modem.pm#is_active()

is_active()

Can be used to check if there is a modem attached to your computer. If modem is alive and responding (on serial link, not to a remote call), is_active() returns true (1), otherwise returns false (0).

Test of modem activity is done through DSR (Data Set Ready) signal. If this signal is in off state, modem is probably turned off, or not working. From my tests I've found that DSR stays in "on" state after more or less one second I turn off my modem, so know you know that.


# $Id: active.pl,v 1.4 2005-04-30 21:45:47 cosimo Exp $ # # This script tries to test if modem is active (on and enabled) # If modem is not active, tries to reset it. # # As I know, this script works to the extent as it: # # 1) Fails if modem is turned off # 2) Succeeds if modem is turned on # # It's not a big thing, I know ... :-( # use Device::Modem; my %config; my $port; if( open CACHED_CONFIG, '< ../.config' ) { while( <CACHED_CONFIG> ) { my @t = split /[\s\t]+/; $config{ $t[0] } = $t[1]; } close CACHED_CONFIG; } if( $config{'tty'} ) { print "Your serial port is `$config{'tty'}' (cached)\n"; $port = $config{'tty'}; } else { $config{'tty'} = $Device::Modem::DEFAULT_PORT; print "What is your serial port? [$config{'tty'}] "; chomp( $port = <STDIN> ); $port ||= $config{'tty'}; if( open( CONFIG, '>../.config' ) ) { print CONFIG "tty\t$port\n"; close CONFIG; } } # ----------------------------------------------------- # BEGIN OF TESTS # ----------------------------------------------------- my $modem = new Device::Modem( port => $port ); if( $modem->connect( baudrate => $config{'baud'} || 19200 ) ) { print "ok 2\n"; } else { print "not ok 2\n"; die "cannot connect to $port serial port!: $!"; } print '- testing if modem is turned on and available', "\n"; my $lOk = 0; if( $lOk = $modem->is_active() ) { print "Ok, modem is active\n"; } else { print "NO! Modem is turned off, or not functioning...\n"; }
  • Comment on Device::Modem - When would "connect" be successful and "is_active" fail?
  • Download Code

Replies are listed 'Best First'.
Re: Device::Modem - When would "connect" be successful and "is_active" fail?
by keszler (Priest) on Nov 09, 2011 at 03:00 UTC

    When in doubt, use the source:

    # instances (Device|Win32)::SerialPort object and initializes communic +ations sub connect { ... # Connect on serial (use different mod for win32) if( $me->ostype eq 'windoze' ) { $me->port( new Win32::SerialPort($me->{'port'}) ); } else { $me->port( new Device::SerialPort($me->{'port'}) ); } ... # Get the modems attention # Send multiple reset commands looking for a sensible response. # A small number of modems need time to settle down and start resp +onding to the serial port ... my $init_response = $me->send_init_string($init_string) || ''; ... $me-> log -> write('info', 'Ok connected' ); $me-> {'CONNECTED'} = 1; }
    So connect creates a (Win32|Device)::SerialPort for the Device::Modem, and connects to and initializes it.
    sub is_active { ... # Modem is active if already connected to a line if( $self->flag('CARRIER') ) { $self->log->write('info', 'carrier is '.$self->flag('CARRIER') +.', modem is connected, it should be active'); $lOk = 1; } else { ... # If DSR signal is on, modem is active my %sig = $self->status(); $lOk = $sig{DSR}; undef %sig; # If we have no success, try to reset if( ! $lOk ) { $self->log->write('warning', 'modem not responding... tryi +ng to reset'); $lOk = $self->reset(); } } $self->log->write('info', 'modem reset result = '.$lOk); return $lOk; }
    And is_active checks for a line connection or DSR signal, and performs a reset if no DSR.

    connect would succeed and is_active would fail when the modem can be initialized (occurs on the TxD/RxD lines) but the SerialPort does not see a DSR signal (DSR line). This could be caused by an unlikely failure of the modem hardware, or much more easily by a bad or incorrectly wired modem cable.