2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 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>
34 #include "sigrok-internal.h"
36 // FIXME: Must be moved, or rather passed as function argument.
41 const char *serial_port_glob[] = {
53 GSList *list_serial_ports(void)
60 ports = g_slist_append(ports, 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, strdup(g.gl_pathv[j]));
78 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 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 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 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 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 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 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 int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
199 if (!GetCommState(hdl, &dcb)) {
200 /* TODO: Error handling. */
204 /* TODO: Rename 'speed' to 'baudrate'. */
206 /* TODO: Support for higher baud rates. */
208 dcb.BaudRate = CBR_115200;
211 dcb.BaudRate = CBR_57600;
214 dcb.BaudRate = CBR_38400;
217 dcb.BaudRate = CBR_19200;
220 dcb.BaudRate = CBR_9600;
223 /* TODO: Error handling. */
227 dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
228 dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */
230 if (!SetCommState(hdl, &dcb)) {
231 /* TODO: Error handling. */
260 if (tcgetattr(fd, &term) < 0)
262 if (cfsetispeed(&term, baud) < 0)
265 term.c_cflag &= ~CSIZE;
277 term.c_cflag &= ~CSTOPB;
282 term.c_cflag |= CSTOPB;
287 term.c_cflag &= ~(IXON | IXOFF | CRTSCTS);
288 switch (flowcontrol) {
290 term.c_cflag |= IXON | IXOFF;
293 term.c_cflag |= CRTSCTS;
298 term.c_iflag &= ~IGNPAR;
299 term.c_cflag &= ~(PARODD | PARENB);
302 term.c_iflag |= IGNPAR;
305 term.c_cflag |= PARENB;
308 term.c_cflag |= PARENB | PARODD;
314 if (tcsetattr(fd, TCSADRAIN, &term) < 0)