2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
21 #include <sys/types.h>
33 #include "libsigrok.h"
34 #include "libsigrok-internal.h"
36 // FIXME: Must be moved, or rather passed as function argument.
41 static const char *serial_port_glob[] = {
53 SR_PRIV GSList *list_serial_ports(void)
60 ports = g_slist_append(ports, g_strdup("COM1"));
66 for (i = 0; serial_port_glob[i]; i++) {
67 if (glob(serial_port_glob[i], 0, NULL, &g))
69 for (j = 0; j < g.gl_pathc; j++)
70 ports = g_slist_append(ports, g_strdup(g.gl_pathv[j]));
78 SR_PRIV int serial_open(const char *pathname, int flags)
81 /* FIXME: Don't hardcode COM1. */
82 hdl = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0,
83 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
84 if (hdl == INVALID_HANDLE_VALUE) {
85 /* TODO: Error handling. */
89 return open(pathname, flags);
94 * Close the serial port.
95 * Returns 0 upon success, -1 upon failure.
97 SR_PRIV int serial_close(int fd)
100 /* Returns non-zero upon success, 0 upon failure. */
101 return (CloseHandle(hdl) == 0) ? -1 : 0;
103 /* Returns 0 upon success, -1 upon failure. */
109 * Flush serial port buffers (if any).
110 * Returns 0 upon success, -1 upon failure.
112 SR_PRIV int serial_flush(int fd)
115 /* Returns non-zero upon success, 0 upon failure. */
116 return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0;
118 /* Returns 0 upon success, -1 upon failure. */
119 return tcflush(fd, TCIOFLUSH);
124 * Write a number of bytes to the specified serial port.
125 * Returns the number of bytes written, or -1 upon failure.
127 SR_PRIV int serial_write(int fd, const void *buf, size_t count)
133 /* Returns non-zero upon success, 0 upon failure. */
134 WriteFile(hdl, buf, count, &tmp, NULL);
136 /* Returns the number of bytes written, or -1 upon failure. */
137 return write(fd, buf, count);
142 * Read a number of bytes from the specified serial port.
143 * Returns the number of bytes read, or -1 upon failure.
145 SR_PRIV int serial_read(int fd, void *buf, size_t count)
151 /* Returns non-zero upon success, 0 upon failure. */
152 return ReadFile(hdl, buf, count, &tmp, NULL);
154 /* Returns the number of bytes read, or -1 upon failure. */
155 return read(fd, buf, count);
159 SR_PRIV void *serial_backup_params(int fd)
164 struct termios *term;
166 /* TODO: 'term' is never g_free()'d? */
167 if (!(term = g_try_malloc(sizeof(struct termios)))) {
168 sr_err("serial: %s: term malloc failed", __func__);
178 SR_PRIV void serial_restore_params(int fd, void *backup)
183 tcsetattr(fd, TCSADRAIN, (struct termios *)backup);
188 * Set serial parameters.
190 * flowcontrol: 1 = rts/cts, 2 = xon/xoff
191 * parity: 0 = none, 1 = even, 2 = odd
193 SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
194 int stopbits, int flowcontrol)
199 if (!GetCommState(hdl, &dcb)) {
200 /* TODO: Error handling. */
205 /* TODO: Support for higher baud rates. */
207 dcb.BaudRate = CBR_115200;
210 dcb.BaudRate = CBR_57600;
213 dcb.BaudRate = CBR_38400;
216 dcb.BaudRate = CBR_19200;
219 dcb.BaudRate = CBR_9600;
222 dcb.BaudRate = CBR_4800;
225 dcb.BaudRate = CBR_2400;
228 /* TODO: Error handling. */
232 dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
233 dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */
235 if (!SetCommState(hdl, &dcb)) {
236 /* TODO: Error handling. */
243 if (tcgetattr(fd, &term) < 0)
273 if (cfsetospeed(&term, baud) < 0)
275 if (cfsetispeed(&term, baud) < 0)
278 term.c_cflag &= ~CSIZE;
290 term.c_cflag &= ~CSTOPB;
295 term.c_cflag |= CSTOPB;
301 term.c_iflag &= ~(IXON | IXOFF);
302 term.c_cflag &= ~CRTSCTS;
303 switch (flowcontrol) {
305 /* No flow control. */
308 term.c_cflag |= CRTSCTS;
311 term.c_iflag |= IXON | IXOFF;
317 term.c_iflag &= ~IGNPAR;
318 term.c_cflag &= ~(PARODD | PARENB);
320 case SERIAL_PARITY_NONE:
321 term.c_iflag |= IGNPAR;
323 case SERIAL_PARITY_EVEN:
324 term.c_cflag |= PARENB;
326 case SERIAL_PARITY_ODD:
327 term.c_cflag |= PARENB | PARODD;
333 /* Some default parameters */
334 term.c_iflag &= ~(ICRNL);
335 term.c_lflag &= ~(ICANON | ECHO);
337 if (tcsetattr(fd, TCSADRAIN, &term) < 0)
344 #define SERIAL_COMM_SPEC "^(\\d+)/([78])([neo])([12])$"
345 SR_PRIV int serial_set_paramstr(int fd, const char *paramstr)
349 int speed, databits, parity, stopbits;
352 speed = databits = parity = stopbits = 0;
353 reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL);
354 if (g_regex_match(reg, paramstr, 0, &match)) {
355 if ((mstr = g_match_info_fetch(match, 1)))
356 speed = strtoul(mstr, NULL, 10);
358 if ((mstr = g_match_info_fetch(match, 2)))
359 databits = strtoul(mstr, NULL, 10);
361 if ((mstr = g_match_info_fetch(match, 3))) {
364 parity = SERIAL_PARITY_NONE;
367 parity = SERIAL_PARITY_EVEN;
370 parity = SERIAL_PARITY_ODD;
375 if ((mstr = g_match_info_fetch(match, 4)))
376 stopbits = strtoul(mstr, NULL, 10);
379 g_match_info_unref(match);
383 return serial_set_params(fd, speed, databits, parity, stopbits, 0);
388 SR_PRIV int serial_readline(int fd, char **buf, int *buflen,
395 start = g_get_monotonic_time();
400 len = maxlen - *buflen - 1;
403 len = serial_read(fd, *buf + *buflen, 1);
406 *(*buf + *buflen) = '\0';
407 if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') {
408 /* Strip LF and terminate. */
409 *(*buf + --*buflen) = '\0';
413 if (g_get_monotonic_time() - start > timeout_ms)
418 sr_dbg("Received %d: '%s'.", *buflen, *buf);