#include <libserial/SerialPort.h>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <unistd.h>
constexpr const char* const SERIAL_PORT_1 = "/dev/ttyUSB0" ;
constexpr const char* const SERIAL_PORT_2 = "/dev/ttyUSB1" ;
int main()
{
using namespace LibSerial ;
try
{
serial_port_1.
Open(SERIAL_PORT_1) ;
serial_port_2.
Open(SERIAL_PORT_2) ;
}
{
std::cerr << "The serial ports did not open correctly." << std::endl ;
return EXIT_FAILURE ;
}
serial_port_1.
SetParity(Parity::PARITY_NONE) ;
serial_port_2.
SetParity(Parity::PARITY_NONE) ;
char write_byte_1 = 'a' ;
char write_byte_2 = 'b' ;
char read_byte_1 = ' ' ;
char read_byte_2 = ' ' ;
std::string write_string_1 =
"\"Do what you can, with what you have, where you are.\" - Theodore Roosevelt" ;
std::string write_string_2 =
"\"Simplicity is prerequisite for reliability.\" - Edsger W. Dijkstra" ;
std::string read_string_1 ;
std::string read_string_2 ;
std::cout << "\nUsing WriteByte() and ReadByte() for one byte of data:"
<< std::endl ;
size_t timeout_milliseconds = 25 ;
try
{
serial_port_1.
ReadByte(read_byte_1, timeout_milliseconds) ;
serial_port_2.
ReadByte(read_byte_2, timeout_milliseconds) ;
}
{
std::cerr << "The ReadByte() call has timed out." << std::endl ;
}
std::cout << "\tSerial Port 1 sent:\t" << write_byte_1 << std::endl
<< "\tSerial Port 2 received:\t" << read_byte_2 << std::endl
<< std::endl ;
std::cout << "\tSerial Port 2 sent:\t" << write_byte_2 << std::endl
<< "\tSerial Port 1 received:\t" << read_byte_1 << std::endl
<< std::endl ;
std::cout << "Using Write() and Read() for a specified number of "
<< "bytes of data:" << std::endl ;
serial_port_1.
Write(write_string_1) ;
serial_port_2.
Write(write_string_2) ;
try
{
serial_port_1.
Read(read_string_1, write_string_2.size(), timeout_milliseconds) ;
serial_port_2.
Read(read_string_2, write_string_1.size(), timeout_milliseconds) ;
}
{
std::cerr << "The Read() call has timed out." << std::endl ;
}
std::cout << "\tSerial Port 1 sent:\t" << write_string_1 << std::endl
<< "\tSerial Port 2 received:\t" << read_string_2 << std::endl
<< std::endl ;
std::cout << "\tSerial Port 2 sent:\t" << write_string_2 << std::endl
<< "\tSerial Port 1 received:\t" << read_string_1 << std::endl
<< std::endl ;
std::string user_input ;
user_input.clear() ;
std::cout << "Using Write() and ReadLine() to write a string and "
<< "read a line of data:" << std::endl << std::endl ;
std::cout << R"(Enter something you would like to send over )"
<< R"(serial, (enter "Q" or "q" to quit): )" << std::flush ;
while(true)
{
std::getline(std::cin, user_input) ;
if (user_input == "q" ||
user_input == "Q" ||
user_input == "")
{
break ;
}
serial_port_1.
Write(user_input +
"\n") ;
std::cout << "\tSerial Port 1 sent:\t" << user_input << std::endl
<< "\tSerial Port 2 received:\t" << read_string_2 << std::endl ;
}
std::cout << "The example program successfully completed!" << std::endl ;
return EXIT_SUCCESS ;
}
Exception error thrown when the serial port could not be opened.
Exception error thrown when data could not be read from the serial port before the timeout had been e...
SerialPort allows an object oriented approach to serial port communication. A serial port object can ...
void SetBaudRate(const BaudRate &baudRate)
Sets the baud rate for the serial port to the specified value.
void SetParity(const Parity &parityType)
Sets the parity type for the serial port.
void WriteByte(char charbuffer)
Writes a single byte to the serial port.
void SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
void Write(const DataBuffer &dataBuffer)
Writes a DataBuffer to the serial port.
void Close()
Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed...
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
void Open(const std::string &fileName, const std::ios_base::openmode &openMode=std::ios_base::in|std::ios_base::out, bool exclusive=true)
Opens the serial port associated with the specified file name and the specified mode.
void Read(DataBuffer &dataBuffer, size_t numberOfBytes=0, size_t msTimeout=0)
Reads the specified number of bytes from the serial port. The method will timeout if no data is recei...
void ReadByte(char &charBuffer, size_t msTimeout=0)
Reads a single byte from the serial port. If no data is available within the specified number of mill...
void ReadLine(std::string &dataString, char lineTerminator='\n', size_t msTimeout=0)
Reads a line of characters from the serial port. The method will timeout if no data is received in th...
void DrainWriteBuffer()
Waits until the write buffer is drained and then returns.
void SetStopBits(const StopBits &stopBits)
Sets the number of stop bits to be used with the serial port.