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>
35 #include <sys/ioctl.h>
38 #include <IOKit/IOKitLib.h>
39 #include <IOKit/serial/IOSerialKeys.h>
40 #include <sys/syslimits.h>
44 #include "linux/serial.h"
47 #include "libserialport.h"
58 /* Standard baud rates. */
60 #define BAUD_TYPE DWORD
61 #define BAUD(n) {CBR_##n, n}
63 #define BAUD_TYPE speed_t
64 #define BAUD(n) {B##n, n}
72 const struct std_baudrate std_baudrates[] = {
75 * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
76 * have documented CBR_* macros.
78 BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
79 BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
80 BAUD(115200), BAUD(128000), BAUD(256000),
82 BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
83 BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
84 BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
86 #if !defined(__APPLE__) && !defined(__OpenBSD__)
92 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
93 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
95 /* Helper functions. */
96 static enum sp_return validate_port(struct sp_port *port);
97 static struct sp_port **list_append(struct sp_port **list, const char *portname);
98 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
99 struct sp_port_config *config);
100 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
101 const struct sp_port_config *config);
103 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
105 struct sp_port *port;
116 if (!(port = malloc(sizeof(struct sp_port))))
119 len = strlen(portname) + 1;
121 if (!(port->name = malloc(len))) {
126 memcpy(port->name, portname, len);
129 port->hdl = INVALID_HANDLE_VALUE;
139 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
146 if (!port || !port->name)
149 return sp_get_port_by_name(port->name, copy_ptr);
152 void sp_free_port(struct sp_port *port)
163 static struct sp_port **list_append(struct sp_port **list, const char *portname)
168 for (count = 0; list[count]; count++);
169 if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
172 if (sp_get_port_by_name(portname, &list[count]) != SP_OK)
174 list[count + 1] = NULL;
178 sp_free_port_list(list);
182 enum sp_return sp_list_ports(struct sp_port ***list_ptr)
184 struct sp_port **list;
187 if (!(list = malloc(sizeof(struct sp_port **))))
195 DWORD max_value_len, max_data_size, max_data_len;
196 DWORD value_len, data_size, data_len;
197 DWORD type, index = 0;
201 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
202 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
206 if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
207 &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
211 max_data_len = max_data_size / sizeof(TCHAR);
212 if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
216 if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
221 value_len = max_value_len + 1,
222 data_size = max_data_size,
223 RegEnumValue(key, index, value, &value_len,
224 NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
226 data_len = data_size / sizeof(TCHAR);
227 data[data_len] = '\0';
229 name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
231 name_len = data_len + 1;
233 if (!(name = malloc(name_len))) {
238 WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
242 if (type == REG_SZ && !(list = list_append(list, name))) {
258 CFMutableDictionaryRef classes;
265 if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
270 if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
275 CFDictionarySetValue(classes,
276 CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
278 if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
283 if (!(path = malloc(PATH_MAX))) {
288 while ((port = IOIteratorNext(iter))) {
289 cf_path = IORegistryEntryCreateCFProperty(port,
290 CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
292 result = CFStringGetCString(cf_path,
293 path, PATH_MAX, kCFStringEncodingASCII);
295 if (result && !(list = list_append(list, path))) {
297 IOObjectRelease(port);
301 IOObjectRelease(port);
306 IOObjectRelease(iter);
311 struct udev_enumerate *ud_enumerate;
312 struct udev_list_entry *ud_list;
313 struct udev_list_entry *ud_entry;
315 struct udev_device *ud_dev, *ud_parent;
318 int fd, ioctl_result;
319 struct serial_struct serial_info;
322 ud_enumerate = udev_enumerate_new(ud);
323 udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
324 udev_enumerate_scan_devices(ud_enumerate);
325 ud_list = udev_enumerate_get_list_entry(ud_enumerate);
326 udev_list_entry_foreach(ud_entry, ud_list) {
327 path = udev_list_entry_get_name(ud_entry);
328 ud_dev = udev_device_new_from_syspath(ud, path);
329 /* If there is no parent device, this is a virtual tty. */
330 ud_parent = udev_device_get_parent(ud_dev);
331 if (ud_parent == NULL) {
332 udev_device_unref(ud_dev);
335 name = udev_device_get_devnode(ud_dev);
336 /* The serial8250 driver has a hardcoded number of ports.
337 * The only way to tell which actually exist on a given system
338 * is to try to open them and make an ioctl call. */
339 driver = udev_device_get_driver(ud_parent);
340 if (driver && !strcmp(driver, "serial8250")) {
341 if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0)
343 ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
345 if (ioctl_result != 0)
347 if (serial_info.type == PORT_UNKNOWN)
350 list = list_append(list, name);
352 udev_device_unref(ud_dev);
359 udev_enumerate_unref(ud_enumerate);
367 sp_free_port_list(list);
374 void sp_free_port_list(struct sp_port **list)
378 for (i = 0; list[i]; i++)
379 sp_free_port(list[i]);
383 static enum sp_return validate_port(struct sp_port *port)
388 if (port->hdl == INVALID_HANDLE_VALUE)
397 #define CHECK_PORT() do { if (!validate_port(port)) return SP_ERR_ARG; } while (0)
399 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
405 DWORD desired_access = 0, flags_and_attributes = 0;
406 char *escaped_port_name;
408 /* Prefix port name with '\\.\' to work with ports above COM9. */
409 if (!(escaped_port_name = malloc(strlen(port->name + 5))))
411 sprintf(escaped_port_name, "\\\\.\\%s", port->name);
413 /* Map 'flags' to the OS-specific settings. */
414 desired_access |= GENERIC_READ;
415 flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
416 if (flags & SP_MODE_RDWR)
417 desired_access |= GENERIC_WRITE;
418 if (flags & SP_MODE_NONBLOCK)
419 flags_and_attributes |= FILE_FLAG_OVERLAPPED;
421 port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0,
422 OPEN_EXISTING, flags_and_attributes, 0);
424 free(escaped_port_name);
426 if (port->hdl == INVALID_HANDLE_VALUE)
430 struct port_data data;
431 struct sp_port_config config;
434 /* Map 'flags' to the OS-specific settings. */
435 if (flags & SP_MODE_RDWR)
436 flags_local |= O_RDWR;
437 if (flags & SP_MODE_RDONLY)
438 flags_local |= O_RDONLY;
439 if (flags & SP_MODE_NONBLOCK)
440 flags_local |= O_NONBLOCK;
442 if ((port->fd = open(port->name, flags_local)) < 0)
445 ret = get_config(port, &data, &config);
452 /* Turn off all serial port cooking. */
453 data.term.c_iflag &= ~(ISTRIP | INLCR | ICRNL);
454 data.term.c_oflag &= ~(ONLCR | OCRNL | ONOCR);
455 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
456 data.term.c_oflag &= ~OFILL;
458 /* Disable canonical mode, and don't echo input characters. */
459 data.term.c_lflag &= ~(ICANON | ECHO);
461 /* Ignore modem status lines; enable receiver */
462 data.term.c_cflag |= (CLOCAL | CREAD);
464 ret = set_config(port, &data, &config);
475 enum sp_return sp_close(struct sp_port *port)
480 /* Returns non-zero upon success, 0 upon failure. */
481 if (CloseHandle(port->hdl) == 0)
483 port->hdl = INVALID_HANDLE_VALUE;
485 /* Returns 0 upon success, -1 upon failure. */
486 if (close(port->fd) == -1)
494 enum sp_return sp_flush(struct sp_port *port)
499 /* Returns non-zero upon success, 0 upon failure. */
500 if (PurgeComm(port->hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0)
503 /* Returns 0 upon success, -1 upon failure. */
504 if (tcflush(port->fd, TCIOFLUSH) < 0)
510 enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count)
520 /* Returns non-zero upon success, 0 upon failure. */
521 if (WriteFile(port->hdl, buf, count, &written, NULL) == 0)
525 /* Returns the number of bytes written, or -1 upon failure. */
526 ssize_t written = write(port->fd, buf, count);
535 enum sp_return sp_read(struct sp_port *port, void *buf, size_t count)
543 DWORD bytes_read = 0;
545 /* Returns non-zero upon success, 0 upon failure. */
546 if (ReadFile(port->hdl, buf, count, &bytes_read, NULL) == 0)
552 /* Returns the number of bytes read, or -1 upon failure. */
553 if ((bytes_read = read(port->fd, buf, count)) < 0)
559 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
560 struct sp_port_config *config)
565 if (!GetCommState(port->hdl, &data->dcb))
568 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
569 if (data->dcb.BaudRate == std_baudrates[i].index) {
570 config->baudrate = std_baudrates[i].value;
575 if (i == NUM_STD_BAUDRATES)
576 /* BaudRate field can be either an index or a custom baud rate. */
577 config->baudrate = data->dcb.BaudRate;
579 config->bits = data->dcb.ByteSize;
581 if (data->dcb.fParity)
582 switch (data->dcb.Parity) {
584 config->parity = SP_PARITY_NONE;
587 config->parity = SP_PARITY_EVEN;
590 config->parity = SP_PARITY_ODD;
596 config->parity = SP_PARITY_NONE;
598 switch (data->dcb.StopBits) {
600 config->stopbits = 1;
603 config->stopbits = 2;
606 config->stopbits = -1;
609 switch (data->dcb.fRtsControl) {
610 case RTS_CONTROL_DISABLE:
611 config->rts = SP_RTS_OFF;
613 case RTS_CONTROL_ENABLE:
614 config->rts = SP_RTS_ON;
616 case RTS_CONTROL_HANDSHAKE:
617 config->rts = SP_RTS_FLOW_CONTROL;
623 config->cts = data->dcb.fOutxCtsFlow ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
625 switch (data->dcb.fDtrControl) {
626 case DTR_CONTROL_DISABLE:
627 config->dtr = SP_DTR_OFF;
629 case DTR_CONTROL_ENABLE:
630 config->dtr = SP_DTR_ON;
632 case DTR_CONTROL_HANDSHAKE:
633 config->dtr = SP_DTR_FLOW_CONTROL;
639 config->dsr = data->dcb.fOutxDsrFlow ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
641 if (data->dcb.fInX) {
643 config->xon_xoff = SP_XONXOFF_INOUT;
645 config->xon_xoff = SP_XONXOFF_IN;
648 config->xon_xoff = SP_XONXOFF_OUT;
650 config->xon_xoff = SP_XONXOFF_DISABLED;
655 if (tcgetattr(port->fd, &data->term) < 0)
658 if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
660 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
661 if (cfgetispeed(&data->term) == std_baudrates[i].index) {
662 config->baudrate = std_baudrates[i].value;
667 if (i == NUM_STD_BAUDRATES)
668 config->baudrate = -1;
670 switch (data->term.c_cflag & CSIZE) {
687 if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR))
688 config->parity = SP_PARITY_NONE;
689 else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
692 config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
694 config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1;
696 if (data->term.c_cflag & CRTSCTS) {
697 config->rts = SP_RTS_FLOW_CONTROL;
698 config->cts = SP_CTS_FLOW_CONTROL;
700 config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
701 config->cts = SP_CTS_IGNORE;
704 config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
705 config->dsr = SP_DSR_IGNORE;
707 if (data->term.c_iflag & IXOFF) {
708 if (data->term.c_iflag & IXON)
709 config->xon_xoff = SP_XONXOFF_INOUT;
711 config->xon_xoff = SP_XONXOFF_IN;
713 if (data->term.c_iflag & IXON)
714 config->xon_xoff = SP_XONXOFF_OUT;
716 config->xon_xoff = SP_XONXOFF_DISABLED;
723 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
724 const struct sp_port_config *config)
729 if (config->baudrate >= 0) {
730 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
731 if (config->baudrate == std_baudrates[i].value) {
732 data->dcb.BaudRate = std_baudrates[i].index;
737 if (i == NUM_STD_BAUDRATES)
738 data->dcb.BaudRate = config->baudrate;
741 if (config->bits >= 0)
742 data->dcb.ByteSize = config->bits;
744 if (config->parity >= 0) {
745 switch (config->parity) {
746 /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */
748 data->dcb.Parity = NOPARITY;
751 data->dcb.Parity = EVENPARITY;
754 data->dcb.Parity = ODDPARITY;
761 if (config->stopbits >= 0) {
762 switch (config->stopbits) {
763 /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
765 data->dcb.StopBits = ONESTOPBIT;
768 data->dcb.StopBits = TWOSTOPBITS;
775 if (config->rts >= 0) {
776 switch (config->rts) {
778 data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
781 data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
783 case SP_RTS_FLOW_CONTROL:
784 data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
791 if (config->cts >= 0) {
792 switch (config->cts) {
794 data->dcb.fOutxCtsFlow = FALSE;
796 case SP_CTS_FLOW_CONTROL:
797 data->dcb.fOutxCtsFlow = TRUE;
804 if (config->dtr >= 0) {
805 switch (config->dtr) {
807 data->dcb.fDtrControl = DTR_CONTROL_DISABLE;
810 data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
812 case SP_DTR_FLOW_CONTROL:
813 data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
820 if (config->dsr >= 0) {
821 switch (config->dsr) {
823 data->dcb.fOutxDsrFlow = FALSE;
825 case SP_DSR_FLOW_CONTROL:
826 data->dcb.fOutxDsrFlow = TRUE;
833 if (config->xon_xoff >= 0) {
834 switch (config->xon_xoff) {
835 case SP_XONXOFF_DISABLED:
836 data->dcb.fInX = FALSE;
837 data->dcb.fOutX = FALSE;
840 data->dcb.fInX = TRUE;
841 data->dcb.fOutX = FALSE;
844 data->dcb.fInX = FALSE;
845 data->dcb.fOutX = TRUE;
847 case SP_XONXOFF_INOUT:
848 data->dcb.fInX = TRUE;
849 data->dcb.fOutX = TRUE;
856 if (!SetCommState(port->hdl, &data->dcb))
861 if (config->baudrate >= 0) {
862 for (i = 0; i < NUM_STD_BAUDRATES; i++) {
863 if (config->baudrate == std_baudrates[i].value) {
864 if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
867 if (cfsetispeed(&data->term, std_baudrates[i].index) < 0)
873 if (i == NUM_STD_BAUDRATES)
877 if (config->bits >= 0) {
878 data->term.c_cflag &= ~CSIZE;
879 switch (config->bits) {
881 data->term.c_cflag |= CS8;
884 data->term.c_cflag |= CS7;
887 data->term.c_cflag |= CS6;
890 data->term.c_cflag |= CS5;
897 if (config->parity >= 0) {
898 data->term.c_iflag &= ~IGNPAR;
899 data->term.c_cflag &= ~(PARENB | PARODD);
900 switch (config->parity) {
902 data->term.c_iflag |= IGNPAR;
905 data->term.c_cflag |= PARENB;
908 data->term.c_cflag |= PARENB | PARODD;
915 if (config->stopbits >= 0) {
916 data->term.c_cflag &= ~CSTOPB;
917 switch (config->stopbits) {
919 data->term.c_cflag &= ~CSTOPB;
922 data->term.c_cflag |= CSTOPB;
929 if (config->rts >= 0 || config->cts >= 0) {
930 /* Asymmetric use of RTS/CTS not supported yet. */
932 if (data->term.c_iflag & CRTSCTS) {
933 /* Flow control can only be disabled for both RTS & CTS together. */
934 if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
935 if (config->cts != SP_CTS_IGNORE)
938 if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
939 if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
943 /* Flow control can only be enabled for both RTS & CTS together. */
944 if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
945 ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
949 if (config->rts >= 0) {
950 if (config->rts == SP_RTS_FLOW_CONTROL) {
951 data->term.c_iflag |= CRTSCTS;
953 int controlbits = TIOCM_RTS;
954 if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
961 if (config->dtr >= 0 || config->dsr >= 0) {
962 /* DTR/DSR flow control not supported yet. */
963 if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
966 if (config->dtr >= 0) {
967 int controlbits = TIOCM_DTR;
968 if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
974 if (config->xon_xoff >= 0) {
975 data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
976 switch (config->xon_xoff) {
977 case SP_XONXOFF_DISABLED:
980 data->term.c_iflag |= IXOFF;
983 data->term.c_iflag |= IXON | IXANY;
985 case SP_XONXOFF_INOUT:
986 data->term.c_iflag |= IXON | IXOFF | IXANY;
993 if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
1000 #define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
1002 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
1004 struct port_data data;
1005 struct sp_port_config prev_config;
1012 TRY(get_config(port, &data, &prev_config));
1013 TRY(set_config(port, &data, config));
1018 #define CREATE_SETTER(x, type) int sp_set_##x(struct sp_port *port, type x) { \
1019 struct port_data data; \
1020 struct sp_port_config config; \
1022 TRY(get_config(port, &data, &config)); \
1024 TRY(set_config(port, &data, &config)); \
1028 CREATE_SETTER(baudrate, int)
1029 CREATE_SETTER(bits, int)
1030 CREATE_SETTER(parity, enum sp_parity)
1031 CREATE_SETTER(stopbits, int)
1032 CREATE_SETTER(rts, enum sp_rts)
1033 CREATE_SETTER(cts, enum sp_cts)
1034 CREATE_SETTER(dtr, enum sp_dtr)
1035 CREATE_SETTER(dsr, enum sp_dsr)
1036 CREATE_SETTER(xon_xoff, enum sp_xonxoff)
1038 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
1040 struct port_data data;
1041 struct sp_port_config config;
1045 TRY(get_config(port, &data, &config));
1047 if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
1048 config.xon_xoff = SP_XONXOFF_INOUT;
1050 config.xon_xoff = SP_XONXOFF_DISABLED;
1052 if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
1053 config.rts = SP_RTS_FLOW_CONTROL;
1054 config.cts = SP_CTS_FLOW_CONTROL;
1056 if (config.rts == SP_RTS_FLOW_CONTROL)
1057 config.rts = SP_RTS_ON;
1058 config.cts = SP_CTS_IGNORE;
1061 if (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
1062 config.dtr = SP_DTR_FLOW_CONTROL;
1063 config.dsr = SP_DSR_FLOW_CONTROL;
1065 if (config.dtr == SP_DTR_FLOW_CONTROL)
1066 config.dtr = SP_DTR_ON;
1067 config.dsr = SP_DSR_IGNORE;
1070 TRY(set_config(port, &data, &config));
1075 int sp_last_error_code(void)
1078 return GetLastError();
1084 char *sp_last_error_message(void)
1088 DWORD error = GetLastError();
1091 FORMAT_MESSAGE_ALLOCATE_BUFFER |
1092 FORMAT_MESSAGE_FROM_SYSTEM |
1093 FORMAT_MESSAGE_IGNORE_INSERTS,
1096 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1102 return strerror(errno);
1106 void sp_free_error_message(char *message)