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: Intermediate:
Serial data linebreaks in string causing problems

 



mrhedin
Novice

Dec 16, 2009, 5:11 AM

Post #1 of 9 (1514 views)
Serial data linebreaks in string causing problems Can't Post

I've run into some problems reading RS-232 data from a serial port, only because the serial data is sent a bit funny from a datalogger and I can not do anything about it.

I'm on a linux machine using the method:

Code
system ("stty -F /dev/ttyS0 raw cs8 ispeed 9600"); 
open (SERIAL, "/dev/ttyS2");
unknown code
close (SERIAL);



The serial data enters like:

Code
[ 7.98241 ; 8.41986 ; 
238.157 ; 3.36645 ; 95.6777
; 1012 ; 13.4016 ;
23.5719 ]


The "linebreaks" causes me problems to get it into a variable.
I would like to store it in a single string like


Code
[ 7.98241 ; 8.41986 ; 238.157 ; 3.36645 ; 95.6777 ; 1012 ; 13.4016 ; 23.5719 ]


If it would have been sent as a single string from the datalogger, I guess i could have matched against the [ and ] but with the linebreaks i can not get it to work.

Does anyone have an idea?


FishMonger
Veteran / Moderator

Dec 16, 2009, 5:39 AM

Post #2 of 9 (1508 views)
Re: [mrhedin] Serial data linebreaks in string causing problems [In reply to] Can't Post

Take a look at Device::SerialPort
http://search.cpan.org/~cook/Device-SerialPort-1.04/SerialPort.pm


mrhedin
Novice

Dec 16, 2009, 6:00 AM

Post #3 of 9 (1502 views)
Re: [FishMonger] Serial data linebreaks in string causing problems [In reply to] Can't Post

I have alreade had a look there but couldn't really find anything that would get me started. I have earlier red serial data quite easily if the string would be sent as a single line, but theese linefeeds mess me up a bit...


FishMonger
Veteran / Moderator

Dec 16, 2009, 6:06 AM

Post #4 of 9 (1500 views)
Re: [mrhedin] Serial data linebreaks in string causing problems [In reply to] Can't Post

Have you tried using a regex or tr to strip out the "linebreaks"?


(This post was edited by FishMonger on Dec 16, 2009, 6:06 AM)


mrhedin
Novice

Dec 16, 2009, 6:12 AM

Post #5 of 9 (1497 views)
Re: [FishMonger] Serial data linebreaks in string causing problems [In reply to] Can't Post

No I haven't. How do you mean?


(This post was edited by mrhedin on Dec 16, 2009, 6:13 AM)


FishMonger
Veteran / Moderator

Dec 16, 2009, 6:20 AM

Post #6 of 9 (1494 views)
Re: [mrhedin] Serial data linebreaks in string causing problems [In reply to] Can't Post

Something like this:

Code
while(<SERIAL>) { 
s/\n//g;
# process the data
}



mrhedin
Novice

Dec 16, 2009, 6:21 AM

Post #7 of 9 (1492 views)
Re: [FishMonger] Serial data linebreaks in string causing problems [In reply to] Can't Post

Ok, thanks, will give it a try right away...


mrhedin
Novice

Dec 17, 2009, 9:02 AM

Post #8 of 9 (1471 views)
Re: [mrhedin] Serial data linebreaks in string causing problems [In reply to] Can't Post

Your suggestion got me started and solved it. Thanks!


Code
system ("stty -F /dev/ttyS0 raw cs8 ispeed 9600"); 
open(SERIAL, "/dev/ttyS0");

while(1)
{
$i = 0;
until($i > 3)
{
$read[$i] = <SERIAL>;
$read[$i] =~ s/\n//g;
$i = $i + 1;
}


open(OUTFILE, ">>$bcppath/test/current.dat") or die "Hedin error: [$!]";
print OUTFILE "$read[0] $read[1] $read[2] $read[3]\n";
close(OUTFILE);
}


turned this serial data:


Code
[ 7.73941 ; 8.27391 ; 

241.502 ; 3.58776 ; 95.6792

; 1012 ; 13.4134 ;

23.7773 ]


into nice serial data:


Code
[ 8.27401 ; 8.27391 ; 238.768 ; 3.5084 ; 95.667 ; 1012 ; 13.4218 ; 23.5682 ]



(This post was edited by mrhedin on Dec 17, 2009, 9:03 AM)


FishMonger
Veteran / Moderator

Dec 17, 2009, 10:01 AM

Post #9 of 9 (1464 views)
Re: [mrhedin] Serial data linebreaks in string causing problems [In reply to] Can't Post

I'd probably write that as:

Code
open my $OUTFILE, '>>', "$bcppath/test/current.dat" 
or die "Hedin error: [$!]";

my $data;
while ( <SERIAL> ) {
chomp;
$data .= $_;
if ( ($. % 4) == 0 ) {
print $OUTFILE "$data\n";
$data = '';
}
}



(This post was edited by FishMonger on Dec 17, 2009, 10:02 AM)

 
 


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

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