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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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


New User

Aug 31, 2012, 9:21 AM

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


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).

#!/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) {

# sync

# get time

# get data

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


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