I am not able to read from my serial device. I can successfully write to it and this is verified by watching the status LED on the device. We have a few Ruby programmers in house and, just to verify that the device is definitely working, I had one of them try to do the same in ruby (send "254", "27", print getc), and it works fine. I like perl, so here I am.

The way that this device (an ADSSR4xPROXR, btw) works is, you send a series of commands to it, and it will then send back "85" which acknowledges that it received your commands. For example, sending "254" then "27" will enable reporting mode, to which the device will send back "85".

I have tried all of the following code, and while all of them successfully get my commands to the device, none of them will print the response from the device:

First
#!/usr/bin/perl use strict; use warnings; my $port = "/dev/ttyUSB0"; open (SERIALPORT, "+<", "$port") or die "can't open $port. "; print SERIALPORT chr(254); print SERIALPORT chr(27); my $result = getc(SERIALPORT); print ord($result) . "\n"; close (SERIALPORT);
Second
#!/usr/bin/perl use strict; use warnings; my $port = "/dev/ttyUSB0"; open (SERIALPORT, "+<", "$port") or die "can't open $port. "; select(SERIALPORT), $| = 1; print SERIALPORT chr(254); print SERIALPORT chr(27); my $result = getc(SERIALPORT); print ord($result) . "\n"; close (SERIALPORT);
Third
#!/usr/bin/perl use strict; use warnings; use Device::SerialPort; my $ob = Device::SerialPort->new("/dev/ttyUSB0") || die "Can't open po +rt: $!"; $ob->baudrate(115200) || die "failed setting baudrate"; $ob->parity("none") || die "failed setting parity"; $ob->databits(8) || die "failed setting databits"; $ob->handshake("none") || die "failed setting handshake"; $ob->stopbits(1) || die "failed setting stopbits"; $ob->write_settings || die "failed writing settings"; my $cmd1 = chr(254); my $cmd2 = chr(27); $ob->write($cmd1); $ob->write($cmd2); open(LOG, ">>", "test2.txt") || die "can't open log file: $!"; open(DEV, "<", "/dev/ttyUSB0") || die "can't open prt: $_"; select(LOG), $| = 1; while($_ = <DEV>) { print LOG $_; } undef $ob;
Fourth
#!/usr/bin/perl -w use strict; use warnings; use Device::SerialPort; my $port = Device::SerialPort->new("/dev/ttyUSB0"); $port->databits(8); $port->baudrate(115200); $port->parity("none"); $port->stopbits(1); $port->handshake("on"); $port->write_settings; $port->write(chr(254)); $port->write(chr(27)); my $char = $port->lookfor(); if ($char) { print "$char"; }

In reply to Reading from a serial device by gnu lnx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.