Google Answers Logo
View Question
 
Q: win32::serialport problem ( Answered,   1 Comment )
Question  
Subject: win32::serialport problem
Category: Computers > Programming
Asked by: patricksullivan-ga
List Price: $4.00
Posted: 13 May 2002 14:35 PDT
Expires: 12 Jun 2002 14:35 PDT
Question ID: 16034
I am trying to get my Windows activestate Perl script to talk to my
Basic Stamp. The perl script uses win32::serialport and the 'lookfor'
method. The perl side has something wrong that makes it only accept my
lookfor words if I type them very slowly, which the stamp can't do.
whats going on?!?! I'm using Win2k

Request for Question Clarification by axe-ga on 13 May 2002 15:55 PDT
Hi,

Is there somewhere you could upload your code, or paste the specific
"problem area" here? It would be helpful in determining the exact
problem.

Thanks!

Clarification of Question by patricksullivan-ga on 13 May 2002 16:23 PDT
Here is the code:
The program does not alway 'see' and trap the incoming are_match words
(NL,START, OKH, etc)

PAtrick

use Win32::SerialPort 0.06;

$Configuration_File_Name = 'testconf.cfg';
$PortName = "COM1";
$PortObj = new Win32::SerialPort ($PortName, $quiet)
       || die "Can't fucking open $PortName: $^E\n";    # $quiet is
optional

  $PortObj->user_msg(ON);
  $PortObj->databits(8);
  $PortObj->baudrate(2400);
  $PortObj->parity("none");
  $PortObj->stopbits(1);
  $PortObj->handshake("none");
  $PortObj->buffers(4096, 4096);
  $PortObj->write_settings || undef $PortObj;
  $PortObj->save($Configuration_File_Name);
  $PortObj->are_match('NL', 'OKW', 'OKH', 'START', 'NEWOK');




sub waitstamp {

  print "Waiting...";
  $waitstr = waitfor(5);
  if ($waitstr) {
    print "Rec'd $waitstr\n";
    return TRUE;
  } 
  else {
    print "Timed Out\n";
    die "Timed Out";
    return FALSE;
    }
}


    use GD;
$file = 'mecopy.png';
$image = GD::Image->newFromPng($file);
($width,$height) = $image->getBounds();

# start communication
$PortObj->write("BLAH GET READY!!!\r");

print "Sending NEW\n";
$PortObj->write("NEW");
sleep 1;
if (!waitstamp(20)) { print "new: timed out"; }

print "$width x $height\n";
#tell the printer the dimensions
print "Sending Width\n";
$PortObj->write("WID$width");
if (!waitstamp(10)) { print "timed out";  byebye(); }

print "Sending Height\n";
$PortObj->write("HET$height");
if (!waitstamp(10)) { print "timed out";  byebye(); }

#send the data lines

for ($i=0;$i<$height;$i++) {
 
    for ($j=0;$j<$width;$j++) {
        $index = $image->getPixel($j, $i);
        ($r,$g,$b) = $image->rgb($index);
        if ($g == 255) { 
	    $PortObj->write("0");
	    print "0";
	  }
        else { 
	    $PortObj->write("1");
	    print "1";
	  }
    }
    print "\n";
#wait for confirm
if (!waitstamp(120)) { 
  print "timed out";
  byebye();
 }

} #next line


  $PortObj->close || die "failed to close";
  undef $PortObj;                               # frees memory back to
perl


sub waitfor {
    my $timeout=$PortObj->get_tick_count + (1000 * shift);
    $PortObj->lookclear;  # clear buffers
    my $gotit = "";

    for (;;) {
        return unless (defined ($gotit = $PortObj->lookfor));
        if ($gotit ne "") {
            my ($found, $end) = $PortObj->lastlook;
            return $gotit.$found;
        }
        return if ($PortObj->reset_error);
	return if ($PortObj->get_tick_count > $timeout);
    }
}
Answer  
Subject: Re: win32::serialport problem
Answered By: ozguru-ga on 01 Jun 2002 01:10 PDT
 
Dear Sir,

While it is not clear what sort of testing you may have already
undertaken, I hope the following information will be helpful.

From your question, it seems that your code works slowly, but possibly
unreliably.

Initially, eliminating hardware problems or trying hardware handshake
is an extremely useful step when debugging serial communication
problems.

Peter Anderson’s excellent tutorial addresses such issues as the
different voltage levels available from the Basic Stamp’s various
ports, the need for RX AND TX even if only communicating in one
direction and the details of the Basic Stamps hardware handshake pin
loopbacks. I appreciate that these will be of little relevance if you
have already established a reliable connection between the Basic Stamp
and the PC using another program or application.

I am afraid I could not see where the parameters such as 10, 20 and
120 were passed to waitstamp this may account for some timing issues.
The WebReference.com link is quite relevant to parameter passing. It
was not clear from the question as to whether any of your timeout
errors had been triggered. I apologise if I am missing something.

Peter H. Anderson’s excellent tutorial specifically dealing with
serial communications for the Basic Stamp.
http://www.phanderson.com/stamp/tutorial_10.html

WebReference.com: Chapter 7  Subroutines from Professional Perl by
Peter Wainwright, Aldo Calpini, Arthur Corliss, Simon Cozens,JJ
Merelo-Guervós, Chris Nandor, Aalhad Saraf, Wrox Press Ltd.
http://www.webreference.com/programming/perl/professional/chap7/3/

This may be of more general interest for other Basic Stamp projects
ideas.
Nuts & Volts Magazine has about 30 mainly hardware projects for the
Basic Stamp
http://www.nutsvolts.com/stmpindx.htm
Comments  
Subject: Re: win32::serialport problem
From: acetylene5-ga on 22 Jun 2004 09:20 PDT
 
I ran into the same problem while trying to capture a data dump being
fed into a serial port.  I'm by no means a perl expert, and my code
differed a little from yours, but I found if I removed the ->lookclear
lines and any ->matchclear lines it worked fine.  My symptoms were as
follows:

Line sent to port:
0005 000000 000012 000256 000334 000495\n

Line returned by faulty program
0012 000256 000334 000495\n

Once I took out the ->lookclear line, it worked fine.  Not exactly
sure why, but I'll take it.

Hope this helps,

Acetylene5

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy