--- 3m-810.pl 2009/06/01 13:09:41 37 +++ 3m-810.pl 2009/06/01 18:36:42 38 @@ -310,16 +310,34 @@ my $path = "$program_path/$tag"; my $data = read_file( $path ); + my $hex_data; - $data = substr($data,0,16); + if ( $data =~ s{^hex\s+}{} ) { + $hex_data = $data; + $hex_data =~ s{\s+}{}g; + } else { - my $hex_data = unpack('H*', $data) . ' 00' x ( 16 - length($data) ); + # pad to block size + $data .= "\0" x ( 4 - ( length($data) % 4 ) ); - print "write_tag $tag = ",dump( $data ), " == $hex_data\n"; + my $max_len = 7 * 4; + + if ( length($data) > $max_len ) { + $data = substr($data,0,$max_len); + warn "strip content to $max_len bytes\n"; + } + + $hex_data = unpack('H*', $data); + } + + my $len = length($hex_data) / 2; + my $blocks = sprintf('%02x', $len / 4); + + print "write_tag $tag = ",dump( $data ), " [$len/$blocks] == $hex_data\n"; cmd( - "d6 00 26 04 $tag 00 06 00 04 11 00 01 $hex_data 00 00 00 00 fd3b", "write $tag", - "d6 00 0d 04 00 $tag 06 afb1", sub { assert() }, + "d6 00 ff 04 $tag 00 $blocks 00 $hex_data ffff", "write $tag", + "d6 00 0d 04 00 $tag $blocks afb1", sub { assert() }, ) foreach ( 1 .. 3 ); # xxx 3m software does this three times! my $to = $path; @@ -380,7 +398,10 @@ sub writechunk { my $str=shift; +warn "DEBUG: ", as_hex($str); my $count = $port->write($str); + my $len = length($str); + die "wrong write length $count != $len in ",as_hex( $str ) if $count != $len; print "#> ", as_hex( $str ), "\t[$count]\n" if $debug; } @@ -447,17 +468,17 @@ sub checksum { my ( $bytes, $checksum ) = @_; - my $xor = crcccitt( substr($bytes,1) ); # skip D6 - warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug; - my $len = ord(substr($bytes,2,1)); my $len_real = length($bytes) - 1; if ( $len_real != $len ) { print "length wrong: $len_real != $len\n"; - $bytes = substr($bytes,0,2) . chr($len_real) . substr($bytes,4); + $bytes = substr($bytes,0,2) . chr($len_real) . substr($bytes,3); } + my $xor = crcccitt( substr($bytes,1) ); # skip D6 + warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug; + if ( defined $checksum && $xor ne $checksum ) { print "checksum doesn't match: ", as_hex($xor), " != ", as_hex($checksum), " data: ", as_hex($bytes), "\n"; return $bytes . $xor;