CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Clearing read buffer for SerialPort IO

 



_saiko
New User

Aug 31, 2012, 9:21 AM

Post #1 of 1 (1226 views)
Clearing read buffer for SerialPort IO Can't Post

Hi,

I'm writing a simple script to get data from temperature sensors on rs232.
It's query-answer based so I first need to send the query in the right form and subsequently read/parse the answer.
Additional sync query before the actual one is needed.

How can I correctly flush the read buffer so I only get the last answer and not everything that was received from sensors?
I tried with lookclear but that doesn't help me.
The $answer ends up having data received after all three queries (writes).

Code
#!/usr/bin/perl -l 

use Device::SerialPort;

$LOGDIR = "/home/vobelic/serial-comm"; # path to data file
$LOGFILE = "lb486.log"; # file name to output to
$ob = "/dev/ttyS1"; # port to watch

$ob = Device::SerialPort->new($ob) || die "Can't Open $ob: $!";

$ob->baudrate(9600) || die "failed setting baudrate";
$ob->parity("none") || die "failed setting parity";
$ob->databits(8) || die "failed setting databits";
$ob->stopbits(1) || die "failed setting stopbits";
$ob->stty_icrnl(1) || die "failed setting convert cr to new line";
$ob->handshake("none") || die "failed setting handshake";

$ob->write_settings || die "no settings";


open( LOG, ">>${LOGDIR}/${LOGFILE}" )
|| die "can't open smdr file $LOGDIR/$LOGFILE for append: $SUB $!\n";

select(LOG), $| = 1; # set nonbuffered mode, gets the chars out NOW



while (1) {
$ob->lookclear;

# sync
$ob->write("\x7e\x00\x00\x00\x00\x00");
$ob->write_drain;
sleep(1);

# get time
$ob->write("\x7e\x00\x00\x03\x00\xfd");
$ob->write_drain;
sleep(1);

$ob->lookclear;
# get data
$ob->write("\x7e\x00\x00\x07\x00\xf9");
$ob->write_drain;
sleep(1);

my $answer = $ob->input;
# or my $answer=$ob->read(255);
print LOG $answer;

}

$ob->close;
undef $ob;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives