first 3M 810 implementation
[Biblio-RFID.git] / lib / RFID / Serial / 3M810.pm
1 package RFID::Serial::3M810;
2
3 use base 'RFID::Serial';
4 use RFID::Serial;
5
6 sub serial_settings {{
7         device    => "/dev/ttyUSB0",
8         baudrate  => "19200",
9         databits  => "8",
10         parity    => "none",
11         stopbits  => "1",
12         handshake => "none",
13 }}
14
15 sub cmd {
16         my ( $hex, $description, $coderef ) = @_;
17         my $bytes = hex2bytes($hex);
18         if ( substr($bytes,0,1) ne "\xD5" ) {
19                 my $len = pack( 'c', length( $bytes ) + 3 );
20                 $bytes = $len . $bytes;
21                 my $checksum = checksum($bytes);
22                 $bytes = "\xD6\x00" . $bytes . $checksum;
23         }
24
25         warn ">> ", as_hex( $bytes ), "\t\t[$description]\n";
26         $port->write( $bytes );
27
28         my $r_len = $port->read(3);
29
30         while ( ! $r_len ) {
31                 warn "# wait for response length 5ms\n";
32                 $r_len = $port->read(3);
33         }
34
35         my $data_len = ord(substr($r_len,2,1)) - 1;
36         my $data = $port->read( $data_len );
37         warn "<< ", as_hex( $r_len . $data ),"\n";
38
39         $coderef->( $data ) if $coderef;
40
41 }
42
43 sub assert {
44         my ( $got, $expected ) = @_;
45         die "got ", as_hex($got), " expected ", as_hex($expected)
46         unless substr($expected,0,length($got)) eq $got;
47 }
48
49 sub init {
50         my $self = shift;
51
52 cmd( 'D5 00  05   04 00 11' => 'hw version' . sub {
53         my $data = shift;
54         assert $data => '04 00 01';
55         my $hw_ver = join('.', unpack('CCCC', substr($data,3)));
56         print "hardware version $hw_ver\n";
57 });
58
59 cmd(
60 '13  04  01 00 02 00 03 00 04 00', 'FIXME: stats?', sub { assert(shift,
61 '13  00  02 01 01 03 02 02 03 00'
62 )});
63
64 }
65
66 1