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);
31 sr_scpi_get_opc(scpi);
34 SR_PRIV const struct mqopt_item *scpi_dmm_lookup_mq_number(
35 const struct sr_dev_inst *sdi, enum sr_mq mq, enum sr_mqflag flag)
37 struct dev_context *devc;
39 const struct mqopt_item *item;
42 for (i = 0; i < devc->model->mqopt_size; i++) {
43 item = &devc->model->mqopts[i];
44 if (item->mq != mq || item->mqflag != flag)
52 SR_PRIV const struct mqopt_item *scpi_dmm_lookup_mq_text(
53 const struct sr_dev_inst *sdi, const char *text)
55 struct dev_context *devc;
57 const struct mqopt_item *item;
60 for (i = 0; i < devc->model->mqopt_size; i++) {
61 item = &devc->model->mqopts[i];
62 if (!item->scpi_func_query || !item->scpi_func_query[0])
64 if (!g_str_has_prefix(text, item->scpi_func_query))
72 SR_PRIV int scpi_dmm_get_mq(const struct sr_dev_inst *sdi,
73 enum sr_mq *mq, enum sr_mqflag *flag, char **rsp,
74 const struct mqopt_item **mqitem)
76 struct dev_context *devc;
81 const struct mqopt_item *item;
93 scpi_dmm_cmd_delay(sdi->conn);
94 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_FUNC);
95 if (!command || !*command)
98 ret = sr_scpi_get_string(sdi->conn, command, &response);
101 if (!response || !*response)
108 item = scpi_dmm_lookup_mq_text(sdi, have);
113 *flag = item->mqflag;
128 SR_PRIV int scpi_dmm_set_mq(const struct sr_dev_inst *sdi,
129 enum sr_mq mq, enum sr_mqflag flag)
131 struct dev_context *devc;
132 const struct mqopt_item *item;
133 const char *mode, *command;
137 item = scpi_dmm_lookup_mq_number(sdi, mq, flag);
141 mode = item->scpi_func_setup;
142 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_SETUP_FUNC);
143 scpi_dmm_cmd_delay(sdi->conn);
144 ret = sr_scpi_send(sdi->conn, command, mode);
151 SR_PRIV int scpi_dmm_get_meas_agilent(const struct sr_dev_inst *sdi, size_t ch)
153 struct sr_scpi_dev_inst *scpi;
154 struct dev_context *devc;
155 struct scpi_dmm_acq_info *info;
156 struct sr_datafeed_analog *analog;
159 enum sr_mqflag mqflag;
165 const struct mqopt_item *item;
170 int sig_digits, val_exp;
176 info = &devc->run_acq_info;
177 analog = &info->analog[ch];
180 * Get the meter's current mode, keep the response around.
181 * Skip the measurement if the mode is uncertain.
183 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
185 g_free(mode_response);
191 g_free(mode_response);
196 * Get the last comma separated field of the function query
197 * response, or fallback to the model's default precision for
198 * the current function. This copes with either of these cases:
199 * VOLT +1.00000E-01,+1.00000E-06
201 * TEMP THER,5000,+1.00000E+00,+1.00000E-01
203 p = sr_scpi_unquote_string(mode_response);
204 fields = g_strsplit(p, ",", 0);
205 count = g_strv_length(fields);
207 snprintf(prec_text, sizeof(prec_text),
208 "%s", fields[count - 1]);
212 } else if (item->default_precision == NO_DFLT_PREC) {
215 snprintf(prec_text, sizeof(prec_text),
216 "1e%d", item->default_precision);
222 * Need to extract the exponent value ourselves, since a strtod()
223 * call will "eat" the exponent, too. Strip space, strip sign,
224 * strip float number (without! exponent), check for exponent
225 * and get exponent value. Accept absence of Esnn suffixes.
227 while (p && *p && g_ascii_isspace(*p))
229 if (p && *p && (*p == '+' || *p == '-'))
231 while (p && *p && g_ascii_isdigit(*p))
233 if (p && *p && *p == '.')
235 while (p && *p && g_ascii_isdigit(*p))
240 else if (*p != 'e' && *p != 'E')
243 ret = sr_atoi(++p, &prec_exp);
244 g_free(mode_response);
249 * Get the measurement value. Make sure to strip trailing space
250 * or else number conversion may fail in fatal ways. Detect OL
251 * conditions. Determine the measurement's precision: Count the
252 * number of significant digits before the period, and get the
255 * The text presentation of values is like this:
257 * Skip space/sign, count digits before the period, skip to the
258 * exponent, get exponent value.
260 * TODO Can sr_parse_rational() return the exponent for us? In
261 * addition to providing a precise rational value instead of a
262 * float that's an approximation of the received value? Can the
263 * 'analog' struct that we fill in carry rationals?
265 * Use double precision FP here during conversion. Optionally
266 * downgrade to single precision later to reduce the amount of
267 * logged information.
269 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
270 if (!command || !*command)
272 scpi_dmm_cmd_delay(scpi);
273 ret = sr_scpi_get_string(scpi, command, &response);
276 g_strstrip(response);
277 use_double = devc->model->digits > 6;
278 ret = sr_atod_ascii(response, &info->d_value);
285 if (info->d_value > +9e37) {
286 info->d_value = +INFINITY;
287 } else if (info->d_value < -9e37) {
288 info->d_value = -INFINITY;
291 while (p && *p && g_ascii_isspace(*p))
293 if (p && *p && (*p == '-' || *p == '+'))
296 while (p && *p && g_ascii_isdigit(*p)) {
300 if (p && *p && *p == '.')
302 while (p && *p && g_ascii_isdigit(*p))
307 else if (*p != 'e' && *p != 'E')
310 ret = sr_atoi(++p, &val_exp);
316 * TODO Come up with the most appropriate 'digits' calculation.
317 * This implementation assumes that either the device provides
318 * the resolution with the query for the meter's function, or
319 * the driver uses a fallback text pretending the device had
320 * provided it. This works with supported Agilent devices.
322 * An alternative may be to assume a given digits count which
323 * depends on the device, and adjust that count based on the
324 * value's significant digits and exponent. But this approach
325 * fails if devices change their digits count depending on
326 * modes or user requests, and also fails when e.g. devices
327 * with "100000 counts" can provide values between 100000 and
328 * 120000 in either 4 or 5 digits modes, depending on the most
329 * recent trend of the values. This less robust approach should
330 * only be taken if the mode inquiry won't yield the resolution
331 * (as e.g. DIOD does on 34405A, though we happen to know the
332 * fixed resolution for this very mode on this very model).
334 * For now, let's keep the prepared code path for the second
335 * approach in place, should some Agilent devices need it yet
336 * benefit from re-using most of the remaining acquisition
342 digits = devc->model->digits;
343 digits -= sig_digits;
348 * Fill in the 'analog' description: value, encoding, meaning.
349 * Callers will fill in the sample count, and channel name,
350 * and will send out the packet.
353 analog->data = &info->d_value;
354 analog->encoding->unitsize = sizeof(info->d_value);
356 info->f_value = info->d_value;
357 analog->data = &info->f_value;
358 analog->encoding->unitsize = sizeof(info->f_value);
360 analog->encoding->digits = digits;
361 analog->meaning->mq = mq;
362 analog->meaning->mqflags = mqflag;
368 unit = SR_UNIT_AMPERE;
370 case SR_MQ_RESISTANCE:
371 case SR_MQ_CONTINUITY:
374 case SR_MQ_CAPACITANCE:
375 unit = SR_UNIT_FARAD;
377 case SR_MQ_TEMPERATURE:
378 unit = SR_UNIT_CELSIUS;
380 case SR_MQ_FREQUENCY:
381 unit = SR_UNIT_HERTZ;
384 unit = SR_UNIT_SECOND;
389 analog->meaning->unit = unit;
390 analog->spec->spec_digits = digits;
395 SR_PRIV int scpi_dmm_get_meas_gwinstek(const struct sr_dev_inst *sdi, size_t ch)
397 struct sr_scpi_dev_inst *scpi;
398 struct dev_context *devc;
399 struct scpi_dmm_acq_info *info;
400 struct sr_datafeed_analog *analog;
403 enum sr_mqflag mqflag;
406 const struct mqopt_item *item;
411 int sig_digits, val_exp;
418 info = &devc->run_acq_info;
419 analog = &info->analog[ch];
422 * Get the meter's current mode, keep the response around.
423 * Skip the measurement if the mode is uncertain.
425 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
427 g_free(mode_response);
433 g_free(mode_response);
436 mmode = atoi(mode_response);
437 g_free(mode_response);
440 * Get the current reading from the meter.
442 scpi_dmm_cmd_delay(scpi);
443 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
444 if (!command || !*command)
446 scpi_dmm_cmd_delay(scpi);
447 ret = sr_scpi_get_string(scpi, command, &response);
450 g_strstrip(response);
451 use_double = devc->model->digits > 6;
452 ret = sr_atod_ascii(response, &info->d_value);
459 if (info->d_value > +9e37) {
460 info->d_value = +INFINITY;
461 } else if (info->d_value < -9e37) {
462 info->d_value = -INFINITY;
465 while (p && *p && g_ascii_isspace(*p))
467 if (p && *p && (*p == '-' || *p == '+'))
470 while (p && *p && g_ascii_isdigit(*p)) {
474 if (p && *p && *p == '.')
476 while (p && *p && g_ascii_isdigit(*p))
481 else if (*p != 'e' && *p != 'E')
484 ret = sr_atoi(++p, &val_exp);
491 * Make sure we report "INFINITY" when meter displays "0L".
496 /* In resitance modes 0L reads as 1.20000E8 or 1.99999E8. */
497 if (strcmp(devc->model->model, "GDM8255A") == 0)
501 if (info->d_value >= limit)
502 info->d_value = +INFINITY;
505 /* In continuity mode 0L reads as 1.20000E3. */
506 if (info->d_value >= 1.2e3)
507 info->d_value = +INFINITY;
510 /* In diode mode 0L reads as 1.00000E0. */
511 if (info->d_value == 1.0e0)
512 info->d_value = +INFINITY;
517 * Calculate 'digits' based on the result of the optional
518 * precision reading which was done at acquisition start.
519 * The GW-Instek manual gives the following information
520 * regarding the resolution:
528 digits = devc->model->digits;
529 if (devc->precision && *devc->precision) {
530 if (g_str_has_prefix(devc->precision, "Slow"))
532 else if (g_str_has_prefix(devc->precision, "Mid"))
534 else if (g_str_has_prefix(devc->precision, "Fast"))
537 sr_info("Unknown precision: '%s'", devc->precision);
541 * Fill in the 'analog' description: value, encoding, meaning.
542 * Callers will fill in the sample count, and channel name,
543 * and will send out the packet.
546 analog->data = &info->d_value;
547 analog->encoding->unitsize = sizeof(info->d_value);
549 info->f_value = info->d_value;
550 analog->data = &info->f_value;
551 analog->encoding->unitsize = sizeof(info->f_value);
553 analog->encoding->digits = digits;
554 analog->meaning->mq = mq;
555 analog->meaning->mqflags = mqflag;
561 unit = SR_UNIT_AMPERE;
563 case SR_MQ_RESISTANCE:
564 case SR_MQ_CONTINUITY:
567 case SR_MQ_CAPACITANCE:
568 unit = SR_UNIT_FARAD;
570 case SR_MQ_TEMPERATURE:
573 unit = SR_UNIT_FAHRENHEIT;
577 unit = SR_UNIT_CELSIUS;
580 case SR_MQ_FREQUENCY:
581 unit = SR_UNIT_HERTZ;
584 unit = SR_UNIT_SECOND;
589 analog->meaning->unit = unit;
590 analog->spec->spec_digits = digits;
595 /* Strictly speaking this is a timer controlled poll routine. */
596 SR_PRIV int scpi_dmm_receive_data(int fd, int revents, void *cb_data)
598 struct sr_dev_inst *sdi;
599 struct sr_scpi_dev_inst *scpi;
600 struct dev_context *devc;
601 struct scpi_dmm_acq_info *info;
602 gboolean sent_sample;
604 struct sr_channel *channel;
617 info = &devc->run_acq_info;
621 for (ch = 0; ch < devc->num_channels; ch++) {
622 /* Check the channel's enabled status. */
623 channel = g_slist_nth_data(sdi->channels, ch);
624 if (!channel->enabled)
628 * Prepare an analog measurement value. Note that digits
629 * will get updated later.
631 info->packet.type = SR_DF_ANALOG;
632 info->packet.payload = &info->analog[ch];
633 sr_analog_init(&info->analog[ch], &info->encoding[ch],
634 &info->meaning[ch], &info->spec[ch], 0);
636 /* Just check OPC before sending another request. */
637 scpi_dmm_cmd_delay(sdi->conn);
640 * Have the model take and interpret a measurement. Lack
641 * of support is pointless, failed retrieval/conversion
642 * is considered fatal. The routine will fill in the
643 * 'analog' details, except for channel name and sample
644 * count (assume one value per channel).
646 * Note that non-zero non-negative return codes signal
647 * that the channel's data shell get skipped in this
648 * iteration over the channels. This copes with devices
649 * or modes where channels may provide data at different
652 if (!devc->model->get_measurement) {
656 ret = devc->model->get_measurement(sdi, ch);
662 /* Send the packet that was filled in by the model's routine. */
663 info->analog[ch].num_samples = 1;
664 info->analog[ch].meaning->channels = g_slist_append(NULL, channel);
665 sr_session_send(sdi, &info->packet);
666 g_slist_free(info->analog[ch].meaning->channels);
670 sr_sw_limits_update_samples_read(&devc->limits, 1);
672 /* Stop acquisition upon communication or data errors. */
673 sr_dev_acquisition_stop(sdi);
676 if (sr_sw_limits_check(&devc->limits))
677 sr_dev_acquisition_stop(sdi);