-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathserial_dev.c
More file actions
193 lines (178 loc) · 4.52 KB
/
serial_dev.c
File metadata and controls
193 lines (178 loc) · 4.52 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
#include "serial_dev.h"
int serial_dev_create(serial_dev_t *dev, const char* port, unsigned baud) {
if (!dev)
return -1;
dev->port = port;
dev->baud = baud;
#ifdef IS_WINDOWS
dev->handle = INVALID_HANDLE_VALUE;
#else
dev->fd = -1;
#endif
return 0;
}
int serial_dev_open(serial_dev_t *dev) {
fprintf(stderr, "Opening serial port %s at %u baud.\n", dev->port, dev->baud);
#ifdef IS_WINDOWS
// Windows quirk: port = "COM10" is invalid, has to be encoded as "\\.\COM10".
// This also works for COM below 9. So, let's give the user the ability to use
// any "COMx" string and just prepend the "\\.\".
char winPortName[64];
if(dev->port[0] != '\\') {
snprintf(winPortName, sizeof(winPortName), "\\\\.\\%s", dev->port);
} else {
// copy verbatim if string already starts with a '\'
snprintf(winPortName, sizeof(winPortName), "%s", dev->port);
}
dev->handle = CreateFileA(winPortName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0,0);
if (dev->handle == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
fprintf(stderr, "Serial port %s not found.\n", dev->port);
// weird: without this, errno = 0 (no error).
_set_errno(ERROR_FILE_NOT_FOUND);
return -1; // Device not found
}
// Error while opening the device
return -1;
}
DCB dcbSerialParams;
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (!GetCommState(dev->handle, &dcbSerialParams)) {
return -1;
}
// set baud and 8N1 serial formatting
dcbSerialParams.BaudRate = dev->baud;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
// write back
if (!SetCommState(dev->handle, &dcbSerialParams)){
return -1;
}
// Set the timeout parameters to "no timeout" (blocking).
// see https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-commtimeouts
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutConstant = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = MAXDWORD;
timeouts.WriteTotalTimeoutMultiplier = 0;
// Write the parameters
if (!SetCommTimeouts(dev->handle, &timeouts)) {
return -1;
}
#else
struct termios attr;
if ((dev->fd = open(dev->port, O_RDWR | O_NOCTTY | O_NONBLOCK)) == -1) {
perror("open");
return -1;
}
// clear the nonblock flag
if (fcntl(dev->fd, F_SETFL, 0) == -1) {
perror("fcntl");
close(dev->fd);
return -1;
}
if (tcgetattr(dev->fd, &attr) == -1) {
perror("tcgetattr");
return -1;
}
cfmakeraw(&attr);
cfsetspeed(&attr, dev->baud);
attr.c_cflag |= CLOCAL;
if (tcsetattr(dev->fd, TCSANOW, &attr) == -1) {
perror("tcsetattr");
return -1;
}
#endif
// all okay if we get here
return 0;
}
int serial_dev_write(serial_dev_t *dev, const void* data, size_t len) {
#ifdef IS_WINDOWS
DWORD dwBytesWritten;
if (!WriteFile(dev->handle, data, len, &dwBytesWritten,NULL)) {
return -1;
}
return (int) dwBytesWritten;
#else
return write(dev->fd, data, len);
#endif
}
int serial_dev_read(serial_dev_t *dev, void* data, size_t len) {
#ifdef IS_WINDOWS
DWORD dwBytesRead = 0;
if (!ReadFile(dev->handle, data, len, &dwBytesRead, NULL)) {
return -1;
}
return (int) dwBytesRead;
#else
int got, count = 0;
/* On Linux, raw mode only blocks on the first character: man 3 termios. */
while (count < len) {
got = read(dev->fd, (char *)data + count, len - count);
if (got < 0)
return got;
count += got;
if (count < len)
usleep((100000 / dev->baud) * (len - count));
}
return count;
#endif
}
int serial_dev_do_dtr_reset(serial_dev_t *dev) {
#ifdef IS_WINDOWS
// EscapeCommFunction returns 0 on fail
if(EscapeCommFunction(dev->handle, SETDTR) == 0) {
return -1;
}
if(EscapeCommFunction(dev->handle, CLRDTR) == 0) {
return -1;
}
#else
int argp = TIOCM_DTR;
// Arduino DTR reset.
if (ioctl(dev->fd, TIOCMBIC, &argp) == -1) {
perror("ioctl");
return -1;
}
if (tcdrain(dev->fd) == -1) {
perror("tcdrain");
return -1;
}
if (ioctl(dev->fd, TIOCMBIS, &argp) == -1) {
perror("ioctl");
return -1;
}
#endif
return 0;
}
int serial_dev_flush_rx(serial_dev_t *dev) {
#ifdef IS_WINDOWS
// PurgeComm returns 0 on fail
if (PurgeComm(dev->handle, PURGE_RXCLEAR) == 0) {
return -1;
}
#else
if (tcflush(dev->fd, TCIFLUSH) == -1) {
perror("tcflush");
return -1;
}
#endif
return 0;
}
int serial_dev_close(serial_dev_t *dev) {
#ifdef IS_WINDOWS
if(!CloseHandle(dev->handle)) {
return -1;
}
dev->handle = INVALID_HANDLE_VALUE;
#else
int ret = 0;
if((ret = close(dev->fd)) != 0) {
return ret;
}
dev->fd = -1;
#endif
return 0;
}