-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial.cpp
More file actions
232 lines (199 loc) · 5.03 KB
/
Copy pathserial.cpp
File metadata and controls
232 lines (199 loc) · 5.03 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
#include "peripherycpp/serial.hpp"
Serial::Serial()
{}
Serial::Serial(std::string device, unsigned int baudrate, unsigned int databits,
int parity, unsigned int stopbits,
bool xonxoff, bool rtscts)
{
open(device, baudrate, databits, parity, stopbits, xonxoff, rtscts);
}
Serial::~Serial()
{
close();
// devices_open[m_device] = false;
}
void Serial::open(std::string device, unsigned int baudrate, unsigned int databits,
int parity, unsigned int stopbits, bool xonxoff, bool rtscts)
{
printf("Serial device %s opening\n", device);
serial_parity_t cpar;
if (parity == 0)
{
cpar = PARITY_NONE;
}
else if (parity == 1)
{
cpar = PARITY_ODD;
}
else
{
cpar = PARITY_EVEN;
}
checkError(serial_open_advanced(&m_serial, device.c_str(), baudrate,
databits, cpar,stopbits, xonxoff, rtscts));
printf("Serial device %s successfully opened", device);
// devices_open[device] = true;
m_device = device;
m_baudrate = baudrate;
m_databits = databits;
m_parity = parity;
m_stopbits = stopbits;
m_xonxoff = xonxoff;
m_rtscts = rtscts;
}
std::vector<uint8_t> Serial::read(size_t len, int timeout_ms)
{
std::vector<uint8_t> data(static_cast<int>(len));
checkError(serial_read(&m_serial, data.data(), len, timeout_ms));
return data;
}
void Serial::read(char* buf, size_t size, int timeout_ms)
{
checkError(serial_read(&m_serial, (uint8_t*)buf , size, timeout_ms));
}
void Serial::read(uint8_t* buf, size_t size, int timeout_ms)
{
checkError(serial_read(&m_serial, buf, size, timeout_ms));
}
void Serial::write(std::vector<uint8_t> data)
{
checkError(serial_write(&m_serial, data.data(), data.size()));
}
void Serial::write(uint8_t* data, size_t size)
{
checkError(serial_write(&m_serial, data, size));
}
void Serial::write(char* data, size_t size)
{
checkError(serial_write(&m_serial, (uint8_t*)data, size));
}
void Serial::flush()
{
checkError(serial_flush(&m_serial));
}
unsigned int Serial::inputWaiting()
{
unsigned int count;
checkError(serial_input_waiting(&m_serial, &count));
return count;
}
unsigned int Serial::outputWaiting()
{
unsigned int count;
checkError(serial_output_waiting(&m_serial, &count));
return count;
}
bool Serial::poll(int timeout_ms)
{
bool pollval = serial_poll(&m_serial, timeout_ms);
checkError(pollval);
return pollval;
}
void Serial::close()
{
checkError(serial_close(&m_serial));
}
// Getters:
uint32_t Serial::getBaudrate()
{
uint32_t baudrate;
checkError(serial_get_baudrate(&m_serial, &baudrate));
return baudrate;
}
unsigned int Serial::getDatabits()
{
unsigned int databits;
checkError(serial_get_databits(&m_serial, &databits));
return databits;
}
int Serial::getParity()
{
serial_parity_t parity;
checkError(serial_get_parity(&m_serial, &parity));
if (parity == PARITY_NONE)
{
return 0;
}
else if (parity == PARITY_ODD)
{
return 1;
}
else
{
return 2;
}
}
unsigned int Serial::getStopbits()
{
unsigned int stopbits;
checkError(serial_get_stopbits(&m_serial, &stopbits));
return stopbits;
}
bool Serial::getxOnxOff()
{
bool xonxoff;
checkError(serial_get_xonxoff(&m_serial, &xonxoff));
return xonxoff;
}
bool Serial::getRtscts()
{
bool rtscts;
checkError(serial_get_rtscts(&m_serial, &rtscts));
return rtscts;
}
// Setters:
void Serial::setBaudrate(uint32_t baudrate)
{
checkError(serial_set_baudrate(&m_serial, baudrate));
}
void Serial::setDatabits(unsigned int databits)
{
checkError(serial_set_baudrate(&m_serial, databits));
}
void Serial::setParity(int parity)
{
serial_parity_t cpar;
if (parity == 0)
{
cpar = PARITY_NONE;
}
else if (parity == 1)
{
cpar = PARITY_ODD;
}
else
{
cpar = PARITY_EVEN;
}
checkError(serial_set_parity(&m_serial, cpar));
}
void Serial::setStopBits(unsigned int stopbits)
{
checkError(serial_set_stopbits(&m_serial, stopbits));
}
void Serial::setxOnxOff(bool enabled)
{
checkError(serial_set_xonxoff(&m_serial, enabled));
}
void Serial::setRtscts(bool enabled)
{
checkError(serial_set_rtscts(&m_serial, enabled));
}
void Serial::reset()
{
close();
open(m_device, m_baudrate, m_databits, m_parity, m_stopbits, m_xonxoff, m_rtscts);
}
void Serial::checkError(int err_code)
{
switch(err_code)
{
case SERIAL_ERROR_ARG: throw SerialArgError(serial_errmsg(&m_serial)); break;
case SERIAL_ERROR_OPEN: throw SerialOpenError(serial_errmsg(&m_serial)); break;
case SERIAL_ERROR_QUERY: throw SerialQueryError(serial_errmsg(&m_serial)); break;
case SERIAL_ERROR_IO: throw SerialIoError(serial_errmsg(&m_serial)); break;
case SERIAL_ERROR_CONFIGURE: throw SerialConfigureError(serial_errmsg(&m_serial)); break;
case SERIAL_ERROR_CLOSE: throw SerialCloseError(serial_errmsg(&m_serial)); break;
default: /* no defined error */ break;
}
}