From: Uwe Hermann Date: Sun, 2 Dec 2012 13:10:27 +0000 (+0100) Subject: serial: Don't hardcode parity and stop bits on Windows. X-Git-Tag: dsupstream~519 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=5ddb0cc7d4850375a9ef741012d229ead9a91490;p=libsigrok.git serial: Don't hardcode parity and stop bits on Windows. --- diff --git a/hardware/common/serial.c b/hardware/common/serial.c index 580bca7e..a18ad0b4 100644 --- a/hardware/common/serial.c +++ b/hardware/common/serial.c @@ -325,8 +325,11 @@ SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate, #ifdef _WIN32 DCB dcb; - if (!GetCommState(hdl, &dcb)) + if (!GetCommState(hdl, &dcb)) { + sr_err("Failed to get comm state on port %s (fd %d): %d.", + serial->port, serial->fd, GetLastError()); return SR_ERR; + } switch (baudrate) { /* @@ -381,9 +384,41 @@ SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate, } sr_spew("Configuring baudrate to %d (%d).", baudrate, dcb.BaudRate); + sr_spew("Configuring %d data bits.", bits); dcb.ByteSize = bits; - dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */ - dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */ + + sr_spew("Configuring %d stop bits.", stopbits); + switch (stopbits) { + /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */ + case 1: + dcb.StopBits = ONESTOPBIT; + break; + case 2: + dcb.StopBits = TWOSTOPBITS; + break; + default: + sr_err("Unsupported stopbits number: %d.", stopbits); + return SR_ERR; + } + + switch (parity) { + /* Note: There's also SPACEPARITY, MARKPARITY (unneeded so far). */ + case SERIAL_PARITY_NONE: + sr_spew("Configuring no parity."); + dcb.Parity = NOPARITY; + break; + case SERIAL_PARITY_EVEN: + sr_spew("Configuring even parity."); + dcb.Parity = EVENPARITY; + break; + case SERIAL_PARITY_ODD: + sr_spew("Configuring odd parity."); + dcb.Parity = ODDPARITY; + break; + default: + sr_err("Unsupported parity setting: %d.", parity); + return SR_ERR; + } if (rts != -1) { sr_spew("Setting RTS %s.", rts ? "high" : "low"); @@ -401,8 +436,11 @@ SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate, dcb.fDtrControl = DTR_CONTROL_DISABLE; } - if (!SetCommState(hdl, &dcb)) + if (!SetCommState(hdl, &dcb)) { + sr_err("Failed to set comm state on port %s (fd %d): %d.", + serial->port, serial->fd, GetLastError()); return SR_ERR; + } #else struct termios term; speed_t baud;