aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan BrĂ¼ns <stefan.bruens@rwth-aachen.de>2017-01-12 01:20:17 +0100
committerUwe Hermann <uwe@hermann-uwe.de>2017-01-21 15:08:21 +0100
commit37ef582d088190ffcf5f8a649ca622cfc52f289a (patch)
tree8fa383ed5468645b7bed1146cd2711fc6521fa4e
parent59b9c3290aef7dfdc58afd0a4db8b347e0878c91 (diff)
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.
-rw-r--r--src/scpi.h2
-rw-r--r--src/scpi/scpi.c21
2 files changed, 12 insertions, 11 deletions
diff --git a/src/scpi.h b/src/scpi.h
index e9e7084..1c2c20d 100644
--- a/src/scpi.h
+++ b/src/scpi.h
@@ -94,7 +94,7 @@ struct sr_scpi_dev_inst {
int (*read_complete)(void *priv);
int (*close)(struct sr_scpi_dev_inst *scpi);
void (*free)(void *priv);
- unsigned int read_timeout_ms;
+ unsigned int read_timeout_us;
void *priv;
/* Only used for quirk workarounds, notably the Rigol DS1000 series. */
uint64_t firmware_version;
diff --git a/src/scpi/scpi.c b/src/scpi/scpi.c
index adb3e75..f6865c3 100644
--- a/src/scpi/scpi.c
+++ b/src/scpi/scpi.c
@@ -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 = *scpi_dev;
scpi->priv = g_malloc0(scpi->priv_size);
- scpi->read_timeout_ms = 1000;
+ scpi->read_timeout_us = 1000 * 1000;
params = g_strsplit(resource, "/", 0);
if (scpi->dev_inst_new(scpi->priv, drvc, resource,
params, serialcomm) != SR_OK) {
@@ -417,8 +417,7 @@ SR_PRIV int sr_scpi_get_data(struct sr_scpi_dev_inst *scpi,
{
int len;
GString *response;
- gint64 laststart;
- unsigned int elapsed_ms;
+ gint64 laststart, now;
unsigned int offset;
int space;
@@ -451,16 +450,18 @@ SR_PRIV int sr_scpi_get_data(struct sr_scpi_dev_inst *scpi,
if (len < 0) {
sr_err("Incompletely read SCPI response.");
return SR_ERR;
- } else if (len > 0) {
- laststart = g_get_monotonic_time();
+ }
+
+ now = g_get_monotonic_time();
+
+ if (len > 0) {
+ laststart = now;
offset += len;
g_string_set_size(response, offset);
- }
- /* Quit reading after a period of time without receive data. */
- elapsed_ms = (g_get_monotonic_time() - laststart) / 1000;
- if (elapsed_ms >= scpi->read_timeout_ms) {
+ } else if ((now - laststart) >= scpi->read_timeout_us) {
+ /* Quit reading after a period of time without receiving data. */
sr_err("Timed out waiting for SCPI response.");
- return SR_ERR;
+ return SR_ERR_TIMEOUT;
}
}