X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=lib%2FRFID%2FSerial%2FCPRM02.pm;h=9613c9036866bef3e28304e1bd647cec824d961e;hb=93623e9dcaf13cb5c91744182c0d57e97d0a4af0;hp=8da6fc7892172c578be51bc5ff2bd68e3b0c9482;hpb=bd829246b2865ee4a167e431d1fe0bdc5124caf1;p=Biblio-RFID.git diff --git a/lib/RFID/Serial/CPRM02.pm b/lib/RFID/Serial/CPRM02.pm index 8da6fc7..9613c90 100644 --- a/lib/RFID/Serial/CPRM02.pm +++ b/lib/RFID/Serial/CPRM02.pm @@ -6,6 +6,8 @@ use RFID::Serial; use Time::HiRes; use Data::Dump qw(dump); +my $debug = 0; + sub serial_settings {{ device => "/dev/ttyUSB0", baudrate => "38400", @@ -37,9 +39,8 @@ sub cpr_m02_checksum { return pack('v', $crc); } -sub cpr_psst_wait { - # Protocol Start Synchronization Time (PSST): 5ms < data timeout 12 ms - Time::HiRes::sleep 0.005; +sub wait_device { + Time::HiRes::sleep 0.010; } our $port; @@ -52,24 +53,30 @@ sub cpr { my $checksum = cpr_m02_checksum($send); $send .= $checksum; - warn ">> ", as_hex( $send ), "\t\t[$description]\n"; + warn "##>> ", as_hex( $send ), "\t\t[$description]\n"; $port->write( $send ); - cpr_psst_wait; + wait_device; my $r_len = $port->read(1); + my $count = 100; while ( ! $r_len ) { - warn "# wait for response length 5ms\n"; - cpr_psst_wait; + if ( $count-- == 0 ) { + warn "no response from device"; + return; + } + wait_device; $r_len = $port->read(1); } + wait_device; + my $data_len = ord($r_len) - 1; my $data = $port->read( $data_len ); - warn "<< ", as_hex( $r_len . $data ),"\n"; + warn "##<< ", as_hex( $r_len . $data ),"\n"; - cpr_psst_wait; + wait_device; $coderef->( $data ) if $coderef; @@ -126,11 +133,11 @@ sub cpr_read { my $DB_SIZE = ord substr($data,6-2,1); $data = substr($data,7-2,-2); - warn "# DB N: $DB_N SIZE: $DB_SIZE ", as_hex( $data ), " transponder_data: [$transponder_data] ",length($transponder_data),"\n"; +# warn "# DB N: $DB_N SIZE: $DB_SIZE ", as_hex( $data ), " transponder_data: [$transponder_data] ",length($transponder_data),"\n"; foreach ( 1 .. $DB_N ) { my $sec = substr($data,0,1); my $db = substr($data,1,$DB_SIZE); - warn "block $_ ",dump( $sec, $db ); + warn "## block $_ ",dump( $sec, $db ) if $debug; $transponder_data .= reverse split(//,$db); $data = substr($data, $DB_SIZE + 1); } @@ -138,15 +145,16 @@ sub cpr_read { $block += 4; } - warn "DATA $hex_uid ", dump($transponder_data); - exit; + warn "# DATA $hex_uid ", dump($transponder_data); + return $transponder_data; } -my $inventory; sub inventory { +my $inventory; + cpr( 'FF B0 01 00', 'ISO - Inventory', sub { my $data = shift; if (length($data) < 5 + 2 ) { @@ -160,15 +168,16 @@ cpr( 'FF B0 01 00', 'ISO - Inventory', sub { 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 ); + $inventory->{$uid} ||= cpr_read( $uid ); + } - warn "inventory: ",dump($inventory); + warn "# inventory: ",dump($inventory); }); + return $inventory; } 1