Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

How to read the RS-232 lines?

by Anonymous Monk
on Oct 02, 2000 at 11:11 UTC ( #34876=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to read input from the serial port from a tone-decoding PIC controller, but so far have failed miserably. The incoming number is 6 digits long. Any ideas or examples I might have look at will be most welcome.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How to read the RS-232 lines?
by Corion (Patriarch) on Oct 02, 2000 at 11:19 UTC
Re: How to read the RS-232 lines?
by kb2yht (Acolyte) on Apr 30, 2001 at 14:05 UTC
    I use Device::SerialPort for talking to the PIC UART:
    use Device::SerialPort; my $port = Device::SerialPort->new( "/dev/ttyS0" ); $port->baudrate(9600); $port->databits(8); $port->stopbits(1); $port->parity("none"); my $string = ''; my $more = 1; while($more) { my $pass = $port->write('S$'); my $end_seen = 0; while ( not $end_seen ) { local $_ = $port->input; if ( $_ ne '' ) { if ( m/S:/ ) { # string start $string = $_; } elsif ( m/:$/ ) { # string end $string .= $_; print $string; # deal with the final string $end_seen=1; $more=0; } else { $string .= $_; } } } }
    Be warned - If you are trying to get something out cleanly in one try, it will probably not work. I found that I have to interrogate the PIC over and over to keep the UART alive and talking to me.
Re: How to read the RS-232 lines?
by Anonymous Monk on Oct 20, 2003 at 14:26 UTC
    There is Win32API::CommPort.
    Once upon a time I used to write testbed code for hardware I designed. Generally the system control and monitoring was all done on RS-232 ports. We were using Windows, and for at least the last 5 or 6 years that I can recall we always did our testing in the lab with Perl.
Re: Answer: Reading RS232 inputs.
by zentara (Archbishop) on Oct 20, 2003 at 14:13 UTC
    Hi, I don't remember where I got this, I think it was off of freshmeat. It is a guy who used the serial port to capture a geiger counter output.
    #!/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 # # 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 fil +e $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 e +ach #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
Re: Reading RS232 inputs.
by wee_chang2002 (Initiate) on Dec 25, 2002 at 03:18 UTC
    i just wrote:

    use Device::SerialPort;


    $port = new Device::SerialPort ( "/dev/ttyS0");

    then run:
    root@linux5 root# perl -c syntax OK
    root@linux5 root# perl -W
    (in cleanup) Can't call method "setcflag" on an undefined value at /usr/lib/perl5/site_perl/5.8.0/Device/ line 541 during global destruction.

    can some tell me what is wrong??

    Originally posted as a Categorized Answer.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://34876]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2023-12-11 21:11 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (41 votes). Check out past polls.