http://qs321.pair.com?node_id=11100707


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

try this

before

my $raw = "\x06\x30\x30\x30\x0D\x0A";
insert
select (undef,undef,undef,.5); # sub sec sleep

hint via https://volkszaehler-users.demo.volkszaehler.narkive.com/yAisjztI/vz-users-wiki#post14

and https://chromium.googlesource.com/apps/libapps/+/HEAD/hterm/doc/ControlSequences.md#C0 saying \x00 is "ignored", but perl will try to send it.

there was a time break between the two echos that may not have existed in your perl

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

      The problem is not the control sequence

      The hints at the link i provided seem to suggest it is in how you are sending the control sequences, that the timeing is very important.

      Try this

      use Fcntl qw/:DEFAULT/; use IO::Termios (); use IO::Stty (); use IO::Handle; # use IO::Socket; use IO::Select; 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 $msg1 = "\x2F\x3F\x21\x0D\x0A"; my $msg2 = "\x06\x30\x30\x30\x0D\x0A"; for my $sleep (qw /.1 .2 .3 .4 .5 .6 .7 .8 .9 1.0 1.1 1.2/) { tryit ($msg1,$msg2,$sleep,2); } exit; sub sw{ my $raw=shift; $handle->syswrite($raw) == length($raw) or die "syswrite : $raw"; } sub tryit { my $msg1=shift; my $msg2=shift; my $sleep=shift; my $timeout=shift; my $sel_read = IO::Select->new(); $sel_read-> add($handle); my $didsomething=1; my $buf; print "waiting $sleep between sends\n"; sw($msg1); select (undef,undef,undef,$sleep); sw($msg2); while ($didsomething) { $didsomething=0; my ($rd_ptr,$wr_ptr,$er_ptr)=IO::Select->select($sel_read,undef,un +def,$timeout); for my $fh (@$rd_ptr) { $buf=''; $didsomething=1; my $rv = sysread($fh, $buf, 64*1024, length($buf)); if (!$rv) { if (defined($rv)) { die 'EOF';} else {die 'Connection terminated';} } # bad rv else { print $buf;} } # $fh } # while print "no chars for $timeout secs\n"; } # tryit #for (1..300) { # my $toread = 1; # $handle->sysread(my $in, $toread) # == $toread or die "sysread"; # print $in; #} $handle->close;

      commented out last for loop

      But why does it work, when I send the sequences from shell and getting it with cat?

      One more guess: the two echo commands in the root node may mean the port is opened and closed twice, I think... you might try replicating that in your Perl code.

      (huck's guess was a good one, too bad it didn't work...)