--- cpr-m02.pl 2010/07/06 15:08:47 81 +++ cpr-m02.pl 2010/07/09 23:10:05 82 @@ -13,7 +13,7 @@ use IO::Socket::INET; -my $debug = 0; +my $debug = 2; my $tags_data; my $tags_security; @@ -155,9 +155,9 @@ sub diag { _message('diag',@_) }; my $device = "/dev/ttyUSB0"; -my $baudrate = "19200"; +my $baudrate = "38400"; my $databits = "8"; -my $parity = "none"; +my $parity = "even"; my $stopbits = "1"; my $handshake = "none"; @@ -264,6 +264,47 @@ #$port->stty_inpck(1); #$port->stty_istrip(1); +sub cpr_m02_checksum { + my $data = shift; + + my $preset = 0xffff; + my $polynom = 0x8408; + + my $crc = $preset; + foreach my $i ( 0 .. length($data) - 1 ) { + $crc ^= ord(substr($data,$i,1)); + for my $j ( 0 .. 7 ) { + if ( $crc & 0x0001 ) { + $crc = ( $crc >> 1 ) ^ $polynom; + } else { + $crc = $crc >> 1; + } + } + warn sprintf('%d %04x', $i, $crc & 0xffff); + } + + return pack('v', $crc); +} + +sub cpr { + my $hex = shift; + my $bytes = str2bytes($hex); + my $len = pack( 'c', length( $bytes ) + 3 ); + my $send = $len . $bytes; + my $checksum = cpr_m02_checksum($send); + $send .= $checksum; + + warn ">> ", as_hex( $send ); + writechunk( $send ); + my $r_len = read_bytes( 1, 'response length' ); + $r_len = ord($r_len) - 1; + my $data = read_bytes( $r_len, 'data' ); + warn "<< ", as_hex( $data ); +} + +cpr( '00 52 00' ); + +exit; # initial hand-shake with device cmd( 'D5 00 05 04 00 11 8C66', 'hw version', @@ -612,7 +653,7 @@ while ( length( $data ) < $len ) { my ( $c, $b ) = $port->read(1); die "no bytes on port: $!" unless defined $b; - #warn "## got $c bytes: ", as_hex($b), "\n"; + warn "## got $c bytes: ", as_hex($b), "\n"; $data .= $b; } $desc ||= '?';