From: Robert Klemme Date: 2005-12-19T17:47:47+09:00 Subject: Re: Better way to read data from IO into packets? Joel VanderWerf wrote: > Levin Alexander wrote: >> Hi, >> >> I have a small program, that reads data from a serial port and chops >> it into packets. A Packet has the following format: >> >> [0x65, 0xEB, , , , data:length>, >> ] > > Why not read enough bytes to make sure you get the length byte: > > s = io.sysread(5) > len = s[4] > s << io.sysread(len+2) > ary = s.unpack "C5a#{len}n" Why do you use sysread? I'd prefer to use #read in this case - I don't see any advantage of resorting to sysread here - it may even prevent read buffering => things get slower than necessary. > Or is the problem that there may be a variable number of "start > bytes"? In that case, maybe you could tell from the first 5 bytes how > many start bytes there are, and then read enough to capture the > length byte. Hm... doesn't seem to be the case. robert