Loading...
Searching...
No Matches
serial_port_read.cpp
#include <libserial/SerialPort.h>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <unistd.h>
constexpr const char* const SERIAL_PORT_1 = "/dev/ttyUSB0" ;
int main()
{
using namespace LibSerial ;
// Instantiate a SerialPort object.
SerialPort serial_port ;
try
{
// Open the Serial Port at the desired hardware port.
serial_port.Open(SERIAL_PORT_1) ;
}
catch (const OpenFailed&)
{
std::cerr << "The serial port did not open correctly." << std::endl ;
return EXIT_FAILURE ;
}
// Set the baud rate of the serial port.
serial_port.SetBaudRate(BaudRate::BAUD_115200) ;
// Set the number of data bits.
serial_port.SetCharacterSize(CharacterSize::CHAR_SIZE_8) ;
// Turn off hardware flow control.
serial_port.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;
// Disable parity.
serial_port.SetParity(Parity::PARITY_NONE) ;
// Set the number of stop bits.
serial_port.SetStopBits(StopBits::STOP_BITS_1) ;
// Wait for data to be available at the serial port.
while(!serial_port.IsDataAvailable())
{
usleep(1000) ;
}
// Specify a timeout value (in milliseconds).
size_t ms_timeout = 250 ;
// Char variable to store data coming from the serial port.
char data_byte ;
// Read one byte from the serial port and print it to the terminal.
try
{
// Read a single byte of data from the serial port.
serial_port.ReadByte(data_byte, ms_timeout) ;
// Show the user what is being read from the serial port.
std::cout << data_byte << std::flush ;
}
catch (const ReadTimeout&)
{
std::cerr << "\nThe ReadByte() call has timed out." << std::endl ;
}
// Wait a brief period for more data to arrive.
usleep(1000) ;
DataBuffer read_buffer ;
try
{
// Read as many bytes as are available during the timeout period.
serial_port.Read(read_buffer, 0, ms_timeout) ;
}
catch (const ReadTimeout&)
{
for (size_t i = 0 ; i < read_buffer.size() ; i++)
{
std::cout << read_buffer.at(i) << std::flush ;
}
std::cerr << "The Read() call timed out waiting for additional data." << std::endl ;
}
// Successful program completion.
std::cout << "The example program successfully completed!" << std::endl ;
return EXIT_SUCCESS ;
}
LibSerial::OpenFailed
Exception error thrown when the serial port could not be opened.
Definition SerialPortConstants.h:156
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::SerialPort::SetBaudRate
void SetBaudRate(const BaudRate &baudRate)
Sets the baud rate for the serial port to the specified value.
Definition SerialPort.cpp:570
LibSerial::SerialPort::SetParity
void SetParity(const Parity &parityType)
Sets the parity type for the serial port.
Definition SerialPort.cpp:606
LibSerial::SerialPort::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 SerialPort.cpp:514
LibSerial::SerialPort::SetFlowControl
void SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
Definition SerialPort.cpp:594
LibSerial::SerialPort::SetCharacterSize
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
Definition SerialPort.cpp:582
LibSerial::SerialPort::IsDataAvailable
bool IsDataAvailable()
Checks if data is available at the input of the serial port.
Definition SerialPort.cpp:552
LibSerial::SerialPort::Read
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...
Definition SerialPort.cpp:710
LibSerial::SerialPort::ReadByte
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...
Definition SerialPort.cpp:730
LibSerial::SerialPort::SetStopBits
void SetStopBits(const StopBits &stopBits)
Sets the number of stop bits to be used with the serial port.
Definition SerialPort.cpp:618