scpi: Query current time just once, use microseconds for timeout

g_get_monotonic_time() returns current time in microseconds, use the same
granularity for storing the read timeout.
There is also no need to check the timeout if data has just been read.
This commit is contained in:
Stefan Brüns 2017-01-12 01:20:17 +01:00 committed by Uwe Hermann
parent 59b9c3290a
commit 37ef582d08
2 changed files with 12 additions and 11 deletions

View File

@ -94,7 +94,7 @@ struct sr_scpi_dev_inst {
int (*read_complete)(void *priv); int (*read_complete)(void *priv);
int (*close)(struct sr_scpi_dev_inst *scpi); int (*close)(struct sr_scpi_dev_inst *scpi);
void (*free)(void *priv); void (*free)(void *priv);
unsigned int read_timeout_ms; unsigned int read_timeout_us;
void *priv; void *priv;
/* Only used for quirk workarounds, notably the Rigol DS1000 series. */ /* Only used for quirk workarounds, notably the Rigol DS1000 series. */
uint64_t firmware_version; uint64_t firmware_version;

View File

@ -190,7 +190,7 @@ SR_PRIV struct sr_scpi_dev_inst *scpi_dev_inst_new(struct drv_context *drvc,
scpi = g_malloc(sizeof(*scpi)); scpi = g_malloc(sizeof(*scpi));
*scpi = *scpi_dev; *scpi = *scpi_dev;
scpi->priv = g_malloc0(scpi->priv_size); scpi->priv = g_malloc0(scpi->priv_size);
scpi->read_timeout_ms = 1000; scpi->read_timeout_us = 1000 * 1000;
params = g_strsplit(resource, "/", 0); params = g_strsplit(resource, "/", 0);
if (scpi->dev_inst_new(scpi->priv, drvc, resource, if (scpi->dev_inst_new(scpi->priv, drvc, resource,
params, serialcomm) != SR_OK) { params, serialcomm) != SR_OK) {
@ -417,8 +417,7 @@ SR_PRIV int sr_scpi_get_data(struct sr_scpi_dev_inst *scpi,
{ {
int len; int len;
GString *response; GString *response;
gint64 laststart; gint64 laststart, now;
unsigned int elapsed_ms;
unsigned int offset; unsigned int offset;
int space; int space;
@ -451,16 +450,18 @@ SR_PRIV int sr_scpi_get_data(struct sr_scpi_dev_inst *scpi,
if (len < 0) { if (len < 0) {
sr_err("Incompletely read SCPI response."); sr_err("Incompletely read SCPI response.");
return SR_ERR; return SR_ERR;
} else if (len > 0) { }
laststart = g_get_monotonic_time();
now = g_get_monotonic_time();
if (len > 0) {
laststart = now;
offset += len; offset += len;
g_string_set_size(response, offset); g_string_set_size(response, offset);
} } else if ((now - laststart) >= scpi->read_timeout_us) {
/* Quit reading after a period of time without receive data. */ /* Quit reading after a period of time without receiving data. */
elapsed_ms = (g_get_monotonic_time() - laststart) / 1000;
if (elapsed_ms >= scpi->read_timeout_ms) {
sr_err("Timed out waiting for SCPI response."); sr_err("Timed out waiting for SCPI response.");
return SR_ERR; return SR_ERR_TIMEOUT;
} }
} }