win32::SerialPort are_match just won't work

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


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.

#!/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(Win32::GetTickCount() > $timeout)
            my ($match, $after, $pattern, $instead) = $ob->lastlook;
            print "match \t<".$match.">\nafter \t<".$after .">\npattern
\t<".$pattern.">\ninstead \t<".$instead.">\n";
            return "TIMEOUT";

$ob = Win32::SerialPort->start ($file) or die
      "Can't start $file\n";

$ob->error_msg(1); # use built-in error messages

$ob->are_match("BUSY","CONNECT","NO DIALTONE",
                  "NO CARRIER","NO ANSWER","-re",

my $reset = 1;
  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;
  print "...\n";

Re: win32::SerialPort are_match just won't work

gormanst schreef:

Quoted text here. Click to load it

You don't mention that you have a working modem attached.

Did you test with HyperTerminal (or alike)? Is the COM1_test.cfg OK?

Quoted text here. Click to load it

Why do you think that a regex is involved in the problem?

Quoted text here. Click to load it

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:

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."

Site Timeline