Home > Archive > PERL Modules > February 2006 > win32::SerialPort are_match just won't work
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
win32::SerialPort are_match just won't work
|
|
| gormanst 2006-02-15, 9:56 pm |
| All,
I have a simple program. All it is trying to do is issue the "AT"
attention command and grab the OK that is the result of the command
using are_match. It never works. I have tried every permutation of the
regex, seriously like 15 different tries. Please, if you can, tell me
what I am doing wrong! I am going to bang my head into the wall soon.
Thanks in advance.
X?X?X?X?X?X?X??X?X?X?X?X?X?XCODE?X?X?X?X?X?X?X?X?X?X?X?X?X?X?
#!/usr/bin/perl -w
use strict;
use Win32::SerialPort 0.19;
my $ob;
my $file = "COM1_test.cfg";
sub what_is_this( $ );
// Debug function to look inside unknown return....
sub what_is_this( $ )
{
my $item = shift;
my $length = length($item);
my @characters = split(//,$item);
my $temp = $item;
$temp =~ tr/\n/N/;
$temp =~ tr/\r/R/;
my $a = 1;
}
// Standard Cut/Paste waitfor from CPAN examples......
sub waitfor {
my $timeout = Win32::GetTickCount() + (1000 * shift);
$ob->lookclear; # clear buffers
my $gotit = "";
for (;;) {
return unless (defined ($gotit = $ob->streamline));
if ($gotit ne "")
{
my ($found, $end) = $ob->lastlook;
return $found;
}
if($ob->reset_error)
{
return"ERROR";
}
if(Win32::GetTickCount() > $timeout)
{
my ($match, $after, $pattern, $instead) = $ob->lastlook;
what_is_this($instead);
print "match \t<".$match.">\nafter \t<".$after .">\npattern
\t<".$pattern.">\ninstead \t<".$instead.">\n";
return "TIMEOUT";
}
}
}
/// EXECUTION STARTS HERE....
$ob = Win32::SerialPort->start ($file) or die
"Can't start $file\n";
$ob->error_msg(1); # use built-in error messages
$ob->user_msg(1);
$ob->are_match("BUSY","CONNECT","NO DIALTONE",
"ERROR","RING",
"NO CARRIER","NO ANSWER","-re",
'/.*(OK).*/');
my $reset = 1;
while($reset)
{
my $command = "AT";
print "Sending <$command>\n";
$ob->write($command ."\r");
# Wait one second for a response
my $resp = waitfor(1);
if($resp ne "" && $resp =~ /OK/)
{
printf "Received <%s>\n", $resp;
$reset = 0;
}
$ob->write("+++\r");
print "...\n";
}
| |
| Dr.Ruud 2006-02-16, 7:55 am |
| gormanst schreef:
> I have a simple program. All it is trying to do is issue the "AT"
> attention command and grab the OK that is the result of the command
> using are_match. It never works.
You don't mention that you have a working modem attached.
Did you test with HyperTerminal (or alike)? Is the COM1_test.cfg OK?
> I have tried every permutation of the
> regex, seriously like 15 different tries. Please, if you can, tell me
> what I am doing wrong! I am going to bang my head into the wall soon.
Why do you think that a regex is involved in the problem?
> if($resp ne "" && $resp =~ /OK/)
> {
> printf "Received <%s>\n", $resp;
> $reset = 0;
> }
You might make that
if ($resp ne "") {
printf "Received <%s>\n", $resp;
$reset = 0 if $resp =~ /OK/;
}
for testing. Also for testing, add a "\n" member to are_match().
The '/.*(OK).*/' member of the are_match() can be written as '/OK/', so
you don't need a regex for that at all.
See also example 7 here:
http://www.foo.be/docs/tpj/issues/v...j0401-0020.html
If all fails, do a factory reset of the modem. Check the registers (like
s3 and s4) for strange values. Also try a ';' before the end-<CR> of the
AT-command: "AT;\r". Test manually with a program like HyperTerminal.
--
Affijn, Ruud
"Gewoon is een tijger."
|
|
|
|
|