]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/scpi-dmm/api.c
scpi-dmm: Gracefully handle meters that lack OPC command.
[libsigrok.git] / src / hardware / scpi-dmm / api.c
index cdd4d7cdf2c14dee75e50ca6f36089ac30c3c260..c6753860b866a072443e9221acd04179bd48ebe9 100644 (file)
@@ -33,6 +33,7 @@ static const uint32_t drvopts[] = {
 
 static const uint32_t devopts_generic[] = {
        SR_CONF_CONTINUOUS,
+       SR_CONF_CONN | SR_CONF_GET,
        SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
        SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
        SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
@@ -82,10 +83,10 @@ static const struct scpi_command cmdset_hp[] = {
 
 static const struct scpi_command cmdset_gwinstek[] = {
        { DMM_CMD_SETUP_REMOTE, "SYST:REM", },
+       { DMM_CMD_SETUP_LOCAL, "SYST:LOC", },
        { DMM_CMD_SETUP_FUNC, "CONF:%s", },
        { DMM_CMD_QUERY_FUNC, "CONF:STAT:FUNC?", },
        { DMM_CMD_START_ACQ, "*CLS;SYST:REM", },
-       { DMM_CMD_STOP_ACQ, "SYST:LOC", },
        { DMM_CMD_QUERY_VALUE, "VAL1?", },
        { DMM_CMD_QUERY_PREC, "SENS:DET:RATE?", },
        ALL_ZERO,
@@ -93,10 +94,11 @@ static const struct scpi_command cmdset_gwinstek[] = {
 
 static const struct scpi_command cmdset_gwinstek_906x[] = {
        { DMM_CMD_SETUP_REMOTE, "SYST:REM", },
+       { DMM_CMD_SETUP_LOCAL, "SYST:LOC", },
        { DMM_CMD_SETUP_FUNC, "CONF:%s", },
        { DMM_CMD_QUERY_FUNC, "CONF?", },
-       { DMM_CMD_START_ACQ, "*CLS;SYST:REM", },
-       { DMM_CMD_STOP_ACQ, "SYST:LOC", },
+       { DMM_CMD_START_ACQ, "INIT", },
+       { DMM_CMD_STOP_ACQ, "ABORT", },
        { DMM_CMD_QUERY_VALUE, "VAL1?", },
        { DMM_CMD_QUERY_PREC, "SENS:DET:RATE?", },
        ALL_ZERO,
@@ -166,57 +168,57 @@ SR_PRIV const struct scpi_dmm_model models[] = {
                1, 5, cmdset_agilent, ARRAY_AND_SIZE(mqopts_agilent_34405a),
                scpi_dmm_get_meas_agilent,
                ARRAY_AND_SIZE(devopts_generic),
-               0,
+               0, 0,
        },
        {
                "Agilent", "34410A",
                1, 6, cmdset_hp, ARRAY_AND_SIZE(mqopts_agilent_34405a),
                scpi_dmm_get_meas_agilent,
                ARRAY_AND_SIZE(devopts_generic),
-               0,
-       },
-       {
-               "Keysight", "34465A",
-               1, 5, cmdset_agilent, ARRAY_AND_SIZE(mqopts_agilent_34405a),
-               scpi_dmm_get_meas_agilent,
-               ARRAY_AND_SIZE(devopts_generic),
-               0,
-       },
-       {
-               "HP", "34401A",
-               1, 6, cmdset_hp, ARRAY_AND_SIZE(mqopts_agilent_34401a),
-               scpi_dmm_get_meas_agilent,
-               ARRAY_AND_SIZE(devopts_generic),
-               /* 34401A: typ. 1020ms for AC readings (default is 1000ms). */
-               1000 * 1500,
+               0, 0,
        },
        {
                "GW", "GDM8251A",
                1, 6, cmdset_gwinstek, ARRAY_AND_SIZE(mqopts_gwinstek_gdm8200a),
                scpi_dmm_get_meas_gwinstek,
                ARRAY_AND_SIZE(devopts_generic),
-               1000 * 2500,
+               1000 * 2500, 0,
        },
        {
                "GW", "GDM8255A",
                1, 6, cmdset_gwinstek, ARRAY_AND_SIZE(mqopts_gwinstek_gdm8200a),
                scpi_dmm_get_meas_gwinstek,
                ARRAY_AND_SIZE(devopts_generic),
-               1000 * 2500,
+               1000 * 2500, 0,
        },
        {
                "GWInstek", "GDM9060",
                1, 6, cmdset_gwinstek_906x, ARRAY_AND_SIZE(mqopts_gwinstek_gdm906x),
                scpi_dmm_get_meas_agilent,
                ARRAY_AND_SIZE(devopts_generic),
-               0,
+               0, 0,
        },
        {
                "GWInstek", "GDM9061",
                1, 6, cmdset_gwinstek_906x, ARRAY_AND_SIZE(mqopts_gwinstek_gdm906x),
                scpi_dmm_get_meas_agilent,
                ARRAY_AND_SIZE(devopts_generic),
-               0,
+               0, 0,
+       },
+       {
+               "HP", "34401A",
+               1, 6, cmdset_hp, ARRAY_AND_SIZE(mqopts_agilent_34401a),
+               scpi_dmm_get_meas_agilent,
+               ARRAY_AND_SIZE(devopts_generic),
+               /* 34401A: typ. 1020ms for AC readings (default is 1000ms). */
+               1000 * 1500, 0,
+       },
+       {
+               "Keysight", "34465A",
+               1, 5, cmdset_agilent, ARRAY_AND_SIZE(mqopts_agilent_34405a),
+               scpi_dmm_get_meas_agilent,
+               ARRAY_AND_SIZE(devopts_generic),
+               0, 0,
        },
 };
 
@@ -239,6 +241,25 @@ static const struct scpi_dmm_model *is_compatible(const char *vendor, const char
        return NULL;
 }
 
+/*
+ * Some devices (such as Owon XDM2041) do not support the standard
+ * OPeration Complete? command. This function tests the command with
+ * a short timeout, and returns TRUE if any reply (busy or not) is received.
+ */
+static gboolean probe_opc_support(struct sr_scpi_dev_inst *scpi)
+{
+       gboolean result;
+       GString *response;
+
+       response = g_string_sized_new(128);
+       result = TRUE;
+       if (sr_scpi_get_data(scpi, SCPI_CMD_OPC, &response) != SR_OK)
+               result = FALSE;
+       g_string_free(response, TRUE);
+
+       return result;
+}
+
 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
 {
        struct sr_scpi_hw_info *hw_info;
@@ -249,6 +270,10 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
        struct dev_context *devc;
        size_t i;
        gchar *channel_name;
+       const char *command;
+
+       if (!probe_opc_support(scpi))
+               scpi->no_opc_command = TRUE;
 
        scpi_dmm_cmd_delay(scpi);
        ret = sr_scpi_get_hw_id(scpi, &hw_info);
@@ -271,6 +296,11 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
        sdi->conn = scpi;
        sdi->driver = &scpi_dmm_driver_info;
        sdi->inst_type = SR_INST_SCPI;
+       ret = sr_scpi_connection_id(scpi, &sdi->connection_id);
+       if (ret != SR_OK) {
+               g_free(sdi->connection_id);
+               sdi->connection_id = NULL;
+       }
        sr_scpi_hw_info_free(hw_info);
        if (model->read_timeout_us)  /* non-default read timeout */
                scpi->read_timeout_us = model->read_timeout_us;
@@ -279,13 +309,22 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
        devc->num_channels = model->num_channels;
        devc->cmdset = model->cmdset;
        devc->model = model;
-       devc->precision = NULL;
 
        for (i = 0; i < devc->num_channels; i++) {
                channel_name = g_strdup_printf("P%zu", i + 1);
                sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, channel_name);
        }
 
+       /*
+        * If device has DMM_CMD_SETUP_LOCAL command, send it now. To avoid
+        * leaving device in remote mode (if only a "scan" is run).
+        */
+       command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_SETUP_LOCAL);
+       if (command && *command) {
+               scpi_dmm_cmd_delay(scpi);
+               sr_scpi_send(scpi, command);
+       }
+
        return sdi;
 }
 
@@ -311,8 +350,11 @@ static int dev_open(struct sr_dev_inst *sdi)
 
 static int dev_close(struct sr_dev_inst *sdi)
 {
+       struct dev_context *devc;
        struct sr_scpi_dev_inst *scpi;
+       const char *command;
 
+       devc = sdi->priv;
        scpi = sdi->conn;
        if (!scpi)
                return SR_ERR_BUG;
@@ -321,6 +363,16 @@ static int dev_close(struct sr_dev_inst *sdi)
        if (sdi->status <= SR_ST_INACTIVE)
                return SR_OK;
 
+       /*
+        * If device has DMM_CMD_SETUP_LOCAL command, send it now
+        * to avoid leaving device in remote mode.
+        */
+       command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_SETUP_LOCAL);
+       if (command && *command) {
+               scpi_dmm_cmd_delay(scpi);
+               sr_scpi_send(scpi, command);
+       }
+
        return sr_scpi_close(scpi);
 }
 
@@ -338,6 +390,11 @@ static int config_get(uint32_t key, GVariant **data,
        devc = sdi->priv;
 
        switch (key) {
+       case SR_CONF_CONN:
+               if (!sdi || !sdi->connection_id)
+                       return SR_ERR_NA;
+               *data = g_variant_new_string(sdi->connection_id);
+               return SR_OK;
        case SR_CONF_LIMIT_SAMPLES:
        case SR_CONF_LIMIT_MSEC:
                return sr_sw_limits_config_get(&devc->limits, key, data);
@@ -449,14 +506,13 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                ret = sr_scpi_get_string(scpi, command, &response);
                if (ret == SR_OK) {
                        g_strstrip(response);
-                       if (devc->precision)
-                               g_free(devc->precision);
-                       devc->precision=g_strdup(response);
+                       g_free(devc->precision);
+                       devc->precision = g_strdup(response);
                        g_free(response);
                        sr_dbg("%s: Precision: '%s'", __func__, devc->precision);
                } else {
-                       sr_info("%s: Precision query ('%s') failed: %d",
-                               __func__, command, ret);
+                       sr_info("Precision query ('%s') failed: %d",
+                               command, ret);
                }
        }
 
@@ -499,10 +555,8 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi)
 
        std_session_send_df_end(sdi);
 
-       if (devc->precision) {
-               g_free(devc->precision);
-               devc->precision = NULL;
-       }
+       g_free(devc->precision);
+       devc->precision = NULL;
 
        return SR_OK;
 }