--- 3m-810.pl 2010/02/15 14:10:08 78 +++ cpr-m02.pl 2010/07/16 13:05:24 87 @@ -10,6 +10,7 @@ use File::Slurp; use JSON; use POSIX qw(strftime); +use Time::HiRes; use IO::Socket::INET; @@ -31,7 +32,7 @@ Reuse => 1 ); - die "can't setup server" unless $server; + die "can't setup server: $!" unless $server; print "Server $0 ready at $server_url\n"; @@ -155,9 +156,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 +265,111 @@ #$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_psst_wait { + # Protocol Start Synchronization Time (PSST): 5ms < data timeout 12 ms + Time::HiRes::sleep 0.005; +} + +sub cpr { + my ( $hex, $description, $coderef ) = @_; + 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 ), "\t\t[$description]\n"; + $port->write( $send ); + + cpr_psst_wait; + + my $r_len = $port->read(1); + + while ( ! $r_len ) { + warn "# wait for response length 5ms\n"; + cpr_psst_wait; + $r_len = $port->read(1); + } + + my $data_len = ord($r_len) - 1; + my $data = $port->read( $data_len ); + warn "<< ", as_hex( $r_len . $data ),"\n"; + + cpr_psst_wait; + + $coderef->( $data ) if $coderef; + +} + +# FF = COM-ADDR any + +cpr( 'FF 52 00', 'Boud Rate Detection' ); + +cpr( 'FF 65', 'Get Software Version' ); + +cpr( 'FF 66 00', 'Get Reader Info - General hard and firware' ); + +cpr( 'FF 69', 'RF Reset' ); + + +sub cpr_read { + my $uid = shift; + my $hex_uid = as_hex($uid); + + cpr( "FF B0 23 01 $hex_uid 00 04", "Read Multiple Blocks $hex_uid" ); +# cpr( "FF B0 2B 01 $hex_uid", "Get System Information $hex_uid" ); +} + + +my $inventory; + +while(1) { + +cpr( 'FF B0 01 00', 'ISO - Inventory', sub { + my $data = shift; + my $data_sets = ord(substr($data,3,1)); + $data = substr($data,4); + foreach ( 1 .. $data_sets ) { + my $tr_type = substr($data,0,1); + die "FIXME only TR-TYPE=3 ISO 15693 supported" unless $tr_type eq "\x03"; + my $dsfid = substr($data,1,1); + my $uid = substr($data,2,8); + $inventory->{$uid}++; + $data = substr($data,10); + warn "# TAG $_ ",as_hex( $tr_type, $dsfid, $uid ),$/; + + cpr_read( $uid ); + } + warn "inventory: ",dump($inventory); +}); + +} + +#cpr( '', '?' ); + +exit; # initial hand-shake with device cmd( 'D5 00 05 04 00 11 8C66', 'hw version', @@ -612,7 +718,8 @@ 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"; + last if $c == 0; $data .= $b; } $desc ||= '?';