2 * This file is part of the libserialport project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de>
6 * Copyright (C) 2013 Martin Ling <martin-libserialport@earth.li>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
32 #include <sys/ioctl.h>
37 #include "serialport.h"
39 static int sp_validate_port(struct sp_port *port)
44 if (port->hdl == INVALID_HANDLE_VALUE)
53 #define CHECK_PORT() do { if (!sp_validate_port(port)) return SP_ERR_ARG; } while (0)
56 * Open the specified serial port.
58 * @param port Pointer to empty port structure allocated by caller.
59 * @param portname Name of port to open.
60 * @param flags Flags to use when opening the serial port. Possible flags
61 * are: SP_MODE_RDWR, SP_MODE_RDONLY, SP_MODE_NONBLOCK.
63 * @return SP_OK on success, SP_ERR_FAIL on failure,
64 * or SP_ERR_ARG if an invalid port or name is passed.
66 int sp_open(struct sp_port *port, char *portname, int flags)
74 port->name = portname;
77 DWORD desired_access = 0, flags_and_attributes = 0;
78 /* Map 'flags' to the OS-specific settings. */
79 desired_access |= GENERIC_READ;
80 flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
81 if (flags & SP_MODE_RDWR)
82 desired_access |= GENERIC_WRITE;
83 if (flags & SP_MODE_NONBLOCK)
84 flags_and_attributes |= FILE_FLAG_OVERLAPPED;
86 port->hdl = CreateFile(port->name, desired_access, 0, 0,
87 OPEN_EXISTING, flags_and_attributes, 0);
88 if (port->hdl == INVALID_HANDLE_VALUE)
92 /* Map 'flags' to the OS-specific settings. */
93 if (flags & SP_MODE_RDWR)
94 flags_local |= O_RDWR;
95 if (flags & SP_MODE_RDONLY)
96 flags_local |= O_RDONLY;
97 if (flags & SP_MODE_NONBLOCK)
98 flags_local |= O_NONBLOCK;
100 if ((port->fd = open(port->name, flags_local)) < 0)
108 * Close the specified serial port.
110 * @param port Pointer to port structure.
112 * @return SP_OK on success, SP_ERR_FAIL on failure,
113 * or SP_ERR_ARG if an invalid port is passed.
115 int sp_close(struct sp_port *port)
120 /* Returns non-zero upon success, 0 upon failure. */
121 if (CloseHandle(port->hdl) == 0)
124 /* Returns 0 upon success, -1 upon failure. */
125 if (close(port->fd) == -1)
133 * Flush serial port buffers.
135 * @param port Pointer to port structure.
137 * @return SP_OK on success, SP_ERR_FAIL on failure,
138 * or SP_ERR_ARG if an invalid port is passed.
140 int sp_flush(struct sp_port *port)
145 /* Returns non-zero upon success, 0 upon failure. */
146 if (PurgeComm(port->hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0)
149 /* Returns 0 upon success, -1 upon failure. */
150 if (tcflush(port->fd, TCIOFLUSH) < 0)
157 * Write a number of bytes to the specified serial port.
159 * @param port Pointer to port structure.
160 * @param buf Buffer containing the bytes to write.
161 * @param count Number of bytes to write.
163 * @return The number of bytes written, SP_ERR_FAIL on failure,
164 * or SP_ERR_ARG if an invalid port is passed.
166 int sp_write(struct sp_port *port, const void *buf, size_t count)
175 /* Returns non-zero upon success, 0 upon failure. */
176 if (WriteFile(port->hdl, buf, count, &written, NULL) == 0)
180 /* Returns the number of bytes written, or -1 upon failure. */
181 ssize_t written = write(port->fd, buf, count);
190 * Read a number of bytes from the specified serial port.
192 * @param port Pointer to port structure.
193 * @param buf Buffer where to store the bytes that are read.
194 * @param count The number of bytes to read.
196 * @return The number of bytes read, SP_ERR_FAIL on failure,
197 * or SP_ERR_ARG if an invalid port is passed.
199 int sp_read(struct sp_port *port, void *buf, size_t count)
207 DWORD bytes_read = 0;
208 /* Returns non-zero upon success, 0 upon failure. */
209 if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0)
214 /* Returns the number of bytes read, or -1 upon failure. */
215 if ((bytes_read = read(port->fd, buf, count)) < 0)
222 * Set serial parameters for the specified serial port.
224 * @param port Pointer to port structure.
225 * @param baudrate The baudrate to set.
226 * @param bits The number of data bits to use.
227 * @param parity The parity setting to use (0 = none, 1 = even, 2 = odd).
228 * @param stopbits The number of stop bits to use (1 or 2).
229 * @param flowcontrol The flow control settings to use (0 = none, 1 = RTS/CTS,
232 * @return The number of bytes read, SP_ERR_FAIL on failure,
233 * or SP_ERR_ARG if an invalid argument is passed.
235 int sp_set_params(struct sp_port *port, int baudrate,
236 int bits, int parity, int stopbits,
237 int flowcontrol, int rts, int dtr)
244 if (!GetCommState(port->hdl, &dcb))
249 * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
250 * have documented CBR_* macros.
253 dcb.BaudRate = CBR_110;
256 dcb.BaudRate = CBR_300;
259 dcb.BaudRate = CBR_600;
262 dcb.BaudRate = CBR_1200;
265 dcb.BaudRate = CBR_2400;
268 dcb.BaudRate = CBR_4800;
271 dcb.BaudRate = CBR_9600;
274 dcb.BaudRate = CBR_14400; /* Not available on Unix? */
277 dcb.BaudRate = CBR_19200;
280 dcb.BaudRate = CBR_38400;
283 dcb.BaudRate = CBR_57600;
286 dcb.BaudRate = CBR_115200;
289 dcb.BaudRate = CBR_128000; /* Not available on Unix? */
292 dcb.BaudRate = CBR_256000; /* Not available on Unix? */
299 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
301 dcb.StopBits = ONESTOPBIT;
304 dcb.StopBits = TWOSTOPBITS;
311 /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */
313 dcb.Parity = NOPARITY;
316 dcb.Parity = EVENPARITY;
319 dcb.Parity = ODDPARITY;
327 dcb.fRtsControl = RTS_CONTROL_ENABLE;
329 dcb.fRtsControl = RTS_CONTROL_DISABLE;
334 dcb.fDtrControl = DTR_CONTROL_ENABLE;
336 dcb.fDtrControl = DTR_CONTROL_DISABLE;
339 if (!SetCommState(port->hdl, &dcb))
346 if (tcgetattr(port->fd, &term) < 0)
404 #if !defined(__APPLE__) && !defined(__OpenBSD__)
413 if (cfsetospeed(&term, baud) < 0)
416 if (cfsetispeed(&term, baud) < 0)
419 term.c_cflag &= ~CSIZE;
431 term.c_cflag &= ~CSTOPB;
434 term.c_cflag &= ~CSTOPB;
437 term.c_cflag |= CSTOPB;
443 term.c_iflag &= ~(IXON | IXOFF);
444 term.c_cflag &= ~CRTSCTS;
445 switch (flowcontrol) {
447 /* No flow control. */
450 term.c_cflag |= CRTSCTS;
453 term.c_iflag |= IXON | IXOFF;
459 term.c_iflag &= ~IGNPAR;
460 term.c_cflag &= ~(PARODD | PARENB);
463 term.c_iflag |= IGNPAR;
466 term.c_cflag |= PARENB;
469 term.c_cflag |= PARENB | PARODD;
475 /* Turn off all serial port cooking. */
476 term.c_iflag &= ~(ISTRIP | INLCR | ICRNL);
477 term.c_oflag &= ~(ONLCR | OCRNL | ONOCR);
478 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
479 term.c_oflag &= ~OFILL;
482 /* Disable canonical mode, and don't echo input characters. */
483 term.c_lflag &= ~(ICANON | ECHO);
485 /* Write the configured settings. */
486 if (tcsetattr(port->fd, TCSADRAIN, &term) < 0)
490 controlbits = TIOCM_RTS;
491 if (ioctl(port->fd, rts ? TIOCMBIS : TIOCMBIC,
497 controlbits = TIOCM_DTR;
498 if (ioctl(port->fd, dtr ? TIOCMBIS : TIOCMBIC,
508 * Get error code for failed operation.
510 * In order to obtain the correct result, this function should be called
511 * straight after the failure, before executing any other system operations.
513 * @return The system's numeric code for the error that caused the last
516 int sp_last_error_code(void)
519 return GetLastError();
526 * Get error message for failed operation.
528 * In order to obtain the correct result, this function should be called
529 * straight after the failure, before executing other system operations.
531 * @return The system's message for the error that caused the last
532 * operation to fail. This string may be allocated by the function,
533 * and can be freed after use by calling sp_free_error_message.
535 char *sp_last_error_message(void)
539 DWORD error = GetLastError();
542 FORMAT_MESSAGE_ALLOCATE_BUFFER |
543 FORMAT_MESSAGE_FROM_SYSTEM |
544 FORMAT_MESSAGE_IGNORE_INSERTS,
547 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
553 return strerror(errno);
558 * Free error message.
560 * This function can be used to free a string returned by the
561 * sp_last_error_message function.
563 void sp_free_error_message(char *message)