#include <libserial/SerialStream.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_stream.
Open(SERIAL_PORT_2) ;
}
{
std::cerr << "The serial port did not open correctly." << std::endl ;
return EXIT_FAILURE ;
}
serial_stream.
SetParity(Parity::PARITY_NONE) ;
std::cout << "Writing input file contents to the serial port." << std::endl ;
char data_byte = 0 ;
while (input_file)
{
input_file.read(&data_byte, 1) ;
serial_stream.write(&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.
SerialStream is a stream class for accessing serial ports on POSIX operating systems....
void DrainWriteBuffer()
Waits until the write buffer is drained and then returns.
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
void SetBaudRate(const BaudRate &baudRate)
Sets the baud rate for the serial port to the specified value.
void SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
void SetStopBits(const StopBits &stopBits)
Sets the number of stop bits to be used with 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 SetParity(const Parity &parityType)
Sets the parity type for the serial port.