-sub v {
- my ($hex,$expect,$desc) = @_;
- my $bytes = hex2bytes( $hex );
+sub crc {
+ my $data = shift;
+ my $crc = 0;
+ for my $i ( 0 .. length($data) - 2 ) {
+ if ( $crc & 0x80 ) {
+ $crc = ( $crc << 1 ) ^ 25;
+ } else {
+ $crc = $crc << 1;
+ }
+ $crc = $crc & 0xff;
+ my $byte = ord(substr($data,$i,1));
+ $crc = $crc ^ $byte;
+# warn "## ", as_hex(chr($byte)), " crc = ", $crc, " ", as_hex(chr($crc));
+ }
+ # ignore checksum errors for FF, we will calulate them!
+ if ( chr($crc) ne substr($data,-1,1) && substr($data,-1,1) ne "\xFF" ) {
+ warn "CRC error for ",as_hex($data), " calulated ", as_hex(chr($crc));
+ }
+
+ return substr($data,0,-1) . chr($crc);
+}
+
+sub v_bytes {
+ my ($bytes,$desc) = @_;