Debian Codegraph @

tutorial/6.B.modem-init

raw
1 #!/usr/bin/perl
2 # This is a very brief script that demonstrates send_slow(). This
3 #command is chiefly for talking to modems and the like where if you send
4 #it stuff too fast it can get mad at you.
5 # Also demonstrated is how to talk to a bidirectional device
6 #handle.
7 #
8 # This example has been changed in 1.05 so that it actually works. :-)
9
10 use Expect;
11
12 $device="/dev/modem" unless $device = shift(@ARGV);
13
14 # open the device
15 open(DEVICE,"+>$device") || die "Couldn't open $device, $!\n";
16
17 # Expectize it. note the \* which we do because we didn't use the OO
18 # method of opening the device.
19 $modem=Expect->exp_init(\*DEVICE);
20 # Let's watch the output. Once again, output isn't automatically
21 # watched since it was an initialized handlde and not a process.
22 $modem->log_stdout(1);
23
24 # Put it in raw mode w/ no echoing so it will operate as expected.
25 $modem->exp_stty('raw','-echo');
26
27 # Send it an ATZH0\r to reset its state.
28 print "Initializing modem\n";
29 $modem->send_slow(.5,"ATZH0\r");
30
31 # How about we look for an OK?
32 $modem->expect(30,'-re','^OK\r?$')|| warn "Never got OK from modem\n";
33 print "Modem initialized.\n";
34
35 # Now let's interact with it so the user can talk to it, dial it out,
36 # whatever. This is basically a poor man's term program.
37 # When you interact() with a handle it puts STDIN in raw mode so
38 # you are assured of interacting cleanly, no escape chars, etc.
39 # This is usually what you want. Here however it's nice to be able to
40 # escape from the session without doing any extra work. So let's make
41 # an escape character of ^W.
42 print "Beginning interaction, escape character is ^W.\n";
43 $modem->interact(\*STDIN,"\027");
44
45 # Done? Reset the modem, close the handle.
46 print $modem "+++";
47 sleep 3;
48 print $modem "ATZH0\r";
49 sleep 1;
50 # Grab any output ready on the handle, toss it.
51 $modem->expect(0);
52 $modem->hard_close();
53
54
55