Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial.cpp
More file actions
536 lines (442 loc) · 15.7 KB
/
Copy pathserial.cpp
File metadata and controls
536 lines (442 loc) · 15.7 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// @ Copyright 2020
#include "libserial/serial.hpp"
#include <iostream>
#include <memory>
#include <poll.h>
#include <string>
#include <vector>
namespace libserial {
Serial::Serial(const std::string& port) {
this->open(port);
this->setBaudRate(BaudRate::BAUD_RATE_9600);
}
Serial::~Serial() {
if (fd_serial_port_ != -1) {
::close(fd_serial_port_);
fd_serial_port_ = -1;
}
}
void Serial::open(const std::string& port) {
// Open the serial port with read/write access, no controlling terminal, and non-blocking mode.
// On many serial devices, opening a port without O_NONBLOCK can block waiting for modem
// control lines/carrier detect, which is a behavior change that can hang callers. By opening
// in non-blocking mode and then immediately clearing that flag, we can avoid this issue while
// still allowing blocking reads/writes as expected.
fd_serial_port_ = ::open(port.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd_serial_port_ == -1) {
throw SerialException("Error opening port " + port + ": " + strerror(errno));
}
int flags = ::fcntl(fd_serial_port_, F_GETFL);
if (flags == -1) {
int saved_errno = errno;
::close(fd_serial_port_);
throw SerialException("Error configuring port " + port + ": " + strerror(saved_errno));
}
if (::fcntl(fd_serial_port_, F_SETFL, flags & ~O_NONBLOCK) == -1) {
int saved_errno = errno;
::close(fd_serial_port_);
throw SerialException("Error configuring port " + port + ": " + strerror(saved_errno));
}
}
void Serial::close() {
if (fd_serial_port_ != -1) {
ssize_t error = ::close(fd_serial_port_);
if (error < 0) {
throw SerialException("Error closing port: " + std::string(strerror(errno)));
}
fd_serial_port_ = -1;
}
}
void Serial::write(std::string_view data) {
if (data.empty()) {
throw IOException("Empty string passed to write function");
}
size_t total_written = 0;
while (total_written < data.size()) {
ssize_t ret = write_(fd_serial_port_,
data.data() + total_written,
data.size() - total_written);
if (ret < 0) {
if (errno == EINTR) continue;
throw IOException("Error writing to serial port: " + std::string(strerror(errno)));
}
if (ret == 0) {
throw IOException("Error writing to serial port: write returned 0");
}
total_written += static_cast<size_t>(ret);
}
}
ssize_t Serial::writeRaw(const uint8_t* data, size_t size) {
if (canonical_mode_ == CanonicalMode::ENABLE) {
throw IOException(
"writeRaw() is not supported in canonical mode; use write() instead");
}
if (!data || size == 0) {
throw IOException("Invalid buffer passed to writeRaw");
}
size_t total_written = 0;
auto start_time = std::chrono::steady_clock::now();
while (total_written < size) {
int timeout_ms = -1;
if (write_timeout_ms_.count() > 0) {
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
if (elapsed >= write_timeout_ms_) {
break;
}
timeout_ms = static_cast<int>((write_timeout_ms_ - elapsed).count());
}
struct pollfd fd_poll;
fd_poll.fd = fd_serial_port_;
fd_poll.events = POLLOUT;
int pool_result = poll_(&fd_poll, 1, timeout_ms);
if (pool_result < 0) {
if (errno == EINTR) continue;
throw IOException("Error in poll(): " + std::string(strerror(errno)));
}
if (pool_result == 0) {
break;
}
// Check for error conditions signaled by poll
if (fd_poll.revents & (POLLERR | POLLHUP | POLLNVAL)) {
throw IOException("Serial port not writable (poll error state)");
}
ssize_t ret = write_(fd_serial_port_,
data + total_written,
size - total_written);
if (ret < 0) {
if (errno == EINTR) continue;
// Defensive: if fd was toggled non-blocking somewhere
if (errno == EAGAIN || errno == EWOULDBLOCK) {
continue;
}
throw IOException("Error writing raw data: " + std::string(strerror(errno)));
}
if (ret == 0) {
throw IOException("Error writing to serial port: write returned 0");
}
total_written += static_cast<size_t>(ret);
}
return static_cast<ssize_t>(total_written);
}
ssize_t Serial::writeRaw(const std::vector<uint8_t>& data) {
if (data.empty()) {
throw IOException("Data vector is empty");
}
return writeRaw(data.data(), data.size());
}
size_t Serial::read(std::string & buffer) {
if (canonical_mode_ == CanonicalMode::DISABLE) {
throw IOException(
"read() is not supported in non-canonical mode; use readBytes(), readUntil() or readRaw() instead");
}
struct pollfd fd_poll;
fd_poll.fd = fd_serial_port_;
fd_poll.events = POLLIN;
int timeout_ms = static_cast<int>(read_timeout_ms_.count());
int poll_result = poll_(&fd_poll, 1, timeout_ms);
if (poll_result < 0) {
throw IOException(std::string("Error in poll(): ") + strerror(errno));
}
if (poll_result == 0) {
throw IOException("Read operation timed out after " + std::to_string(timeout_ms) +
" milliseconds");
}
buffer.resize(max_safe_read_size_);
ssize_t bytes_read = read_(fd_serial_port_, buffer.data(), max_safe_read_size_);
if (bytes_read < 0) {
throw IOException(std::string("Error reading from serial port: ") + strerror(errno));
}
buffer.resize(static_cast<size_t>(bytes_read));
return static_cast<size_t>(bytes_read);
}
size_t Serial::readBytes(std::string & buffer, size_t num_bytes) {
if (canonical_mode_ == CanonicalMode::ENABLE) {
throw IOException(
"readBytes() is not supported in canonical mode; use read() or readUntil() instead");
}
if (num_bytes == 0) {
throw IOException("Number of bytes requested must be greater than zero");
}
buffer.clear();
buffer.resize(num_bytes);
ssize_t bytes_read = read_(fd_serial_port_, buffer.data(), num_bytes); // codacy-ignore[buffer-boundary]
if (bytes_read < 0) {
throw IOException("Error reading from serial port: " + std::string(strerror(errno)));
}
buffer.resize(static_cast<size_t>(bytes_read));
return static_cast<size_t>(bytes_read);
}
size_t Serial::readUntil(std::string & buffer, char terminator) {
buffer.clear();
char temp_char = '\0';
auto start_time = std::chrono::steady_clock::now();
while (temp_char != terminator) {
if (buffer.size() >= max_safe_read_size_) {
throw IOException("Read buffer exceeded maximum size limit of " +
std::to_string(max_safe_read_size_) +
" bytes without finding terminator");
}
// Check timeout if enabled (0 means no timeout)
if (read_timeout_ms_.count() > 0) {
auto current_time = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(current_time -
start_time).count();
if (elapsed >= static_cast<int64_t>(read_timeout_ms_.count())) {
throw IOException("Read timeout exceeded while waiting for terminator");
}
// Use poll() to check if data is available with remaining timeout.
struct pollfd fd_poll;
fd_poll.fd = fd_serial_port_;
fd_poll.events = POLLIN;
int64_t remaining_timeout = read_timeout_ms_.count() - elapsed;
int timeout_ms = static_cast<int>(remaining_timeout);
int poll_result = poll_(&fd_poll, 1, timeout_ms);
if (poll_result < 0) {
throw IOException("Error in poll(): " + std::string(strerror(errno)));
}
else if (poll_result == 0) {
throw IOException("Read timeout exceeded while waiting for data");
}
}
ssize_t bytes_read = read_(fd_serial_port_, &temp_char, 1);
if (bytes_read < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
throw IOException("Error reading from serial port: " + std::string(strerror(errno)));
}
else if (bytes_read == 0) {
throw IOException("Connection closed while reading: no terminator found");
}
buffer.push_back(temp_char);
}
return buffer.size();
}
ssize_t Serial::readRaw(uint8_t* buffer, size_t size) {
if (canonical_mode_ == CanonicalMode::ENABLE) {
throw IOException(
"readRaw() is not supported in canonical mode; use read() or readUntil() instead");
}
if (!buffer || size == 0) {
throw IOException("Invalid buffer passed to readRaw");
}
size_t total_read = 0;
auto start_time = std::chrono::steady_clock::now();
while (total_read < size) {
int timeout_ms = -1;
if (read_timeout_ms_.count() > 0) {
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
if (elapsed >= read_timeout_ms_) {
break; // timeout reached → return what we have
}
timeout_ms = static_cast<int>((read_timeout_ms_ - elapsed).count());
}
struct pollfd fd_poll;
fd_poll.fd = fd_serial_port_;
fd_poll.events = POLLIN;
int poll_result = poll_(&fd_poll, 1, timeout_ms);
if (poll_result < 0) {
if (errno == EINTR) continue;
throw IOException("Error in poll(): " + std::string(strerror(errno)));
}
if (poll_result == 0) {
break;
}
ssize_t ret = read_(fd_serial_port_,
buffer + total_read,
size - total_read);
if (ret < 0) {
if (errno == EINTR) continue;
if (errno == EAGAIN || errno == EWOULDBLOCK) {
continue;
}
throw IOException("Error reading raw data: " + std::string(strerror(errno)));
}
if (ret == 0) {
break;
}
total_read += static_cast<size_t>(ret);
}
return static_cast<ssize_t>(total_read);
}
void Serial::flushInputBuffer() {
if (ioctl_(fd_serial_port_, TCFLSH, TCIFLUSH) != 0) {
throw SerialException("Error flushing input buffer: " + std::string(strerror(errno)));
}
}
void Serial::setTermios2() {
ssize_t error = ioctl_(fd_serial_port_, TCSETS2, &options_);
if (error < 0) {
throw SerialException("Error set Termios2: " + std::string(strerror(errno)));
}
}
void Serial::setBaudRate(unsigned int baud_rate) {
this->getTermios2();
options_.c_cflag &= ~CBAUD;
options_.c_cflag |= BOTHER;
options_.c_ispeed = baud_rate;
options_.c_ospeed = baud_rate;
this->setTermios2();
}
void Serial::setBaudRate(BaudRate baud_rate) {
this->setBaudRate(static_cast<unsigned int>(baud_rate));
}
void Serial::setReadTimeout(std::chrono::milliseconds timeout) {
read_timeout_ms_ = timeout;
this->setTimeOut(static_cast<uint16_t>(timeout.count() / 100));
}
void Serial::setWriteTimeout(std::chrono::milliseconds timeout) {
write_timeout_ms_ = timeout;
}
void Serial::setDataLength(DataLength nbits) {
this->getTermios2();
options_.c_cflag &= ~CSIZE;
switch (nbits) {
case DataLength::FIVE:
options_.c_cflag |= CS5;
break;
case DataLength::SIX:
options_.c_cflag |= CS6;
break;
case DataLength::SEVEN:
options_.c_cflag |= CS7;
break;
case DataLength::EIGHT:
options_.c_cflag |= CS8;
break;
}
this->setTermios2();
}
void Serial::setParity(Parity parity) {
this->getTermios2();
switch (parity) {
case Parity::DISABLE:
options_.c_cflag &= ~PARENB;
break;
case Parity::ENABLE:
options_.c_cflag |= PARENB;
break;
}
this->setTermios2();
}
void Serial::setStopBits(StopBits stop_bits) {
this->getTermios2();
switch (stop_bits) {
case StopBits::ONE:
options_.c_cflag &= ~CSTOP;
break;
case StopBits::TWO:
options_.c_cflag |= CSTOP;
break;
}
this->setTermios2();
}
void Serial::setFlowControl([[maybe_unused]] FlowControl flow_control) {
// this->getTermios2();
// switch (flow_control) {
// case FlowControl::Software:
// // options_.c_cflag &= ~CRTSCTS;
// // options_.c_oflag |= (OPOST | ONLCR);
// // options_.c_iflag |= (IXON | IXOFF );
// options_.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)
// options_.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
// options_.c_cflag &= ~CSIZE; // Clear all bits that set the data size
// options_.c_cflag |= CS8; // 8 bits per byte (most common)
// options_.c_cflag &= ~CRTSCTS; // DISABLE RTS/CTS hardware flow control (most common)
// options_.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
// options_.c_lflag &= ~ICANON;
// options_.c_lflag &= ~ECHO; // DISABLE echo
// options_.c_lflag &= ~ECHOE; // DISABLE erasure
// options_.c_lflag &= ~ECHONL; // DISABLE new-line echo
// options_.c_lflag &= ~ISIG; // DISABLE interpretation of INTR, QUIT and SUSP
// options_.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
// options_.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // DISABLE any special handling of received bytes
// options_.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
// options_.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
// // options_.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT PRESENT ON LINUX)
// // options_.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars (0x004) in output (NOT PRESENT ON LINUX)
// options_.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
// options_.c_cc[VMIN] = 0;
// // options_.c_cc[VEOF] = '\r';
// break;
// case FlowControl::Hardware:
// options_.c_cflag |= CRTSCTS;
// options_.c_iflag &= ~(IXON | IXOFF | IXANY);
// default:
// options_.c_cflag &= ~CRTSCTS;
// break;
// }
// this->setTermios2();
}
void Serial::setCanonicalMode(CanonicalMode mode) {
canonical_mode_ = mode;
this->getTermios2();
switch (canonical_mode_) {
case CanonicalMode::ENABLE:
options_.c_lflag |= (ICANON);
break;
case CanonicalMode::DISABLE:
options_.c_lflag &= ~(ICANON);
break;
}
this->setTermios2();
}
void Serial::setTerminator(Terminator term) {
terminator_ = term;
}
void Serial::setTimeOut(uint16_t time) {
this->getTermios2();
options_.c_cc[VTIME] = time;
this->setTermios2();
}
void Serial::setMinNumberCharRead(uint16_t num) {
min_number_char_read_ = num;
this->getTermios2();
options_.c_cc[VMIN] = min_number_char_read_;
this->setTermios2();
}
void Serial::setMaxSafeReadSize(size_t size) {
max_safe_read_size_ = size;
}
size_t Serial::getMaxSafeReadSize() const {
return max_safe_read_size_;
}
int Serial::getAvailableData() const {
int bytes_available;
if (ioctl_(fd_serial_port_, FIONREAD, &bytes_available) < 0) {
throw SerialException("Error getting available data: " + std::string(strerror(errno)));
}
return bytes_available;
}
int Serial::getBaudRate() const {
this->getTermios2();
return (static_cast<int>(options_.c_ispeed));
}
DataLength Serial::getDataLength() const {
this->getTermios2();
switch (options_.c_cflag & CSIZE) {
case CS5: return DataLength::FIVE;
case CS6: return DataLength::SIX;
case CS7: return DataLength::SEVEN;
case CS8: return DataLength::EIGHT;
default: return DataLength::EIGHT;
}
}
std::chrono::milliseconds Serial::getReadTimeout() const {
this->getTermios2();
return std::chrono::milliseconds(options_.c_cc[VTIME] * 100);
}
uint16_t Serial::getMinNumberCharRead() const {
this->getTermios2();
return static_cast<uint16_t>(options_.c_cc[VMIN]);
}
void Serial::getTermios2() const {
ssize_t error = ioctl_(fd_serial_port_, TCGETS2, &options_);
if (error < 0) {
throw SerialException("Error get Termios2: " + std::string(strerror(errno)));
}
}
} // namespace libserial
You can’t perform that action at this time.
