Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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]
[shmem]: anyways, do as is your wont. I won't fight western hypocresy anymore - for today. Let's call that a day, and done.
[erix]: yeah, scanning pre-loaded. I wouldn't have it any other way. As I say, my attention is a rare commodity, jealously guarded.
[erix]: ok, ok. happy pipe-dreams :P
[shmem]: have a good time inside your information bubble.
[erix]: perhaps we can have common hindsight at these thing in a few years time

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2018-03-19 21:17 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (246 votes). Check out past polls.