forked from julianoes/rtk-sender-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial-comms.cpp
153 lines (134 loc) · 3.64 KB
/
serial-comms.cpp
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
#include "serial-comms.h"
#include <cstdio>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
SerialComms::~SerialComms()
{
reset();
}
bool SerialComms::init(std::string path)
{
// open() sometimes hangs on macOS or Linux devices unless you give it O_NONBLOCK.
fd_ = open(path.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd_ == -1) {
printf("open failed: %s\n", strerror(errno));
return false;
}
// We need to clear the O_NONBLOCK again.
if (fcntl(fd_, F_SETFL, 0) == -1) {
printf("open failed: %s\n", strerror(errno));
return false;
}
return true;
}
bool SerialComms::set_baudrate(unsigned baudrate)
{
const int baudrate_define = define_from_baudrate(baudrate);
struct termios tc {};
if (tcgetattr(fd_, &tc) != 0) {
printf("tcgetattr failed: %s\n", strerror(errno));
reset();
return false;
}
tc.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON);
tc.c_oflag &= ~(OCRNL | ONLCR | ONLRET | ONOCR | OFILL | OPOST);
tc.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG | TOSTOP);
tc.c_cflag &= ~(CSIZE | PARENB | CRTSCTS);
tc.c_cflag |= CS8;
tc.c_cc[VMIN] = 0; // Let's wait for 10 bytes.
tc.c_cc[VTIME] = 10; // Timeout after 1 second.
tc.c_cflag |= CLOCAL; // Without this a write() blocks indefinitely.
if (cfsetispeed(&tc, baudrate_define) != 0) {
printf("cfsetispeed failed: %s\n", strerror(errno));
close(fd_);
return false;
}
if (cfsetospeed(&tc, baudrate_define) != 0) {
printf("cfsetospeed failed: %s\n", strerror(errno));
close(fd_);
return false;
}
if (tcsetattr(fd_, TCSANOW, &tc) != 0) {
printf("tcsetattr failed: %s\n", strerror(errno));
close(fd_);
return false;
}
printf("set baudrate to %u\n", baudrate);
return true;
}
ssize_t SerialComms::read(uint8_t* bytes, unsigned bytes_len)
{
ssize_t result = ::read(fd_, bytes, bytes_len);
if (result >= 0) {
printf("read %zi bytes\n", result);
} else {
printf("read failed: %s\n", strerror(errno));
}
return result;
}
ssize_t SerialComms::write(const uint8_t* bytes, unsigned bytes_len)
{
ssize_t result = ::write(fd_, bytes, bytes_len);
if (result >= 0) {
printf("wrote %zi bytes\n", result);
} else {
printf("write failed: %s\n", strerror(errno));
}
return result;
}
void SerialComms::reset()
{
if (fd_ > 0) {
close(fd_);
fd_ = -1;
}
buffer_.clear();
}
int SerialComms::define_from_baudrate(int baudrate)
{
switch (baudrate) {
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
case 230400:
return B230400;
case 460800:
return B460800;
case 500000:
return B500000;
case 576000:
return B576000;
case 921600:
return B921600;
case 1000000:
return B1000000;
case 1152000:
return B1152000;
case 1500000:
return B1500000;
case 2000000:
return B2000000;
case 2500000:
return B2500000;
case 3000000:
return B3000000;
case 3500000:
return B3500000;
case 4000000:
return B4000000;
default: {
printf("Unknown baudrate\n");
return -1;
}
}
}