#include <libserial/SerialPort.h>
#include <cstdlib>
#include <fstream>
#include <iostream>
constexpr const char* const SERIAL_PORT_2 = "/dev/ttyUSB1" ;
int main(int argc, char** argv)
{
using namespace LibSerial ;
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " <filename>" << std::endl ;
return 1 ;
}
std::ifstream input_file(argv[1]) ;
if (!input_file.good())
{
std::cerr << "Error: Could not open file "
<< argv[1] << " for reading." << std::endl ;
return 1 ;
}
try
{
serial_port.
Open(SERIAL_PORT_2) ;
}
{
std::cerr << "The serial port did not open correctly." << std::endl ;
return EXIT_FAILURE ;
}
std::cout << "Writing input file contents to the serial port." << std::endl ;
while (input_file)
{
char data_byte ;
input_file.read(&data_byte, 1) ;
std::cout << data_byte ;
}
std::cout << "The example program successfully completed!" << std::endl ;
return EXIT_SUCCESS ;
}
Exception error thrown when the serial port could not be opened.
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 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 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.