-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSerialPort.cpp
More file actions
296 lines (229 loc) · 6.89 KB
/
Copy pathSerialPort.cpp
File metadata and controls
296 lines (229 loc) · 6.89 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
#ifdef _WIN32
#include "stdint.h"
#include <windows.h>
#include <windowsx.h>
#include "PComm.h"
#else
#include <string>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#endif
#include <iostream>
#include <sstream>
#include <errno.h>
#include <cstring>
#include "Logger.h"
#include "config.h"
#include "SerialPort.h"
#ifdef _WIN32
#else
//POSIX specifies serial timeout in tenth of second
//#define PORT_READ_TIMEOUT (10)
#define PORT_READ_TIMEOUT (10)
#endif
SerialPort::SerialPort(void):serial_port(""),portFileDescriptor(-1){
}
int32_t SerialPort::serialOpen(const std::string &portName, uint32_t baudRate, SerialPort::SerialHandshake handshake,
SerialPort::SerialParity parity, SerialPort::SerialStopBits stopBits){
int32_t result=0;
serial_port.assign(portName.begin(), portName.end());
//remove compiler warnings
result=static_cast<uint32_t>(baudRate);
result=static_cast<uint32_t>(handshake);
result=static_cast<uint32_t>(parity);
result=static_cast<uint32_t>(stopBits);
#ifdef _WIN32
#else
struct termios newOptions;
portFileDescriptor = open(serial_port.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
if(portFileDescriptor<0){
return portFileDescriptor;
}
fcntl(portFileDescriptor, F_SETFL, 0);
result=tcgetattr(portFileDescriptor, &newOptions);
if(result<0){
Logger::instance().logInfo("serial port parameters read call failed");
close(portFileDescriptor);
return result;
}
if(parity==SERIAL_PARITY_NONE){
newOptions.c_cflag &= ~PARENB;
}else if(parity==SERIAL_PARITY_EVEN){
newOptions.c_cflag |= PARENB;
}else{
newOptions.c_cflag |= PARODD;
}
newOptions.c_iflag &= ~ICRNL;
newOptions.c_cflag &= ~CSTOPB;
newOptions.c_cflag &= ~CSIZE;
newOptions.c_cflag |= CS8;
if(handshake==HANDSHAKE_NONE){
//No HW or SW flow control
newOptions.c_iflag &= ~(IXON | IXOFF | IXANY | CRTSCTS);
}else if(handshake==HANDSHAKE_RTS_CTS){
//Disable SW flow control
newOptions.c_iflag &= ~(IXON | IXOFF | IXANY);
//Enable HW flow control
//newOptions.c_cflag |= CNEW_RTSCTS;
newOptions.c_cflag |= CRTSCTS;
}else{
newOptions.c_cflag &= ~CRTSCTS;
//Enable SW flow control
newOptions.c_iflag |= (IXON | IXOFF);
}
//raw input
newOptions.c_cflag |= (CLOCAL | CREAD);
newOptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
//raw output
newOptions.c_oflag &= ~OPOST;
newOptions.c_cc[VMIN] = 1;
newOptions.c_cc[VTIME] = PORT_READ_TIMEOUT;
//newOptions.c_cc[VTIME] = 0;
speed_t portSpeed;
switch (baudRate){
case 115200:
portSpeed=B115200;
break;
case 57600:
portSpeed=B57600;
break;
case 38400:
portSpeed=B38400;
break;
case 19200:
portSpeed=B19200;
break;
case 9600:
portSpeed=B9600;
break;
case 4800:
portSpeed=B4800;
break;
case 2400:
portSpeed=B2400;
break;
case 1200:
portSpeed=B1200;
break;
default:
//sanity check
Logger::instance().logError("unsupported serial port baudrate defined");
close(portFileDescriptor);
return EINVAL;
break;
}
result=cfsetispeed(&newOptions, portSpeed);
if(result<0){
Logger::instance().logError("set serial port input speed failed");
close(portFileDescriptor);
return result;
}
result=cfsetospeed(&newOptions, portSpeed);
if(result<0){
Logger::instance().logError("set serial port output speed failed");
close(portFileDescriptor);
return result;
}
result=tcsetattr(portFileDescriptor, TCSANOW, &newOptions);
if(result<0){
Logger::instance().logError("write serial port parameters failed");
close(portFileDescriptor);
return result;
}
result=tcflush(portFileDescriptor, TCIFLUSH);
if(result<0){
Logger::instance().logError("serial port input flush failed");
return result;
}
#endif
return result;
}
int32_t SerialPort::serialClose(){
int32_t result=0;
#ifdef _WIN32
#else
//suppress warning
//result=portName.length();
result=close(portFileDescriptor);
#endif
if(result!=E_OK){
stringstream errStrstr;
errStrstr<<result;
Logger::instance().logError("Serial port close failed. Error number: "+errStrstr.str());
errStrstr.clear();
errStrstr.str(std::string());
}
return result;
}
int32_t SerialPort::serialRead(char *data, int32_t maxDataLength){
int32_t result=0;
char buffer[0x200];
if(maxDataLength>static_cast<int32_t>(sizeof(buffer))){
return EINVAL;
}
memset(buffer, 0x00, sizeof(buffer));
#ifdef _WIN32
#else
struct termios newOptions;
fcntl(portFileDescriptor, F_SETFL, 0);
result=tcgetattr(portFileDescriptor, &newOptions);
if(result<0){
Logger::instance().logError("serialRead(), read serial port parameters call failed");
close(portFileDescriptor);
return result;
}
if(maxDataLength>1){
//newOptions.c_cc[VMIN] = maxDataLength;
newOptions.c_cc[VMIN] = 1;
}else{
newOptions.c_cc[VMIN] = 0;
}
newOptions.c_cc[VTIME] = PORT_READ_TIMEOUT;
//newOptions.c_cc[VTIME] = 0;
result=tcsetattr(portFileDescriptor, TCSANOW, &newOptions);
if(result<0){
Logger::instance().logError("serialRead(), write serial port parameters failed");
close(portFileDescriptor);
return result;
}
fcntl(portFileDescriptor, F_SETFL, 0);
result=read(portFileDescriptor, buffer, maxDataLength);
if(result<0){
Logger::instance().logError("serial port read failed");
}
#endif
if(result>0 && result<=maxDataLength){
memcpy(data, buffer, static_cast<size_t>(result));
}else if(result>maxDataLength){
memcpy(data, buffer, static_cast<size_t>(maxDataLength));
}
if(result>0){
//result=E_OK;
}else if(result==0){
//hack
//result=EHOSTUNREACH;
}
return result;
}
int32_t SerialPort::serialWrite(const unsigned char *data, size_t dataLength){
int32_t result=0;
#ifdef _WIN32
#else
// result=tcflush(portFileDescriptor, TCIFLUSH);
// if(result<0){
// Logger::instance().logError("serial port input flush failed");
// return result;
// }
result=write(portFileDescriptor, data, dataLength);
if(result!=static_cast<int32_t>(dataLength)){
Logger::instance().logFatal("serial port write failed");
return result;
}else{
result=E_OK;
}
#endif
return result;
}