Tue 12 Jun 2007
Automate serial devices communications with expect-perl & cu
Enviado al blog por el pinche cash segun el bajo la categoria Linux, Perl for Retarded ppl, Software, Ubuntu
Sin chismes (se el primer chismoso!)
Initial problem:
“I’m trying to use expect to automate the communications with a serial modem using cu, but i never got answer to thr AT commands from the modem .”
Code:
$rootexpect->expect($timeout, "nnected");
$rootexpect->send("AT\n");
$rootexpect->expect($timeout,"OK");
my $x;
$rootexpect->interact($x, 'XXX');
Here is the debug output (Looks like modem is not acepting the \n after AT)
spawn id(4): Does `\007Connected.\r\n'
match:
pattern #1: -ex `nnected'? YES!!
Before match string: `\007Co'
Match string: `nnected'
After match string: `.\r\n'
Matchlist: ()
spawn id(4): list of patterns:
#1: -ex `OK'
spawn id(4): Does `.\r\n'
match:
pattern #1: -ex `OK'? No.
spawn id(4): Does `.\r\n\nAT\n'
match:
pattern #1: -ex `OK'? No.
With the script @ interact mode AT responds fine
at
OK
I connected a cisco router’s console port (instead of the modem), changed the expect commands and the router successfully returns the output of the command ‘sh ver’…
The solution to the problem…
$rootexpect->exp_internal('2');
$rootexpect->expect($taim, "nnected");
$rootexpect->send("AT\015\012");
$rootexpect->expect($taim,"OK");
$rootexpect->send("ATDT 99999999\015\012");
my $x;
$rootexpect->interact($x, 'XXX');
Explanation:
(Taken from http://www.rocketaware.com/perl/perlfaq8/How_do_I_read_and_write_the_seri.htm )
Some devices will be expecting a “\r” at the end of each line rather than a “\n”. In some ports of perl, “\r” and “\n” are different from their usual (Unix) ASCII values of “\012” and “\015”. You may have to give the numeric values you want directly, using octal (“\015”), hex (“0x0D”), or as a control-character specification (“\cM”).
print DEV “atv1\012″; # wrong, for some devices
print DEV “atv1\015″; # right, for some devices
Even though with normal text files, a “\n” will do the trick, there is still no unified scheme for terminating a line that is portable between Unix, DOS/Win, and Macintosh, except to terminate ALL line ends with “\015\012”, and strip what you don’t need from the output. This applies especially to socket I/O and autoflushing,
Incoherencias parecidas y calificaciones a este post:

