Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

r/w attached infrared head on /dev/ttyUSB0

by stoerti (Novice)
on May 25, 2019 at 15:43 UTC ( #11100518=perlquestion: print w/replies, xml ) Need Help??

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

Hi there. I tried thousand ways the last weeks and I am desparing.
I need to write and read to /dev/ttyUSB0 where a infrared head is attached to read an electric meter.

The settings are 300 baud 7E1, no handshake
If I do this from shell in that way:
1. cat /dev/ttyUSB0&amp 2. stty -F /dev/ttyUSB0 300 -parodd cs7 -cstopb parenb -ixoff -crtscts + -hupcl -ixon -opost -onlcr -isig -icanon -iexten -echo -echoe -echoc +tl -echoke 3. echo -n -e '\x2F\x3F\x21\x0D\x0A' > /dev/ttyUSB0 4. echo -n -e '\x06\x30\x30\x30\x0D\x0A' > /dev/ttyUSB0
I get an answer as expected:

/LOG4LK13BD202015 C.1.0(06091772) 0.0.0(001LOG0006091772) F.F(0000) 1.8.0(021183.342*kWh) C.7.1(00000000) C.7.2(00000001) C.7.3(00000002) 0.2.1(ver.02, 150228, 671A) C.2.1(1510121019) C.2.9(1510121019)

Now I try to realize in perl to catch this output but all ways I have gone, nothing comes back to perl.
Only using Device::SerialPort I receive the first line "/LOG4LK13BD202015" but not more.

One of many examples:

open( com, "+&lt;","/dev/ttyUSB0" ) || die "Cannot open serial port : +$!\n"; my $raw = "\x2F\x3F\x21\x0D\x0A"; print com $raw; my $raw = "\x06\x30\x30\x30\x0D\x0A"; print com $raw; while( 1 ) { my $in = <com&gt;; print "$in\n"; }
gives me:
/?! 000 /LOG4LK13BD202015

I tried system calls in different way, different ways to open an file handle on the device, some perl modules - nothing worked for me.
Has anyone an idea or realized something like that in the past and can give me some help?

2019-05-28 Athanasius added code tags

Replies are listed 'Best First'.
Re: r/w attached infrared head on /dev/ttyUSB0
by haukex (Bishop) on May 25, 2019 at 16:27 UTC
      Thank you for the answer but it isn't.

      Thats my code:

      use Fcntl qw/:DEFAULT/; use IO::Termios (); use IO::Stty (); sysopen my $fh, '/dev/ttyUSB0', O_RDWR or die "sysopen: $!"; my $handle = IO::Termios->new($fh) or die "IO::Termios->new: $!"; $handle->set_mode('300,7,e,1'); IO::Stty::stty($handle, qw/ raw -parodd cs7 -cstopb parenb -ixoff -crt +scts -hupcl -ixon -opost -onlcr -isig -icanon -iexten -echo -echoe -e +choctl -echoke/); my $raw = "\x2F\x3F\x21\x0D\x0A"; $handle->syswrite($raw) == length($raw) or die "syswrite"; my $raw = "\x06\x30\x30\x30\x0D\x0A"; $handle->syswrite($raw) == length($raw) or die "syswrite"; for (1..300) { my $toread = 1; $handle->sysread(my $in, $toread) == $toread or die "sysread"; print $in; } $handle->close;
      And it is always the same output:

      /?! 000 /LOG4LK13BD202015
      Thats all - thats not, what I need

      2019-05-28 Athanasius added code tags

        Unfortunately, I wasn't able to reproduce anything like the issue you're having on my system, at least using a fake PTY. I ran both a shell script with the commands from the root node and this Perl script under strace -s256 -fe trace=read,write,ioctl, and in both cases the basic outline seemed to be the same, in particular, this bit was the same for both commands:

        write(1, "/?!\r\n", 5) = 5 write(1, "\006000\r\n", 6) = 6

        ... and I was able to send all of the response lines from your root node to the Perl script and it received it just fine. This seems to indicate to me that the device you are talking to is behaving differently for the shell commands and the Perl script, for which I think the most likely explanation is either differing configuration or differences in what gets sent to the device, which is why I was wondering what the difference between the two is, but I don't see it yet.

        I did have some issues where my stty complained about not knowing the modes crtscts onlcr echoctl echoke, but I'm not sure that has to do with the issue. Do you need all of those switches, or would the simpler form I showed in my node, cs7 parenb -parodd raw -echo, be enough? What OS are you on exactly? Do you happen to know what kernel driver this device is using? You could try running the same strace I showed above to see if there are significant differences between the shell commands and the Perl script on your system.

        A couple of other ideas to check on: I did notice you added raw to the stty modes, I'm not sure if that makes a difference on your system. Also, I am wondering why you are getting the output /?! 000, since that's actually what your code is sending, i.e. maybe something went wrong in disabling echo. Plus, in the root node you're opening the port with cat before you do the stty, have you tried the cat afterwards to see if that makes a difference? Are you sure the code you've posted here is exactly what you are running? (In your root node, I'm not sure what the "Finish!" means.) Are you using strict and warnings? Lastly, are you sure that no other commands are mucking with the port while your Perl script is running, e.g. did you remember to kill all the backgrounded cat & commands?

        Given that you are running at 300 baud, i would like to see what happens when you change 1.300 to 1.3000000

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-05-23 02:26 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (81 votes). Check out past polls.