LibSerial 1.0.0
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
Loading...
Searching...
No Matches
serial_stream_read_write.cpp
1
5#include <libserial/SerialStream.h>
6
7#include <cstdlib>
8#include <cstring>
9#include <fstream>
10#include <iostream>
11#include <unistd.h>
12
13constexpr const char* const SERIAL_PORT_1 = "/dev/ttyUSB0" ;
14constexpr const char* const SERIAL_PORT_2 = "/dev/ttyUSB1" ;
15
20int main()
21{
22 using namespace LibSerial ;
23
24 // Instantiate two SerialStream objects.
25 SerialStream serial_stream_1 ;
26 SerialStream serial_stream_2 ;
27
28 try
29 {
30 // Open the Serial Ports at the desired hardware devices.
31 serial_stream_1.Open(SERIAL_PORT_1) ;
32 serial_stream_2.Open(SERIAL_PORT_2) ;
33 }
34 catch (const OpenFailed&)
35 {
36 std::cerr << "The serial ports did not open correctly." << std::endl ;
37 return EXIT_FAILURE ;
38 }
39
40 // Set the baud rates.
41 serial_stream_1.SetBaudRate(BaudRate::BAUD_115200) ;
42 serial_stream_2.SetBaudRate(BaudRate::BAUD_115200) ;
43
44 // Set the number of data bits.
45 serial_stream_1.SetCharacterSize(CharacterSize::CHAR_SIZE_8) ;
46 serial_stream_2.SetCharacterSize(CharacterSize::CHAR_SIZE_8) ;
47
48 // Turn off hardware flow control.
49 serial_stream_1.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;
50 serial_stream_2.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;
51
52 // Disable parity.
53 serial_stream_1.SetParity(Parity::PARITY_NONE) ;
54 serial_stream_2.SetParity(Parity::PARITY_NONE) ;
55
56 // Set the number of stop bits.
57 serial_stream_1.SetStopBits(StopBits::STOP_BITS_1) ;
58 serial_stream_2.SetStopBits(StopBits::STOP_BITS_1) ;
59
60 // Variables to store outgoing and incoming data.
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" ;
63
64 std::string read_string_1 = "" ;
65 std::string read_string_2 = "" ;
66
67 // Print to the terminal what will take place next.
68 std::cout << "\nUsing write() and read() for a specified number of "
69 << "bytes of data:" << std::endl ;
70
71 // Write a specified number of bytes of data.
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()) ;
74
75 // Wait until the data has actually been transmitted.
76 serial_stream_1.DrainWriteBuffer() ;
77 serial_stream_2.DrainWriteBuffer() ;
78
79 // Char arrays to store incoming data.
80 char* read_array_1 = new char[write_string_2.size()] ;
81 char* read_array_2 = new char[write_string_1.size()] ;
82
83 // Use inheritted std::istream read() method to read the data.
84 serial_stream_1.read(read_array_1, write_string_2.size()) ;
85 serial_stream_2.read(read_array_2, write_string_1.size()) ;
86
87 // Print to the terminal what was sent and what was received.
88 std::cout << "\tSerial Port 1 sent:\t" << write_string_1 << std::endl
89 << "\tSerial Port 2 received:\t" << read_array_2 << std::endl
90 << std::endl ;
91
92 std::cout << "\tSerial Port 2 sent:\t" << write_string_2 << std::endl
93 << "\tSerial Port 1 received:\t" << read_array_1 << std::endl
94 << std::endl ;
95
96 // Print to the terminal what will take place next.
97 std::cout << "Using the \"<<\" operator and getline() for a line of data:"
98 << std::endl ;
99
100 // Write a line at each serial port.
101 serial_stream_1 << write_string_1 << std::endl ;
102 serial_stream_2 << write_string_2 << std::endl ;
103
104 // Wait until the data has actually been transmitted.
105 serial_stream_1.DrainWriteBuffer() ;
106 serial_stream_2.DrainWriteBuffer() ;
107
108 // Read a line at each serial port.
109 std::getline(serial_stream_1, read_string_1) ;
110 std::getline(serial_stream_2, read_string_2) ;
111
112 // Print to the terminal what was sent and what was received.
113 std::cout << "\tSerial Port 1 sent:\t" << write_string_1 << std::endl
114 << "\tSerial Port 2 received:\t" << read_string_2 << std::endl
115 << std::endl ;
116
117 std::cout << "\tSerial Port 2 sent:\t" << write_string_2 << std::endl
118 << "\tSerial Port 1 received:\t" << read_string_1 << std::endl
119 << std::endl ;
120
121 // Variable to hold user input.
122 std::string user_input ;
123 user_input.clear() ;
124
125 // Prompt the user for input.
126 std::cout << "Type something you would like to send over serial,"
127 << " (enter \"Q\" or \"q\" to quit): " << std::flush ;
128
129 while(true)
130 {
131 // Get input from the user.
132 std::getline(std::cin, user_input) ;
133
134 if (user_input == "q" ||
135 user_input == "Q" ||
136 user_input == "")
137 {
138 break ;
139 }
140
141 // Print to the terminal what will take place next.
142 std::cout << "Using the \"<<\" and \">>\" operators to send "
143 << "and receive your data: " << std::endl ;
144
145 // Write the user input to the serial port.
146 serial_stream_1 << user_input << std::endl ;
147
148 // Read the data transmitted from the corresponding serial port.
149 // Note: The ">>" operator behavior is tricky if whitespace or
150 // nothing is entered by the user!
151 serial_stream_2 >> read_string_2 ;
152
153 // Print to the terminal what was sent and what was received.
154 std::cout << "\tSerial Port 1 sent:\t" << user_input << std::endl
155 << "\tSerial Port 2 received:\t" << read_string_2 << std::endl ;
156 }
157
158 // Close the serial ports and end the program.
159 serial_stream_1.Close() ;
160 serial_stream_2.Close() ;
161
162 // Successful program completion.
163 std::cout << "The example program successfully completed!" << std::endl ;
164 return EXIT_SUCCESS ;
165}
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, 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.