in reply to Re: Re: Com and Serial Ports
in thread Com and Serial Ports

Your applying binmode to the output file, by which time I think the damage has already been done.

What I was suggesting was to apply binmode to the input file. Whilst you don't open this directly, looking at the source of Device::SerialPort, I noticed that the filehandle it use for the serial port is accessable via the object as $obj->{FD}, hence it should be possible to enable the :utf8 IO layer on it using the snippet I included. I've modified your snippet

#!/usr/bin/perl use warnings; use Device::SerialPort 0.22; $LOGDIR = "/home/neo/telex_lusa/log"; $LOGFILE = "geiger.log"; $PORT = "/dev/ttyS0"; $ob = Device::SerialPort->new ($PORT) || die "Can't Open $PORT: $!"; ## MODIFIED HERE!! ## FD is the key used internally by D::SP for the file handle ## NOTE: The IOLayer is ':utf8' not ':UTF8'! binmode $ob->{FD}, ':utf8'; $ob->baudrate(2400) || 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(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: $_"; while($_ = <DEV>){ print LOG $_; } undef $ob;

As I said, I've no way to verify this. For the record and those that come after you, please post a reply stating one way or another whether this works.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

Replies are listed 'Best First'.
Re: Re: Re: Com and Serial Ports
by nofernandes (Beadle) on Sep 29, 2003 at 10:06 UTC

    Thank you for your reply and for your help!

    Your snippet gives me the following error binmode() on unopened filehandle 3 at nelo.pl line 16., do you have a clue why?

    Thank you!!

    Nuno

      Sorry, but I don't. If you have called $obj = Device::SerialPort->new(...), and not received a message telling you "can't open device: ...", then the port should be open and the handle obj->{FD} should be valid.

      If the port failed to open during the new() call, then you would have received a null return from new() and your attempt to use $obj->{FD} in the call to binmode would have resulted in a "Use of uninitialized value in ref-to-glob cast at ..." message.

      Without seeing the code you used, it is impossible to try and diagnose this further.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      If I understand your problem, I can solve it! Of course, the same can be said for you.