5#include <libserial/SerialStream.h>
13constexpr const char*
const SERIAL_PORT_1 =
"/dev/ttyUSB0" ;
14constexpr const char*
const SERIAL_PORT_2 =
"/dev/ttyUSB1" ;
22 using namespace LibSerial ;
31 serial_stream_1.
Open(SERIAL_PORT_1) ;
32 serial_stream_2.
Open(SERIAL_PORT_2) ;
36 std::cerr <<
"The serial ports did not open correctly." << std::endl ;
41 serial_stream_1.
SetBaudRate(BaudRate::BAUD_115200) ;
42 serial_stream_2.
SetBaudRate(BaudRate::BAUD_115200) ;
53 serial_stream_1.
SetParity(Parity::PARITY_NONE) ;
54 serial_stream_2.
SetParity(Parity::PARITY_NONE) ;
57 serial_stream_1.
SetStopBits(StopBits::STOP_BITS_1) ;
58 serial_stream_2.
SetStopBits(StopBits::STOP_BITS_1) ;
61 std::string write_string_1 =
"\"Do what you can, with what you have, where you are.\" - Theodore Roosevelt" ;
62 std::string write_string_2 =
"\"Simplicity is prerequisite for reliability.\" - Edsger W. Dijkstra" ;
64 std::string read_string_1 =
"" ;
65 std::string read_string_2 =
"" ;
68 std::cout <<
"\nUsing write() and read() for a specified number of "
69 <<
"bytes of data:" << std::endl ;
72 serial_stream_1.write(write_string_1.c_str(), write_string_1.size()) ;
73 serial_stream_2.write(write_string_2.c_str(), write_string_2.size()) ;
80 char* read_array_1 =
new char[write_string_2.size()] ;
81 char* read_array_2 =
new char[write_string_1.size()] ;
84 serial_stream_1.read(read_array_1, write_string_2.size()) ;
85 serial_stream_2.read(read_array_2, write_string_1.size()) ;
88 std::cout <<
"\tSerial Port 1 sent:\t" << write_string_1 << std::endl
89 <<
"\tSerial Port 2 received:\t" << read_array_2 << std::endl
92 std::cout <<
"\tSerial Port 2 sent:\t" << write_string_2 << std::endl
93 <<
"\tSerial Port 1 received:\t" << read_array_1 << std::endl
97 std::cout <<
"Using the \"<<\" operator and getline() for a line of data:"
101 serial_stream_1 << write_string_1 << std::endl ;
102 serial_stream_2 << write_string_2 << std::endl ;
109 std::getline(serial_stream_1, read_string_1) ;
110 std::getline(serial_stream_2, read_string_2) ;
113 std::cout <<
"\tSerial Port 1 sent:\t" << write_string_1 << std::endl
114 <<
"\tSerial Port 2 received:\t" << read_string_2 << std::endl
117 std::cout <<
"\tSerial Port 2 sent:\t" << write_string_2 << std::endl
118 <<
"\tSerial Port 1 received:\t" << read_string_1 << std::endl
122 std::string user_input ;
126 std::cout <<
"Type something you would like to send over serial,"
127 <<
" (enter \"Q\" or \"q\" to quit): " << std::flush ;
132 std::getline(std::cin, user_input) ;
134 if (user_input ==
"q" ||
142 std::cout <<
"Using the \"<<\" and \">>\" operators to send "
143 <<
"and receive your data: " << std::endl ;
146 serial_stream_1 << user_input << std::endl ;
151 serial_stream_2 >> read_string_2 ;
154 std::cout <<
"\tSerial Port 1 sent:\t" << user_input << std::endl
155 <<
"\tSerial Port 2 received:\t" << read_string_2 << std::endl ;
159 serial_stream_1.
Close() ;
160 serial_stream_2.
Close() ;
163 std::cout <<
"The example program successfully completed!" << std::endl ;
164 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 Close()
Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed...
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)
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.