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>
35 // FIXME: Must be moved, or rather passed as function argument.
40 char *serial_port_glob[] = {
52 GSList *list_serial_ports(void)
59 ports = g_slist_append(ports, strdup("COM1"));
65 for (i = 0; serial_port_glob[i]; i++) {
66 if (glob(serial_port_glob[i], 0, NULL, &g))
68 for (j = 0; j < g.gl_pathc; j++)
69 ports = g_slist_append(ports, strdup(g.gl_pathv[j]));
77 int serial_open(const char *pathname, int flags)
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. */
88 return open(pathname, flags);
93 * Close the serial port.
94 * Returns 0 upon success, -1 upon failure.
96 int serial_close(int fd)
99 /* Returns non-zero upon success, 0 upon failure. */
100 return (CloseHandle(hdl) == 0) ? -1 : 0;
102 /* Returns 0 upon success, -1 upon failure. */
108 * Flush serial port buffers (if any).
109 * Returns 0 upon success, -1 upon failure.
111 int serial_flush(int fd)
114 /* Returns non-zero upon success, 0 upon failure. */
115 return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0;
117 /* Returns 0 upon success, -1 upon failure. */
118 return tcflush(fd, TCIOFLUSH);
123 * Write a number of bytes to the specified serial port.
124 * Returns the number of bytes written, or -1 upon failure.
126 int serial_write(int fd, const void *buf, size_t count)
132 /* Returns non-zero upon success, 0 upon failure. */
133 WriteFile(hdl, buf, count, &tmp, NULL);
135 /* Returns the number of bytes written, or -1 upon failure. */
136 return write(fd, buf, count);
141 * Read a number of bytes from the specified serial port.
142 * Returns the number of bytes read, or -1 upon failure.
144 int serial_read(int fd, void *buf, size_t count)
150 /* Returns non-zero upon success, 0 upon failure. */
151 return ReadFile(hdl, buf, count, &tmp, NULL);
153 /* Returns the number of bytes read, or -1 upon failure. */
154 return read(fd, buf, count);
158 void *serial_backup_params(int fd)
163 struct termios *term;
165 term = malloc(sizeof(struct termios));
172 void serial_restore_params(int fd, void *backup)
177 tcsetattr(fd, TCSADRAIN, (struct termios *)backup);
182 * Set serial parameters.
184 * flowcontrol: 1 = rts/cts, 2 = xon/xoff
185 * parity: 0 = none, 1 = even, 2 = odd
187 int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
193 if (!GetCommState(hdl, &dcb)) {
194 /* TODO: Error handling. */
198 /* TODO: Rename 'speed' to 'baudrate'. */
200 /* TODO: Support for higher baud rates. */
202 dcb.BaudRate = CBR_115200;
205 dcb.BaudRate = CBR_57600;
208 dcb.BaudRate = CBR_38400;
211 dcb.BaudRate = CBR_19200;
214 dcb.BaudRate = CBR_9600;
217 /* TODO: Error handling. */
221 dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
222 dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */
224 if (!SetCommState(hdl, &dcb)) {
225 /* TODO: Error handling. */
252 if (tcgetattr(fd, &term) < 0)
254 if (cfsetispeed(&term, baud) < 0)
257 term.c_cflag &= ~CSIZE;
269 term.c_cflag &= ~CSTOPB;
274 term.c_cflag |= CSTOPB;
279 term.c_cflag &= ~(IXON | IXOFF | CRTSCTS);
280 switch (flowcontrol) {
282 term.c_cflag |= IXON | IXOFF;
285 term.c_cflag |= CRTSCTS;
290 term.c_iflag &= ~IGNPAR;
291 term.c_cflag &= ~(PARODD | PARENB);
294 term.c_iflag |= IGNPAR;
297 term.c_cflag |= PARENB;
300 term.c_cflag |= PARENB | PARODD;
306 if (tcsetattr(fd, TCSADRAIN, &term) < 0)