5#include <libserial/SerialPort.h>
12constexpr const char*
const SERIAL_PORT_1 =
"/dev/ttyUSB0" ;
20 using namespace LibSerial ;
28 serial_port.
Open(SERIAL_PORT_1) ;
32 std::cerr <<
"The serial port did not open correctly." << std::endl ;
46 serial_port.
SetParity(Parity::PARITY_NONE) ;
58 size_t ms_timeout = 250 ;
67 serial_port.
ReadByte(data_byte, ms_timeout) ;
70 std::cout << data_byte << std::flush ;
74 std::cerr <<
"\nThe ReadByte() call has timed out." << std::endl ;
80 DataBuffer read_buffer ;
85 serial_port.
Read(read_buffer, 0, ms_timeout) ;
89 for (
size_t i = 0 ; i < read_buffer.size() ; i++)
91 std::cout << read_buffer.at(i) << std::flush ;
94 std::cerr <<
"The Read() call timed out waiting for additional data." << std::endl ;
98 std::cout <<
"The example program successfully completed!" << std::endl ;
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 SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
bool IsDataAvailable()
Checks if data is available at the input of 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 SetStopBits(const StopBits &stopBits)
Sets the number of stop bits to be used with the serial port.