]> sigrok.org Git - libsigrok.git/commitdiff
serial: determine timeout from most recent set_params() values
authorGerhard Sittig <redacted>
Sun, 26 Mar 2017 15:56:36 +0000 (16:56 +0100)
committerUwe Hermann <redacted>
Sun, 2 Jun 2019 18:39:02 +0000 (20:39 +0200)
Store the most recent successfully applied set of parameters for serial
communication. Re-use these values as a fallback to calculate timeouts,
when the underlying transport fails to provide the current settings.

src/libsigrok-internal.h
src/serial.c

index 053ee4dfeadde7fad1d258fa48b57c447df09287..812cf3ceca11811aebe7569c6e14439375a7febb 100644 (file)
@@ -726,6 +726,12 @@ struct sr_serial_dev_inst {
        char *port;
        /** Comm params for serial_set_paramstr(). */
        char *serialcomm;
+       struct {
+               int bit_rate;
+               int data_bits;
+               int parity_bits;
+               int stop_bits;
+       } comm_params;
        /** libserialport port handle */
        struct sp_port *sp_data;
 };
index 37b6187525881a5a9c98e92ebf8aecaffd83cdb5..86009e9b917d32a87c7d47b66c868222aa469c46 100644 (file)
@@ -468,6 +468,15 @@ SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
                return SR_ERR;
        }
 
+       serial->comm_params.bit_rate = baudrate;
+       serial->comm_params.data_bits = bits;
+       serial->comm_params.parity_bits = parity ? 1 : 0;
+       serial->comm_params.stop_bits = stopbits;
+       sr_dbg("DBG: %s() rate %d, %d%s%d", __func__,
+                       baudrate, bits,
+                       (parity == 0) ? "n" : "x",
+                       stopbits);
+
        return SR_OK;
 }
 
@@ -980,6 +989,7 @@ SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes)
        if (sp_new_config(&config) < 0)
                return timeout_ms;
 
+       /* Get the bitrate and frame length. */
        bits = baud = 0;
        do {
                if (sp_get_config(port->sp_data, config) < 0)
@@ -997,7 +1007,14 @@ SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes)
                        break;
                baud = tmp;
        } while (FALSE);
+       if (!bits || !baud) {
+               baud = port->comm_params.bit_rate;
+               bits = 1 + port->comm_params.data_bits +
+                       port->comm_params.parity_bits +
+                       port->comm_params.stop_bits;
+       }
 
+       /* Derive the timeout. */
        if (bits && baud) {
                /* Throw in 10ms for misc OS overhead. */
                timeout_ms = 10;