Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading serial port with Perl

620 views
Skip to first unread message

PčéJäy

unread,
Mar 17, 2004, 1:18:45 AM3/17/04
to
Hi everyone,

I need a simple way to get the input from a serial port one line at a time
and put it into a variable, using Perl.

I've searched Google for the last 30 mins or so but everything is too
complicated for me to understand... I don't have much experience with perl.

Any examples/pointers would be much appreciated!!


Paul.j


PčéJäy

unread,
Mar 17, 2004, 7:39:25 PM3/17/04
to
Brilliant!!! Thanks.


"zentara" <zen...@highstream.net> wrote in message
news:lffg50521nlousijr...@4ax.com...

> I found this googling once before, but I can't find the link again, so
> I will just show it.
> #!/usr/bin/perl
>
> #the ubiquitous RS-232 Serial Port. built an interface
> #for my Aware Electronics Geiger Counter (RM-70) that
> #used a Basic Stamp IISX chip set. Every 20 seconds it
> #would spit out an ASCII string of radiation and
> #temperature data. I had hunted around for examples
> #using the Device::SerialPort module and found many.
> #Most were copies of code used to read PBX data. But
> #it wouldn't work. Ouch. Finally, after much research
> #I realized that - by golly - you had to terminate
> #the IO with a new line and not a carriage return.
> #Oddly doing a cat </dev/ttyS0 worked, which faked me
> #out. So I added the line in the tty setup below that
> #converts CR to NL and zap-ity-do-dah it started terminating
> #and sending each read. Long live RS-232. Please send
> #along any suggestions and improvements. This also probably
> #explains why I had failed to get good reads from a
> #cheap-o RS-232 capable DVM a few years ago.
>
> #This was done in Perl 5.8.0 under RH Linux 9.0.
>
> #file geiger.pl
> #
> # Author: David Drake
> # Date: July 18, 2003
> # Requirements: Device::SerialPort 0.22 (from cpan July 2003)
> #
> # Version: 1.0
> #This script is used to read a serial port to obtain data from a
> #combined Geiger counter and temperature sensor.
> #The Geiger Counter is an Aware Electronics RM-70 unit. Each count
> #maps to one microR per hour. The RM-70 pulse output is sent to a
> #Basic Stamp-IISX microcontroller. The BS2SX accumulates counts for 20
> #seconds and then sends a serial data stream out of its serial port.
> #The data stream goes into the input serial port on the Linux system.
> #This program then tabulates the data to a log file.
>
> use Device::SerialPort;
> use Time::gmtime;
>
> $LOGDIR = "/home/zentara/perlplay/serial-comm"; # path to data file
> $LOGFILE = "geiger.log"; # file name to output to
> $PORT = "/dev/ttyS1"; # port to watch
>
> #
> #
> # Serial Settings
> #
>
> #make the serial port object
> #note the need to convert carriage returns to new lines to terminate
> each
> #read.
>
> $ob = Device::SerialPort->new($PORT) || die "Can't Open $PORT: $!";
>
> $ob->baudrate(9600) || die "failed setting baudrate";
> $ob->parity("none") || die "failed setting parity";
> $ob->databits(8) || die "failed setting databits";
> $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 the logfile, and Port
> #
>
> 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
>
> open( DEV, "<$PORT" ) || die "Cannot open $PORT: $_";
>
> #
> # Loop forver, logging data to the log file
> #
>
> while ( $_ = <DEV> ) { # print input device to file
> $gmc = gmctime();
> print LOG $gmc, " ", $_;
> }
>
> undef $ob;
>
> #we are done dude
> __END__
>
>
>
> --
> I'm not really a human, but I play one on earth.
> http://zentara.net/japh.html


Grant Edwards

unread,
Mar 17, 2004, 10:55:04 PM3/17/04
to
On 2004-03-18, PèéJäy <spam_m...@waterlogic.com.au> wrote:

> Brilliant!!! Thanks.

[100+ lines clipped]

You had to quote all 100+ lines just to add that? I guess what
they say about Perl is right...

--
Grant Edwards grante Yow! HELLO KITTY gang
at terrorizes town, family
visi.com STICKERED to death!

PčéJäy

unread,
Mar 18, 2004, 3:05:20 AM3/18/04
to
"Grant Edwards" <gra...@visi.com> wrote in message
news:40591d98$0$41287$a186...@newsreader.visi.com...

> On 2004-03-18, PèéJäy <spam_m...@waterlogic.com.au> wrote:
>
> > Brilliant!!! Thanks.
>
> [100+ lines clipped]
>
> You had to quote all 100+ lines just to add that? I guess what
> they say about Perl is right...

Sorry, I didn't realise you were using a 1200 Baud modem...


Grant Edwards

unread,
Mar 18, 2004, 10:16:02 AM3/18/04
to
On 2004-03-18, PèéJäy <spam_m...@waterlogic.com.au> wrote:
> "Grant Edwards" <gra...@visi.com> wrote in message
> news:40591d98$0$41287$a186...@newsreader.visi.com...
>> On 2004-03-18, PèéJäy <spam_m...@waterlogic.com.au> wrote:
>>
>> > Brilliant!!! Thanks.
>>
>> [100+ lines clipped]
>>
>> You had to quote all 100+ lines just to add that? I guess what
>> they say about Perl is right...
>
> Sorry, I didn't realise you were using a 1200 Baud modem...

The rules of good etiquette and manners often outlive the
practical reasons for the original custom. I doubt you or
anybody you meet ever carries a dagger either, but refusing to
shake hands would still be rude.

--
Grant Edwards grante Yow! Please come home with
at me... I have Tylenol!!
visi.com

0 new messages