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 |
|
|
156 |
sub diag { _message('diag',@_) }; |
sub diag { _message('diag',@_) }; |
157 |
|
|
158 |
my $device = "/dev/ttyUSB0"; |
my $device = "/dev/ttyUSB0"; |
159 |
my $baudrate = "19200"; |
my $baudrate = "38400"; |
160 |
my $databits = "8"; |
my $databits = "8"; |
161 |
my $parity = "none"; |
my $parity = "even"; |
162 |
my $stopbits = "1"; |
my $stopbits = "1"; |
163 |
my $handshake = "none"; |
my $handshake = "none"; |
164 |
|
|
265 |
#$port->stty_inpck(1); |
#$port->stty_inpck(1); |
266 |
#$port->stty_istrip(1); |
#$port->stty_istrip(1); |
267 |
|
|
268 |
|
sub cpr_m02_checksum { |
269 |
|
my $data = shift; |
270 |
|
|
271 |
|
my $preset = 0xffff; |
272 |
|
my $polynom = 0x8408; |
273 |
|
|
274 |
|
my $crc = $preset; |
275 |
|
foreach my $i ( 0 .. length($data) - 1 ) { |
276 |
|
$crc ^= ord(substr($data,$i,1)); |
277 |
|
for my $j ( 0 .. 7 ) { |
278 |
|
if ( $crc & 0x0001 ) { |
279 |
|
$crc = ( $crc >> 1 ) ^ $polynom; |
280 |
|
} else { |
281 |
|
$crc = $crc >> 1; |
282 |
|
} |
283 |
|
} |
284 |
|
# warn sprintf('%d %04x', $i, $crc & 0xffff); |
285 |
|
} |
286 |
|
|
287 |
|
return pack('v', $crc); |
288 |
|
} |
289 |
|
|
290 |
|
sub cpr { |
291 |
|
my ( $hex, $description ) = @_; |
292 |
|
my $bytes = str2bytes($hex); |
293 |
|
my $len = pack( 'c', length( $bytes ) + 3 ); |
294 |
|
my $send = $len . $bytes; |
295 |
|
my $checksum = cpr_m02_checksum($send); |
296 |
|
$send .= $checksum; |
297 |
|
|
298 |
|
warn ">> ", as_hex( $send ), "\t\t[$description]\n"; |
299 |
|
$port->write( $send ); |
300 |
|
|
301 |
|
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"; |
310 |
|
$r_len = ord($r_len) - 1; |
311 |
|
my $data = $port->read( $r_len ); |
312 |
|
warn "<< ", as_hex( $data ); |
313 |
|
|
314 |
|
Time::HiRes::sleep 0.050; |
315 |
|
} |
316 |
|
|
317 |
|
# FF = COM-ADDR any |
318 |
|
|
319 |
|
cpr( 'FF 52 00', 'Boud Rate Detection' ); |
320 |
|
|
321 |
|
cpr( 'FF 65', 'Get Software Version' ); |
322 |
|
|
323 |
|
cpr( 'FF 66 00', 'Get Reader Info - General hard and firware' ); |
324 |
|
|
325 |
|
cpr( 'FF 69', 'RF Reset' ); |
326 |
|
|
327 |
|
cpr( 'FF B0 01 00', 'ISO - Inventory' ); |
328 |
|
|
329 |
|
#cpr( '', '?' ); |
330 |
|
|
331 |
|
exit; |
332 |
# initial hand-shake with device |
# initial hand-shake with device |
333 |
|
|
334 |
cmd( 'D5 00 05 04 00 11 8C66', 'hw version', |
cmd( 'D5 00 05 04 00 11 8C66', 'hw version', |
677 |
while ( length( $data ) < $len ) { |
while ( length( $data ) < $len ) { |
678 |
my ( $c, $b ) = $port->read(1); |
my ( $c, $b ) = $port->read(1); |
679 |
die "no bytes on port: $!" unless defined $b; |
die "no bytes on port: $!" unless defined $b; |
680 |
#warn "## got $c bytes: ", as_hex($b), "\n"; |
warn "## got $c bytes: ", as_hex($b), "\n"; |
681 |
|
last if $c == 0; |
682 |
$data .= $b; |
$data .= $b; |
683 |
} |
} |
684 |
$desc ||= '?'; |
$desc ||= '?'; |