]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/common/serial.c
fix hex output
[libsigrok.git] / hardware / common / serial.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <string.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <unistd.h>
25#ifdef _WIN32
26#include <conio.h>
27#else
28#include <glob.h>
29#include <termios.h>
30#endif
31#include <stdlib.h>
32#include <glib.h>
33#include <sigrok.h>
34
35// FIXME: Must be moved, or rather passed as function argument.
36#ifdef _WIN32
37HANDLE hdl;
38#endif
39
40char *serial_port_glob[] = {
41 /* Linux */
42 "/dev/ttyS*",
43 "/dev/ttyUSB*",
44 "/dev/ttyACM*",
45 /* MacOS X */
46 "/dev/ttys*",
47 "/dev/tty.USB-*",
48 "/dev/tty.Modem-*",
49 NULL,
50};
51
52GSList *list_serial_ports(void)
53{
54 GSList *ports;
55
56#ifdef _WIN32
57 /* TODO */
58 ports = NULL;
59 ports = g_slist_append(ports, strdup("COM1"));
60#else
61 glob_t g;
62 unsigned int i, j;
63
64 ports = NULL;
65 for (i = 0; serial_port_glob[i]; i++) {
66 if (glob(serial_port_glob[i], 0, NULL, &g))
67 continue;
68 for (j = 0; j < g.gl_pathc; j++)
69 ports = g_slist_append(ports, strdup(g.gl_pathv[j]));
70 globfree(&g);
71 }
72#endif
73
74 return ports;
75}
76
77int serial_open(const char *pathname, int flags)
78{
79#ifdef _WIN32
80 /* FIXME: Don't hardcode COM1. */
81 hdl = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0,
82 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
83 if (hdl == INVALID_HANDLE_VALUE) {
84 /* TODO: Error handling. */
85 }
86 return 0;
87#else
88 return open(pathname, flags);
89#endif
90}
91
92/*
93 * Close the serial port.
94 * Returns 0 upon success, -1 upon failure.
95 */
96int serial_close(int fd)
97{
98#ifdef _WIN32
99 /* Returns non-zero upon success, 0 upon failure. */
100 return (CloseHandle(hdl) == 0) ? -1 : 0;
101#else
102 /* Returns 0 upon success, -1 upon failure. */
103 return close(fd);
104#endif
105}
106
107/*
108 * Flush serial port buffers (if any).
109 * Returns 0 upon success, -1 upon failure.
110 */
111int serial_flush(int fd)
112{
113#ifdef _WIN32
114 /* Returns non-zero upon success, 0 upon failure. */
115 return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0;
116#else
117 /* Returns 0 upon success, -1 upon failure. */
118 return tcflush(fd, TCIOFLUSH);
119#endif
120}
121
122/*
123 * Write a number of bytes to the specified serial port.
124 * Returns the number of bytes written, or -1 upon failure.
125 */
126int serial_write(int fd, const void *buf, size_t count)
127{
128#ifdef _WIN32
129 DWORD tmp = 0;
130
131 /* FIXME */
132 /* Returns non-zero upon success, 0 upon failure. */
133 WriteFile(hdl, buf, count, &tmp, NULL);
134#else
135 /* Returns the number of bytes written, or -1 upon failure. */
136 return write(fd, buf, count);
137#endif
138}
139
140/*
141 * Read a number of bytes from the specified serial port.
142 * Returns the number of bytes read, or -1 upon failure.
143 */
144int serial_read(int fd, void *buf, size_t count)
145{
146#ifdef _WIN32
147 DWORD tmp = 0;
148
149 /* FIXME */
150 /* Returns non-zero upon success, 0 upon failure. */
151 return ReadFile(hdl, buf, count, &tmp, NULL);
152#else
153 /* Returns the number of bytes read, or -1 upon failure. */
154 return read(fd, buf, count);
155#endif
156}
157
158void *serial_backup_params(int fd)
159{
160#ifdef _WIN32
161 /* TODO */
162#else
163 struct termios *term;
164
165 term = malloc(sizeof(struct termios));
166 tcgetattr(fd, term);
167
168 return term;
169#endif
170}
171
172void serial_restore_params(int fd, void *backup)
173{
174#ifdef _WIN32
175 /* TODO */
176#else
177 tcsetattr(fd, TCSADRAIN, (struct termios *)backup);
178#endif
179}
180
181/*
182 * Set serial parameters.
183 *
184 * flowcontrol: 1 = rts/cts, 2 = xon/xoff
185 * parity: 0 = none, 1 = even, 2 = odd
186 */
187int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
188 int flowcontrol)
189{
190#ifdef _WIN32
191 DCB dcb;
192
193 if (!GetCommState(hdl, &dcb)) {
194 /* TODO: Error handling. */
195 return SIGROK_ERR;
196 }
197
198 /* TODO: Rename 'speed' to 'baudrate'. */
199 switch(speed) {
200 /* TODO: Support for higher baud rates. */
201 case 115200:
202 dcb.BaudRate = CBR_115200;
203 break;
204 case 57600:
205 dcb.BaudRate = CBR_57600;
206 break;
207 case 38400:
208 dcb.BaudRate = CBR_38400;
209 break;
210 case 19200:
211 dcb.BaudRate = CBR_19200;
212 break;
213 case 9600:
214 dcb.BaudRate = CBR_9600;
215 break;
216 default:
217 /* TODO: Error handling. */
218 break;
219 }
220 dcb.ByteSize = bits;
221 dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
222 dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */
223
224 if (!SetCommState(hdl, &dcb)) {
225 /* TODO: Error handling. */
226 return SIGROK_ERR;
227 }
228#else
229 struct termios term;
230 speed_t baud;
231
232 switch (speed) {
233 case 9600:
234 baud = B9600;
235 break;
236 case 38400:
237 baud = B38400;
238 break;
239 case 57600:
240 baud = B57600;
241 break;
242 case 115200:
243 baud = B115200;
244 break;
245 case 460800:
246 baud = B460800;
247 break;
248 default:
249 return SIGROK_ERR;
250 }
251
252 if (tcgetattr(fd, &term) < 0)
253 return SIGROK_ERR;
254 if (cfsetispeed(&term, baud) < 0)
255 return SIGROK_ERR;
256
257 term.c_cflag &= ~CSIZE;
258 switch (bits) {
259 case 8:
260 term.c_cflag |= CS8;
261 break;
262 case 7:
263 term.c_cflag |= CS7;
264 break;
265 default:
266 return SIGROK_ERR;
267 }
268
269 term.c_cflag &= ~CSTOPB;
270 switch (stopbits) {
271 case 1:
272 break;
273 case 2:
274 term.c_cflag |= CSTOPB;
275 default:
276 return SIGROK_ERR;
277 }
278
279 term.c_cflag &= ~(IXON | IXOFF | CRTSCTS);
280 switch (flowcontrol) {
281 case 2:
282 term.c_cflag |= IXON | IXOFF;
283 break;
284 case 1:
285 term.c_cflag |= CRTSCTS;
286 default:
287 return SIGROK_ERR;
288 }
289
290 term.c_iflag &= ~IGNPAR;
291 term.c_cflag &= ~(PARODD | PARENB);
292 switch (parity) {
293 case 0:
294 term.c_iflag |= IGNPAR;
295 break;
296 case 1:
297 term.c_cflag |= PARENB;
298 break;
299 case 2:
300 term.c_cflag |= PARENB | PARODD;
301 break;
302 default:
303 return SIGROK_ERR;
304 }
305
306 if (tcsetattr(fd, TCSADRAIN, &term) < 0)
307 return SIGROK_ERR;
308#endif
309
310 return SIGROK_OK;
311}