-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathucomm.c
More file actions
270 lines (249 loc) · 7.05 KB
/
Copy pathucomm.c
File metadata and controls
270 lines (249 loc) · 7.05 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
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
//
// uComm
// Minimalist cross-platform serial port library
//
// https://github.com/matveyt/ucomm
//
#include "ucomm.h"
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#elif defined(__unix__)
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#if !defined(O_CLOEXEC)
#define O_CLOEXEC 0
#endif // O_CLOEXEC
#if !defined(TIOCINQ)
#define TIOCINQ FIONREAD
#endif // TIOCINQ
#endif
intptr_t ucomm_open(const char* port, unsigned baud, unsigned config)
{
intptr_t fd;
#if defined(_WIN32)
char fullname[sizeof("\\\\.\\COMnnn")];
if (port == NULL) {
port = "\\\\.\\COM3";
} else if (lstrlenA(port) <= (int)sizeof("COMnnn") - 1) {
// "COMnnn" to "\\\\.\\COMnnn"
lstrcpyA(fullname, "\\\\.\\");
port = lstrcatA(fullname, port);
}
HANDLE h = CreateFileA(port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
0, NULL);
if (h != INVALID_HANDLE_VALUE) {
SetupComm(h, 1024, 1024);
fd = (intptr_t)h;
} else
fd = -1;
#elif defined(__unix__)
fd = open(port ? port : "/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_CLOEXEC);
#endif
if (fd != -1) {
ucomm_reset(fd, baud, config);
ucomm_timeout(fd, UCOMM_DEFAULT_TIMEOUT);
}
return fd;
}
int ucomm_close(intptr_t fd)
{
#if defined(_WIN32)
return CloseHandle((HANDLE)fd) ? 0 : -1;
#elif defined(__unix__)
return close(fd);
#endif
}
static unsigned baudrate(unsigned baud)
{
#if defined(_WIN32)
return baud ? baud : 115200;
#elif defined(__unix__)
static const unsigned ubr[] = {
#define B(n) (B##n), (n),
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(128000)*/ B(230400)
/*B(256000)*/ B(460800) /*B(500000)*/ /*B(576000)*/ B(921600) /*B(1000000)*/
/*B(1152000)*/ /*B(1500000)*/ /*B(2000000)*/ /*B(2500000)*/ /*B(3000000)*/
/*B(3500000)*/ /*B(4000000)*/
#undef B
};
for (ssize_t i = sizeof(ubr) / sizeof(ubr[0]) - 1; i > 0; i -= 2)
if (baud >= ubr[i])
return ubr[i - 1];
return B115200;
#endif
}
int ucomm_reset(intptr_t fd, unsigned baud, unsigned config)
{
// config 0x801 => 8-N-1
unsigned databits = (config >> 8) & 0x0f; // 5..8
unsigned parity = (config >> 4) & 0x0f; // 0..2
unsigned stopbits = (config) & 0x0f; // 1..2
if (databits < 5 || databits > 8)
databits = 8;
if (parity > 2)
parity = 0;
if (stopbits != 2)
stopbits = 1;
#if defined(_WIN32)
DCB dcb = {
.DCBlength = sizeof(DCB),
.BaudRate = baudrate(baud),
.fBinary = 1,
.fParity = !!parity,
.fDtrControl = DTR_CONTROL_DISABLE,
.fRtsControl = RTS_CONTROL_DISABLE,
.XonLim = 256,
.XoffLim = 256,
.ByteSize = (BYTE)databits,
.Parity = (BYTE)(!parity ? NOPARITY : (parity == 1) ? ODDPARITY :
EVENPARITY),
.StopBits = (BYTE)((stopbits == 1) ? ONESTOPBIT : TWOSTOPBITS),
};
ucomm_purge(fd);
return SetCommState((HANDLE)fd, &dcb) ? 0 : -1;
#elif defined(__unix__)
struct termios tio;
tcgetattr(fd, &tio);
//cfmakeraw(&tio);
tio.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | INPCK | ISTRIP | INLCR | IGNCR |
ICRNL | IXON | PARMRK);
tio.c_oflag &= ~(OPOST);
tio.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL | IEXTEN);
switch (databits) {
case 5: tio.c_cflag |= CS5; break;
case 6: tio.c_cflag |= CS6; break;
case 7: tio.c_cflag |= CS7; break;
case 8: tio.c_cflag |= CS8; break;
}
if (parity != 0) {
tio.c_iflag |= INPCK;
tio.c_cflag |= (parity == 1) ? (PARENB | PARODD) : PARENB;
}
tio.c_cflag |= (stopbits == 2) ? CSTOPB : 0;
tio.c_cflag |= (CREAD | CLOCAL);
speed_t ubr = baudrate(baud);
cfsetispeed(&tio, ubr);
cfsetospeed(&tio, ubr);
return tcsetattr(fd, TCSAFLUSH, &tio);
#endif
}
int ucomm_purge(intptr_t fd)
{
#if defined(_WIN32)
return PurgeComm((HANDLE)fd, PURGE_RXCLEAR | PURGE_TXCLEAR) ? 0 : -1;
#elif defined(__unix__)
return tcflush(fd, TCIOFLUSH);
#endif
}
int ucomm_timeout(intptr_t fd, unsigned ms)
{
#if defined(_WIN32)
COMMTIMEOUTS timeouts = {
.ReadIntervalTimeout = ms ? ms : MAXDWORD,
.ReadTotalTimeoutMultiplier = 0,
.ReadTotalTimeoutConstant = ms,
.WriteTotalTimeoutMultiplier = 0,
.WriteTotalTimeoutConstant = 0,
};
return SetCommTimeouts((HANDLE)fd, &timeouts) ? 0 : -1;
#elif defined(__unix__)
struct termios tio;
tcgetattr(fd, &tio);
tio.c_cc[VMIN] = 0;
tio.c_cc[VTIME] = (ms / 100) + !!(ms % 100);
return tcsetattr(fd, TCSANOW, &tio);
#endif
}
int ucomm_dtr(intptr_t fd, int pulldown)
{
#if defined(_WIN32)
return EscapeCommFunction((HANDLE)fd, pulldown ? SETDTR : CLRDTR) ? 0 : - 1;
#elif defined(__unix__)
int arg = TIOCM_DTR;
return ioctl(fd, pulldown ? TIOCMBIS : TIOCMBIC, &arg);
#endif
}
int ucomm_rts(intptr_t fd, int pulldown)
{
#if defined(_WIN32)
return EscapeCommFunction((HANDLE)fd, pulldown ? SETRTS : CLRRTS) ? 0 : -1;
#elif defined(__unix__)
int arg = TIOCM_RTS;
return ioctl(fd, pulldown ? TIOCMBIS : TIOCMBIC, &arg);
#endif
}
ssize_t ucomm_available(intptr_t fd)
{
#if defined(_WIN32)
COMSTAT stat;
return ClearCommError((HANDLE)fd, NULL, &stat) ? (LONG)stat.cbInQue : -1;
#elif defined(__unix__)
int available;
return ioctl(fd, TIOCINQ, &available) < 0 ? -1 : available;
#endif
}
int ucomm_getc(intptr_t fd)
{
uint8_t b;
#if defined(_WIN32)
DWORD part;
ReadFile((HANDLE)fd, &b, sizeof(b), &part, NULL);
#elif defined(__unix__)
ssize_t part = read(fd, &b, sizeof(b));
#endif
return (part == sizeof(b)) ? (int)b : -1;
}
int ucomm_putc(intptr_t fd, int ch)
{
uint8_t b = (uint8_t)ch;
#if defined(_WIN32)
DWORD part;
WriteFile((HANDLE)fd, &b, sizeof(b), &part, NULL);
#elif defined(__unix__)
ssize_t part = write(fd, &b, sizeof(b));
#endif
return (part == sizeof(b)) ? (int)b : -1;
}
ssize_t ucomm_read(intptr_t fd, void* buffer, size_t length)
{
ssize_t sz = 0;
while (sz < (ssize_t)length) {
#if defined(_WIN32)
DWORD part;
BOOL ok = ReadFile((HANDLE)fd, (uint8_t*)buffer + sz, length - sz, &part, NULL);
#elif defined(__unix__)
ssize_t part = read(fd, (uint8_t*)buffer + sz, length - sz);
int ok = (part >= 0);
#endif
if (!ok && sz <= 0)
return -1;
if (part <= 0)
break;
sz += part;
}
return sz;
}
ssize_t ucomm_write(intptr_t fd, const void* buffer, size_t length)
{
ssize_t sz = 0;
while (sz < (ssize_t)length) {
#if defined(_WIN32)
DWORD part;
BOOL ok = WriteFile((HANDLE)fd, (uint8_t*)buffer + sz, length - sz, &part, NULL);
#elif defined(__unix__)
ssize_t part = write(fd, (uint8_t*)buffer + sz, length - sz);
int ok = (part >= 0);
#endif
if (!ok && sz <= 0)
return -1;
if (part <= 0)
break;
sz += part;
}
return sz;
}