use RFID::Serial;
use Time::HiRes;
+use Data::Dump qw(dump);
+
+my $debug = 1;
sub serial_settings {{
device => "/dev/ttyUSB0",
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;
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;
}
-sub cpr_read {
- my $uid = shift;
- my $hex_uid = as_hex($uid);
+sub read_blocks {
+ my $tag = shift;
+ $tag = shift if ref $tag;
my $max_block;
- cpr( "FF B0 2B 01 $hex_uid", "Get System Information $hex_uid", sub {
+ cpr( "FF B0 2B 01 $tag", "Get System Information $tag", sub {
my $data = shift;
warn "# data ",as_hex($data);
$max_block = ord($SIZE);
});
- my $transponder_data;
+ my $tag_blocks;
my $block = 0;
while ( $block < $max_block ) {
- cpr( sprintf("FF B0 23 01 $hex_uid %02x 04", $block), "Read Multiple Blocks $block", sub {
+ cpr( sprintf("FF B0 23 01 $tag %02x 04", $block), "Read Multiple Blocks $block", sub {
my $data = shift;
my $DB_N = ord substr($data,5-2,1);
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";
- foreach ( 1 .. $DB_N ) {
- my $sec = substr($data,0,1);
+# warn "# DB N: $DB_N SIZE: $DB_SIZE ", as_hex( $data ), " transponder_data: [$transponder_data] ",length($transponder_data),"\n";
+ foreach my $n ( 1 .. $DB_N ) {
+ my $sec = ord(substr($data,0,1));
my $db = substr($data,1,$DB_SIZE);
- warn "block $_ ",dump( $sec, $db );
- $transponder_data .= reverse split(//,$db);
+ warn "## block $n ",dump( $sec, $db ) if $debug;
+ $tag_blocks->{$tag}->[$block+$n] = reverse split(//,$db);
$data = substr($data, $DB_SIZE + 1);
}
});
$block += 4;
}
- warn "DATA $hex_uid ", dump($transponder_data);
- exit;
+ warn "# tag_blocks ",dump($tag_blocks),$/;
+ return $tag_blocks;
}
-my $inventory;
sub inventory {
+ my @tags;
+
cpr( 'FF B0 01 00', 'ISO - Inventory', sub {
my $data = shift;
if (length($data) < 5 + 2 ) {
warn "# no tags in range\n";
return;
}
+
my $data_sets = ord(substr($data,3,1));
$data = substr($data,4);
foreach ( 1 .. $data_sets ) {
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 );
+ push @tags, hex_tag $uid;
+
}
- warn "inventory: ",dump($inventory);
});
+ warn "# tags ",dump(@tags),$/;
+ return @tags;
}
1