-
Notifications
You must be signed in to change notification settings - Fork 9
/
serial.cpp
429 lines (370 loc) · 8.9 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*========================================================================
Serial.cc : Class wrapper for Serial I/O on Linux
------------------------------------------------------------------------
Copyright (C) 1999-2002 James R. Bruce
School of Computer Science, Carnegie Mellon University
------------------------------------------------------------------------
This software is distributed under the GNU General Public License,
version 2. If you do not have a copy of this licence, visit
www.gnu.org, or write: Free Software Foundation, 59 Temple Place,
Suite 330 Boston, MA 02111-1307 USA. This program is distributed
in the hope that it will be useful, but WITHOUT ANY WARRANTY,
including MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
========================================================================*/
#ifndef WIN32
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#else
# include <cstdlib>
# include <cstdio>
#endif
#include <QDebug>
#include "serial.h"
/**************************** TYPES ******************************************/
// #define RADIO_DEVICE "/dev/ttyS0"
namespace HAL
{
#ifndef WIN32
const speed_t baud_rates[][2] =
{
{ B0, 0 },
{ B50, 50 },
{ B75, 75 },
{ B110, 110 },
{ B134, 134 },
{ B150, 150 },
{ B200, 200 },
{ B300, 300 },
{ B600, 600 },
{ B1200, 1200 },
{ B1800, 1800 },
{ B2400, 2400 },
{ B4800, 4800 },
{ B9600, 9600 },
{ B19200, 19200 },
{ B38400, 38400 },
{ B57600, 57600 },
{ B115200, 115200 },
{ B230400, 230400 }
// { B460800, 460800 }
};
/**************************** CODE *******************************************/
/*
* baud_rate_to_flag -
*
* convert from raw baud rate to teh
* appropriate baud flag
*/
speed_t baud_rate_to_flag(unsigned int speed)
{
int i;
for (i = 0; i < 20; i++)
{
if (baud_rates[i][1] == speed)
return(baud_rates[i][0]);
}
return 0;
}
/*
* baud_flag_to_rate -
*
* This function converts from the speed s to
* the baud flag
*/
int baud_flag_to_rate(speed_t s)
{
int i;
for(i = 0; i < 20; i++)
{
if (baud_rates[i][0] == s)
return(baud_rates[i][1]);
}
return 0;
}
#endif
/**************************** SERIAL CLASS ***********************************/
/*
* open -
*
* This function opens the serial port device and configures it
* to operate at the specified baud rate and parameters
*
* rETURN VALUE: TRUE on success, FALSE on failure
*/
bool Serial::Open(const std::string& device, int baud)
{
#ifdef WIN32
DCB dcb;
hCom = CreateFile( device.c_str(),
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attributes
OPEN_EXISTING, 0, NULL);
if (hCom == INVALID_HANDLE_VALUE)
{
err("ERROR: Can't open com port %s", device.c_str());
return false;
}
if (!GetCommState(hCom, &dcb))
{
err("ERROR: Can't get CommState.\n");
Close();
return false;
}
switch (baud)
{
case 9600:
dcb.BaudRate = CBR_9600;
break;
case 19200:
dcb.BaudRate = CBR_19200;
break;
case 110:
dcb.BaudRate = CBR_110;
break;
case 300:
dcb.BaudRate = CBR_300;
break;
case 38400:
dcb.BaudRate = CBR_38400;
break;
case 600:
dcb.BaudRate = CBR_600;
break;
case 56000:
dcb.BaudRate = CBR_56000;
break;
case 1200:
dcb.BaudRate = CBR_1200;
break;
case 57600:
dcb.BaudRate = CBR_57600;
break;
case 2400:
dcb.BaudRate = CBR_2400;
break;
case 115200:
dcb.BaudRate = CBR_115200;
break;
case 4800:
dcb.BaudRate = CBR_4800;
break;
case 128000:
dcb.BaudRate = CBR_128000;
break;
case 256000:
dcb.BaudRate = CBR_256000;
break;
case 14400:
dcb.BaudRate = CBR_14400;
break;
default:
fprintf(stderr, "ERROR: Baut rate currently not supported.\n");
Close();
return false;
}
dcb.ByteSize = 8;
dcb.fParity = FALSE;
dcb.fDsrSensitivity = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
if(!SetCommState(hCom, &dcb))
{
fprintf(stderr, "ERROR: Can't set CommState.\n");
Close();
return false;
}
return true;
#else
struct termios tio;
speed_t bf;
int baud_in, baud_out;
// open device
fd = ::open(device.c_str(), O_RDWR | O_SYNC);
perror("><");
qDebug() << "fd = " << fd;
if (fd < 0)
return(false);
/* set control attributes -
* binary I/O, no flow control
*/
tcgetattr(fd, &tio);
cfmakeraw(&tio);
tio.c_cflag &= ~(CRTSCTS | IXON | IXOFF);
// set serial port speed
bf = baud_rate_to_flag(baud);
cfsetispeed(&tio, bf);
cfsetospeed(&tio, bf);
tcsetattr(fd, TCSANOW, &tio);
// test what actually got set
memset(&tio, 0, sizeof(tio));
tcgetattr(fd, &tio);
baud_in = baud_flag_to_rate(cfgetispeed(&tio));
baud_out = baud_flag_to_rate(cfgetospeed(&tio));
return((baud_in == baud) && (baud_out == baud));
#endif
}
/*
* close -
*
* this function closes the serial port module
*/
void Serial::Close(void)
{
#ifdef WIN32
if(!CloseHandle(hCom))
fprintf(stderr, "ERROR: Can't close comm port.\n");
hCom = INVALID_HANDLE_VALUE;
#else
if(fd)
{
::close(fd);
fd = 0;
}
#endif
}
/*
* Read -
*
* function reads bytes from the serial port
*/
#ifdef WIN32
int Serial::Read(void *buf, int size)
{
DWORD bytesTransfered;
if(!ReadFile(hCom, (char*)buf, size, &bytesTransfered, NULL))
{
err("ERROR: Can't read from comm port.");
}
return bytesTransfered;
}
#endif
/*
* Write -
*
* Function writes bytes to the serial port
*/
#ifdef WIN32
int Serial::Write(void *buf, int size)
{
DWORD bytesTransfered;
if(!WriteFile(hCom, (char*)buf, size, &bytesTransfered, NULL ))
{
err(stderr, "Can't write to comm port.");
}
return bytesTransfered;
}
#endif
/*
* Writebyte -
*
* Function writes a signel byte to the serial port
*/
int Serial::WriteByte(char b)
{
#ifdef WIN32
return Write(&b, 1);
#else
return (::write(fd, &b, 1));
#endif
}
/*
* ReadByte -
*
* function reads a single byte from the serial port
*/
int Serial::ReadByte(void)
{
#ifdef WIN32
char b;
return Read(&b, 1);
return b;
#else
char data;
int ret_val;
if ((ret_val = ::read(fd, &data, 1)) < 0)
return (ret_val);
return (data);
#endif
}
/*
* ReadByteTimeout -
*
* function reads a single byte from the serial port, timing out after given ms if no byte is found.
* NOTE: does not work on WIN32, simply becomes ReadByte
*/
int Serial::ReadByteTimeout(float ms, bool &ok)
{
ok = false;
#ifdef WIN32
char b;
return Read(&b, 1);
return b;
#else
int ret_val;
char data;
FD_ZERO(&set); /* clear the set */
FD_SET(fd, &set); /* add our file descriptor to the set */
timeout.tv_sec = 0;
timeout.tv_usec = ms*1000;
int rv = select(fd + 1, &set, NULL, NULL, &timeout);
if(rv == -1)
perror("select"); /* an error accured */
else if(rv == 0){}
//printf("><><><><><><>::::::::::><><><><><><><>timeout\n"); /* a timeout occured */
else {
ok = true;
if ((ret_val = ::read(fd, &data, 1)) < 0)
return (ret_val);
return (data);
}
return data;
#endif
}
int Serial::ReadNBytesTimeout(int n, char *data, float ms, bool &ok)
{
ok = false;
#ifdef WIN32
char b;
return Read(&b, 1);
return b;
#else
int ret_val;
FD_ZERO(&set); /* clear the set */
FD_SET(fd, &set); /* add our file descriptor to the set */
timeout.tv_sec = 0;
timeout.tv_usec = ms*1000;
int rv = select(fd + 1, &set, NULL, NULL, &timeout);
if(rv == -1)
perror("select"); /* an error accured */
else if(rv == 0){}
// printf("><><><><><><>::::::::::><><><><><><><>timeout\n"); /* a timeout occured */
else {
ok = true;
ret_val = ::read(fd, data, n);
return (ret_val);
}
return -1;
#endif
}
void Serial::Clear()
{
#if WIN32
#else
tcflush(fd,TCIOFLUSH);
#endif
}
} // namespace HAL