forked from bartprescott/vbus-collector
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserial.c
469 lines (411 loc) · 12.3 KB
/
serial.c
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//****************************************************************************
// serial.c
//
// (c) Hewell Technology Ltd. 2014
//
//****************************************************************************
#define _DEFAULT_SOURCE // for cfmakeraw()
#include <errno.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
static FILE *log_fp = NULL;
#ifdef __WXMSW__
#include <windows.h>
#include <winerror.h>
// To get windows device functions
#include <Setupapi.h>
#include <devguid.h>
static HANDLE handle = INVALID_HANDLE_VALUE;
#else // __WXMSW__
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <linux/serial.h>
#include <sys/ioctl.h>
static int fd = -1;
#endif
int is_data_available(int fd);
static char error_str[128] = "";
//***************************************************************************
static void debug_log(const uint8_t *buf, uint16_t len, bool out)
{
if (log_fp != NULL)
{
fprintf(log_fp, "%s ", out ? "\n-->" : "<--");
for(int i = 0; i < len; i++)
fprintf(log_fp, "%02X ", buf[i]);
fprintf(log_fp, "\n");
}
fflush(log_fp);
}
//***************************************************************************
void debug_comment(const char *comment, ...)
{
if (log_fp != NULL)
{
va_list ap;
va_start(ap, comment);
vfprintf(log_fp, comment, ap);
va_end(ap);
}
}
//***************************************************************************
const char *serial_get_error(void)
{
return error_str;
}
//***************************************************************************
bool serial_open_port(const char *port)
{
bool ret = false;
#ifdef __WXMSW__
if (handle == INVALID_HANDLE_VALUE)
{
if (*port > 0)
{
char device[64];
snprintf(device, sizeof(device), "\\\\.\\COM%d", *port);
handle = CreateFile(device,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (handle != INVALID_HANDLE_VALUE) {
ret = true;
}
else
{
strncpy(error_str, "Error opening COM port", sizeof(error_str));
}
}
else
{
strncpy(error_str, "Invalid COM port", sizeof(error_str));
}
}
else
{
strncpy(error_str, "Port already open", sizeof(error_str));
}
log_fp = fopen("debug.log", "w");
#else // !__WXMSW__
if (fd < 0)
{
fd = open(port, O_RDWR | O_NONBLOCK); //new open file "DESCRIPTION"!
if (fd >= 0) {
// Port Available
ret = true;
} else {
// Port NOT Available
ret = false;
}
}
else
{
strncpy(error_str, "Port already open", sizeof(error_str));
}
#endif // __WXMSW__
return ret;
}
#ifndef __WXMSW__
//***************************************************************************
int rate_to_constant(int baudrate) {
#define B(x) case x: return B##x
switch(baudrate) {
B(50); B(75); B(110); B(134); B(150);
B(200); B(300); B(600); B(1200); B(1800);
B(2400); B(4800); B(9600); B(19200); B(38400);
B(57600); B(115200); B(230400); B(460800); B(500000);
B(576000); B(921600); B(1000000);B(1152000);B(1500000);
default: return 0;
}
#undef B
}
#endif // __WXMSW__
//***************************************************************************
bool serial_set_baud_rate(int rate)
{
bool ret = false;
#ifdef __WXMSW__
DCB dcb_config;
if (handle != INVALID_HANDLE_VALUE)
{
if (GetCommState(handle, &dcb_config))
{
dcb_config.BaudRate = rate;
dcb_config.ByteSize = 8;
dcb_config.Parity = NOPARITY;
dcb_config.StopBits = ONESTOPBIT;
dcb_config.fBinary = TRUE;
dcb_config.fParity = FALSE;
dcb_config.fOutxCtsFlow = FALSE;
dcb_config.fOutxDsrFlow = FALSE;
dcb_config.fDtrControl = DTR_CONTROL_DISABLE;
dcb_config.fDsrSensitivity = FALSE;
dcb_config.fTXContinueOnXoff = TRUE;
dcb_config.fOutX = FALSE;
dcb_config.fInX = FALSE;
dcb_config.fRtsControl = RTS_CONTROL_DISABLE;
if (SetCommState(handle, &dcb_config))
{
COMMTIMEOUTS cto;
if (GetCommTimeouts(handle, &cto))
{
// Set non-blocking reads
cto.ReadIntervalTimeout = MAXDWORD;
cto.ReadTotalTimeoutConstant = 0;
cto.ReadTotalTimeoutMultiplier = 0;
SetCommTimeouts(handle, &cto);
ret = true;
}
}
else
{
strncpy(error_str, "Error configuring COM port", sizeof(error_str));
}
}
else
{
strncpy(error_str, "Error reading COM port settings", sizeof(error_str));
}
}
else
{
strncpy(error_str, "Port not open", sizeof(error_str));
}
#else // !__WXMSW__
struct termios attr;
struct serial_struct serinfo;
// (Not too sure what the below does?!)
int speed = rate_to_constant(rate);
if (speed == 0)
{
//Custom divisor
serinfo.reserved_char[0] = 0;
if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
{
return 0;
}
serinfo.flags &= ~ASYNC_SPD_MASK;
serinfo.flags |= ASYNC_SPD_CUST;
serinfo.custom_divisor = (serinfo.baud_base + (rate / 2)) / rate;
if (serinfo.custom_divisor < 1)
{
serinfo.custom_divisor = 1;
}
if (ioctl(fd, TIOCSSERIAL, &serinfo) < 0)
{
strncpy(error_str, "Error with TIOCSSERIAL (Setting serial line information)", sizeof(error_str));
return 0;
}
if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
{
strncpy(error_str, "Error with TIOCGSERIAL (Getting serial line information)", sizeof(error_str));
return 0;
}
if (serinfo.custom_divisor * rate != serinfo.baud_base)
{
// warnx("Actual baudrate is %d / %d = %f",
// serinfo.baud_base, serinfo.custom_divisor,
// (float)serinfo.baud_base / serinfo.custom_divisor);
}
}
else
{
strncpy(error_str, "Error reading COM port settings", sizeof(error_str));
}
//Set baud rate
fcntl(fd, F_SETFL, O_NONBLOCK); //Sets file "DESCRIPTOR" to open
tcgetattr(fd, &attr);
/*Currently this code has only been tested when using the non-standard baud
rate of 625000. Should you use a baud rate other than this, there will
likely be errors. the functions below would likely need changing.*/
cfsetispeed(&attr, speed ?: B38400);
cfsetospeed(&attr, speed ?: B38400);
cfmakeraw(&attr); /* set serial port to raw mode. Need to move this to own
function call*/
//attr.c_cflag |= (CLOCAL | CREAD);
//attr.c_cflag &= ~CRTSCTS;
//attr.c_lflag &= ~ICANON; // Set non-canonical mode
attr.c_cc[VMIN] = 1;
attr.c_cc[VTIME] = 10; // Set timeout of 0.1 seconds
// Input flags - Turn off input processing
// convert break to null byte, no CR to NL translation,
// no NL to CR translation, don't mark parity errors or breaks
// no input parity check, don't strip high bit off,
// no XON/XOFF software flow control
//
attr.c_iflag &= ~(IGNBRK | BRKINT | ICRNL |
INLCR | PARMRK | INPCK |
ISTRIP | IXON | IXOFF | IXANY);
//
// Output flags - Turn off output processing
// no CR to NL translation, no NL to CR-NL translation,
// no NL to CR translation, no column 0 CR suppression,
// no Ctrl-D suppression, no fill characters, no case mapping,
// no local output processing
//
// config.c_oflag &= ~(OCRNL | ONLCR | ONLRET |
// ONOCR | ONOEOT| OFILL | OLCUC | OPOST);
attr.c_oflag = 0;
//
// No line processing:
// echo off, echo newline off, canonical mode off,
// extended input processing off, signal chars off
//
attr.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
//
// Turn off character processing
// clear current char size mask, no parity checking,
// no output processing, force 8 bit input
attr.c_cflag |= (CLOCAL | CREAD | CS8);
attr.c_cflag &= ~(CSIZE | CRTSCTS | PARENB);
if (tcsetattr(fd, TCSANOW, &attr) != 0)
{
strncpy(error_str, "Error reading COM port settings", sizeof(error_str));
}
else
{
ret = true;
}
#endif // __WXMSW__
return ret;
}
//***************************************************************************
bool serial_close_port(void)
{
#ifdef __WXMSW__
if (handle != INVALID_HANDLE_VALUE)
{
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;
}
#else // __WXMSW__
if (fd >= 0)
{
close(fd);
fd = -1;
}
#endif // __WXMSW__
if (log_fp != NULL)
{
fclose(log_fp);
log_fp = NULL;
}
return true;
}
//***************************************************************************
ssize_t serial_write(const void *buf, size_t count)
{
ssize_t ret = -1;
#ifdef __WXMSW__
DWORD written;
if (WriteFile(handle, buf, count, &written, NULL))
{
ret = written;
}
#else
ret = write(fd, buf, count);
#endif
debug_log(buf, ret, true);
return ret;
}
//***************************************************************************
ssize_t serial_read(void *buf, size_t count)
{
ssize_t ret = -1;
ssize_t bytes_recieved;
void *buf_ptr = buf;
ssize_t original_count = count;
while (count > 0)
{
int status_data_available = is_data_available(fd);
// Data may be available...
if (status_data_available > 0)
{
#ifdef __WXMSW__
DWORD read;
if (ReadFile(handle, buf, count, &read, NULL)) {
bytes_recieved = read;
}
#else
bytes_recieved = read(fd, buf_ptr, count);
#endif
buf_ptr += bytes_recieved;
count -= bytes_recieved;
}
else if (status_data_available == 0)
{
// Timed out - nothing to read!
// Hence "original_count" will equal "count"
break;
}
else if (status_data_available < 0)
{
// Error in SELECT function
// Hope this doesn't happen!
return -1;
}
}
ret = original_count - count; //For a data packet this value should be 247
debug_log(buf, ret, false);
return ret;
}
//***************************************************************************
int is_data_available(int fd)
{
#ifdef __WXMSW__
return 1;
#else // !__WXMSW__
fd_set rd, err;
struct timeval wait = {1, 0}; //Wait 1 second
FD_ZERO(&rd);
FD_SET(fd, &rd);
int ret = select(fd + 1, &rd, NULL, &err, &wait);
// Timeout expired...
if (ret == 0)
{
return ret;
}
if (ret > 0)
{
//An event is pending
if (FD_ISSET(fd, &rd))
{
return ret; // A "read" is now available
}
}
else if (ret < 0)
{
// An EINTR error occurred, just print it to stdout
if (errno != EINTR)
{
perror("select");
printf("Error on select(): %s\n", strerror(errno));
errno = 0;
ret = -1;
}
else
{
//Ignore EINTR
//errno = 0;
//perror("Xselect");
//printf("XError on select(): %s\n", strerror(errno));
errno = 0;
ret = 0; //Ignore EINTR, treat as a timeout
}
}
return ret;
#endif // __WXMSW__
}