LibSerial 1.0.0
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
Loading...
Searching...
No Matches
serial_port_read_write.cpp
1
5#include <libserial/SerialPort.h>
6
7#include <cstdlib>
8#include <fstream>
9#include <iostream>
10#include <unistd.h>
11
12constexpr const char* const SERIAL_PORT_1 = "/dev/ttyUSB0" ;
13constexpr const char* const SERIAL_PORT_2 = "/dev/ttyUSB1" ;
14
19int main()
20{
21 using namespace LibSerial ;
22
23 // Instantiate two SerialPort objects.
24 SerialPort serial_port_1 ;
25 SerialPort serial_port_2 ;
26
27 try
28 {
29 // Open the Serial Ports at the desired hardware devices.
30 serial_port_1.Open(SERIAL_PORT_1) ;
31 serial_port_2.Open(SERIAL_PORT_2) ;
32 }
33 catch (const OpenFailed&)
34 {
35 std::cerr << "The serial ports did not open correctly." << std::endl ;
36 return EXIT_FAILURE ;
37 }
38
39 // Set the baud rates.
40 serial_port_1.SetBaudRate(BaudRate::BAUD_115200) ;
41 serial_port_2.SetBaudRate(BaudRate::BAUD_115200) ;
42
43 // Set the number of data bits.
44 serial_port_1.SetCharacterSize(CharacterSize::CHAR_SIZE_8) ;
45 serial_port_2.SetCharacterSize(CharacterSize::CHAR_SIZE_8) ;
46
47 // Set the hardware flow control.
48 serial_port_1.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;
49 serial_port_2.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;
50
51 // Set the parity.
52 serial_port_1.SetParity(Parity::PARITY_NONE) ;
53 serial_port_2.SetParity(Parity::PARITY_NONE) ;
54
55 // Set the number of stop bits.
56 serial_port_1.SetStopBits(StopBits::STOP_BITS_1) ;
57 serial_port_2.SetStopBits(StopBits::STOP_BITS_1) ;
58
59 // Setup variables to store outgoing and incoming data.
60 char write_byte_1 = 'a' ;
61 char write_byte_2 = 'b' ;
62
63 char read_byte_1 = ' ' ;
64 char read_byte_2 = ' ' ;
65
66 // Variables to store outgoing and incoming data.
67 std::string write_string_1 =
68 "\"Do what you can, with what you have, where you are.\" - Theodore Roosevelt" ;
69
70 std::string write_string_2 =
71 "\"Simplicity is prerequisite for reliability.\" - Edsger W. Dijkstra" ;
72
73 std::string read_string_1 ;
74 std::string read_string_2 ;
75
76 // Print to the terminal what will take place next.
77 std::cout << "\nUsing WriteByte() and ReadByte() for one byte of data:"
78 << std::endl ;
79
80 // Write a single byte of data to the serial ports.
81 serial_port_1.WriteByte(write_byte_1) ;
82 serial_port_2.WriteByte(write_byte_2) ;
83
84 // Wait until the data has actually been transmitted.
85 serial_port_1.DrainWriteBuffer() ;
86 serial_port_2.DrainWriteBuffer() ;
87
88 // Specify a timeout value (in milliseconds).
89 size_t timeout_milliseconds = 25 ;
90
91 try
92 {
93 // Read a single byte of data from the serial ports.
94 serial_port_1.ReadByte(read_byte_1, timeout_milliseconds) ;
95 serial_port_2.ReadByte(read_byte_2, timeout_milliseconds) ;
96 }
97 catch (const ReadTimeout&)
98 {
99 std::cerr << "The ReadByte() call has timed out." << std::endl ;
100 }
101
102 // Print to the terminal what was sent and what was received.
103 std::cout << "\tSerial Port 1 sent:\t" << write_byte_1 << std::endl
104 << "\tSerial Port 2 received:\t" << read_byte_2 << std::endl
105 << std::endl ;
106
107 std::cout << "\tSerial Port 2 sent:\t" << write_byte_2 << std::endl
108 << "\tSerial Port 1 received:\t" << read_byte_1 << std::endl
109 << std::endl ;
110
111 // Print to the terminal what will take place next.
112 std::cout << "Using Write() and Read() for a specified number of "
113 << "bytes of data:" << std::endl ;
114
115 // Write a string to each serial port.
116 serial_port_1.Write(write_string_1) ;
117 serial_port_2.Write(write_string_2) ;
118
119 // Wait until the data has actually been transmitted.
120 serial_port_1.DrainWriteBuffer() ;
121 serial_port_2.DrainWriteBuffer() ;
122
123 try
124 {
125 // Read the appropriate number of bytes from each serial port.
126 serial_port_1.Read(read_string_1, write_string_2.size(), timeout_milliseconds) ;
127 serial_port_2.Read(read_string_2, write_string_1.size(), timeout_milliseconds) ;
128 }
129 catch (const ReadTimeout&)
130 {
131 std::cerr << "The Read() call has timed out." << std::endl ;
132 }
133
134 // Print to the terminal what was sent and what was received.
135 std::cout << "\tSerial Port 1 sent:\t" << write_string_1 << std::endl
136 << "\tSerial Port 2 received:\t" << read_string_2 << std::endl
137 << std::endl ;
138
139 std::cout << "\tSerial Port 2 sent:\t" << write_string_2 << std::endl
140 << "\tSerial Port 1 received:\t" << read_string_1 << std::endl
141 << std::endl ;
142
143 // Variable to hold user input.
144 std::string user_input ;
145 user_input.clear() ;
146
147 // Print to the terminal what will take place next.
148 std::cout << "Using Write() and ReadLine() to write a string and "
149 << "read a line of data:" << std::endl << std::endl ;
150
151 // Prompt the user for input.
152 std::cout << R"(Enter something you would like to send over )"
153 << R"(serial, (enter "Q" or "q" to quit): )" << std::flush ;
154
155 while(true)
156 {
157 // Get input from the user.
158 std::getline(std::cin, user_input) ;
159
160 if (user_input == "q" ||
161 user_input == "Q" ||
162 user_input == "")
163 {
164 break ;
165 }
166
167 // Write the user input to the serial port.
168 serial_port_1.Write(user_input + "\n") ;
169
170 // Read the data transmitted from the corresponding serial port.
171 serial_port_2.ReadLine(read_string_2) ;
172
173 // Print to the terminal what was sent and what was received.
174 std::cout << "\tSerial Port 1 sent:\t" << user_input << std::endl
175 << "\tSerial Port 2 received:\t" << read_string_2 << std::endl ;
176 }
177
178 // Close the serial ports and end the program.
179 serial_port_1.Close() ;
180 serial_port_2.Close() ;
181
182 // Successful program completion.
183 std::cout << "The example program successfully completed!" << std::endl ;
184 return EXIT_SUCCESS ;
185}
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 ...
Definition SerialPort.h:56
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 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 WriteByte(char charbuffer)
Writes a single byte to the serial port.
void SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
void Write(const DataBuffer &dataBuffer)
Writes a DataBuffer to the serial port.
void Close()
Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed...
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
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 ReadLine(std::string &dataString, char lineTerminator='\n', size_t msTimeout=0)
Reads a line of characters from the serial port. The method will timeout if no data is received in th...
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.