Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by haukex (Archbishop)
on May 25, 2019 at 23:59 UTC ( [id://11100541]=note: print w/replies, xml ) Need Help??


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

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?

Replies are listed 'Best First'.
Re^4: r/w attached infrared head on /dev/ttyUSB0
by stoerti (Novice) on May 26, 2019 at 07:36 UTC
    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.

        I tried now a complete other raspberry with an older installation of raspbian.
        It is exactly the same situation.

        So I can be sure, there are no other running processes or something other what kills my script.


        I now it can be working.
        Some years ago I had this thing working, but after some years the SD card was crashed.
        And I have a backup - problem is, I don't know where...
        What I remember, I had the same problems but I made it working.
        Thanks for your help but that doesn't work too.
        The problem is not the control sequence, the problem is, that the data is not coming back completely, only the first line comes back.
        But why does it work, when I send the sequences from shell and getting it with cat?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-03-29 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found