1 package RFID::Serial::3M810;
3 use base 'RFID::Serial';
6 use Data::Dump qw(dump);
11 sub serial_settings {{
12 device => "/dev/ttyUSB1", # FIXME comment out before shipping
26 my ( $count, $str ) = $port->read(3);
27 my $data = $port->read( ord(substr($str,2,1)) );
28 warn "drain ",as_hex( $str, $data ),"\n";
36 my $crc = Digest::CRC->new(
37 # midified CCITT to xor with 0xffff instead of 0x0000
38 width => 16, init => 0xffff, xorout => 0xffff, refout => 0, poly => 0x1021, refin => 0,
41 pack('n', $crc->digest);
45 Time::HiRes::sleep 0.015;
49 my ( $hex, $description, $coderef ) = @_;
50 my $bytes = hex2bytes($hex);
51 if ( substr($bytes,0,1) !~ /(\xD5|\xD6)/ ) {
52 my $len = pack( 'n', length( $bytes ) + 2 );
53 $bytes = $len . $bytes;
54 my $checksum = checksum($bytes);
55 $bytes = "\xD6" . $bytes . $checksum;
58 warn ">> ", as_hex( $bytes ), "\t\t[$description]\n";
59 $port->write( $bytes );
63 my $r_len = $port->read(3);
67 $r_len = $port->read(3);
72 my $len = ord( substr($r_len,2,1) );
73 $data = $port->read( $len );
74 warn "<< ", as_hex($r_len,$data)," $len\n";
76 $coderef->( $data ) if $coderef;
81 my ( $got, $expected ) = @_;
82 $expected = hex2bytes($expected);
84 my $len = length($got);
85 $len = length($expected) if length $expected < $len;
87 confess "got ", as_hex($got), " expected ", as_hex($expected)
88 unless substr($got,0,$len) eq substr($expected,0,$len);
90 return substr($got,$len);
96 'D5 00 05 04 00 11 8C66', 'hw version', sub {
98 my $rest = assert $data => '04 00 11';
99 my $hw_ver = join('.', unpack('CCCC', $rest));
100 print "hardware version $hw_ver\n";
104 '13 04 01 00 02 00 03 00 04 00','FIXME: stats? rf-on?', sub { assert(shift,
105 '13 00 02 01 01 03 02 02 03 00'
109 sub tag_hex { uc(unpack('H16', shift)) }
115 cmd( 'FE 00 05', 'scan for tags', sub {
117 my $rest = assert $data => 'FE 00 00 05';
118 my $nr = ord( substr( $rest, 0, 1 ) );
121 warn "# no tags in range\n";
123 my $tags = substr( $rest, 1 );
124 my $tl = length( $tags );
125 die "wrong length $tl for $nr tags: ",dump( $tags ) if $tl =! $nr * 8;
127 foreach ( 0 .. $nr - 1 ) {
128 my $tag = tag_hex substr($tags, $_ * 8, 8);
129 $invetory->{$tag} ||= read_tag($tag);
140 # cards 16, stickers: 8
141 my $max_rfid_block = 8;
145 my ( $data, $hex ) = @_;
146 my $b = hex2bytes $hex;
148 if ( substr($data,0,$l) eq $b ) {
149 warn "_matched $hex [$l] in ",as_hex($data);
150 return substr($data,$l);
155 my $tag = shift || confess "no tag?";
156 warn "# read $tag\n";
161 sprintf( "02 $tag %02x %02x", $start, $blocks ) => "read $tag $start/$blocks", sub {
163 if ( my $rest = _matched $data => '02 00' ) {
165 my $tag = tag_hex substr($rest,0,8);
166 my $blocks = ord(substr($rest,8,1));
167 warn "# response from $tag $blocks blocks ",as_hex substr($rest,9);
168 foreach ( 1 .. $blocks ) {
169 my $pos = ( $_ - 1 ) * 6 + 9;
170 my $nr = unpack('v', substr($rest,$pos,2));
171 my $payload = substr($rest,$pos+2,4);
172 warn "## pos $pos block $nr ",as_hex($payload), $/;
173 $tag_blocks->{$tag}->[$nr] = $payload;
175 } elsif ( my $rest = _matched $data => 'FE 00 00 05 01' ) {
176 warn "FIXME ready? ",as_hex $test;
177 } elsif ( my $rest = _matched $data => '02 06' ) {
178 warn "ERROR ",as_hex($rest);
182 warn "# tag_blocks ",dump($tag_blocks);