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