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);
157 SR_PRIV const char *scpi_dmm_get_range_text(const struct sr_dev_inst *sdi)
159 struct dev_context *devc;
161 const struct mqopt_item *mqitem;
163 char *response, *pos;
169 ret = scpi_dmm_get_mq(sdi, NULL, NULL, NULL, &mqitem);
172 if (!mqitem || !mqitem->scpi_func_setup)
174 if (mqitem->drv_flags & FLAG_NO_RANGE)
177 scpi_dmm_cmd_delay(sdi->conn);
178 ret = sr_scpi_cmd(sdi, devc->cmdset, 0, NULL,
179 DMM_CMD_QUERY_RANGE_AUTO, mqitem->scpi_func_setup);
182 ret = sr_scpi_get_bool(sdi->conn, NULL, &is_auto);
189 * Get the response into a text buffer. The range value may be
190 * followed by a precision value separated by comma. Common text
191 * to number conversion support code may assume that the input
192 * text spans to the end of the text, need not accept trailing
193 * text which is not part of a number.
195 scpi_dmm_cmd_delay(sdi->conn);
196 ret = sr_scpi_cmd(sdi, devc->cmdset, 0, NULL,
197 DMM_CMD_QUERY_RANGE, mqitem->scpi_func_setup);
201 ret = sr_scpi_get_string(sdi->conn, NULL, &response);
206 pos = strchr(response, ',');
209 ret = sr_atod_ascii_digits(response, &range, &digits);
213 snprintf(devc->range_text, sizeof(devc->range_text), "%lf", range);
214 return devc->range_text;
217 SR_PRIV int scpi_dmm_set_range_from_text(const struct sr_dev_inst *sdi,
220 struct dev_context *devc;
222 const struct mqopt_item *item;
227 if (!range || !*range)
230 ret = scpi_dmm_get_mq(sdi, NULL, NULL, NULL, &item);
233 if (!item || !item->scpi_func_setup)
235 if (item->drv_flags & FLAG_NO_RANGE)
238 is_auto = g_ascii_strcasecmp(range, "auto") == 0;
239 scpi_dmm_cmd_delay(sdi->conn);
240 ret = sr_scpi_cmd(sdi, devc->cmdset, 0, NULL, DMM_CMD_SETUP_RANGE,
241 item->scpi_func_setup, is_auto ? "AUTO" : range);
248 SR_PRIV GVariant *scpi_dmm_get_range_text_list(const struct sr_dev_inst *sdi)
255 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
257 * Add more items _when_ the connected device supports a fixed
258 * or known set of ranges. The Agilent protocol is flexible and
259 * tolerant, set requests accept any value, and the device will
260 * use an upper limit which is at least the specified value.
261 * The values are communicated as mere numbers without units.
263 list = g_variant_builder_end(&gvb);
268 SR_PRIV int scpi_dmm_get_meas_agilent(const struct sr_dev_inst *sdi, size_t ch)
270 struct sr_scpi_dev_inst *scpi;
271 struct dev_context *devc;
272 struct scpi_dmm_acq_info *info;
273 struct sr_datafeed_analog *analog;
276 enum sr_mqflag mqflag;
282 const struct mqopt_item *item;
287 int sig_digits, val_exp;
294 info = &devc->run_acq_info;
295 analog = &info->analog[ch];
298 * Get the meter's current mode, keep the response around.
299 * Skip the measurement if the mode is uncertain.
301 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
303 g_free(mode_response);
309 g_free(mode_response);
314 * Get the last comma separated field of the function query
315 * response, or fallback to the model's default precision for
316 * the current function. This copes with either of these cases:
317 * VOLT +1.00000E-01,+1.00000E-06
319 * TEMP THER,5000,+1.00000E+00,+1.00000E-01
321 p = sr_scpi_unquote_string(mode_response);
322 fields = g_strsplit(p, ",", 0);
323 count = g_strv_length(fields);
325 snprintf(prec_text, sizeof(prec_text),
326 "%s", fields[count - 1]);
330 } else if (item->default_precision == NO_DFLT_PREC) {
333 snprintf(prec_text, sizeof(prec_text),
334 "1e%d", item->default_precision);
340 * Need to extract the exponent value ourselves, since a strtod()
341 * call will "eat" the exponent, too. Strip space, strip sign,
342 * strip float number (without! exponent), check for exponent
343 * and get exponent value. Accept absence of Esnn suffixes.
345 while (p && *p && g_ascii_isspace(*p))
347 if (p && *p && (*p == '+' || *p == '-'))
349 while (p && *p && g_ascii_isdigit(*p))
351 if (p && *p && *p == '.')
353 while (p && *p && g_ascii_isdigit(*p))
358 else if (*p != 'e' && *p != 'E')
361 ret = sr_atoi(++p, &prec_exp);
362 g_free(mode_response);
367 * Get the measurement value. Make sure to strip trailing space
368 * or else number conversion may fail in fatal ways. Detect OL
369 * conditions. Determine the measurement's precision: Count the
370 * number of significant digits before the period, and get the
373 * The text presentation of values is like this:
375 * Skip space/sign, count digits before the period, skip to the
376 * exponent, get exponent value.
378 * TODO Can sr_parse_rational() return the exponent for us? In
379 * addition to providing a precise rational value instead of a
380 * float that's an approximation of the received value? Can the
381 * 'analog' struct that we fill in carry rationals?
383 * Use double precision FP here during conversion. Optionally
384 * downgrade to single precision later to reduce the amount of
385 * logged information.
387 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
388 if (!command || !*command)
390 scpi_dmm_cmd_delay(scpi);
391 ret = sr_scpi_get_string(scpi, command, &response);
394 g_strstrip(response);
395 use_double = devc->model->digits > 6;
396 ret = sr_atod_ascii(response, &info->d_value);
404 if (info->d_value > +limit) {
405 info->d_value = +INFINITY;
406 } else if (info->d_value < -limit) {
407 info->d_value = -INFINITY;
410 while (p && *p && g_ascii_isspace(*p))
412 if (p && *p && (*p == '-' || *p == '+'))
415 while (p && *p && g_ascii_isdigit(*p)) {
419 if (p && *p && *p == '.')
421 while (p && *p && g_ascii_isdigit(*p))
426 else if (*p != 'e' && *p != 'E')
429 ret = sr_atoi(++p, &val_exp);
435 * TODO Come up with the most appropriate 'digits' calculation.
436 * This implementation assumes that either the device provides
437 * the resolution with the query for the meter's function, or
438 * the driver uses a fallback text pretending the device had
439 * provided it. This works with supported Agilent devices.
441 * An alternative may be to assume a given digits count which
442 * depends on the device, and adjust that count based on the
443 * value's significant digits and exponent. But this approach
444 * fails if devices change their digits count depending on
445 * modes or user requests, and also fails when e.g. devices
446 * with "100000 counts" can provide values between 100000 and
447 * 120000 in either 4 or 5 digits modes, depending on the most
448 * recent trend of the values. This less robust approach should
449 * only be taken if the mode inquiry won't yield the resolution
450 * (as e.g. DIOD does on 34405A, though we happen to know the
451 * fixed resolution for this very mode on this very model).
453 * For now, let's keep the prepared code path for the second
454 * approach in place, should some Agilent devices need it yet
455 * benefit from re-using most of the remaining acquisition
461 digits = devc->model->digits;
462 digits -= sig_digits;
467 * Fill in the 'analog' description: value, encoding, meaning.
468 * Callers will fill in the sample count, and channel name,
469 * and will send out the packet.
472 analog->data = &info->d_value;
473 analog->encoding->unitsize = sizeof(info->d_value);
475 info->f_value = info->d_value;
476 analog->data = &info->f_value;
477 analog->encoding->unitsize = sizeof(info->f_value);
479 analog->encoding->digits = digits;
480 analog->meaning->mq = mq;
481 analog->meaning->mqflags = mqflag;
487 unit = SR_UNIT_AMPERE;
489 case SR_MQ_RESISTANCE:
490 case SR_MQ_CONTINUITY:
493 case SR_MQ_CAPACITANCE:
494 unit = SR_UNIT_FARAD;
496 case SR_MQ_TEMPERATURE:
497 unit = SR_UNIT_CELSIUS;
499 case SR_MQ_FREQUENCY:
500 unit = SR_UNIT_HERTZ;
503 unit = SR_UNIT_SECOND;
508 analog->meaning->unit = unit;
509 analog->spec->spec_digits = digits;
514 SR_PRIV int scpi_dmm_get_meas_gwinstek(const struct sr_dev_inst *sdi, size_t ch)
516 struct sr_scpi_dev_inst *scpi;
517 struct dev_context *devc;
518 struct scpi_dmm_acq_info *info;
519 struct sr_datafeed_analog *analog;
522 enum sr_mqflag mqflag;
525 const struct mqopt_item *item;
530 int sig_digits, val_exp;
537 info = &devc->run_acq_info;
538 analog = &info->analog[ch];
541 * Get the meter's current mode, keep the response around.
542 * Skip the measurement if the mode is uncertain.
544 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
546 g_free(mode_response);
552 g_free(mode_response);
555 mmode = atoi(mode_response);
556 g_free(mode_response);
559 * Get the current reading from the meter.
561 scpi_dmm_cmd_delay(scpi);
562 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
563 if (!command || !*command)
565 scpi_dmm_cmd_delay(scpi);
566 ret = sr_scpi_get_string(scpi, command, &response);
569 g_strstrip(response);
570 use_double = devc->model->digits > 6;
571 ret = sr_atod_ascii(response, &info->d_value);
579 if (devc->model->infinity_limit != 0.0)
580 limit = devc->model->infinity_limit;
581 if (info->d_value >= +limit) {
582 info->d_value = +INFINITY;
583 } else if (info->d_value <= -limit) {
584 info->d_value = -INFINITY;
587 while (p && *p && g_ascii_isspace(*p))
589 if (p && *p && (*p == '-' || *p == '+'))
592 while (p && *p && g_ascii_isdigit(*p)) {
596 if (p && *p && *p == '.')
598 while (p && *p && g_ascii_isdigit(*p))
603 else if (*p != 'e' && *p != 'E')
606 ret = sr_atoi(++p, &val_exp);
613 * Make sure we report "INFINITY" when meter displays "0L".
618 /* In resitance modes 0L reads as 1.20000E8 or 1.99999E8. */
620 if (strcmp(devc->model->model, "GDM8255A") == 0)
622 if (info->d_value >= limit)
623 info->d_value = +INFINITY;
626 /* In continuity mode 0L reads as 1.20000E3. */
627 if (info->d_value >= 1.2e3)
628 info->d_value = +INFINITY;
631 /* In diode mode 0L reads as 1.00000E0. */
632 if (info->d_value == 1.0e0)
633 info->d_value = +INFINITY;
638 * Calculate 'digits' based on the result of the optional
639 * precision reading which was done at acquisition start.
640 * The GW-Instek manual gives the following information
641 * regarding the resolution:
649 digits = devc->model->digits;
650 if (devc->precision && *devc->precision) {
651 if (g_str_has_prefix(devc->precision, "Slow"))
653 else if (g_str_has_prefix(devc->precision, "Mid"))
655 else if (g_str_has_prefix(devc->precision, "Fast"))
658 sr_info("Unknown precision: '%s'", devc->precision);
662 * Fill in the 'analog' description: value, encoding, meaning.
663 * Callers will fill in the sample count, and channel name,
664 * and will send out the packet.
667 analog->data = &info->d_value;
668 analog->encoding->unitsize = sizeof(info->d_value);
670 info->f_value = info->d_value;
671 analog->data = &info->f_value;
672 analog->encoding->unitsize = sizeof(info->f_value);
674 analog->encoding->digits = digits;
675 analog->meaning->mq = mq;
676 analog->meaning->mqflags = mqflag;
682 unit = SR_UNIT_AMPERE;
684 case SR_MQ_RESISTANCE:
685 case SR_MQ_CONTINUITY:
688 case SR_MQ_CAPACITANCE:
689 unit = SR_UNIT_FARAD;
691 case SR_MQ_TEMPERATURE:
694 unit = SR_UNIT_FAHRENHEIT;
698 unit = SR_UNIT_CELSIUS;
701 case SR_MQ_FREQUENCY:
702 unit = SR_UNIT_HERTZ;
705 unit = SR_UNIT_SECOND;
710 analog->meaning->unit = unit;
711 analog->spec->spec_digits = digits;
716 /* Strictly speaking this is a timer controlled poll routine. */
717 SR_PRIV int scpi_dmm_receive_data(int fd, int revents, void *cb_data)
719 struct sr_dev_inst *sdi;
720 struct sr_scpi_dev_inst *scpi;
721 struct dev_context *devc;
722 struct scpi_dmm_acq_info *info;
723 gboolean sent_sample;
725 struct sr_channel *channel;
738 info = &devc->run_acq_info;
742 for (ch = 0; ch < devc->num_channels; ch++) {
743 /* Check the channel's enabled status. */
744 channel = g_slist_nth_data(sdi->channels, ch);
745 if (!channel->enabled)
749 * Prepare an analog measurement value. Note that digits
750 * will get updated later.
752 info->packet.type = SR_DF_ANALOG;
753 info->packet.payload = &info->analog[ch];
754 sr_analog_init(&info->analog[ch], &info->encoding[ch],
755 &info->meaning[ch], &info->spec[ch], 0);
757 /* Just check OPC before sending another request. */
758 scpi_dmm_cmd_delay(sdi->conn);
761 * Have the model take and interpret a measurement. Lack
762 * of support is pointless, failed retrieval/conversion
763 * is considered fatal. The routine will fill in the
764 * 'analog' details, except for channel name and sample
765 * count (assume one value per channel).
767 * Note that non-zero non-negative return codes signal
768 * that the channel's data shell get skipped in this
769 * iteration over the channels. This copes with devices
770 * or modes where channels may provide data at different
773 if (!devc->model->get_measurement) {
777 ret = devc->model->get_measurement(sdi, ch);
783 /* Send the packet that was filled in by the model's routine. */
784 info->analog[ch].num_samples = 1;
785 info->analog[ch].meaning->channels = g_slist_append(NULL, channel);
786 sr_session_send(sdi, &info->packet);
787 g_slist_free(info->analog[ch].meaning->channels);
791 sr_sw_limits_update_samples_read(&devc->limits, 1);
793 /* Stop acquisition upon communication or data errors. */
794 sr_dev_acquisition_stop(sdi);
797 if (sr_sw_limits_check(&devc->limits))
798 sr_dev_acquisition_stop(sdi);