-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserial.cpp
356 lines (302 loc) · 8.04 KB
/
serial.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
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
/*
Name: serial.cpp
Author: cLx
Description: Pour utiliser les ports séries sous nux et windows
Date: 08/06/08 - premiere ébauche version windows
14/09/09 - on continue le boulot sous linux...
27/10/14 - ajout de nouvelles fonctions et mise au propre
Copyright: cLx <[email protected]>
*/
#ifdef WIN32
// Window$
#include <windows.h>
#define DEFAULTPORT "COM1"
#define FDTYPE HANDLE
#else
// Linux
#include <iostream>
#include <stdlib.h>
#include <termios.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <limits.h>
#define DEFAULTPORT "/dev/ttyUSB0"
#endif
#include "serial.h"
#ifdef WIN32
int serial::open(const char *portname, unsigned int baud, int bits=8, char parity='N', int stopbits=1){
close();
fd = CreateFile(portname?portname:DEFAULTPORT, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH|FILE_FLAG_NO_BUFFERING, NULL);
if(fd == INVALID_HANDLE_VALUE){
opened = 0;
return 0;
}
else { opened = 1; }
// clear buffers
PurgeComm(fd,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
myDCB.DCBlength = sizeof(DCB);
// get the actual port's configuration
GetCommState(fd, &myDCB);
myDCB.BaudRate=baud;
myDCB.ByteSize=bits;
switch(parity){
case 'o':
case 'O':
myDCB.Parity=ODDPARITY;
break;
case 'e':
case 'E':
myDCB.Parity=EVENPARITY;
break;
case 'm':
case 'M':
myDCB.Parity=MARKPARITY;
break;
case 'n':
case 'N':
default:
myDCB.Parity=NOPARITY;
break;
}
switch(stopbits){
case 15:
myDCB.StopBits=ONE5STOPBITS;
break;
case 2:
myDCB.StopBits=TWOSTOPBITS;
break;
case 1:
default:
myDCB.StopBits=ONESTOPBIT;
break;
}
myDCB.fDtrControl=DTR_CONTROL_DISABLE;
if(!SetCommState(fd,&myDCB)){
return 0;
}
return 1;
}
void serial::close(void) {
if (opened){
CloseHandle(fd);
opened = 0;
fd = NULL;
}
}
int serial::send(char car){
DWORD NumBytes=0;
char str[2] = {car, '\0'};
if (!opened){ return 0; }
return WriteFile(fd, str, 1, &NumBytes, NULL);
}
int serial::send(char* str){
DWORD NumBytes=0;
if (!opened){ return 0; }
return WriteFile(fd, str, strlen(str), &NumBytes, NULL);
}
int serial::send(char* str, size_t len){
DWORD NumBytes=0;
if (!opened){ return 0; }
return WriteFile(fd, str, len, &NumBytes, NULL);
}
unsigned int serial::nbqueue(void){
COMSTAT Stat;
DWORD err;
ClearCommError(fd, &err, &Stat);
return Stat.cbInQue;
}
void serial::rts(int sw){
EscapeCommFunction(fd, sw?SETRTS:CLRRTS);
}
void serial::dtr(int sw){
EscapeCommFunction(fd, sw?SETDTR:CLRDTR);
}
void serial::brk(int sw){
EscapeCommFunction(fd, sw?SETBREAK:CLRBREAK);
}
unsigned int serial::receive(char *buf, size_t buf_size){
unsigned int toread = nbqueue();
DWORD nbreaded;
if (!toread) { return 0; }
if (buf_size<toread) { toread = buf_size; }
if(ReadFile(fd, buf, toread-1, &nbreaded, NULL)==0){
return 0; // awww
}
buf[nbreaded] = '\0';
return nbreaded;
}
unsigned int serial::binreceive(unsigned char *buf, size_t buf_size){
unsigned int toread = nbqueue();
DWORD nbreaded;
if (!toread) { return 0; }
if (buf_size<toread) { toread = buf_size; }
if(ReadFile(fd, buf, toread, &nbreaded, NULL)==0){
return 0;
}
return nbreaded;
}
#else
// VERSION LINUX ICI !
int serial::open(const char *portname, unsigned int baud, int bits=8, char parity='N', int stopbits=1){
struct termios options;
speed_t spd;
fd = ::open(portname, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if((fd == -1)||(!fd)) {
fd = 0;
return 0;
}
opened = 1;
if(tcgetattr(fd, &options)<0){perror("tcgetattr()");}
cfmakeraw(&options);
switch(baud){
case 50: spd = B50; break;
case 75: spd = B75; break;
case 110: spd = B110; break;
case 134: spd = B134; break;
case 150: spd = B150; break;
case 200: spd = B200; break;
case 300: spd = B300; break;
case 600: spd = B600; break;
case 1200: spd = B1200; break;
case 1800: spd = B1800; break;
case 2400: spd = B2400; break;
case 4800: spd = B4800; break;
case 9600: spd = B9600; break;
case 19200: spd = B19200; break;
case 38400: spd = B38400; break;
case 57600: spd = B57600; break;
case 115200: spd = B115200; break;
#ifdef B230400
case 230400: spd = B230400; break;
#endif
#ifdef B460800
case 460800: spd = B460800; break;
#endif
#ifdef B921600
case 921600: spd = B921600; break;
#endif
default:
return 0;
break;
}
options.c_oflag = 0;
options.c_lflag = 0;
options.c_iflag = 0;
options.c_cflag = 0;
if (cfsetospeed(&options, spd)<0){ perror("cfsetospeed()"); }
if (cfsetispeed(&options, spd)<0){ perror("cfsetispeed()"); }
options.c_oflag &= ~OPOST; // raw mode for output
options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // raw mode for input
options.c_iflag &= ~(BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL);
options.c_iflag |= IGNBRK; // ignore breaks
options.c_iflag &= ~(IXON | IXOFF | IXANY); // no software flow control
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~(CSIZE | PARENB);
options.c_cflag |= CS8;
if (stopbits==1) {
options.c_cflag &= ~CSTOPB;
}
else {
options.c_cflag |= CSTOPB;
}
options.c_cflag &= ~CSIZE; // mask the character size bits
options.c_cflag |= CS8; // 8 bits
if (parity=='E'){ // parity even
options.c_cflag |= PARENB;
}
else if (parity=='O'){ // parity odd
options.c_cflag |= (PARENB | PARODD);
}
else { // no parity
if (parity!='N') { fprintf(stderr, "Parity should be E, O or N (defaut)\n"); }
options.c_cflag &= (~PARENB & ~PARODD);
}
if(tcsetattr(fd, TCSANOW, &options)){perror("tcsetattr()");};
if(tcsetattr(fd, TCSAFLUSH, &options)){perror("tcsetattr()");};
clear_buffer();
return 0;
}
void serial::close(void) {
if (opened){
::close(fd);
opened = 0;
fd = 0;
}
}
int serial::send(char car){
char str[2] = {car, '\0'};
if (!opened){ return 0; }
return write(fd, str, 1);
}
int serial::send(char* str){
if (!opened){ return 0; }
return write(fd, str, strlen(str));
}
int serial::send(char* str, size_t len){
if (!opened){ return 0; }
return write(fd, str, len);
}
/* TODO: Adapting that code for linux ! */
unsigned int serial::nbqueue(void){
fprintf(stderr, "nbqueue() is not yet implemented for Linux !\n");
return 0;
}
unsigned int serial::receive(char *buf, size_t buf_size){
signed int nbreaded;
if (buf_size>SSIZE_MAX) { buf_size = SSIZE_MAX; }
if (!opened){ return 0; }
nbreaded = read(fd, buf, buf_size-1);
if (nbreaded<1) {
return 0;
}
buf[nbreaded] = '\0';
return nbreaded;
}
unsigned int serial::binreceive(unsigned char *buf, size_t buf_size){
signed int nbreaded;
if (!opened){ return 0; }
nbreaded = read(fd, buf, buf_size);
if (nbreaded<1) { return 0; }
return nbreaded;
}
//TODO: The following three functions are untested yet!
void serial::rts(int sw){
//int ioctl( fd, COMMAND, (int *)flags );
ioctl(fd, TIOCM_RTS, sw);
}
void serial::dtr(int sw){
ioctl(fd, TIOCM_DTR, sw);
}
void serial::brk(int sw){
ioctl(fd, TCSBRK, sw);
//int tcsendbreak(fd, sw); // POSIX, not act exactly like ioctl (delays)
}
#endif
/// et ensuite tout ce qui est commun aux deux !
int serial::isopened(void){
return opened;
}
serial::~serial(){
close();
}
serial::serial(){
opened = 0;
fd = 0; //NULL;
}
serial::serial(const char *portname, unsigned int baud=9600){
serial();
if (portname) { open(portname, baud, 8, 'N', 1); }
}
int serial::open(const char *portname, unsigned int baud=9600){
if (portname) { open(portname, baud, 8, 'N', 1); }
return opened;
}
void serial::clear_buffer(void){
char fakebuf[512];
if (!opened) { return; }
while(receive(fakebuf, sizeof(fakebuf)));
}