Loading...
Searching...
No Matches
LibSerial

# Welcome to LibSerial

LibSerial provides a collection of C++ classes that allow object oriented access to serial ports on POSIX systems.

The SerialPort class is available to simplified access to serial port settings along with a set of convenient read/write methods.
This class is useful for embedded systems where a complete C++ STL may not be available.

The SerialStream class allows access to serial ports in the same manner as standard C++ iostream objects.

Member functions are provided in both classes for setting serial port parameters such as baud rate, character size, flow control, etc.

LibSerial exists to simplify serial port programming on POSIX systems.
Here is short example using libserial:

#include <libserial/SerialPort.h>
#include <libserial/SerialStream.h>
#include <iostream>
constexpr const char* const SERIAL_PORT_1 = "/dev/ttyUSB0" ;
constexpr const char* const SERIAL_PORT_2 = "/dev/ttyUSB1" ;
int main()
{
using LibSerial::SerialPort ;
using LibSerial::SerialStream ;
// Instantiate a Serial Port and a Serial Stream object.
SerialPort serial_port ;
SerialStream serial_stream ;
// Open the hardware serial ports.
serial_port.Open( SERIAL_PORT_1 ) ;
serial_stream.Open( SERIAL_PORT_2 ) ;
// Set the baud rates.
using LibSerial::BaudRate ;
serial_port.SetBaudRate( BaudRate::BAUD_115200 ) ;
serial_stream.SetBaudRate( BaudRate::BAUD_115200 ) ;
char write_byte_1 = 'a' ;
char write_byte_2 = 'b' ;
char read_byte_1 = 'A' ;
char read_byte_2 = 'B' ;
// Write a character.
serial_port.WriteByte(write_byte_1) ;
serial_stream << write_byte_2 ;
size_t timeout_milliseconds = 5 ;
using LibSerial::ReadTimeout ;
try
{
// Read a character.
serial_port.ReadByte(read_byte_1, timeout_milliseconds) ;
serial_stream >> read_byte_2 ;
}
catch (const ReadTimeout&)
{
std::cerr << "The Read() call has timed out." << std::endl ;
}
std::cout << "serial_port read: " << read_byte_1 << std::endl ;
std::cout << "serial_stream read: " << read_byte_2 << std::endl ;
// Close the Serial Port and Serial Stream.
serial_port.Close() ;
serial_stream.Close() ;
}
LibSerial::ReadTimeout
Exception error thrown when data could not be read from the serial port before the timeout had been e...
Definition SerialPortConstants.h:172
LibSerial::SerialPort
SerialPort allows an object oriented approach to serial port communication. A serial port object can ...
Definition SerialPort.h:56
LibSerial::SerialStream
SerialStream is a stream class for accessing serial ports on POSIX operating systems....
Definition SerialStream.h:74
LibSerial::SerialStream::Open
void Open(const std::string &fileName, const std::ios_base::openmode &openMode=std::ios_base::in|std::ios_base::out)
Opens the serial port associated with the specified file name and the specified mode.
Definition SerialStream.cpp:83

In addition to the C++ programming languge, LibSerial releases after version 0.6.0 also provide bindings to several scripting languages such as Python, Perl, PHP, Java, and Ruby. This provides developers a wide range languages to select when writing applications that need access to serial ports on POSIX compatible operating systems. LibSerial has received most extensive testing under Linux operating system.