On 12/18/05, Joel VanderWerf <vjoel / path.berkeley.edu> wrote:
> > 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, <type:8>, <counter:8>, <length:8>, data:length>, <crc:16>]>> Why not read enough bytes to make sure you get the length byte:
Because the application may be started in the middle of a packet orthe stream may be corrupted due to transmission errors.
But you are right, I should optimize that and only read single bytesif I need to resynchronize.
> s = io.sysread(5)> len = s[4]> s << io.sysread(len+2)> ary = s.unpack "C5a#{len}n">> 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.
Hmm, maybe I can use regular expressions to check for the correct format:
  buffer = "bad data" << [0x65,0xEB,0,4,65,66,67,68,00,00].pack("C*")  buffer.scan( /    \x65\xEB     # startbytes    (.)          # type    (.)          # length-byte    (.*)         # data    (..)         # checksum  /x )
I would need a way to discard old or bad data from the buffer,probably need to think about it more
(btw: is there a way to use the length-byte in the regular expressionitself?  Something like /(.)(.{\1})/)
Thank you,Levin