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) {
{
int len;
GString *response;
- gint64 laststart;
- unsigned int elapsed_ms;
+ gint64 laststart, now;
unsigned int offset;
int space;
+ /* Optionally send caller provided command. */
if (command) {
if (sr_scpi_send(scpi, command) != SR_OK)
return SR_ERR;
}
+ /* Initiate SCPI read operation. */
if (sr_scpi_read_begin(scpi) != SR_OK)
return SR_ERR;
+ /* Keep reading until completion or until timeout. */
laststart = g_get_monotonic_time();
response = *scpi_response;
-
offset = response->len;
while (!sr_scpi_read_complete(scpi)) {
+ /* Resize the buffer when free space drops below a threshold. */
space = response->allocated_len - response->len;
if (space < 128) {
g_string_set_size(response, response->len + 1024);
+ g_string_set_size(response, offset);
space = response->allocated_len - response->len;
}
+ /* Read another chunk of the response. */
len = sr_scpi_read_data(scpi, &response->str[offset], space);
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);
- }
- 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;
}
}