Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Difficulties with Device::SerialPort

by EvanK (Chaplain)
on Sep 07, 2005 at 13:52 UTC ( [id://489872]=perlquestion: print w/replies, xml ) Need Help??

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

I've written an app in a win32 environment that i'm now trying to port over to linux. I was using Win32::SerialPort, and now am trying to use Device::SerialPort. The thing is, I keep getting these errors, and I dont understand what they mean.

Every time I try to read from the serial port with the module's embedded read() or input() methods, i get Error #9 in Device::SerialPort::read. Now, they're not fatal errors, but the result is that I cannot read ANY data from the serial port.

I've traced this Error #9 through the module's source code and I think it's a POSIX error in the read_vmin() and input() subs, but I can't figure out anything further. I emailed the author of the module, and he had no idea what that error msg meant. Any ideas from a wiser monk than I?

__________
Give a man a match and he'll be warm for an hour. Set him on fire and he'll be warm for the rest of his life.

Replies are listed 'Best First'.
Re: Difficulties with Device::SerialPort
by sgifford (Prior) on Sep 07, 2005 at 15:16 UTC
    Assuming "Error #9" is a standard Unix error, it's EBADF, or "Bad file number". That probably means the device isn't being opened properly. Are you checking for possible errors after opening the device? Can you get your program down to a few lines that demonstrate the problem, and post that here?
      hmmm...I've tried this two ways, one with jsut the built-in i/o methods, and another way with a tied filehandle. here's what i'm usign right now:
      # note, ive tried ttyS0 and S1, same error with both $port = '/dev/ttyS0'; # creates a hidden config file in user's home dir $config = '~/.conf'; # this tests for an existing config file, # and creates one if necessary... if(! -e $config) { $PortObj = new Device::SerialPort ($port) || die(RED,"Can't open $port: $^E\n",RESET); $PortObj->databits(7); $PortObj->baudrate(19200); $PortObj->parity("even"); $PortObj->stopbits(1); $PortObj->handshake("xoff"); $PortObj->buffers(4096, 4096); $PortObj->write_settings || undef $PortObj; $PortObj->save("$config"); $PortObj->close || die(RED,"failed to close",RESET); undef $PortObj; } # and this connects to the port by loading said # config file and tying to SERIAL filehandle $PortObj = tie (*SERIAL, 'Device::SerialPort', "$config") || die(RED,"Can't open $port: $^E\n",RESET); # use non-blocking read on serial port $PortObj->read_char_time(5); $PortObj->read_const_time(500); # create flag variables initialized to zero... my $EXIT = 0; # ...when a trapped system signal is sent, set flag to 1 $SIG{'INT'} = sub { $EXIT = 1 };
      i've used this before with a tied filehandle, so it should work.

      __________
      Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
      - Terry Pratchett

        While, probably somewhat lateral to your current problem, please be aware that despite documentation to the contrary, the tied-handle interfaces of the Win32::SerialPort and Device::SerialPort modules are not the same - If you have a read through the source for Device::SerialPort, you will find that the TIEHANDLE subroutine calls the new method in place of the start method as what occurs in Win32::SerialPort - A convenience in Device::SerialPort which means that you do not need to have a configuration file for the serial port previously written, but one which breaks compatability with Win32::SerialPort.

         

        perl -le "print unpack'N', pack'B32', '00000000000000000000001000000000'"

Re: Difficulties with Device::SerialPort
by zentara (Archbishop) on Sep 08, 2005 at 10:17 UTC
    Here is an old usenet posting which has some good tips in, which you might try. It works on linux.
    #!/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 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

    I'm not really a human, but I play one on earth. flash japh
      Hi Monks, I am using the above code to read the data coming from a X-bee module connected to one of the usb ports. I the code is not giving any error nither it is printing any thing into the file. It is going into some infinite loop!! Thanks in Advance. shahbaz
Re: Difficulties with Device::SerialPort
by 5mi11er (Deacon) on Sep 07, 2005 at 15:54 UTC
    Complete shot in the dark, but make sure you have appropriate permissions set on the /dev/ttyS0 files. That has bitten me in the past...

    -Scott

      already thought of that, i even tried running the script as root.

      it seems to have no problem with me print()ing to the serial port, but reading in any data, it's as if the module doesnt even see the port is there.
      # it seems to have no problem here print SERIAL $IM{'POL'}; # but HERE... read(SERIAL,$data,$BUFFER);

      __________
      Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
      - Terry Pratchett

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2024-04-16 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found