| 1 | package Biblio::RFID::Reader::Serial; | ||||||||||
| 2 | |||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 5 | ▶ | use Device::SerialPort qw(:STAT); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | ▶ | use Data::Dump qw(dump); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | Biblio::RFID::Reader::Serial - base class for serial RFID readers | ||||||||||
| 12 | |||||||||||
| 13 | =head1 METHODS | ||||||||||
| 14 | |||||||||||
| 15 | =head2 new | ||||||||||
| 16 | |||||||||||
| 17 | Open serial port (if needed) and init reader | ||||||||||
| 18 | |||||||||||
| 19 | =cut | ||||||||||
| 20 | |||||||||||
| 21 | sub new { | ||||||||||
| 22 | 2 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 23 | 2 | my $self = {@_}; | |||||||||
| 24 | 2 | bless $self, $class; | |||||||||
| 25 | |||||||||||
| 26 | 2 | ▶ | $self->port && return $self; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | |||||||||||
| 30 | =head2 port | ||||||||||
| 31 | |||||||||||
| 32 | Tries to open usb serial ports C</dev/ttyUSB*> | ||||||||||
| 33 | |||||||||||
| 34 | my $serial_obj = $self->port; | ||||||||||
| 35 | |||||||||||
| 36 | To try just one device use C<RFID_DEVICE=/dev/ttyUSB1> enviroment variable | ||||||||||
| 37 | |||||||||||
| 38 | =cut | ||||||||||
| 39 | |||||||||||
| 40 | 5 | our $serial_device; | |||||||||
| 41 | |||||||||||
| 42 | sub port { | ||||||||||
| 43 | 2 | ▶ | my $self = shift; | ||||||||
|
Subroutine
port: called CC=8 SCAR=32.6
Pod
documented
| |||||||||||
| 44 | |||||||||||
| 45 | 2 | ▶ | return $self->{port} if defined $self->{port}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | |||||||||||
| 47 | 2 | my $settings = $self->serial_settings; | |||||||||
| 48 | 1 | ▶ | my @devices = $ENV{RFID_DEVICE} ? ( $ENV{RFID_DEVICE} ) : glob '/dev/ttyUSB*'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 1 | warn "# port devices ",dump(@devices); | |||||||||
| 50 | |||||||||||
| 51 | 1 | foreach my $device ( @devices ) { | |||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | next if $serial_device->{$device}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | if ( my $port = Device::SerialPort->new($device) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | |||||||||||
| 57 | 0 | foreach my $opt ( qw/handshake baudrate databits parity stopbits/ ) { | |||||||||
| 58 | 0 | $port->$opt( $settings->{$opt} ); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 0 | $self->{port} = $port; | |||||||||
| 62 | |||||||||||
| 63 | 0 | warn "# probe by init $device ",ref($self); | |||||||||
| 64 | 0 | ▶ | if ( $self->init ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | warn "init OK ", ref($self), " $device settings ",dump $settings; | |||||||||
| 66 | 0 | $serial_device->{$device} = $port; | |||||||||
| 67 | 0 | last; | |||||||||
| 68 | } else { | ||||||||||
| 69 | 0 | $self->{port} = 0; | |||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 1 | warn "# serial_device ",dump($serial_device); | |||||||||
| 75 | |||||||||||
| 76 | 1 | return $self->{port}; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 5 | 1 | |||||||||
| 80 | __END__ | ||||||||||