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 term = malloc(sizeof(struct termios));
173 void serial_restore_params(int fd, void *backup)
178 tcsetattr(fd, TCSADRAIN, (struct termios *)backup);
183 * Set serial parameters.
185 * flowcontrol: 1 = rts/cts, 2 = xon/xoff
186 * parity: 0 = none, 1 = even, 2 = odd
188 int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
194 if (!GetCommState(hdl, &dcb)) {
195 /* TODO: Error handling. */
199 /* TODO: Rename 'speed' to 'baudrate'. */
201 /* TODO: Support for higher baud rates. */
203 dcb.BaudRate = CBR_115200;
206 dcb.BaudRate = CBR_57600;
209 dcb.BaudRate = CBR_38400;
212 dcb.BaudRate = CBR_19200;
215 dcb.BaudRate = CBR_9600;
218 /* TODO: Error handling. */
222 dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
223 dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */
225 if (!SetCommState(hdl, &dcb)) {
226 /* TODO: Error handling. */
255 if (tcgetattr(fd, &term) < 0)
257 if (cfsetispeed(&term, baud) < 0)
260 term.c_cflag &= ~CSIZE;
272 term.c_cflag &= ~CSTOPB;
277 term.c_cflag |= CSTOPB;
282 term.c_cflag &= ~(IXON | IXOFF | CRTSCTS);
283 switch (flowcontrol) {
285 term.c_cflag |= IXON | IXOFF;
288 term.c_cflag |= CRTSCTS;
293 term.c_iflag &= ~IGNPAR;
294 term.c_cflag &= ~(PARODD | PARENB);
297 term.c_iflag |= IGNPAR;
300 term.c_cflag |= PARENB;
303 term.c_cflag |= PARENB | PARODD;
309 if (tcsetattr(fd, TCSADRAIN, &term) < 0)