Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Trying to read from serial port - not getting a response

by ColonelPanic (Friar)
on Dec 04, 2012 at 13:22 UTC ( #1007087=note: print w/ replies, xml ) Need Help??


in reply to Trying to read from serial port - not getting a response

The first time you reach the part that reads the response, there probably won't be anything there yet, because it takes some time for the response to come back. However, your outer while loop contains a flush(), which will discard any data before you see it.

I suggest, at least to start out, that you redo your code in a simpler format: open the port only once, send your data, then have a while loop that reads the port until you see data. i.e. use something like this to read the result:

while (1) { my $ret=<PORT> print $ret; if ($ret=~/^OK/ || $ret=~/^ERROR/) { last} }

Caveat: not tested; I don't really have a way to do so.



When's the last time you used duct tape on a duct? --Larry Wall


Comment on Re: Trying to read from serial port - not getting a response
Select or Download Code
Re^2: Trying to read from serial port - not getting a response
by adiuva (Sexton) on Dec 04, 2012 at 13:33 UTC
    I got it "simpler" before changing to this version, but it didn't work either. Here's the modified version:
    use Strict; use IO::Socket; use IO::Select; my $COMport=33; #open Port or die if blocked / wrong port if (!open( PORT, "+>\\\\.\\COM$COMport" )) { die ("Can't open COM$COMport"); } PORT->autoflush(1); PORT->flush(); print "\nEnter AT command:"; my $ATcmd=<STDIN>; chomp($ATcmd); # remove CRLF $ATcmd =~ s/^\s+//; # remove leading space $ATcmd =~ s/\s+$//; # remove trailing space print "Sending: '$ATcmd'\n"; print PORT $ATcmd . "\r\n"; sleep(2); #Reading the response print "reading\n"; while (1) { my $ret=<PORT>; print $ret; if ($ret=~/^OK/ || $ret=~/^ERROR/) { last} } close(PORT); print "done!";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007087]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (19)
As of 2014-08-22 19:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (163 votes), past polls