--- 3m-810.pl 2008/10/03 12:31:58 18 +++ 3m-810.pl 2009/03/28 03:47:10 23 @@ -6,9 +6,27 @@ use Data::Dump qw/dump/; use Carp qw/confess/; +use Getopt::Long; + +use IO::Socket::INET; + +my $meteor = IO::Socket::INET->new( '192.168.1.13:4671' ) || die "can't connect to meteor: $!"; + +sub meteor { + my ( $item, $html ) = @_; + warn ">> meteor $item $html\n"; + print $meteor "ADDMESSAGE test $item|" . localtime() . "
$html\n"; +} 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 +39,25 @@ '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 $!; + +my $verbose = $debug > 0 ? $debug-- : 0; + =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 @@ -52,23 +82,18 @@ =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 $tags_data; +my $visible_tags; -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); $parity=$port->parity($parity); $stopbits=$port->stopbits($stopbits); -print "## using $device $baudrate $databits $parity $stopbits\n"; +print "## using $device $baudrate $databits $parity $stopbits debug: $debug verbose: $verbose\n"; # Just in case: reset our timing and buffers $port->lookclear(); @@ -81,23 +106,29 @@ # initial hand-shake with device -cmd( 'D5 00 05 04 00 11 8C66', 'hw version?', - 'D5 00 09 04 00 11 0A 05 00 02 7250', 'hw 10.5.0.2', sub { - print "hardware version ", join('.', unpack('CCCC', skip_assert(3) )), "\n"; +cmd( 'D5 00 05 04 00 11 8C66', 'hw version', + 'D5 00 09 04 00 11 0A 05 00 02 7250', sub { + my $hw_ver = join('.', unpack('CCCC', skip_assert(3) )); + print "hardware version $hw_ver\n"; + meteor( -1, "Found reader $hw_ver" ); }); -cmd( 'D6 00 0C 13 04 01 00 02 00 03 00 04 00 AAF2','stats?', - 'D6 00 0C 13 00 02 01 01 03 02 02 03 00 E778','FIXME: unimplemented', sub { assert() } ); +cmd( 'D6 00 0C 13 04 01 00 02 00 03 00 04 00 AAF2','FIXME: stats?', + 'D6 00 0C 13 00 02 01 01 03 02 02 03 00 E778', sub { assert() } ); # start scanning for tags -cmd( 'D6 00 05 FE 00 05 FA40', "XXX scan $_", - 'D6 00 07 FE 00 00 05 00 C97B', 'no tag', sub { -dispatch( - 'D6 00 0F FE 00 00 05 ',# 01 E00401003123AA26 941A # seen, serial length: 8 - sub { - my $rest = shift || die "no rest?"; - my $nr = ord( substr( $rest, 0, 1 ) ); +cmd( 'D6 00 05 FE 00 05 FA40', "scan for tags, retry $_", + 'D6 00 0F FE 00 00 05 ', sub { # 01 E00401003123AA26 941A # seen, serial length: 8 + my $rest = shift || die "no rest?"; + my $nr = ord( substr( $rest, 0, 1 ) ); + + if ( ! $nr ) { + print "no tags in range\n"; + update_visible_tags(); + meteor( -1, "No tags in range" ); + } else { + my $tags = substr( $rest, 1 ); my $tl = length( $tags ); @@ -106,33 +137,66 @@ my @tags; 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(',', @tags ) , "\n"; + print "$nr tags in range: ", join(',', @tags ) , "\n"; + + update_visible_tags( @tags ); - # XXX read first tag - read_tag( @tags ); + my $html = join('', map { "
  • $_" } @tags); + meteor( 0, "Tags:" ); + } + } +) foreach ( 1 .. 1000 ); + + + +sub update_visible_tags { + my @tags = @_; + my $last_visible_tags = $visible_tags; + $visible_tags = {}; + + foreach my $tag ( @tags ) { + if ( ! defined $last_visible_tags->{$tag} ) { + read_tag( $tag ); + $visible_tags->{$tag}++; + } else { + warn "## using cached data for $tag" if $debug; } -) } + delete $last_visible_tags->{$tag}; # leave just missing tags + } -) foreach ( 1 .. 100 ); + foreach my $tag ( keys %$last_visible_tags ) { + my $data = delete $tags_data->{$tag}; + print "removed tag $tag with data ",dump( $data ),"\n"; + } + + warn "## update_visible_tags(",dump( @tags ),") = ",dump( $visible_tags )," removed: ",dump( $last_visible_tags ), " data: ",dump( $tags_data ) if $debug; +} -my $read_cached; sub read_tag { my ( $tag ) = @_; + confess "no tag?" unless $tag; + + return if defined $tags_data->{$tag}; + 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"; + 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", sub { + print "FIXME: tag $tag ready?\n"; + }, + "D6 00 1F 02 00", sub { # $tag 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 $tag = uc(unpack('H16',substr( $rest, 0, 8 ))); + my $blocks = ord(substr($rest,8,1)); + $rest = substr($rest,9); # leave just data blocks my @data; foreach my $nr ( 0 .. $blocks - 1 ) { - my $block = substr( $rest, 1 + $nr * 6, 6 ); + my $block = substr( $rest, $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; @@ -141,10 +205,10 @@ 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"; - }) - }); + $tags_data->{ $tag } = join('', @data); + print "DATA $tag ",dump( $tags_data ), "\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) { @@ -156,8 +220,13 @@ } warn "?? D6 00 0F FE 00 00 05 01 $tag 941A ##### ready?\n"; + my $item = unpack('H*', substr($tag,-8) ) % 100000; + meteor( $item, "Loading $item" ); + } +exit; + for ( 1 .. 3 ) { # ++-->type 00-0a @@ -188,7 +257,7 @@ { my $str=shift; my $count = $port->write($str); - print "#> ", as_hex( $str ), "\t[$count]" if $debug; + print "#> ", as_hex( $str ), "\t[$count]\n" if $debug; } sub as_hex { @@ -236,24 +305,6 @@ return substr( $assert->{payload}, $to ); } -our $dispatch; -sub dispatch { - my ( $pattern, $coderef ) = @_; - my $patt = substr( str2bytes($pattern), 3 ); # just payload - my $l = length($patt); - my $p = substr( $assert->{payload}, 0, $l ); - warn "## dispatch pattern $pattern [$l] ",dump( $patt, $p ) if $debug; - - if ( $assert->{payload} eq $assert->{expect} ) { - warn "## no dispatch, payload expected" if $debug; - } elsif ( $p eq $patt ) { - # if matched call with rest of payload - $coderef->( substr( $assert->{payload}, $l ) ); - } else { - warn "## dispatch ignored" if $debug; - } -} - use Digest::CRC; sub crcccitt { @@ -289,9 +340,9 @@ return $bytes . $checksum; } -sub readchunk { - my ( $parser ) = @_; +our $dispatch; +sub readchunk { sleep 1; # FIXME remove # read header of packet @@ -305,14 +356,28 @@ warn "## payload too short $payload_len != $len\n" if $payload_len != $len; my $checksum = substr( $data, -2, 2 ); - checksum( $header . $length . $payload, $checksum ); + checksum( $header . $length . $payload , $checksum ); - print "<< ",as_hex( $header ), " [$len] ", as_hex( $payload ), " | sum: ",as_hex($checksum),"\n"; + print "<< ",as_hex( $header ), " [$len] ", as_hex( $payload ), " | sum: ",as_hex($checksum),"\n" if $verbose; $assert->{len} = $len; $assert->{payload} = $payload; - $parser->( $len, $payload ) if $parser && ref($parser) eq 'CODE'; + my $full = $header . $length . $data; # full + # find longest match for incomming data + my ($to) = grep { + my $match = substr($payload,0,length($_)); + m/^\Q$match\E/ + } sort { length($a) <=> length($b) } keys %$dispatch; + warn "?? payload dispatch to ",dump( $payload, $dispatch, $to ) if $debug; + + if ( defined $to ) { + my $rest = substr( $payload, length($to) ); + warn "## DISPATCH payload to with rest", dump( $payload, $to, $rest ) if $debug; + $dispatch->{ $to }->( $rest ); + } else { + print "NO DISPATCH for ",dump( $full ),"\n"; + } return $data; } @@ -330,20 +395,30 @@ } sub cmd { - my ( $cmd, $cmd_desc, $expect, $expect_desc, $coderef ) = @_; + my $cmd = shift || confess "no cmd?"; + my $cmd_desc = shift || confess "no description?"; + my @expect = @_; + 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"; + warn ">> ", as_hex( $bytes ), "\t## $cmd_desc\n" if $verbose; $assert->{send} = $cmd; writechunk( $bytes ); - if ( $expect ) { - warn "?? $expect", $expect_desc ? "\t## $expect_desc" : '', "\n"; - $assert->{expect} = substr(str2bytes($expect), 3, -2); # just expected payload - readchunk( $coderef ); + while ( @expect ) { + my $pattern = str2bytes( shift @expect ) || confess "no pattern?"; + my $coderef = shift @expect || confess "no coderef?"; + confess "not coderef" unless ref $coderef eq 'CODE'; + + next if defined $dispatch->{ $pattern }; + + $dispatch->{ substr($pattern,3) } = $coderef; + warn "++ dispatch ", as_hex($pattern) ,dump( $dispatch ) if $debug; } + + readchunk; }