Advertisement

03.14.2008 at 07:21AM PDT, ID: 23241755
[x]
Attachment Details

Flushing the serial input port with PERL

Asked by tedzini in Perl Programming Language

Tags: CPAN, PERL, 5.8.8, running on Linux

I am reading a simple ASCII string from /dev/ttyS0 continuously from a backgrounded perl script.
The device is sending a small "ALOHA" string every ~0.5 seconds. After a really long time, the script
seems to hang on the read. In the main body, I do intentionly sleep for a second for various reasons.
I am thinking there could be a buffer overflow, etc.

How can I  reliably flush all the input on the serial input?

I have pasted the subs below regarding opening and reading the serial port.

Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
sub initSerialPort {
	local $portnum = shift;
	local $serdev = "ttyS" . $portnum;
	print "$serdev\n";
	local $serport;
	open ( $serport, "+</dev/$serdev") || die "cannot open serial port $serdev: $!\n";
	system ("stty 2400 raw </dev/$serdev");
	system("stty -F /dev/$serdev -a > $serdev-config.txt");
	return $serport
}
 
sub readUntil {
	my $port = shift;
	my $pat = shift;
	my $data = "";
	my $buf = "";
	my $br;
	my $timeout = 10000;
 
	do {
		$br = sysread($port, $buf, 256);
		$data.=$buf if $br > 0;
	} while ( $data !~ /$pat/i && $timeout--);
 
	return $data;
 
Loading Advertisement...
 
[+][-]03.15.2008 at 08:09AM PDT, ID: 21132886

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.15.2008 at 11:45AM PDT, ID: 21133783

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 07:01PM PDT, ID: 21157896

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Perl Programming Language
Tags: CPAN, PERL, 5.8.8, running on Linux
Sign Up Now!
Solution Provided By: Adam314
Participating Experts: 2
Solution Grade: B
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628