--- 3m-810.pl 2008/09/28 22:10:55 8 +++ 3m-810.pl 2008/10/03 15:38:08 19 @@ -6,9 +6,17 @@ use Data::Dump qw/dump/; use Carp qw/confess/; +use Getopt::Long; my $debug = 0; +my $device = "/dev/ttyUSB0"; +my $baudrate = "19200"; +my $databits = "8"; +my $parity = "none"; +my $stopbits = "1"; +my $handshake = "none"; + my $response = { 'd500090400110a0500027250' => 'version?', 'd60007fe00000500c97b' => 'no tag in range', @@ -21,13 +29,23 @@ 'd60017fe00000502e00401003123aa26e00401003123aa283124' => 'tag #1 + tag red-stripe', }; +GetOptions( + 'd|debug+' => \$debug, + 'device=s' => \$device, + 'baudrate=i' => \$baudrate, + 'databits=i' => \$databits, + 'parity=s' => \$parity, + 'stopbits=i' => \$stopbits, + 'handshake=s' => \$handshake, +) or die $!; + =head1 NAME 3m-810 - support for 3M 810 RFID reader =head1 SYNOPSIS -3m-810.pl [DEVICE [BAUD [DATA [PARITY [STOP [FLOW]]]]]] +3m-810.pl --device /dev/ttyUSB0 =head1 DESCRIPTION @@ -39,6 +57,8 @@ L +L + =head1 AUTHOR Dobrica Pavlinusic L @@ -50,16 +70,8 @@ =cut -# your serial port. -my ($device,$baudrate,$databits,$parity,$stopbits,$handshake)=@ARGV; -$device ||= "/dev/ttyUSB0"; -$baudrate ||= "19200"; -$databits ||= "8"; -$parity ||= "none"; -$stopbits ||= "1"; -$handshake ||= "none"; - -my $port=new Device::SerialPort($device) || die "new($device): $!\n"; +my $port=new Device::SerialPort($device) || die "can't open serial port $device: $!\n"; +warn "using $device $handshake $baudrate $databits $parity $stopbits" if $debug; $handshake=$port->handshake($handshake); $baudrate=$port->baudrate($baudrate); $databits=$port->databits($databits); @@ -102,26 +114,61 @@ die "wrong length $tl for $nr tags: ",dump( $tags ) if $tl =! $nr * 8; my @tags; - push @tags, substr($tags, $_ * 8, 8) foreach ( 0 .. $nr - 1 ); + push @tags, uc(unpack('H16', substr($tags, $_ * 8, 8))) foreach ( 0 .. $nr - 1 ); warn "## tags ",as_hex($tags), " [$tl] = ",dump( $tags ) if $debug; - print "seen $nr tags: ", join(',', map { unpack('H16', $_) } @tags ) , "\n"; + print "seen $nr tags: ", join(',', @tags ) , "\n"; + + # read data from tag + read_tag( $_ ) foreach @tags; + } ) } ) foreach ( 1 .. 100 ); -cmd( 'D6 00 0D 02 E00401003123AA26 00 03 1CC4', 'read offset: 0 blocks: 3' ); +my $read_cached; -# D6 00 1F 02 00 E00401003123AA26 03 00 00 04 11 00 01 01 00 30 30 30 30 02 00 30 30 30 30 E5F4 -warn "D6 00 1F 02 00 E00401003123AA26 03 00 00 04 11 00 01 01 00 31 32 33 34 02 00 35 36 37 38 531F\n"; +sub read_tag { + my ( $tag ) = @_; -cmd( 'D6 00 0D 02 E00401003123AA26 03 04 3970', 'read offset: 3 blocks: 4' ); + print "read_tag $tag\n"; + return if $read_cached->{ $tag }++; + + cmd( "D6 00 0D 02 $tag 00 03 1CC4", 'read $tag offset: 0 blocks: 3', + "D6 00 0F FE 00 00 05 01 $tag 941A", "$tag ready?", sub { +dispatch( "D6 00 1F 02 00 $tag ", sub { # 03 00 00 04 11 00 01 01 00 31 32 33 34 02 00 35 36 37 38 531F\n"; + my $rest = shift || die "no rest?"; + warn "## DATA ", dump( $rest ) if $debug; + my $blocks = ord(substr($rest,0,1)); + my @data; + foreach my $nr ( 0 .. $blocks - 1 ) { + my $block = substr( $rest, 1 + $nr * 6, 6 ); + warn "## block ",as_hex( $block ) if $debug; + my $ord = unpack('v',substr( $block, 0, 2 )); + die "got block $ord, expected block $nr from ",dump( $block ) if $ord != $nr; + my $data = substr( $block, 2 ); + die "data payload should be 4 bytes" if length($data) != 4; + warn sprintf "## tag %08s %02d %s |%-4s|\n", $tag, $ord, as_hex( $data ), $data; + $data[ $ord ] = $data; + } + $read_cached->{ $tag } = join('', @data); + print "DATA $tag ",dump( $read_cached->{ $tag } ), "\n"; + }) + }); + + # D6 00 1F 02 00 $tag 03 00 00 04 11 00 01 01 00 30 30 30 30 02 00 30 30 30 30 E5F4 +if (0) { + cmd( "D6 00 0D 02 $tag 03 04 3970", 'read offset: 3 blocks: 4' ); + + # D6 00 25 02 00 $tag 04 03 00 30 30 00 00 04 00 00 00 00 00 + # $tag 05 00 00 00 00 00 06 00 00 00 00 00 B9BA + warn "?? D6 00 25 02 00 $tag 04 03 00 39 30 31 32 04 00 ....\n"; +} + warn "?? D6 00 0F FE 00 00 05 01 $tag 941A ##### ready?\n"; + +} -# D6 00 25 02 00 E00401003123AA26 04 03 00 30 30 00 00 04 00 00 00 00 00 -# 05 00 00 00 00 00 06 00 00 00 00 00 B9BA -warn "D6 00 25 02 00 E00401003123AA26 04 03 00 39 30 31 32 04 00 33 34 35 36 - 05 00 00 00 00 00 06 00 00 00 00 00 524B\n"; -warn "D6 00 0F FE 00 00 05 01 E00401003123AA26 941A ##### ready?\n"; +exit; for ( 1 .. 3 ) { @@ -153,7 +200,7 @@ { my $str=shift; my $count = $port->write($str); - print "#> ", as_hex( $str ), "\t[$count]\n"; + print "#> ", as_hex( $str ), "\t[$count]\n" if $debug; } sub as_hex { @@ -204,6 +251,9 @@ our $dispatch; sub dispatch { my ( $pattern, $coderef ) = @_; + + $dispatch->{ $pattern } = $coderef; + my $patt = substr( str2bytes($pattern), 3 ); # just payload my $l = length($patt); my $p = substr( $assert->{payload}, 0, $l ); @@ -219,15 +269,39 @@ } } +use Digest::CRC; + +sub crcccitt { + my $bytes = shift; + my $crc = Digest::CRC->new( + # midified CCITT to xor with 0xffff instead of 0x0000 + width => 16, init => 0xffff, xorout => 0xffff, refout => 0, poly => 0x1021, refin => 0, + ) or die $!; + $crc->add( $bytes ); + pack('n', $crc->digest); +} + # my $checksum = checksum( $bytes ); # my $checksum = checksum( $bytes, $original_checksum ); sub checksum { my ( $bytes, $checksum ) = @_; - my $xor = 0; + + my $xor = crcccitt( substr($bytes,1) ); # skip D6 + warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug; + + my $len = ord(substr($bytes,2,1)); + my $len_real = length($bytes) - 1; + + if ( $len_real != $len ) { + print "length wrong: $len_real != $len\n"; + $bytes = substr($bytes,0,2) . chr($len_real) . substr($bytes,4); + } if ( defined $checksum && $xor ne $checksum ) { - printf "checksum doesn't match: %04x != %04x data: %s\n", $xor, $checksum; + print "checksum doesn't match: ", as_hex($xor), " != ", as_hex($checksum), " data: ", as_hex($bytes), "\n"; + return $bytes . $xor; } + return $bytes . $checksum; } sub readchunk { @@ -261,10 +335,9 @@ sub str2bytes { my $str = shift || confess "no str?"; my $b = $str; - $b =~ s/\s+(\S\S)(\S\S)+\s*/ $1 $2/; # fix checksum - $b =~ s/\s+$//; - $b =~ s/\s+/\\x/g; - $b = '"\x' . $b . '"'; + $b =~ s/\s+//g; + $b =~ s/(..)/\\x$1/g; + $b = "\"$b\""; my $bytes = eval $b; die $@ if $@; warn "## str2bytes( $str ) => $b => ",as_hex($bytes) if $debug; @@ -275,6 +348,9 @@ my ( $cmd, $cmd_desc, $expect, $expect_desc, $coderef ) = @_; my $bytes = str2bytes( $cmd ); + # fix checksum if needed + $bytes = checksum( substr( $bytes, 0, -2 ), substr( $bytes, -2, 2 ) ); + warn ">> ", as_hex( $bytes ), "\t## $cmd_desc\n"; $assert->{send} = $cmd; writechunk( $bytes );