X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fcommon%2Fserial.c;h=6cf43f987f6bdec63d9d4f86824f5f95d955c88e;hb=9116262931773877e4dd279336ebb2be8f182c05;hp=580bca7e1ee768f40cfc67438277584ba0b602fd;hpb=5ae35c29a7010cd018e43d04a809e1c60b7b72fa;p=libsigrok.git diff --git a/hardware/common/serial.c b/hardware/common/serial.c index 580bca7e..6cf43f98 100644 --- a/hardware/common/serial.c +++ b/hardware/common/serial.c @@ -278,14 +278,6 @@ SR_PRIV int serial_read(struct sr_serial_dev_inst *serial, void *buf, #else /* Returns the number of bytes read, or -1 upon failure. */ ret = read(serial->fd, buf, count); - if (ret < 0) - /* - * Should be sr_err(), but that would yield lots of - * "Resource temporarily unavailable" messages. - */ - sr_spew("Read error: %s (fd %d).", strerror(errno), serial->fd); - else - sr_spew("Read %d/%d bytes (fd %d).", ret, count, serial->fd); #endif return ret; @@ -325,8 +317,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 +376,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 +428,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; @@ -617,6 +647,7 @@ SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, speed = databits = parity = stopbits = 0; rts = dtr = -1; + sr_spew("Parsing parameters from \"%s\".", paramstr); reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL); if (g_regex_match(reg, paramstr, 0, &match)) { if ((mstr = g_match_info_fetch(match, 1))) @@ -680,11 +711,13 @@ SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, g_match_info_unref(match); g_regex_unref(reg); - if (speed) - return serial_set_params(serial, speed, databits, parity, stopbits, - 0, rts, dtr); - else + if (speed) { + return serial_set_params(serial, speed, databits, parity, + stopbits, 0, rts, dtr); + } else { + sr_dbg("Could not infer speed from parameter string."); return SR_ERR_ARG; + } } /** @@ -793,7 +826,7 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial, if (len > 0) { ibuf += len; } else if (len == 0) { - sr_spew("Error: Only read 0 bytes."); + /* No logging, already done in serial_read(). */ } else { /* Error reading byte, but continuing anyway. */ }