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 int scpi_dmm_get_meas_agilent(const struct sr_dev_inst *sdi, size_t ch)
159 struct sr_scpi_dev_inst *scpi;
160 struct dev_context *devc;
161 struct scpi_dmm_acq_info *info;
162 struct sr_datafeed_analog *analog;
165 enum sr_mqflag mqflag;
171 const struct mqopt_item *item;
176 int sig_digits, val_exp;
183 info = &devc->run_acq_info;
184 analog = &info->analog[ch];
187 * Get the meter's current mode, keep the response around.
188 * Skip the measurement if the mode is uncertain.
190 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
192 g_free(mode_response);
198 g_free(mode_response);
203 * Get the last comma separated field of the function query
204 * response, or fallback to the model's default precision for
205 * the current function. This copes with either of these cases:
206 * VOLT +1.00000E-01,+1.00000E-06
208 * TEMP THER,5000,+1.00000E+00,+1.00000E-01
210 p = sr_scpi_unquote_string(mode_response);
211 fields = g_strsplit(p, ",", 0);
212 count = g_strv_length(fields);
214 snprintf(prec_text, sizeof(prec_text),
215 "%s", fields[count - 1]);
219 } else if (item->default_precision == NO_DFLT_PREC) {
222 snprintf(prec_text, sizeof(prec_text),
223 "1e%d", item->default_precision);
229 * Need to extract the exponent value ourselves, since a strtod()
230 * call will "eat" the exponent, too. Strip space, strip sign,
231 * strip float number (without! exponent), check for exponent
232 * and get exponent value. Accept absence of Esnn suffixes.
234 while (p && *p && g_ascii_isspace(*p))
236 if (p && *p && (*p == '+' || *p == '-'))
238 while (p && *p && g_ascii_isdigit(*p))
240 if (p && *p && *p == '.')
242 while (p && *p && g_ascii_isdigit(*p))
247 else if (*p != 'e' && *p != 'E')
250 ret = sr_atoi(++p, &prec_exp);
251 g_free(mode_response);
256 * Get the measurement value. Make sure to strip trailing space
257 * or else number conversion may fail in fatal ways. Detect OL
258 * conditions. Determine the measurement's precision: Count the
259 * number of significant digits before the period, and get the
262 * The text presentation of values is like this:
264 * Skip space/sign, count digits before the period, skip to the
265 * exponent, get exponent value.
267 * TODO Can sr_parse_rational() return the exponent for us? In
268 * addition to providing a precise rational value instead of a
269 * float that's an approximation of the received value? Can the
270 * 'analog' struct that we fill in carry rationals?
272 * Use double precision FP here during conversion. Optionally
273 * downgrade to single precision later to reduce the amount of
274 * logged information.
276 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
277 if (!command || !*command)
279 scpi_dmm_cmd_delay(scpi);
280 ret = sr_scpi_get_string(scpi, command, &response);
283 g_strstrip(response);
284 use_double = devc->model->digits > 6;
285 ret = sr_atod_ascii(response, &info->d_value);
293 if (info->d_value > +limit) {
294 info->d_value = +INFINITY;
295 } else if (info->d_value < -limit) {
296 info->d_value = -INFINITY;
299 while (p && *p && g_ascii_isspace(*p))
301 if (p && *p && (*p == '-' || *p == '+'))
304 while (p && *p && g_ascii_isdigit(*p)) {
308 if (p && *p && *p == '.')
310 while (p && *p && g_ascii_isdigit(*p))
315 else if (*p != 'e' && *p != 'E')
318 ret = sr_atoi(++p, &val_exp);
324 * TODO Come up with the most appropriate 'digits' calculation.
325 * This implementation assumes that either the device provides
326 * the resolution with the query for the meter's function, or
327 * the driver uses a fallback text pretending the device had
328 * provided it. This works with supported Agilent devices.
330 * An alternative may be to assume a given digits count which
331 * depends on the device, and adjust that count based on the
332 * value's significant digits and exponent. But this approach
333 * fails if devices change their digits count depending on
334 * modes or user requests, and also fails when e.g. devices
335 * with "100000 counts" can provide values between 100000 and
336 * 120000 in either 4 or 5 digits modes, depending on the most
337 * recent trend of the values. This less robust approach should
338 * only be taken if the mode inquiry won't yield the resolution
339 * (as e.g. DIOD does on 34405A, though we happen to know the
340 * fixed resolution for this very mode on this very model).
342 * For now, let's keep the prepared code path for the second
343 * approach in place, should some Agilent devices need it yet
344 * benefit from re-using most of the remaining acquisition
350 digits = devc->model->digits;
351 digits -= sig_digits;
356 * Fill in the 'analog' description: value, encoding, meaning.
357 * Callers will fill in the sample count, and channel name,
358 * and will send out the packet.
361 analog->data = &info->d_value;
362 analog->encoding->unitsize = sizeof(info->d_value);
364 info->f_value = info->d_value;
365 analog->data = &info->f_value;
366 analog->encoding->unitsize = sizeof(info->f_value);
368 analog->encoding->digits = digits;
369 analog->meaning->mq = mq;
370 analog->meaning->mqflags = mqflag;
376 unit = SR_UNIT_AMPERE;
378 case SR_MQ_RESISTANCE:
379 case SR_MQ_CONTINUITY:
382 case SR_MQ_CAPACITANCE:
383 unit = SR_UNIT_FARAD;
385 case SR_MQ_TEMPERATURE:
386 unit = SR_UNIT_CELSIUS;
388 case SR_MQ_FREQUENCY:
389 unit = SR_UNIT_HERTZ;
392 unit = SR_UNIT_SECOND;
397 analog->meaning->unit = unit;
398 analog->spec->spec_digits = digits;
403 SR_PRIV int scpi_dmm_get_meas_gwinstek(const struct sr_dev_inst *sdi, size_t ch)
405 struct sr_scpi_dev_inst *scpi;
406 struct dev_context *devc;
407 struct scpi_dmm_acq_info *info;
408 struct sr_datafeed_analog *analog;
411 enum sr_mqflag mqflag;
414 const struct mqopt_item *item;
419 int sig_digits, val_exp;
426 info = &devc->run_acq_info;
427 analog = &info->analog[ch];
430 * Get the meter's current mode, keep the response around.
431 * Skip the measurement if the mode is uncertain.
433 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
435 g_free(mode_response);
441 g_free(mode_response);
444 mmode = atoi(mode_response);
445 g_free(mode_response);
448 * Get the current reading from the meter.
450 scpi_dmm_cmd_delay(scpi);
451 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
452 if (!command || !*command)
454 scpi_dmm_cmd_delay(scpi);
455 ret = sr_scpi_get_string(scpi, command, &response);
458 g_strstrip(response);
459 use_double = devc->model->digits > 6;
460 ret = sr_atod_ascii(response, &info->d_value);
468 if (devc->model->infinity_limit != 0.0)
469 limit = devc->model->infinity_limit;
470 if (info->d_value >= +limit) {
471 info->d_value = +INFINITY;
472 } else if (info->d_value <= -limit) {
473 info->d_value = -INFINITY;
476 while (p && *p && g_ascii_isspace(*p))
478 if (p && *p && (*p == '-' || *p == '+'))
481 while (p && *p && g_ascii_isdigit(*p)) {
485 if (p && *p && *p == '.')
487 while (p && *p && g_ascii_isdigit(*p))
492 else if (*p != 'e' && *p != 'E')
495 ret = sr_atoi(++p, &val_exp);
502 * Make sure we report "INFINITY" when meter displays "0L".
507 /* In resitance modes 0L reads as 1.20000E8 or 1.99999E8. */
509 if (strcmp(devc->model->model, "GDM8255A") == 0)
511 if (info->d_value >= limit)
512 info->d_value = +INFINITY;
515 /* In continuity mode 0L reads as 1.20000E3. */
516 if (info->d_value >= 1.2e3)
517 info->d_value = +INFINITY;
520 /* In diode mode 0L reads as 1.00000E0. */
521 if (info->d_value == 1.0e0)
522 info->d_value = +INFINITY;
527 * Calculate 'digits' based on the result of the optional
528 * precision reading which was done at acquisition start.
529 * The GW-Instek manual gives the following information
530 * regarding the resolution:
538 digits = devc->model->digits;
539 if (devc->precision && *devc->precision) {
540 if (g_str_has_prefix(devc->precision, "Slow"))
542 else if (g_str_has_prefix(devc->precision, "Mid"))
544 else if (g_str_has_prefix(devc->precision, "Fast"))
547 sr_info("Unknown precision: '%s'", devc->precision);
551 * Fill in the 'analog' description: value, encoding, meaning.
552 * Callers will fill in the sample count, and channel name,
553 * and will send out the packet.
556 analog->data = &info->d_value;
557 analog->encoding->unitsize = sizeof(info->d_value);
559 info->f_value = info->d_value;
560 analog->data = &info->f_value;
561 analog->encoding->unitsize = sizeof(info->f_value);
563 analog->encoding->digits = digits;
564 analog->meaning->mq = mq;
565 analog->meaning->mqflags = mqflag;
571 unit = SR_UNIT_AMPERE;
573 case SR_MQ_RESISTANCE:
574 case SR_MQ_CONTINUITY:
577 case SR_MQ_CAPACITANCE:
578 unit = SR_UNIT_FARAD;
580 case SR_MQ_TEMPERATURE:
583 unit = SR_UNIT_FAHRENHEIT;
587 unit = SR_UNIT_CELSIUS;
590 case SR_MQ_FREQUENCY:
591 unit = SR_UNIT_HERTZ;
594 unit = SR_UNIT_SECOND;
599 analog->meaning->unit = unit;
600 analog->spec->spec_digits = digits;
605 /* Strictly speaking this is a timer controlled poll routine. */
606 SR_PRIV int scpi_dmm_receive_data(int fd, int revents, void *cb_data)
608 struct sr_dev_inst *sdi;
609 struct sr_scpi_dev_inst *scpi;
610 struct dev_context *devc;
611 struct scpi_dmm_acq_info *info;
612 gboolean sent_sample;
614 struct sr_channel *channel;
627 info = &devc->run_acq_info;
631 for (ch = 0; ch < devc->num_channels; ch++) {
632 /* Check the channel's enabled status. */
633 channel = g_slist_nth_data(sdi->channels, ch);
634 if (!channel->enabled)
638 * Prepare an analog measurement value. Note that digits
639 * will get updated later.
641 info->packet.type = SR_DF_ANALOG;
642 info->packet.payload = &info->analog[ch];
643 sr_analog_init(&info->analog[ch], &info->encoding[ch],
644 &info->meaning[ch], &info->spec[ch], 0);
646 /* Just check OPC before sending another request. */
647 scpi_dmm_cmd_delay(sdi->conn);
650 * Have the model take and interpret a measurement. Lack
651 * of support is pointless, failed retrieval/conversion
652 * is considered fatal. The routine will fill in the
653 * 'analog' details, except for channel name and sample
654 * count (assume one value per channel).
656 * Note that non-zero non-negative return codes signal
657 * that the channel's data shell get skipped in this
658 * iteration over the channels. This copes with devices
659 * or modes where channels may provide data at different
662 if (!devc->model->get_measurement) {
666 ret = devc->model->get_measurement(sdi, ch);
672 /* Send the packet that was filled in by the model's routine. */
673 info->analog[ch].num_samples = 1;
674 info->analog[ch].meaning->channels = g_slist_append(NULL, channel);
675 sr_session_send(sdi, &info->packet);
676 g_slist_free(info->analog[ch].meaning->channels);
680 sr_sw_limits_update_samples_read(&devc->limits, 1);
682 /* Stop acquisition upon communication or data errors. */
683 sr_dev_acquisition_stop(sdi);
686 if (sr_sw_limits_check(&devc->limits))
687 sr_dev_acquisition_stop(sdi);