unify read_blocks and invetory interface
[Biblio-RFID.git] / lib / RFID / Serial.pm
1 package RFID::Serial;
2
3 use warnings;
4 use strict;
5
6 use base 'Exporter';
7 our @EXPORT = qw( hex2bytes as_hex hex_tag );
8
9 use Device::SerialPort qw(:STAT);
10 use Data::Dump qw(dump);
11
12 =head1 NAME
13
14 RFID::Serial - support serial RFID devices
15
16 =cut
17
18 our $VERSION = '0.01';
19
20 my $debug = 0;
21
22
23 =head1 SYNOPSIS
24
25 This module tries to support USB serial RFID readers wsing simple API
26 which is sutable for direct mapping to REST JSONP service.
27
28 Perhaps a little code snippet.
29
30     use RFID::Serial;
31
32     my $rfid = RFID::Serial->new(
33                 device => '/dev/ttyUSB0', # with fallback to RFID_DEVICE
34         );
35
36 =head1 SUBROUTINES/METHODS
37
38 =head2 new
39
40 =cut
41
42 sub new {
43         my $class = shift;
44         my $self = {@_};
45         bless $self, $class;
46
47         $self->port;
48
49         $self->init;
50
51         return $self;
52 }
53
54 sub port {
55         my $self = shift;
56
57         return $self->{port} if defined $self->{port};
58
59         my $settings = $self->serial_settings;
60         $settings->{device} ||= $ENV{RFID_DEVICE};
61         warn "# settings ",dump $settings;
62
63         $self->{port} = Device::SerialPort->new( $settings->{device} )
64         || die "can't open serial port: $!\n";
65
66         $self->{port}->$_( $settings->{$_} )
67         foreach ( qw/handshake baudrate databits parity stopbits/ );
68
69 }
70
71 sub init {
72         warn "no init";
73 }
74
75 =head1 EXPORT
76
77 Formatting functions are exported
78
79 =head2 hex2bytes
80
81   my $bytes = hex2bytes($hex);
82
83 =cut
84
85 sub hex2bytes {
86         my $str = shift || die "no str?";
87         my $b = $str;
88         $b =~ s/\s+//g;
89         $b =~ s/(..)/\\x$1/g;
90         $b = "\"$b\"";
91         my $bytes = eval $b;
92         die $@ if $@;
93         warn "## str2bytes( $str ) => $b => ",as_hex($bytes) if $debug;
94         return $bytes;
95 }
96
97 =head2 as_hex
98
99   print as_hex( $bytes );
100
101 =cut
102
103 sub as_hex {
104         my @out;
105         foreach my $str ( @_ ) {
106                 my $hex = uc unpack( 'H*', $str );
107                 $hex =~ s/(..)/$1 /g if length( $str ) > 2;
108                 $hex =~ s/\s+$//;
109                 push @out, $hex;
110         }
111         return join(' | ', @out);
112 }
113
114 =head2 hex_tag
115
116   print hex_tag $bytes;
117
118 =cut
119
120 sub hex_tag { uc(unpack('H16', shift)) }
121
122
123 =head1 AUTHOR
124
125 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
126
127 =head1 BUGS
128
129 Please report any bugs or feature requests to C<bug-rfid-serial at rt.cpan.org>, or through
130 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=RFID-Serial>.  I will be notified, and then you'll
131 automatically be notified of progress on your bug as I make changes.
132
133
134
135
136 =head1 SUPPORT
137
138 You can find documentation for this module with the perldoc command.
139
140     perldoc RFID::Serial
141
142
143 You can also look for information at:
144
145 =over 4
146
147 =item * RT: CPAN's request tracker
148
149 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=RFID-Serial>
150
151 =item * AnnoCPAN: Annotated CPAN documentation
152
153 L<http://annocpan.org/dist/RFID-Serial>
154
155 =item * CPAN Ratings
156
157 L<http://cpanratings.perl.org/d/RFID-Serial>
158
159 =item * Search CPAN
160
161 L<http://search.cpan.org/dist/RFID-Serial/>
162
163 =back
164
165
166 =head1 ACKNOWLEDGEMENTS
167
168
169 =head1 LICENSE AND COPYRIGHT
170
171 Copyright 2010 Dobrica Pavlinusic.
172
173 This program is free software; you can redistribute it and/or modify
174 it under the terms of the GNU General Public License as published by
175 the Free Software Foundation; version 2 dated June, 1991 or at your option
176 any later version.
177
178 This program is distributed in the hope that it will be useful,
179 but WITHOUT ANY WARRANTY; without even the implied warranty of
180 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
181 GNU General Public License for more details.
182
183 A copy of the GNU General Public License is available in the source tree;
184 if not, write to the Free Software Foundation, Inc.,
185 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
186
187
188 =cut
189
190 1; # End of RFID::Serial