c2fb7075a6cffb618d8c78c7042c922d2c00d211
Benjamin Renard initial commit

Benjamin Renard authored 11 years ago

1) #!/usr/bin/perl
2) #
3) # gnokii-init-modem
4) #
5) # ABOUT
6) #
7) #   Copyright Benjamin Renard 2012 <brenard@easter-eggs.com>
8) #
9) #   Inspired by Roy Sigurd Karlsbakk's 'gnokii-init-modem' script
10) #   Source : http://karlsbakk.net/gnokii-init-modem/
11) #
12) #   Licensed under GPL v2.0. See LICENSE file for details.
13) #
14) #   This software initialize a GSM modem.
15) #
16) 
17) use Device::SerialPort qw( :PARAM :STAT 0.07 );
18) 
19) #
20) # Configuration
21) #
22) 
23) 
24) my $port_name = '/dev/ttyUSB0';		# This is the serial port device.
25) my $pin = '0000';			# Here goes our pin code.
26) my $ready_timeout = 60;			# GSM-ready timeout in seconds.
27) my $debug=1;
28) 
29) # Communication settings
30) my $baudrate = 9600;			# Set to current speed.
31) my $parity = 'none';			# Set to 'none', 'even', or 'odd'.
32) my $databits = 8;			# Between 5 and 8. Only 8 is commonly used.
33) my $stopbits = 1;			# Set to 1 or 2. 1 is most common.
34) my $handshake = 'rts';			# Set to 'none', 'rts' or 'xoff'
35) 
Benjamin Renard Optimize return in non-debu...

Benjamin Renard authored 11 years ago

36) # Auto-flush
37) local $| = 1;
38) 
Benjamin Renard initial commit

Benjamin Renard authored 11 years ago

39) # Functions
40) 
41) sub debug {
42) 	(my $text)=@_;
43) 	if ($debug eq 0) {
44) 		print ".";
45) 	}
46) 	else {
47) 		print "$text\n";
48) 	}
49) }
50) 
51) # Main code
52) 
Benjamin Renard Optimize return in non-debu...

Benjamin Renard authored 11 years ago

53) print "Initialising GSM modem at $port_name ...";
Benjamin Renard initial commit

Benjamin Renard authored 11 years ago

54) 
55) debug "";
56) debug "Open device $port_name";
57) my $PortObj = new Device::SerialPort ($port_name) or
58) 	die "Can't open device $port_name: $!\n";
59) 
60) debug "Set options";
61) $PortObj->handshake($handshake);
62) $PortObj->baudrate($baudrate);
63) $PortObj->parity($parity);
64) $PortObj->databits($databits);
65) $PortObj->stopbits($stopbits); 
66) 
67) $PortObj->read_char_time(0);     # don't wait for each character
68) $PortObj->read_const_time(1000); # 1 second per unfulfilled "read" call
69) 
70) debug "Start communication";
71) debug " -> Send ATZ";
72) $PortObj->write("ATZ\r\n"); sleep(2);
73) debug " -> Send ATE1";
74) $PortObj->write("ATE1\r\n"); sleep(2);
75) debug " -> Send AT+CMEE=1";
76) $PortObj->write("AT+CMEE=1\r\n"); sleep(2);
77) debug " -> Send AT+CSCS";
78) $PortObj->write("AT+CSCS\r\n"); sleep(2);
79) 
80) debug " -> Read return ...";
81) (my $count_in, my $string_in) = $PortObj->read(256);
82) debug "Return :\n\n----\n$string_in----\n";
83) if ($string_in =~ /CME ERROR/ms) {
84) 	debug "SIM locked.";
85) 	debug " -> Send AT+CPIN=\"PIN\"";
86) 	$PortObj->write("AT+CPIN=\"$pin\"\r\n"); sleep(2);
87) 	debug " -> Read return ...";
88) 	(my $count_in, my $string_in) = $PortObj->read(256);
89) 	debug "Return :\n\n----\n$string_in----\n";
90) 	if ($string_in =~ /OK/ms) {
Benjamin Renard Optimize return in non-debu...

Benjamin Renard authored 11 years ago

91) 		print " Sim unlocked\n";
Benjamin Renard initial commit

Benjamin Renard authored 11 years ago

92) 		exit 0;
93) 	}
94) 	else {
Benjamin Renard Optimize return in non-debu...

Benjamin Renard authored 11 years ago

95) 		print " ERROR unlocking SIM (bad PIN code ?)\n";
Benjamin Renard initial commit

Benjamin Renard authored 11 years ago

96) 		exit 1;
97) 	}
98) }
99) else {
Benjamin Renard Optimize return in non-debu...

Benjamin Renard authored 11 years ago

100) 	print " SIM already unlocked\n";