line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package RPi::Serial; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
67836
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
32
|
|
5
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
468
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '3.00'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
require XSLoader; |
10
|
|
|
|
|
|
|
XSLoader::load('RPi::Serial', $VERSION); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
0
|
|
|
0
|
1
|
|
my ($class, $device, $baud) = @_; |
14
|
0
|
|
|
|
|
|
my $self = bless {}, $class; |
15
|
0
|
|
|
|
|
|
$self->fd(tty_open($device, $baud)); |
16
|
0
|
|
|
|
|
|
return $self; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
sub close { |
19
|
0
|
|
|
0
|
1
|
|
tty_close($_[0]->fd); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
sub avail { |
22
|
0
|
|
|
0
|
1
|
|
return tty_available($_[0]->fd); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
sub fd { |
25
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
26
|
0
|
0
|
|
|
|
|
$self->{fd} = shift if @_; |
27
|
0
|
|
|
|
|
|
return $self->{fd}; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
sub flush { |
30
|
0
|
|
|
0
|
1
|
|
tty_flush($_[0]->fd); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
sub putc { |
33
|
0
|
|
|
0
|
1
|
|
tty_putc($_[0]->fd, $_[1]); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
sub puts { |
36
|
0
|
|
|
0
|
1
|
|
tty_puts($_[0]->fd, $_[1]); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
sub getc { |
39
|
0
|
|
|
0
|
1
|
|
return tty_getc($_[0]->fd); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
sub gets { |
42
|
0
|
|
|
0
|
1
|
|
return tty_gets($_[0]->fd, $_[1]); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
sub DESTROY { |
45
|
0
|
|
|
0
|
|
|
tty_close($_[0]->fd); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
0
|
|
|
sub __placeholder {} # vim folds |
48
|
|
|
|
|
|
|
1; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 NAME |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
RPi::Serial - Basic read/write interface to a serial port |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 SYNOPSIS |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
use RPi::Serial; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
my $dev = "/dev/ttyAMA0"; |
59
|
|
|
|
|
|
|
my $baud = 115200; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $ser = RPi::Serial->new($dev, $baud); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
$ser->putc(5); |
64
|
|
|
|
|
|
|
$ser->puts("hello, world!"); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $char = $ser->getc; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $num_bytes = 12; |
69
|
|
|
|
|
|
|
my $str = $ser->gets($num_bytes); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
$ser->flush; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
my $bytes_available = $ser->avail; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
$ser->close; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 DESCRIPTION |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Provides basic read and write functionality of a UART serial interface |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 WARNING |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
If using on a Raspberry Pi platform: |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
In order to use GPIO pins 14 and 15 as a serial interface on the Raspberry Pi, |
86
|
|
|
|
|
|
|
you need to disable the built-in Bluetooth adaptor. This distribution will not |
87
|
|
|
|
|
|
|
operate correctly without this being done. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
To disable Bluetooth on the Pi, edit the C, and add the |
90
|
|
|
|
|
|
|
following line: |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
dtoverlay=pi3-disable-bt-overlay |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Save the file, then reboot the Pi. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 METHODS |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 new($device, $baud); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Opens the specified serial port at the specified baud rate, and returns a new |
101
|
|
|
|
|
|
|
L object. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Parameters: |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
$device |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Mandatory, String: The serial device to open (eg: C<"/dev/ttyAMA0">. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
$baud |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Mandatory, Integer: A valud baud rate to use. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 close |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Closes an already open serial device. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 avail |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Returns the number of bytes waiting to be read if any. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head2 flush |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Flush any data currently in the serial buffer. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head2 fd |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Returns the C file descriptor for the current serial object. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 getc |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Retrieve a single character from the serial port. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head2 gets($num_bytes) |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Read a specified number of bytes into a string. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Parameters: |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
$num_bytes |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Mandatory, Integer; The number of bytes to read. If this number is larger than |
142
|
|
|
|
|
|
|
what is available to be read, a 10 second timeout will briefly hand your |
143
|
|
|
|
|
|
|
application. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head2 putc($char) |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Writes a single character to the serial device. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Parameters: |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
$char |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Mandatory, Unsigned Char: The character to write to the port. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 puts($string) |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Write a character string to the serial device. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Parameters: |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
$string |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Mandatory, String: Whatever you want to write to the serial line. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 AUTHOR |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Steve Bertrand, C<< >> |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Copyright 2018 Steve Bertrand. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
174
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
175
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
See L for more information. |