- $self->{logfh}->print(sprintf('% 10.3f', $now - $self->{starttime}),
- (map { sprintf(" %02x", $_) } @bytes),
- "\n");
-
- push @{ $self->{bytes} }, @bytes;
- @bytes = @{ $self->{bytes} };
-
- my @bad_bytes;
-
- while (@bytes >= 2) {
- if ($bytes[0] != 0x55) {
- push @bad_bytes, shift @bytes;
- next;
- }
- my $cmd = $bytes[1];
-
- my $packet_size = $cmd >= 0x40 && $cmd <= 0x46 ? 4 : 9;
- last if @bytes <= $packet_size;
-
- if ($bytes[$packet_size] != 0x05
- || SCX::CRC::digest(@bytes[0..$packet_size-2])
- != $bytes[$packet_size-1]) {
- push @bad_bytes, shift @bytes;
- next;
- }
-
- if (@bad_bytes) {
- $self->{logfh}->print("Cannot parse bytes",
- (map { sprintf(' %02x', $_) } @bad_bytes),
- "\n");
- @bad_bytes = ();
- }
-
- $self->{logfh}->print("Callback\n");
- &{ $self->{callback} }(@bytes[1..$packet_size]);
- splice @bytes, 0, $packet_size+1;
- }
- if (@bad_bytes) {
- $self->{logfh}->print("Cannot parse bytes",
- (map { sprintf(' %02x', $_) } @bad_bytes),
- "\n");
- @bad_bytes = ();
- }
-
- @{ $self->{bytes} } = @bytes;