Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007087]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2018-01-21 00:35 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.