Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Re: Com and Serial Ports

by nofernandes (Beadle)
on Sep 26, 2003 at 17:21 UTC ( [id://294504]=note: print w/replies, xml ) Need Help??


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

I have this code that i got from here.

Iīve used as you said the binmode $ob->{LOG}, ':UTF8'; but the problem is that it canīt catch the correct format!

How can i change the format in order to save the data from the serial port to the file correctly?

#!/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: $!"; $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"; binmode $ob->{LOG}, ':UTF8'; select(LOG), $| = 1; # set nonbuffered mode, gets the chars out NOW open(DEV, "<$PORT") || die "Cannot open $PORT: $_"; while($_ = <DEV>){ print LOG $_; } undef $ob;

Thank you very much!

Nuno Fernandes

Replies are listed 'Best First'.
Re: Re: Re: Com and Serial Ports
by BrowserUk (Patriarch) on Sep 26, 2003 at 19:20 UTC

    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.

      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-24 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found