5 use Device::SerialPort;
6 use Data::Dump qw(dump);
8 my $device = $ENV{DEVICE} || shift @ARGV || '/dev/ttyUSB0';
10 my $port = Device::SerialPort->new( $device ) || die "$device: $!";
11 $port->baudrate(115200) || die "fail $device baudrate";
12 $port->parity("none") || die "fail $device parity";
13 $port->databits(8) || die "fail $device databits";
14 $port->stopbits(1) || die "fail $device stopbits";
15 $port->handshake("rts") || die "fail $device handshake";
17 $port->write_settings || die "no $device settings";
23 $port->read_char_time(3000); # 5 s char timeout
24 $port->read_const_time(3000); # 1 s read timeout
26 while ( my $drain = $port->input ) {
27 warn "# drain $drain\n# /drain\n";
31 warn "## using $device\n";
34 my ($cmd,$message) = @_;
35 print STDERR ">>> $cmd >>> $message\n";
36 $port->write("$cmd\r");
37 # $port->write_done(1); # flush
39 $port->read( length($cmd) + 1 ); # slurp echo
41 my $ret = $port->read(1);
43 while ( my $c = $port->input ) {
44 print STDERR "<<< ",dump($ret . $c), $/;
46 # last if $c =~ m/^OK\r/;
48 goto again unless $ret =~ s/\r\n(OK|ERROR)\r\n$//s || warn "ERROR: can't find OK or ERROR status!\n";
50 print "$cmd [$message] = $ret\n";
54 #c 'ATE0' => 'echo off';
59 c 'AT+CPIN?' => 'SIM pin';
61 my $manufacturer = c 'AT+CGMI' => 'manufacturer';
63 c 'AT+CGMM' => 'model';
65 c 'AT+CGMR' => 'revision';
67 c 'AT+CGSN' => 'serial';
69 c 'AT+GCAP' => 'capabilities';
71 c 'AT+CIMI' => 'IMSI';
73 c 'AT+CNUM' => 'MSISDN';
75 #c 'AT+COPS=?' => 'operators'; # times out
77 c 'AT+CLCK=?' => 'facility lock';
79 if ( $manufacturer =~ m/Huawei/i ) {
83 c 'AT^HWVER' => 'hardware version';
85 c 'AT^CVOICE?' => 'is voice enabled?';
87 c 'AT^DDSETEX=?' => 'voice output';
89 c 'AT^SYSINFO' => 'sys info';
91 } elsif ( $manufacturer =~ m/ZTE/i ) {
95 c 'AT+GMR' => 'revision';
103 $port->close || die "can't close port $!";
105 warn "## $device closed\n";