/
serial.cpp
358 lines (299 loc) · 7.18 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
#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>
//#include <QDebug>
#else
# include <cstdlib>
# include <cstdio>
#endif
//#include "logger.h"
#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 the
* 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
*
* Retrun 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);
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));
// tcflush(fd, TCIOFLUSH);
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
}
void Serial::WriteGen(void *buffer, int nbyte)
{
::write(fd,buffer,nbyte);
}
void Serial::WriteString(std::string s)
{
for(int i = 0; i < s.size(); ++i)
{
WriteByte(s.at(i));
}
WriteByte('\n');
}
} // namespace HAL