File Coverage

blib/lib/RPi/Serial.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 15 15 100.0


line stmt bran cond sub pod time code
1             package RPi::Serial;
2              
3 1     1   21646 use 5.006;
  1         6  
4 1     1   7 use strict;
  1         3  
  1         27  
5 1     1   7 use warnings;
  1         7  
  1         39  
6              
7 1     1   581 use parent 'WiringPi::API';
  1         333  
  1         6  
8              
9             our $VERSION = '0.01';
10              
11             sub new {
12             my ($class, $device, $baud) = @_;
13             my $self = bless {}, $class;
14             $self->fd($self->serial_open($device, $baud));
15             return $self;
16             }
17             sub close {
18             $_[0]->serial_close($_[0]->fd);
19             }
20             sub avail {
21             return $_[0]->serial_data_avail($_[0]->fd);
22             }
23             sub fd {
24             my $self = shift;
25             $self->{fd} = shift if @_;
26             return $self->{fd};
27             }
28             sub flush {
29             $_[0]->serial_flush($_[0]->fd);
30             }
31             sub putc {
32             $_[0]->serial_put_char($_[0]->fd, $_[1]);
33             }
34             sub puts {
35             $_[0]->serial_puts($_[0]->fd, $_[1]);
36             }
37             sub getc {
38             return $_[0]->serial_get_char($_[0]->fd);
39             }
40             sub gets {
41             return $_[0]->serial_gets($_[0]->fd, $_[1]);
42             }
43              
44             sub __placeholder {} # vim folds
45             1;
46              
47             =head1 NAME
48              
49             RPi::Serial - Basic read/write interface to a serial port
50              
51             =head1 SYNOPSIS
52              
53             use RPi::Serial;
54              
55             my $dev = "/dev/ttyAMA0";
56             my $baud = 115200;
57            
58             my $ser = RPi::Serial->new($dev, $baud);
59              
60             $ser->putc(5);
61             $ser->puts("hello, world!");
62              
63             my $char = $ser->getc;
64              
65             my $num_bytes = 12;
66             my $str = $ser->gets($num_bytes);
67              
68             $ser->flush;
69              
70             my $bytes_available = $ser->avail;
71              
72             $ser->close;
73              
74             =head1 METHODS
75              
76             =head2 new($device, $baud);
77              
78             Opens the specified serial port at the specified baud rate, and returns a new
79             L object.
80              
81             Parameters:
82              
83             $device
84              
85             Mandatory, String: The serial device to open (eg: C<"/dev/ttyAMA0">.
86              
87             $baud
88              
89             Mandatory, Integer: A valud baud rate to use.
90              
91             =head2 close
92              
93             Closes an already open serial device.
94              
95             =head2 avail
96              
97             Returns the number of bytes waiting to be read if any.
98              
99             =head2 flush
100              
101             Flush any data currently in the serial buffer.
102              
103             =head2 fd
104              
105             Returns the C file descriptor for the current serial object.
106              
107             =head2 getc
108              
109             Retrieve a single character from the serial port.
110              
111             =head2 gets($num_bytes)
112              
113             Read a specified number of bytes into a string.
114              
115             Parameters:
116              
117             $num_bytes
118              
119             Mandatory, Integer; The number of bytes to read. If this number is larger than
120             what is available to be read, a 10 second timeout will briefly hand your
121             application.
122              
123             =head2 putc($char)
124              
125             Writes a single character to the serial device.
126              
127             Parameters:
128              
129             $char
130              
131             Mandatory, Unsigned Char: The character to write to the port.
132              
133             =head2 puts($string)
134              
135             Write a character string to the serial device.
136              
137             Parameters:
138              
139             $string
140              
141             Mandatory, String: Whatever you want to write to the serial line.
142              
143             =head1 AUTHOR
144              
145             Steve Bertrand, C<< >>
146              
147             =head1 LICENSE AND COPYRIGHT
148              
149             Copyright 2017 Steve Bertrand.
150              
151             This program is free software; you can redistribute it and/or modify it
152             under the terms of either: the GNU General Public License as published
153             by the Free Software Foundation; or the Artistic License.
154              
155             See L for more information.