10 |
use File::Slurp; |
use File::Slurp; |
11 |
use JSON; |
use JSON; |
12 |
use POSIX qw(strftime); |
use POSIX qw(strftime); |
13 |
|
use Time::HiRes; |
14 |
|
|
15 |
use IO::Socket::INET; |
use IO::Socket::INET; |
16 |
|
|
17 |
my $debug = 2; |
my $debug = 0; |
18 |
|
|
19 |
my $tags_data; |
my $tags_data; |
20 |
my $tags_security; |
my $tags_security; |
281 |
$crc = $crc >> 1; |
$crc = $crc >> 1; |
282 |
} |
} |
283 |
} |
} |
284 |
warn sprintf('%d %04x', $i, $crc & 0xffff); |
# warn sprintf('%d %04x', $i, $crc & 0xffff); |
285 |
} |
} |
286 |
|
|
287 |
return pack('v', $crc); |
return pack('v', $crc); |
288 |
} |
} |
289 |
|
|
290 |
sub cpr { |
sub cpr { |
291 |
my ( $hex, $description ) = shift; |
my ( $hex, $description, $coderef ) = @_; |
292 |
my $bytes = str2bytes($hex); |
my $bytes = str2bytes($hex); |
293 |
my $len = pack( 'c', length( $bytes ) + 3 ); |
my $len = pack( 'c', length( $bytes ) + 3 ); |
294 |
my $send = $len . $bytes; |
my $send = $len . $bytes; |
295 |
my $checksum = cpr_m02_checksum($send); |
my $checksum = cpr_m02_checksum($send); |
296 |
$send .= $checksum; |
$send .= $checksum; |
297 |
|
|
298 |
warn ">> ", as_hex( $send ), "[$description]\n"; |
warn ">> ", as_hex( $send ), "\t\t[$description]\n"; |
299 |
$port->write( $send ); |
$port->write( $send ); |
300 |
|
|
301 |
my $r_len = $port->read(1); |
my $r_len = $port->read(1); |
302 |
|
|
303 |
|
while ( ! $r_len ) { |
304 |
|
warn "# wait for response length 0.050\n"; |
305 |
|
Time::HiRes::sleep 0.050; |
306 |
|
$r_len = $port->read(1); |
307 |
|
} |
308 |
|
|
309 |
warn "<< response len: ", as_hex($r_len), "\n"; |
warn "<< response len: ", as_hex($r_len), "\n"; |
310 |
$r_len = ord($r_len) - 1; |
$r_len = ord($r_len) - 1; |
311 |
my $data = $port->read( $r_len ); |
my $data = $port->read( $r_len ); |
312 |
warn "<< ", as_hex( $data ); |
warn "<< ", as_hex( $data ); |
313 |
|
|
314 |
|
my $t = Time::HiRes::time; |
315 |
|
|
316 |
|
$coderef->( $data ) if $coderef; |
317 |
|
|
318 |
warn "## ",dump( $port->read(1) ); |
my $dt = Time::HiRes::time - $t; |
319 |
|
if ( $dt < 0.050 ) { |
320 |
|
my $s = 0.050 - $dt; |
321 |
|
warn "# sleep for more $s\n"; |
322 |
|
Time::HiRes::sleep $s; |
323 |
|
} |
324 |
} |
} |
325 |
|
|
326 |
#cpr( 'FF 52 00', 'detect boud rate' ); |
# FF = COM-ADDR any |
327 |
|
|
328 |
#cpr( '00 65', 'software version' ); |
cpr( 'FF 52 00', 'Boud Rate Detection' ); |
329 |
|
|
330 |
cpr( 'FF 65', 'get ? info' ); |
cpr( 'FF 65', 'Get Software Version' ); |
331 |
|
|
332 |
cpr( 'FF 69 00', 'get reader info' ); |
cpr( 'FF 66 00', 'Get Reader Info - General hard and firware' ); |
333 |
|
|
334 |
cpr( 'FF B0 01 00', '?' ); |
cpr( 'FF 69', 'RF Reset' ); |
335 |
|
|
336 |
cpr( 'FF 69', '?' ); |
my $inventory; |
337 |
|
|
338 |
|
while(1) { |
339 |
|
|
340 |
|
cpr( 'FF B0 01 00', 'ISO - Inventory', sub { |
341 |
|
my $data = shift; |
342 |
|
my $data_sets = ord(substr($data,3,1)); |
343 |
|
$data = substr($data,4); |
344 |
|
foreach ( 1 .. $data_sets ) { |
345 |
|
my $tr_type = substr($data,0,1); |
346 |
|
my $dsfid = substr($data,1,1); |
347 |
|
my $uid = substr($data,2,8); |
348 |
|
$inventory->{$uid}++; |
349 |
|
$data = substr($data,10); |
350 |
|
warn "# TAG $_ ",as_hex( $tr_type, $dsfid, $uid ),$/; |
351 |
|
} |
352 |
|
warn "inventory: ",dump($inventory); |
353 |
|
}); |
354 |
|
|
355 |
|
} |
356 |
|
|
357 |
#cpr( '', '?' ); |
#cpr( '', '?' ); |
358 |
|
|