+

Last commit for gnokii-init-modem: c2fb7075a6cffb618d8c78c7042c922d2c00d211

Optimize return in non-debug mode

Benjamin Renard [2012-07-26 10:38:59]
Optimize return in non-debug mode
#!/usr/bin/perl
#
# gnokii-init-modem
#
# ABOUT
#
#   Copyright Benjamin Renard 2012 <brenard@easter-eggs.com>
#
#   Inspired by Roy Sigurd Karlsbakk's 'gnokii-init-modem' script
#   Source : http://karlsbakk.net/gnokii-init-modem/
#
#   Licensed under GPL v2.0. See LICENSE file for details.
#
#   This software initialize a GSM modem.
#

use Device::SerialPort qw( :PARAM :STAT 0.07 );

#
# Configuration
#


my $port_name = '/dev/ttyUSB0';		# This is the serial port device.
my $pin = '0000';			# Here goes our pin code.
my $ready_timeout = 60;			# GSM-ready timeout in seconds.
my $debug=1;

# Communication settings
my $baudrate = 9600;			# Set to current speed.
my $parity = 'none';			# Set to 'none', 'even', or 'odd'.
my $databits = 8;			# Between 5 and 8. Only 8 is commonly used.
my $stopbits = 1;			# Set to 1 or 2. 1 is most common.
my $handshake = 'rts';			# Set to 'none', 'rts' or 'xoff'

# Auto-flush
local $| = 1;

# Functions

sub debug {
	(my $text)=@_;
	if ($debug eq 0) {
		print ".";
	}
	else {
		print "$text\n";
	}
}

# Main code

print "Initialising GSM modem at $port_name ...";

debug "";
debug "Open device $port_name";
my $PortObj = new Device::SerialPort ($port_name) or
	die "Can't open device $port_name: $!\n";

debug "Set options";
$PortObj->handshake($handshake);
$PortObj->baudrate($baudrate);
$PortObj->parity($parity);
$PortObj->databits($databits);
$PortObj->stopbits($stopbits);

$PortObj->read_char_time(0);     # don't wait for each character
$PortObj->read_const_time(1000); # 1 second per unfulfilled "read" call

debug "Start communication";
debug " -> Send ATZ";
$PortObj->write("ATZ\r\n"); sleep(2);
debug " -> Send ATE1";
$PortObj->write("ATE1\r\n"); sleep(2);
debug " -> Send AT+CMEE=1";
$PortObj->write("AT+CMEE=1\r\n"); sleep(2);
debug " -> Send AT+CSCS";
$PortObj->write("AT+CSCS\r\n"); sleep(2);

debug " -> Read return ...";
(my $count_in, my $string_in) = $PortObj->read(256);
debug "Return :\n\n----\n$string_in----\n";
if ($string_in =~ /CME ERROR/ms) {
	debug "SIM locked.";
	debug " -> Send AT+CPIN=\"PIN\"";
	$PortObj->write("AT+CPIN=\"$pin\"\r\n"); sleep(2);
	debug " -> Read return ...";
	(my $count_in, my $string_in) = $PortObj->read(256);
	debug "Return :\n\n----\n$string_in----\n";
	if ($string_in =~ /OK/ms) {
		print " Sim unlocked\n";
		exit 0;
	}
	else {
		print " ERROR unlocking SIM (bad PIN code ?)\n";
		exit 1;
	}
}
else {
	print " SIM already unlocked\n";
	exit 0;
}
ViewGit