-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSerial.cpp
More file actions
76 lines (70 loc) · 1.42 KB
/
Serial.cpp
File metadata and controls
76 lines (70 loc) · 1.42 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
#include <unordered_map>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include "Linux.hpp"
#include "Serial.hpp"
namespace Linux {
#define X(b) { b, B##b }
static const std::unordered_map<int, int> baud_constants = {
X(50),
X(75),
X(110),
X(134),
X(150),
X(200),
X(300),
X(600),
X(1200),
X(1800),
X(2400),
X(4800),
X(9600),
X(19200),
X(38400),
X(57600),
X(115200),
X(230400),
X(460800),
X(500000),
X(921600),
X(1000000),
X(1152000),
X(1500000),
X(2000000),
X(2500000),
X(3000000),
X(3500000),
X(4000000),
};
#undef X
Serial::Serial(const std::string& path, int baud, Flags flags) :
File(path, file_read_write, file_none, flags)
{
const int fd = get_fd();
const auto baud_it = baud_constants.find(baud);
if (baud_it == baud_constants.end()) {
throw SystemError("Unsupported baud rate: " + std::to_string(baud));
}
/* Get current UART configuration */
struct termios t;
if (tcgetattr(fd, &t) < 0) {
throw SystemError("tcgetattr failed");
}
/* Set baud in config structure */
if (cfsetspeed(&t, baud_it->second) < 0) {
throw SystemError("cfsetspeed failed");
}
/* Other configuration (no stop bit, no flow control) */
t.c_cflag &= ~(CSTOPB | CRTSCTS);
cfmakeraw(&t);
/* Re-configure interface */
if (tcsetattr(fd, TCSANOW, &t) < 0) {
throw SystemError("tcsetattr failed");
}
/* Flush buffer */
if (tcflush(fd, TCIOFLUSH) < 0) {
throw SystemError("tcflush failed");
}
}
}