-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBoostSerial.h
339 lines (291 loc) · 9.07 KB
/
BoostSerial.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#ifndef BOOST_SERIAL_H
#define BOOST_SERIAL_H
#include <array>
#include <vector>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <sstream>
#include <iomanip>
#include <bitset>
#include <chrono>
#include <boost/asio.hpp>
class BoostSerial
{
public:
typedef boost::asio::serial_port_base::flow_control::type flowControlType;
typedef boost::asio::serial_port_base::parity::type parityType;
typedef boost::asio::serial_port_base::stop_bits::type stopBitsType;
typedef boost::system::errc::errc_t errorCode;
enum format
{
BIN = 0,
OCT = 1,
DEC = 2,
HEX = 3
};
private:
//serial device stuff
boost::asio::io_service serial_service;
std::unique_ptr<boost::asio::io_service::work> serial_work;
boost::asio::serial_port serial;
//async stuff
std::unique_ptr<std::thread> asyncReadThread;
std::array<uint8_t, 256> buf;
mutable std::mutex readBufferMtx; //for usableReadBuffer and readBufferSize
std::vector<uint8_t> usableReadBuffer;
void asyncReadHandler(const boost::system::error_code &error, std::size_t bytes_transferred);
mutable std::mutex writeMtx; //for writeLocked
std::condition_variable writeCv; // wait for notify from asyncWriteHandler
bool writeLocked = false; //block thread if previous async write hasn't finished
void asyncWriteHandler(const boost::system::error_code &error, std::size_t bytes_transferred);
mutable std::mutex errMtx;
int err;
//serial config stuff
int baud = 115200;
//software / hardware / none
flowControlType flowControl = flowControlType::none;
unsigned int characterSize = 8;
//odd / even / none
parityType parity = parityType::none;
//one / onepointfive / two
stopBitsType stopBits = stopBitsType::one;
unsigned int readBufferSize = 256;
unsigned int timeoutVal = 1000;
void printString(std::string const &);
public:
/**
* @brief Class constructor.
*/
BoostSerial() : serial_service(), serial(serial_service), asyncReadThread(nullptr) {}
/**
* @brief Class destructor.
*/
~BoostSerial();
/**
* @brief Open serial port and start async read thread.
*
* @param dname Name of serial port to connect to.
* @param baud_ Baud rate.
* @param flowControl_ Flow control either none / software / hardware.
* @param characterSize_ Character size.
* @param parity_ Parity either none / odd / even.
* @param stopBits Number of stop bits either one / onepointfive / two.
*/
void open(std::string dname,
unsigned int baud_ = 115200,
flowControlType flowControl_ = flowControlType::none,
unsigned int characterSize_ = 8,
parityType parity_ = parityType::none,
stopBitsType stopBits_ = stopBitsType::one);
/**
* @brief Check whether serial port is open.
*
* @return True if open, false otherwise.
*/
bool isOpen() const{return serial.is_open();}
/**
* @brief Close serial port.
*/
void close();
/**
* @brief Check whether something happened to serial port.
*
* @return False if something happened, true on successful operation.
*/
bool good() const;
/**
* @brief Clear port's error flag.
*/
void clear();
/**
* @brief Get error code of previous operation
*
* @return Error code, see: https://www.boost.org/doc/libs/1_68_0/boost/system/error_code.hpp
*/
int getErr() const;
/**
* @brief Write single byte.
*
* @param c Byte to be written.
* @return Number of bytes written (should be 1).
*/
unsigned int write(uint8_t c);
/**
* @brief Write vector of bytes.
*
* @param v Vector to be written.
* @return Number of bytes written (should be size of v).
*/
unsigned int write(std::vector<uint8_t> const &v);
/**
* @brief Write any data to serial port using stringstream.
*
* @param a Data to be written.
* @param option For integers it's display option (either BIN, DEC, OCT, HEX),
* for floating point it's decimal point precision
* for others it's useless.
* @return Number of bytes written.
*/
template <class T>
unsigned int print(T const &a, unsigned int option = DEC);
/**
* @brief Write any data to serial port using stringstream, appends a newline at the end.
*
* @param a Data to be written.
* @param option For integers it's display option (either BIN, DEC, OCT, HEX),
* for floating point it's decimal point precision
* for others it's useless.
* @return Number of bytes written.
*/
template <class T>
unsigned int println(T const &a, unsigned int option = DEC);
/**
* @brief Read one character from serial port.
*
* @return Byte or -1 if buffer is empty.
*/
int16_t read();
/**
* @brief Read all available bytes to be read from serial port.
*
* @return Content of serial port's buffer. The buffer is cleared after this operation.
*/
std::vector<uint8_t> readBuffer();
/**
* @brief Read bytes until number of bytes has been read or timeout.
*
* @param len Number of bytes to read.
* @return Vector of bytes.
*/
std::vector<uint8_t> readBytes(uint16_t len = 0xFFFF);
/**
* @brief Read bytes until given value or given number of bytes has been read or timeout, whichever criteria has been met first.
*
* @param givenByte The "stop" byte. This byte is not included in result vector.
* @param len Maximum number of bytes to read.
* @return Vector of bytes from serial port.
*/
std::vector<uint8_t> readBytesUntil(uint8_t givenByte, uint16_t len = 0xFFFF);
/**
* @brief Read string until \0 or timeout.
*
* @return String received from serial port.
*/
std::string readString(){return readStringUntil();}
/**
* @brief Read string until given character, \0 or timeout, whichever criteria has been met first.
*
* @param givenChar The "stop" character. This character is not included in result string.
* @return String received from serial port.
*/
std::string readStringUntil(char givenChar = '\0');
/**
* @brief Check next character in serial port's receive buffer without removing it.
*
* @return Next character in serial port's receive buffer or -1 if buffer is empty
*/
int16_t peek() const;
/**
* @brief Check whether there is data awaiting in receive buffer.
*
* @return Number of bytes awaiting in receive buffer.
*/
unsigned int available() const;
/**
* @brief Check whether serial port is idle.
*
* @return True if there is no write operation on serial port.
*/
bool idle() const;
/**
* @brief Clear receive buffer.
*/
void flush(){readBuffer();}
/**
* @brief Set baud rate.
*
* @param b Baud rate to be set.
*/
void setBaud(unsigned int b = 115200);
/**
* @brief Set flow control.
*
* @param t Flow control to be set, either none / software / hardware.
*/
void setFlowControl(flowControlType t = flowControlType::none);
/**
* @brief Set character size.
*
* @param s Character size to be set.
*/
void setCharacterSize(unsigned int s = 8);
/**
* @brief Set paryty.
*
* @param t Parity to be set, either none / odd / even.
*/
void setParity(parityType t = parityType::none);
/**
* @brief Set stop bits.
*
* @param t Stop bits to be set, either one / onepointfive / two.
*/
void setStopBits(stopBitsType t = stopBitsType::one);
/**
* @brief Set buffer size.
*
* @param b Buffer size to be set.
*/
void setBufferSize(unsigned int b = 256);
/**
* @brief Set timeout.
*
* @param t Timeout to be set in milliseconds.
*/
void setTimeout(unsigned int t = 1000){timeoutVal = t;}
/**
* @brief Get baud rate.
*
* @return Baud rate of serial port.
*/
unsigned int getBaud() const{return baud;}
/**
* @brief Get flow control.
*
* @return Flow control of serial port.
*/
flowControlType getFlowControl() const{return flowControl;}
/**
* @brief Get character size.
*
* @return Character size of serial port.
*/
unsigned int getCharacterSize() const{return characterSize;}
/**
* @brief Get parity.
*
* @return Parity of serial port.
*/
parityType getParity() const{return parity;}
/**
* @brief Get stop bits.
*
* @return Stop bits of serial port.
*/
stopBitsType getStopBits() const{return stopBits;}
/**
* @brief Get buffer size.
*
* @return Buffer size of serial port.
*/
unsigned int getBufferSize() const;
/**
* @brief Get timeout.
*
* @return Timeout of serial port.
*/
unsigned int getTimeout() const{return timeoutVal;}
};
#endif