line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
######################################################################################### |
2
|
|
|
|
|
|
|
# Package HiPi::Device::SerialPort |
3
|
|
|
|
|
|
|
# Description: Serial Port driver |
4
|
|
|
|
|
|
|
# Copyright : Copyright (c) 2013-2017 Mark Dootson |
5
|
|
|
|
|
|
|
# License : This is free software; you can redistribute it and/or modify it under |
6
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
7
|
|
|
|
|
|
|
######################################################################################### |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package HiPi::Device::SerialPort; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
######################################################################################### |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
1087
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
14
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
28
|
|
15
|
1
|
|
|
1
|
|
5
|
use parent qw( HiPi::Device ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
16
|
1
|
|
|
1
|
|
67
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
60
|
|
17
|
1
|
|
|
1
|
|
6
|
use Try::Tiny; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
641
|
|
18
|
|
|
|
|
|
|
require Device::SerialPort if $^O =~ /^linux$/i; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION ='0.81'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
__PACKAGE__->create_accessors( qw( portopen baudrate parity stopbits databits serialdriver ) ); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub new { |
25
|
0
|
|
|
0
|
0
|
|
my( $class, %userparams ) = @_; |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
my %params = ( |
28
|
|
|
|
|
|
|
# standard device |
29
|
|
|
|
|
|
|
devicename => '/dev/ttyAMA0', |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# serial port |
32
|
|
|
|
|
|
|
baudrate => 9600, |
33
|
|
|
|
|
|
|
parity => 'none', |
34
|
|
|
|
|
|
|
stopbits => 1, |
35
|
|
|
|
|
|
|
databits => 8, |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# this |
38
|
|
|
|
|
|
|
serialdriver => undef, |
39
|
|
|
|
|
|
|
portopen => 0, |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# get user params |
44
|
0
|
|
|
|
|
|
foreach my $key( keys (%params) ) { |
45
|
0
|
0
|
|
|
|
|
$params{$key} = $userparams{$key} if exists($userparams{$key}); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# warn user about unsupported params |
49
|
0
|
|
|
|
|
|
foreach my $key( keys (%userparams) ) { |
50
|
0
|
0
|
|
|
|
|
carp(qq(unknown parameter name ) . $key) if not exists($params{$key}); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
0
|
0
|
|
|
|
|
my $driver = Device::SerialPort->new( $params{devicename} ) or |
54
|
|
|
|
|
|
|
croak qq(unable to open device $params{devicename}); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
try { |
57
|
0
|
|
|
0
|
|
|
$driver->baudrate($params{baudrate}); |
58
|
0
|
|
|
|
|
|
$driver->parity($params{parity}); |
59
|
0
|
|
|
|
|
|
$driver->stopbits($params{stopbits}); |
60
|
0
|
|
|
|
|
|
$driver->databits($params{databits}); |
61
|
0
|
|
|
|
|
|
$driver->handshake('none'); |
62
|
0
|
|
|
|
|
|
$driver->write_settings; |
63
|
|
|
|
|
|
|
} catch { |
64
|
0
|
|
|
0
|
|
|
croak(qq(failed to set serial port params : $_) ); |
65
|
0
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
$params{serialdriver} = $driver; |
68
|
0
|
|
|
|
|
|
$params{portopen} = 1; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new( %params ) ; |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
return $self; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub write { |
76
|
0
|
|
|
0
|
0
|
|
my($self, $buffer) = @_; |
77
|
0
|
0
|
|
|
|
|
return unless $self->portopen; |
78
|
0
|
|
|
|
|
|
my $result = $self->serialdriver->write($buffer); |
79
|
0
|
|
|
|
|
|
$self->serialdriver->write_drain; |
80
|
0
|
|
|
|
|
|
return $result; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub can_read { |
84
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub read { |
88
|
0
|
|
|
0
|
0
|
|
my($self, $timeout) = @_; |
89
|
0
|
|
0
|
|
|
|
$timeout ||= 0; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub close { |
93
|
0
|
0
|
|
0
|
0
|
|
return unless $_[0]->portopen; |
94
|
0
|
|
|
|
|
|
$_[0]->portopen( 0 ); |
95
|
0
|
0
|
|
|
|
|
$_[0]->serialdriver->close or croak q(failed to close serial port); |
96
|
0
|
|
|
|
|
|
$_[0]->serialdriver( undef ); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
__END__ |