13 |
|
|
14 |
use IO::Socket::INET; |
use IO::Socket::INET; |
15 |
|
|
16 |
my $debug = 0; |
my $debug = 2; |
17 |
|
|
18 |
my $tags_data; |
my $tags_data; |
19 |
my $tags_security; |
my $tags_security; |
31 |
Reuse => 1 |
Reuse => 1 |
32 |
); |
); |
33 |
|
|
34 |
die "can't setup server" unless $server; |
die "can't setup server: $!" unless $server; |
35 |
|
|
36 |
print "Server $0 ready at $server_url\n"; |
print "Server $0 ready at $server_url\n"; |
37 |
|
|
155 |
sub diag { _message('diag',@_) }; |
sub diag { _message('diag',@_) }; |
156 |
|
|
157 |
my $device = "/dev/ttyUSB0"; |
my $device = "/dev/ttyUSB0"; |
158 |
my $baudrate = "19200"; |
my $baudrate = "38400"; |
159 |
my $databits = "8"; |
my $databits = "8"; |
160 |
my $parity = "none"; |
my $parity = "even"; |
161 |
my $stopbits = "1"; |
my $stopbits = "1"; |
162 |
my $handshake = "none"; |
my $handshake = "none"; |
163 |
|
|
264 |
#$port->stty_inpck(1); |
#$port->stty_inpck(1); |
265 |
#$port->stty_istrip(1); |
#$port->stty_istrip(1); |
266 |
|
|
267 |
|
sub cpr_m02_checksum { |
268 |
|
my $data = shift; |
269 |
|
|
270 |
|
my $preset = 0xffff; |
271 |
|
my $polynom = 0x8408; |
272 |
|
|
273 |
|
my $crc = $preset; |
274 |
|
foreach my $i ( 0 .. length($data) - 1 ) { |
275 |
|
$crc ^= ord(substr($data,$i,1)); |
276 |
|
for my $j ( 0 .. 7 ) { |
277 |
|
if ( $crc & 0x0001 ) { |
278 |
|
$crc = ( $crc >> 1 ) ^ $polynom; |
279 |
|
} else { |
280 |
|
$crc = $crc >> 1; |
281 |
|
} |
282 |
|
} |
283 |
|
warn sprintf('%d %04x', $i, $crc & 0xffff); |
284 |
|
} |
285 |
|
|
286 |
|
return pack('v', $crc); |
287 |
|
} |
288 |
|
|
289 |
|
sub cpr { |
290 |
|
my ( $hex, $description ) = shift; |
291 |
|
my $bytes = str2bytes($hex); |
292 |
|
my $len = pack( 'c', length( $bytes ) + 3 ); |
293 |
|
my $send = $len . $bytes; |
294 |
|
my $checksum = cpr_m02_checksum($send); |
295 |
|
$send .= $checksum; |
296 |
|
|
297 |
|
warn ">> ", as_hex( $send ), "[$description]\n"; |
298 |
|
$port->write( $send ); |
299 |
|
my $r_len = $port->read(1); |
300 |
|
warn "<< response len: ", as_hex($r_len), "\n"; |
301 |
|
$r_len = ord($r_len) - 1; |
302 |
|
my $data = $port->read( $r_len ); |
303 |
|
warn "<< ", as_hex( $data ); |
304 |
|
|
305 |
|
warn "## ",dump( $port->read(1) ); |
306 |
|
} |
307 |
|
|
308 |
|
#cpr( 'FF 52 00', 'detect boud rate' ); |
309 |
|
|
310 |
|
#cpr( '00 65', 'software version' ); |
311 |
|
|
312 |
|
cpr( 'FF 65', 'get ? info' ); |
313 |
|
|
314 |
|
cpr( 'FF 69 00', 'get reader info' ); |
315 |
|
|
316 |
|
cpr( 'FF B0 01 00', '?' ); |
317 |
|
|
318 |
|
cpr( 'FF 69', '?' ); |
319 |
|
|
320 |
|
#cpr( '', '?' ); |
321 |
|
|
322 |
|
exit; |
323 |
# initial hand-shake with device |
# initial hand-shake with device |
324 |
|
|
325 |
cmd( 'D5 00 05 04 00 11 8C66', 'hw version', |
cmd( 'D5 00 05 04 00 11 8C66', 'hw version', |
668 |
while ( length( $data ) < $len ) { |
while ( length( $data ) < $len ) { |
669 |
my ( $c, $b ) = $port->read(1); |
my ( $c, $b ) = $port->read(1); |
670 |
die "no bytes on port: $!" unless defined $b; |
die "no bytes on port: $!" unless defined $b; |
671 |
#warn "## got $c bytes: ", as_hex($b), "\n"; |
warn "## got $c bytes: ", as_hex($b), "\n"; |
672 |
|
last if $c == 0; |
673 |
$data .= $b; |
$data .= $b; |
674 |
} |
} |
675 |
$desc ||= '?'; |
$desc ||= '?'; |