2 * This file is part of the libsigrok project.
4 * Copyright (C) 2018 Gerhard Sittig <gerhard.sittig@gmx.net>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #define WITH_CMD_DELAY 0 /* TODO See which devices need delays. */
27 SR_PRIV void scpi_dmm_cmd_delay(struct sr_scpi_dev_inst *scpi)
30 g_usleep(WITH_CMD_DELAY * 1000);
32 if (!scpi->no_opc_command)
33 sr_scpi_get_opc(scpi);
36 SR_PRIV const struct mqopt_item *scpi_dmm_lookup_mq_number(
37 const struct sr_dev_inst *sdi, enum sr_mq mq, enum sr_mqflag flag)
39 struct dev_context *devc;
41 const struct mqopt_item *item;
44 for (i = 0; i < devc->model->mqopt_size; i++) {
45 item = &devc->model->mqopts[i];
46 if (item->mq != mq || item->mqflag != flag)
54 SR_PRIV const struct mqopt_item *scpi_dmm_lookup_mq_text(
55 const struct sr_dev_inst *sdi, const char *text)
57 struct dev_context *devc;
59 const struct mqopt_item *item;
62 for (i = 0; i < devc->model->mqopt_size; i++) {
63 item = &devc->model->mqopts[i];
64 if (!item->scpi_func_query || !item->scpi_func_query[0])
66 if (!g_str_has_prefix(text, item->scpi_func_query))
74 SR_PRIV int scpi_dmm_get_mq(const struct sr_dev_inst *sdi,
75 enum sr_mq *mq, enum sr_mqflag *flag, char **rsp,
76 const struct mqopt_item **mqitem)
78 struct dev_context *devc;
83 const struct mqopt_item *item;
95 scpi_dmm_cmd_delay(sdi->conn);
96 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_FUNC);
97 if (!command || !*command)
100 ret = sr_scpi_get_string(sdi->conn, command, &response);
103 if (!response || !*response) {
112 item = scpi_dmm_lookup_mq_text(sdi, have);
117 *flag = item->mqflag;
122 sr_warn("Unknown measurement quantity: %s", have);
134 SR_PRIV int scpi_dmm_set_mq(const struct sr_dev_inst *sdi,
135 enum sr_mq mq, enum sr_mqflag flag)
137 struct dev_context *devc;
138 const struct mqopt_item *item;
139 const char *mode, *command;
143 item = scpi_dmm_lookup_mq_number(sdi, mq, flag);
147 mode = item->scpi_func_setup;
148 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_SETUP_FUNC);
149 scpi_dmm_cmd_delay(sdi->conn);
150 ret = sr_scpi_send(sdi->conn, command, mode);
153 if (item->drv_flags & FLAG_CONF_DELAY)
154 g_usleep(devc->model->conf_delay_us);
159 SR_PRIV const char *scpi_dmm_get_range_text(const struct sr_dev_inst *sdi)
161 struct dev_context *devc;
163 const struct mqopt_item *mqitem;
165 char *response, *pos;
171 ret = scpi_dmm_get_mq(sdi, NULL, NULL, NULL, &mqitem);
174 if (!mqitem || !mqitem->scpi_func_setup)
176 if (mqitem->drv_flags & FLAG_NO_RANGE)
179 scpi_dmm_cmd_delay(sdi->conn);
180 ret = sr_scpi_cmd(sdi, devc->cmdset, 0, NULL,
181 DMM_CMD_QUERY_RANGE_AUTO, mqitem->scpi_func_setup);
184 ret = sr_scpi_get_bool(sdi->conn, NULL, &is_auto);
191 * Get the response into a text buffer. The range value may be
192 * followed by a precision value separated by comma. Common text
193 * to number conversion support code may assume that the input
194 * text spans to the end of the text, need not accept trailing
195 * text which is not part of a number.
197 scpi_dmm_cmd_delay(sdi->conn);
198 ret = sr_scpi_cmd(sdi, devc->cmdset, 0, NULL,
199 DMM_CMD_QUERY_RANGE, mqitem->scpi_func_setup);
203 ret = sr_scpi_get_string(sdi->conn, NULL, &response);
208 pos = strchr(response, ',');
211 ret = sr_atod_ascii_digits(response, &range, &digits);
215 snprintf(devc->range_text, sizeof(devc->range_text), "%lf", range);
216 return devc->range_text;
219 SR_PRIV int scpi_dmm_set_range_from_text(const struct sr_dev_inst *sdi,
222 struct dev_context *devc;
224 const struct mqopt_item *item;
229 if (!range || !*range)
232 ret = scpi_dmm_get_mq(sdi, NULL, NULL, NULL, &item);
235 if (!item || !item->scpi_func_setup)
237 if (item->drv_flags & FLAG_NO_RANGE)
240 is_auto = g_ascii_strcasecmp(range, "auto") == 0;
241 scpi_dmm_cmd_delay(sdi->conn);
242 ret = sr_scpi_cmd(sdi, devc->cmdset, 0, NULL, DMM_CMD_SETUP_RANGE,
243 item->scpi_func_setup, is_auto ? "AUTO" : range);
246 if (item->drv_flags & FLAG_CONF_DELAY)
247 g_usleep(devc->model->conf_delay_us);
252 SR_PRIV GVariant *scpi_dmm_get_range_text_list(const struct sr_dev_inst *sdi)
259 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
261 * Add more items _when_ the connected device supports a fixed
262 * or known set of ranges. The Agilent protocol is flexible and
263 * tolerant, set requests accept any value, and the device will
264 * use an upper limit which is at least the specified value.
265 * The values are communicated as mere numbers without units.
267 list = g_variant_builder_end(&gvb);
272 SR_PRIV int scpi_dmm_get_meas_agilent(const struct sr_dev_inst *sdi, size_t ch)
274 struct sr_scpi_dev_inst *scpi;
275 struct dev_context *devc;
276 struct scpi_dmm_acq_info *info;
277 struct sr_datafeed_analog *analog;
280 enum sr_mqflag mqflag;
286 const struct mqopt_item *item;
291 int sig_digits, val_exp;
298 info = &devc->run_acq_info;
299 analog = &info->analog[ch];
302 * Get the meter's current mode, keep the response around.
303 * Skip the measurement if the mode is uncertain.
305 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
307 g_free(mode_response);
313 g_free(mode_response);
318 * Get the last comma separated field of the function query
319 * response, or fallback to the model's default precision for
320 * the current function. This copes with either of these cases:
321 * VOLT +1.00000E-01,+1.00000E-06
323 * TEMP THER,5000,+1.00000E+00,+1.00000E-01
325 p = sr_scpi_unquote_string(mode_response);
326 fields = g_strsplit(p, ",", 0);
327 count = g_strv_length(fields);
329 snprintf(prec_text, sizeof(prec_text),
330 "%s", fields[count - 1]);
334 } else if (item->default_precision == NO_DFLT_PREC) {
337 snprintf(prec_text, sizeof(prec_text),
338 "1e%d", item->default_precision);
344 * Need to extract the exponent value ourselves, since a strtod()
345 * call will "eat" the exponent, too. Strip space, strip sign,
346 * strip float number (without! exponent), check for exponent
347 * and get exponent value. Accept absence of Esnn suffixes.
349 while (p && *p && g_ascii_isspace(*p))
351 if (p && *p && (*p == '+' || *p == '-'))
353 while (p && *p && g_ascii_isdigit(*p))
355 if (p && *p && *p == '.')
357 while (p && *p && g_ascii_isdigit(*p))
362 else if (*p != 'e' && *p != 'E')
365 ret = sr_atoi(++p, &prec_exp);
366 g_free(mode_response);
371 * Get the measurement value. Make sure to strip trailing space
372 * or else number conversion may fail in fatal ways. Detect OL
373 * conditions. Determine the measurement's precision: Count the
374 * number of significant digits before the period, and get the
377 * The text presentation of values is like this:
379 * Skip space/sign, count digits before the period, skip to the
380 * exponent, get exponent value.
382 * TODO Can sr_parse_rational() return the exponent for us? In
383 * addition to providing a precise rational value instead of a
384 * float that's an approximation of the received value? Can the
385 * 'analog' struct that we fill in carry rationals?
387 * Use double precision FP here during conversion. Optionally
388 * downgrade to single precision later to reduce the amount of
389 * logged information.
391 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
392 if (!command || !*command)
394 scpi_dmm_cmd_delay(scpi);
395 ret = sr_scpi_get_string(scpi, command, &response);
398 g_strstrip(response);
399 use_double = devc->model->digits > 6;
400 ret = sr_atod_ascii(response, &info->d_value);
408 if (info->d_value > +limit) {
409 info->d_value = +INFINITY;
410 } else if (info->d_value < -limit) {
411 info->d_value = -INFINITY;
414 while (p && *p && g_ascii_isspace(*p))
416 if (p && *p && (*p == '-' || *p == '+'))
419 while (p && *p && g_ascii_isdigit(*p)) {
423 if (p && *p && *p == '.')
425 while (p && *p && g_ascii_isdigit(*p))
430 else if (*p != 'e' && *p != 'E')
433 ret = sr_atoi(++p, &val_exp);
439 * TODO Come up with the most appropriate 'digits' calculation.
440 * This implementation assumes that either the device provides
441 * the resolution with the query for the meter's function, or
442 * the driver uses a fallback text pretending the device had
443 * provided it. This works with supported Agilent devices.
445 * An alternative may be to assume a given digits count which
446 * depends on the device, and adjust that count based on the
447 * value's significant digits and exponent. But this approach
448 * fails if devices change their digits count depending on
449 * modes or user requests, and also fails when e.g. devices
450 * with "100000 counts" can provide values between 100000 and
451 * 120000 in either 4 or 5 digits modes, depending on the most
452 * recent trend of the values. This less robust approach should
453 * only be taken if the mode inquiry won't yield the resolution
454 * (as e.g. DIOD does on 34405A, though we happen to know the
455 * fixed resolution for this very mode on this very model).
457 * For now, let's keep the prepared code path for the second
458 * approach in place, should some Agilent devices need it yet
459 * benefit from re-using most of the remaining acquisition
465 digits = devc->model->digits;
466 digits -= sig_digits;
471 * Fill in the 'analog' description: value, encoding, meaning.
472 * Callers will fill in the sample count, and channel name,
473 * and will send out the packet.
476 analog->data = &info->d_value;
477 analog->encoding->unitsize = sizeof(info->d_value);
479 info->f_value = info->d_value;
480 analog->data = &info->f_value;
481 analog->encoding->unitsize = sizeof(info->f_value);
483 analog->encoding->digits = digits;
484 analog->meaning->mq = mq;
485 analog->meaning->mqflags = mqflag;
491 unit = SR_UNIT_AMPERE;
493 case SR_MQ_RESISTANCE:
494 case SR_MQ_CONTINUITY:
497 case SR_MQ_CAPACITANCE:
498 unit = SR_UNIT_FARAD;
500 case SR_MQ_TEMPERATURE:
501 unit = SR_UNIT_CELSIUS;
503 case SR_MQ_FREQUENCY:
504 unit = SR_UNIT_HERTZ;
507 unit = SR_UNIT_SECOND;
512 analog->meaning->unit = unit;
513 analog->spec->spec_digits = digits;
518 SR_PRIV int scpi_dmm_get_meas_gwinstek(const struct sr_dev_inst *sdi, size_t ch)
520 struct sr_scpi_dev_inst *scpi;
521 struct dev_context *devc;
522 struct scpi_dmm_acq_info *info;
523 struct sr_datafeed_analog *analog;
526 enum sr_mqflag mqflag;
529 const struct mqopt_item *item;
534 int sig_digits, val_exp;
541 info = &devc->run_acq_info;
542 analog = &info->analog[ch];
545 * Get the meter's current mode, keep the response around.
546 * Skip the measurement if the mode is uncertain.
548 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
550 g_free(mode_response);
556 g_free(mode_response);
559 mmode = atoi(mode_response);
560 g_free(mode_response);
563 * Get the current reading from the meter.
565 scpi_dmm_cmd_delay(scpi);
566 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
567 if (!command || !*command)
569 scpi_dmm_cmd_delay(scpi);
570 ret = sr_scpi_get_string(scpi, command, &response);
573 g_strstrip(response);
574 use_double = devc->model->digits > 6;
575 ret = sr_atod_ascii(response, &info->d_value);
583 if (devc->model->infinity_limit != 0.0)
584 limit = devc->model->infinity_limit;
585 if (info->d_value >= +limit) {
586 info->d_value = +INFINITY;
587 } else if (info->d_value <= -limit) {
588 info->d_value = -INFINITY;
591 while (p && *p && g_ascii_isspace(*p))
593 if (p && *p && (*p == '-' || *p == '+'))
596 while (p && *p && g_ascii_isdigit(*p)) {
600 if (p && *p && *p == '.')
602 while (p && *p && g_ascii_isdigit(*p))
607 else if (*p != 'e' && *p != 'E')
610 ret = sr_atoi(++p, &val_exp);
617 * Make sure we report "INFINITY" when meter displays "0L".
622 /* In resitance modes 0L reads as 1.20000E8 or 1.99999E8. */
624 if (strcmp(devc->model->model, "GDM8255A") == 0)
626 if (info->d_value >= limit)
627 info->d_value = +INFINITY;
630 /* In continuity mode 0L reads as 1.20000E3. */
631 if (info->d_value >= 1.2e3)
632 info->d_value = +INFINITY;
635 /* In diode mode 0L reads as 1.00000E0. */
636 if (info->d_value == 1.0e0)
637 info->d_value = +INFINITY;
642 * Calculate 'digits' based on the result of the optional
643 * precision reading which was done at acquisition start.
644 * The GW-Instek manual gives the following information
645 * regarding the resolution:
653 digits = devc->model->digits;
654 if (devc->precision && *devc->precision) {
655 if (g_str_has_prefix(devc->precision, "Slow"))
657 else if (g_str_has_prefix(devc->precision, "Mid"))
659 else if (g_str_has_prefix(devc->precision, "Fast"))
662 sr_info("Unknown precision: '%s'", devc->precision);
666 * Fill in the 'analog' description: value, encoding, meaning.
667 * Callers will fill in the sample count, and channel name,
668 * and will send out the packet.
671 analog->data = &info->d_value;
672 analog->encoding->unitsize = sizeof(info->d_value);
674 info->f_value = info->d_value;
675 analog->data = &info->f_value;
676 analog->encoding->unitsize = sizeof(info->f_value);
678 analog->encoding->digits = digits;
679 analog->meaning->mq = mq;
680 analog->meaning->mqflags = mqflag;
686 unit = SR_UNIT_AMPERE;
688 case SR_MQ_RESISTANCE:
689 case SR_MQ_CONTINUITY:
692 case SR_MQ_CAPACITANCE:
693 unit = SR_UNIT_FARAD;
695 case SR_MQ_TEMPERATURE:
698 unit = SR_UNIT_FAHRENHEIT;
702 unit = SR_UNIT_CELSIUS;
705 case SR_MQ_FREQUENCY:
706 unit = SR_UNIT_HERTZ;
709 unit = SR_UNIT_SECOND;
714 analog->meaning->unit = unit;
715 analog->spec->spec_digits = digits;
720 /* Strictly speaking this is a timer controlled poll routine. */
721 SR_PRIV int scpi_dmm_receive_data(int fd, int revents, void *cb_data)
723 struct sr_dev_inst *sdi;
724 struct sr_scpi_dev_inst *scpi;
725 struct dev_context *devc;
726 struct scpi_dmm_acq_info *info;
727 gboolean sent_sample;
729 struct sr_channel *channel;
742 info = &devc->run_acq_info;
746 for (ch = 0; ch < devc->num_channels; ch++) {
747 /* Check the channel's enabled status. */
748 channel = g_slist_nth_data(sdi->channels, ch);
749 if (!channel->enabled)
753 * Prepare an analog measurement value. Note that digits
754 * will get updated later.
756 info->packet.type = SR_DF_ANALOG;
757 info->packet.payload = &info->analog[ch];
758 sr_analog_init(&info->analog[ch], &info->encoding[ch],
759 &info->meaning[ch], &info->spec[ch], 0);
761 /* Just check OPC before sending another request. */
762 scpi_dmm_cmd_delay(sdi->conn);
765 * Have the model take and interpret a measurement. Lack
766 * of support is pointless, failed retrieval/conversion
767 * is considered fatal. The routine will fill in the
768 * 'analog' details, except for channel name and sample
769 * count (assume one value per channel).
771 * Note that non-zero non-negative return codes signal
772 * that the channel's data shell get skipped in this
773 * iteration over the channels. This copes with devices
774 * or modes where channels may provide data at different
777 if (!devc->model->get_measurement) {
781 ret = devc->model->get_measurement(sdi, ch);
787 /* Send the packet that was filled in by the model's routine. */
788 info->analog[ch].num_samples = 1;
789 info->analog[ch].meaning->channels = g_slist_append(NULL, channel);
790 sr_session_send(sdi, &info->packet);
791 g_slist_free(info->analog[ch].meaning->channels);
795 sr_sw_limits_update_samples_read(&devc->limits, 1);
797 /* Stop acquisition upon communication or data errors. */
798 sr_dev_acquisition_stop(sdi);
801 if (sr_sw_limits_check(&devc->limits))
802 sr_dev_acquisition_stop(sdi);