in reply to Problem with logging binary data through serial port
If the last byte of the binary data is 0x0D, the logged data will miss a 0x0D character.
\x0D should not disappear. I suspect you're in Windows, and that you didn't binmode the filehandle from which you are reading. Be sure to use binmode on both the handle from which you are reading and the one to which you are writing.
sysread returned immediately whenever new data arrived serial port,
That read and sysread are not guaranteed to return LENGTH bytes is documented. If you need more bytes, just use a loop.
my $buf = ''; my $offset = 0; DATA: for (;;) { # For each record my $buf = ''; my $offset = 0; my $rec; do { # Until we have a record. my $len = sysread(STDIN, $buf, 1024, $offset); die("Unable to read from XXX: $!\n") if not defined $len; die("Unable to read from XXX: Premature end of file\n") if not $len and $offset; last DATA if not $len; my $pos = index($buf, "\x0D\x0A", $offset); $offset += $len; } while $pos < 0; $rec = substr($buf, 0, $pos+2); $buf = substr($buf, $pos+2); ... process $rec ... }
Update: Optimized the code slightly.
|
|---|