RPi::Serial - Basic read/write interface to a serial port
use RPi::Serial; my $dev = "/dev/ttyAMA0"; my $baud = 115200; my $ser = RPi::Serial->new($dev, $baud); $ser->putc(5); $ser->puts("hello, world!"); my $char = $ser->getc; my $num_bytes = 12; my $str = $ser->gets($num_bytes); $ser->flush; my $bytes_available = $ser->avail; $ser->close;
Provides basic read and write functionality of a UART serial interface
If using on a Raspberry Pi platform:
In order to use GPIO pins 14 and 15 as a serial interface on the Raspberry Pi, you need to disable the built-in Bluetooth adaptor. This distribution will not operate correctly without this being done.
To disable Bluetooth on the Pi, edit the /boot/config.txt, and add the following line:
/boot/config.txt
dtoverlay=pi3-disable-bt-overlay
Save the file, then reboot the Pi.
Opens the specified serial port at the specified baud rate, and returns a new RPi::Serial object.
Parameters:
$device
Mandatory, String: The serial device to open (eg: "/dev/ttyAMA0".
"/dev/ttyAMA0"
$baud
Mandatory, Integer: A valud baud rate to use.
Closes an already open serial device.
Returns the number of bytes waiting to be read if any.
Flush any data currently in the serial buffer.
Returns the ioctl file descriptor for the current serial object.
ioctl
Retrieve a single character from the serial port.
Read a specified number of bytes into a string.
$num_bytes
Mandatory, Integer; The number of bytes to read. If this number is larger than what is available to be read, a 10 second timeout will briefly hand your application.
Writes a single character to the serial device.
$char
Mandatory, Unsigned Char: The character to write to the port.
Write a character string to the serial device.
$string
Mandatory, String: Whatever you want to write to the serial line.
Steve Bertrand, <steveb at cpan.org>
<steveb at cpan.org>
Copyright 2018 Steve Bertrand.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install RPi::Serial, copy and paste the appropriate command in to your terminal.
cpanm
cpanm RPi::Serial
CPAN shell
perl -MCPAN -e shell install RPi::Serial
For more information on module installation, please visit the detailed CPAN module installation guide.