Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: r/w attached infrared head on /dev/ttyUSB0

by stoerti (Novice)
on May 25, 2019 at 22:17 UTC ( #11100535=note: print w/replies, xml ) Need Help??


in reply to Re: r/w attached infrared head on /dev/ttyUSB0
in thread r/w attached infrared head on /dev/ttyUSB0

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

Replies are listed 'Best First'.
Re^3: r/w attached infrared head on /dev/ttyUSB0
by haukex (Chancellor) on May 25, 2019 at 23:59 UTC

    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?

      I tried different versions of Ubuntu. Now I have installed the actual image from volkszaehler.org
      It runs on a raspberry pi
      The "cat /dev/ttyUSB0&" makes no difference if started before or after the stty settings.

      The stty arguments are from a documentation, I tried your style and it seems to be running the same as before.
      The meter system giving the commands "/?!" and "000" back, when I send it to him.
      Every line ends with "\r\n".

      I changed my code:

      use Fcntl qw/:DEFAULT/;<br> use IO::Termios ();<br> use IO::Stty ();<br> <br> sysopen my $fh, '/dev/ttyUSB0', O_RDWR or die "sysopen: $!";<br> my $handle = IO::Termios->new($fh) or die "IO::Termios->new: $!";<br> $handle->set_mode('300,7,e,1');<br> IO::Stty::stty($handle, qw/ cs7 parenb -parodd raw -echo /);<br> <br> my $raw = "\x2F\x3F\x21\x0D\x0A";<br> $handle->syswrite($raw) == length($raw) or die "syswrite";<br> my $raw = "\x06\x30\x30\x30\x0D\x0A";<br> $handle->syswrite($raw) == length($raw) or die "syswrite";<br> <br> while(1) {<br> my $toread = 1;<br> $handle->sysread(my $in, $toread) == $toread or die "sysread";<br> print $in;<br> }<br> <br> $handle->close;<br> <br> <br>
      And you see in strace my problem: (only last lines of output)
      read(4, "/", 1) = 1<br> read(4, "L", 1) = 1<br> read(4, "O", 1) = 1<br> read(4, "G", 1) = 1<br> read(4, "4", 1) = 1<br> read(4, "L", 1) = 1<br> read(4, "K", 1) = 1<br> read(4, "1", 1) = 1<br> read(4, "3", 1) = 1<br> read(4, "B", 1) = 1<br> read(4, "D", 1) = 1<br> read(4, "2", 1) = 1<br> read(4, "0", 1) = 1<br> read(4, "2", 1) = 1<br> read(4, "0", 1) = 1<br> read(4, "1", 1) = 1<br> read(4, "5", 1) = 1<br> read(4, "\r", 1) = 1<br> read(4, "\n", 1) = 1<br> write(1, "/LOG4LK13BD202015\r\n", 19/LOG4LK13BD202015) = 19<br> read(4, <br>

      After "read(4," the script waits endless

      The only way it works, when I send the sequence (stty setting, then /?!\r\n, then 000\r\n) directly into a shell
      So I can not understand what the problem could be.

        Thanks for the update - it's strange, and unfortunately I'm running out of ideas at the moment... I use the IO::Termios+IO::Stty combination on a Raspberry Pi myself, running a stock Raspbian, to read multiple FTDI chips and other dataloggers at once, and it works fine. Did you compare the strace results of the shell and the Perl scripts to make sure the same thing gets sent? Are you certain there are no other processes on the system trying to open/read the port? For example, did you make sure to kill all the cat processes you backgrounded, and check for other daemons? For example, I once had trouble with gpsd trying to get to the serial port when I was trying to use it as well, or that website mentions a vzlogger process that is included as part of the image.

Re^3: r/w attached infrared head on /dev/ttyUSB0
by huck (Parson) on May 25, 2019 at 23:31 UTC

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

      change 1.300 to 1.3000000

      Since I don't see any timeouts set up, I doubt that trying to read 3 megabytes instead of 300 bytes from the port would make a difference... the OP's expected result is somewhere around 200 bytes long.

        I didnt see where it was said the perl program was killed either, and so the loop of 300 must have terminated. So i wondered if there were non printing characters being returned while the device was processing. A larger "window" might show results after some period of time.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2019-11-20 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (96 votes). Check out past polls.

    Notices?