From: Dobrica Pavlinusic Date: Mon, 26 Jul 2010 16:35:43 +0000 (+0200) Subject: as_hex and hex2bytes X-Git-Tag: RFID-Biblio-0.02~167 X-Git-Url: http://git.rot13.org/?p=Biblio-RFID.git;a=commitdiff_plain;h=f628b762aa3b1763621eba07da3f67fa51f49f6b as_hex and hex2bytes --- diff --git a/lib/RFID/Serial.pm b/lib/RFID/Serial.pm index a829946..82e1c61 100644 --- a/lib/RFID/Serial.pm +++ b/lib/RFID/Serial.pm @@ -3,51 +3,107 @@ package RFID::Serial; use warnings; use strict; -=head1 NAME - -RFID::Serial - The great new RFID::Serial! +use Device::SerialPort qw(:STAT); +use Data::Dump qw(dump); -=head1 VERSION +=head1 NAME -Version 0.01 +RFID::Serial - support serial RFID devices =cut our $VERSION = '0.01'; +my $debug = 0; + +use base 'Exporter'; +our @EXPORT = qw( hex2bytes as_hex ); + =head1 SYNOPSIS -Quick summary of what the module does. +This module tries to support USB serial RFID readers wsing simple API +which is sutable for direct mapping to REST JSONP service. Perhaps a little code snippet. use RFID::Serial; - my $foo = RFID::Serial->new(); - ... - -=head1 EXPORT - -A list of functions that can be exported. You can delete this section -if you don't export anything, such as for a purely object-oriented module. + my $rfid = RFID::Serial->new( + device => '/dev/ttyUSB0', # with fallback to RFID_DEVICE + ); =head1 SUBROUTINES/METHODS -=head2 function1 +=head2 new =cut -sub function1 { +sub new { + my $class = shift; + my $self = {@_}; + bless $self, $class; + + $self->port; + + $self->init; + + return $self; +} + +sub port { + my $self = shift; + + return $self->{port} if defined $self->{port}; + + my $settings = $self->serial_settings; + $settings->{device} ||= $ENV{RFID_DEVICE}; + warn "# settings ",dump $settings; + + $self->{port} = Device::SerialPort->new( $settings->{device} ) + || die "can't open serial port: $!\n"; + + $self->{port}->$_( $settings->{$_} ) + foreach ( qw/handshake baudrate databits parity stopbits/ ); + } -=head2 function2 +sub init { + warn "no init"; +} + +=head1 EXPORT + +Formatting functions are exported + +=head2 hex2bytes =cut -sub function2 { +sub hex2bytes { + my $str = shift || die "no str?"; + my $b = $str; + $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; + return $bytes; } +sub as_hex { + my @out; + foreach my $str ( @_ ) { + my $hex = uc unpack( 'H*', $str ); + $hex =~ s/(..)/$1 /g if length( $str ) > 2; + $hex =~ s/\s+$//; + push @out, $hex; + } + return join(' | ', @out); +} + + =head1 AUTHOR Dobrica Pavlinusic, C<< >>