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)
110 item = scpi_dmm_lookup_mq_text(sdi, have);
115 *flag = item->mqflag;
120 sr_warn("Unknown measurement quantity: %s", have);
132 SR_PRIV int scpi_dmm_set_mq(const struct sr_dev_inst *sdi,
133 enum sr_mq mq, enum sr_mqflag flag)
135 struct dev_context *devc;
136 const struct mqopt_item *item;
137 const char *mode, *command;
141 item = scpi_dmm_lookup_mq_number(sdi, mq, flag);
145 mode = item->scpi_func_setup;
146 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_SETUP_FUNC);
147 scpi_dmm_cmd_delay(sdi->conn);
148 ret = sr_scpi_send(sdi->conn, command, mode);
155 SR_PRIV int scpi_dmm_get_meas_agilent(const struct sr_dev_inst *sdi, size_t ch)
157 struct sr_scpi_dev_inst *scpi;
158 struct dev_context *devc;
159 struct scpi_dmm_acq_info *info;
160 struct sr_datafeed_analog *analog;
163 enum sr_mqflag mqflag;
169 const struct mqopt_item *item;
174 int sig_digits, val_exp;
181 info = &devc->run_acq_info;
182 analog = &info->analog[ch];
185 * Get the meter's current mode, keep the response around.
186 * Skip the measurement if the mode is uncertain.
188 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
190 g_free(mode_response);
196 g_free(mode_response);
201 * Get the last comma separated field of the function query
202 * response, or fallback to the model's default precision for
203 * the current function. This copes with either of these cases:
204 * VOLT +1.00000E-01,+1.00000E-06
206 * TEMP THER,5000,+1.00000E+00,+1.00000E-01
208 p = sr_scpi_unquote_string(mode_response);
209 fields = g_strsplit(p, ",", 0);
210 count = g_strv_length(fields);
212 snprintf(prec_text, sizeof(prec_text),
213 "%s", fields[count - 1]);
217 } else if (item->default_precision == NO_DFLT_PREC) {
220 snprintf(prec_text, sizeof(prec_text),
221 "1e%d", item->default_precision);
227 * Need to extract the exponent value ourselves, since a strtod()
228 * call will "eat" the exponent, too. Strip space, strip sign,
229 * strip float number (without! exponent), check for exponent
230 * and get exponent value. Accept absence of Esnn suffixes.
232 while (p && *p && g_ascii_isspace(*p))
234 if (p && *p && (*p == '+' || *p == '-'))
236 while (p && *p && g_ascii_isdigit(*p))
238 if (p && *p && *p == '.')
240 while (p && *p && g_ascii_isdigit(*p))
245 else if (*p != 'e' && *p != 'E')
248 ret = sr_atoi(++p, &prec_exp);
249 g_free(mode_response);
254 * Get the measurement value. Make sure to strip trailing space
255 * or else number conversion may fail in fatal ways. Detect OL
256 * conditions. Determine the measurement's precision: Count the
257 * number of significant digits before the period, and get the
260 * The text presentation of values is like this:
262 * Skip space/sign, count digits before the period, skip to the
263 * exponent, get exponent value.
265 * TODO Can sr_parse_rational() return the exponent for us? In
266 * addition to providing a precise rational value instead of a
267 * float that's an approximation of the received value? Can the
268 * 'analog' struct that we fill in carry rationals?
270 * Use double precision FP here during conversion. Optionally
271 * downgrade to single precision later to reduce the amount of
272 * logged information.
274 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
275 if (!command || !*command)
277 scpi_dmm_cmd_delay(scpi);
278 ret = sr_scpi_get_string(scpi, command, &response);
281 g_strstrip(response);
282 use_double = devc->model->digits > 6;
283 ret = sr_atod_ascii(response, &info->d_value);
291 if (info->d_value > +limit) {
292 info->d_value = +INFINITY;
293 } else if (info->d_value < -limit) {
294 info->d_value = -INFINITY;
297 while (p && *p && g_ascii_isspace(*p))
299 if (p && *p && (*p == '-' || *p == '+'))
302 while (p && *p && g_ascii_isdigit(*p)) {
306 if (p && *p && *p == '.')
308 while (p && *p && g_ascii_isdigit(*p))
313 else if (*p != 'e' && *p != 'E')
316 ret = sr_atoi(++p, &val_exp);
322 * TODO Come up with the most appropriate 'digits' calculation.
323 * This implementation assumes that either the device provides
324 * the resolution with the query for the meter's function, or
325 * the driver uses a fallback text pretending the device had
326 * provided it. This works with supported Agilent devices.
328 * An alternative may be to assume a given digits count which
329 * depends on the device, and adjust that count based on the
330 * value's significant digits and exponent. But this approach
331 * fails if devices change their digits count depending on
332 * modes or user requests, and also fails when e.g. devices
333 * with "100000 counts" can provide values between 100000 and
334 * 120000 in either 4 or 5 digits modes, depending on the most
335 * recent trend of the values. This less robust approach should
336 * only be taken if the mode inquiry won't yield the resolution
337 * (as e.g. DIOD does on 34405A, though we happen to know the
338 * fixed resolution for this very mode on this very model).
340 * For now, let's keep the prepared code path for the second
341 * approach in place, should some Agilent devices need it yet
342 * benefit from re-using most of the remaining acquisition
348 digits = devc->model->digits;
349 digits -= sig_digits;
354 * Fill in the 'analog' description: value, encoding, meaning.
355 * Callers will fill in the sample count, and channel name,
356 * and will send out the packet.
359 analog->data = &info->d_value;
360 analog->encoding->unitsize = sizeof(info->d_value);
362 info->f_value = info->d_value;
363 analog->data = &info->f_value;
364 analog->encoding->unitsize = sizeof(info->f_value);
366 analog->encoding->digits = digits;
367 analog->meaning->mq = mq;
368 analog->meaning->mqflags = mqflag;
374 unit = SR_UNIT_AMPERE;
376 case SR_MQ_RESISTANCE:
377 case SR_MQ_CONTINUITY:
380 case SR_MQ_CAPACITANCE:
381 unit = SR_UNIT_FARAD;
383 case SR_MQ_TEMPERATURE:
384 unit = SR_UNIT_CELSIUS;
386 case SR_MQ_FREQUENCY:
387 unit = SR_UNIT_HERTZ;
390 unit = SR_UNIT_SECOND;
395 analog->meaning->unit = unit;
396 analog->spec->spec_digits = digits;
401 SR_PRIV int scpi_dmm_get_meas_gwinstek(const struct sr_dev_inst *sdi, size_t ch)
403 struct sr_scpi_dev_inst *scpi;
404 struct dev_context *devc;
405 struct scpi_dmm_acq_info *info;
406 struct sr_datafeed_analog *analog;
409 enum sr_mqflag mqflag;
412 const struct mqopt_item *item;
417 int sig_digits, val_exp;
424 info = &devc->run_acq_info;
425 analog = &info->analog[ch];
428 * Get the meter's current mode, keep the response around.
429 * Skip the measurement if the mode is uncertain.
431 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
433 g_free(mode_response);
439 g_free(mode_response);
442 mmode = atoi(mode_response);
443 g_free(mode_response);
446 * Get the current reading from the meter.
448 scpi_dmm_cmd_delay(scpi);
449 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
450 if (!command || !*command)
452 scpi_dmm_cmd_delay(scpi);
453 ret = sr_scpi_get_string(scpi, command, &response);
456 g_strstrip(response);
457 use_double = devc->model->digits > 6;
458 ret = sr_atod_ascii(response, &info->d_value);
466 if (devc->model->infinity_limit != 0.0)
467 limit = devc->model->infinity_limit;
468 if (info->d_value >= +limit) {
469 info->d_value = +INFINITY;
470 } else if (info->d_value <= -limit) {
471 info->d_value = -INFINITY;
474 while (p && *p && g_ascii_isspace(*p))
476 if (p && *p && (*p == '-' || *p == '+'))
479 while (p && *p && g_ascii_isdigit(*p)) {
483 if (p && *p && *p == '.')
485 while (p && *p && g_ascii_isdigit(*p))
490 else if (*p != 'e' && *p != 'E')
493 ret = sr_atoi(++p, &val_exp);
500 * Make sure we report "INFINITY" when meter displays "0L".
505 /* In resitance modes 0L reads as 1.20000E8 or 1.99999E8. */
507 if (strcmp(devc->model->model, "GDM8255A") == 0)
509 if (info->d_value >= limit)
510 info->d_value = +INFINITY;
513 /* In continuity mode 0L reads as 1.20000E3. */
514 if (info->d_value >= 1.2e3)
515 info->d_value = +INFINITY;
518 /* In diode mode 0L reads as 1.00000E0. */
519 if (info->d_value == 1.0e0)
520 info->d_value = +INFINITY;
525 * Calculate 'digits' based on the result of the optional
526 * precision reading which was done at acquisition start.
527 * The GW-Instek manual gives the following information
528 * regarding the resolution:
536 digits = devc->model->digits;
537 if (devc->precision && *devc->precision) {
538 if (g_str_has_prefix(devc->precision, "Slow"))
540 else if (g_str_has_prefix(devc->precision, "Mid"))
542 else if (g_str_has_prefix(devc->precision, "Fast"))
545 sr_info("Unknown precision: '%s'", devc->precision);
549 * Fill in the 'analog' description: value, encoding, meaning.
550 * Callers will fill in the sample count, and channel name,
551 * and will send out the packet.
554 analog->data = &info->d_value;
555 analog->encoding->unitsize = sizeof(info->d_value);
557 info->f_value = info->d_value;
558 analog->data = &info->f_value;
559 analog->encoding->unitsize = sizeof(info->f_value);
561 analog->encoding->digits = digits;
562 analog->meaning->mq = mq;
563 analog->meaning->mqflags = mqflag;
569 unit = SR_UNIT_AMPERE;
571 case SR_MQ_RESISTANCE:
572 case SR_MQ_CONTINUITY:
575 case SR_MQ_CAPACITANCE:
576 unit = SR_UNIT_FARAD;
578 case SR_MQ_TEMPERATURE:
581 unit = SR_UNIT_FAHRENHEIT;
585 unit = SR_UNIT_CELSIUS;
588 case SR_MQ_FREQUENCY:
589 unit = SR_UNIT_HERTZ;
592 unit = SR_UNIT_SECOND;
597 analog->meaning->unit = unit;
598 analog->spec->spec_digits = digits;
603 /* Strictly speaking this is a timer controlled poll routine. */
604 SR_PRIV int scpi_dmm_receive_data(int fd, int revents, void *cb_data)
606 struct sr_dev_inst *sdi;
607 struct sr_scpi_dev_inst *scpi;
608 struct dev_context *devc;
609 struct scpi_dmm_acq_info *info;
610 gboolean sent_sample;
612 struct sr_channel *channel;
625 info = &devc->run_acq_info;
629 for (ch = 0; ch < devc->num_channels; ch++) {
630 /* Check the channel's enabled status. */
631 channel = g_slist_nth_data(sdi->channels, ch);
632 if (!channel->enabled)
636 * Prepare an analog measurement value. Note that digits
637 * will get updated later.
639 info->packet.type = SR_DF_ANALOG;
640 info->packet.payload = &info->analog[ch];
641 sr_analog_init(&info->analog[ch], &info->encoding[ch],
642 &info->meaning[ch], &info->spec[ch], 0);
644 /* Just check OPC before sending another request. */
645 scpi_dmm_cmd_delay(sdi->conn);
648 * Have the model take and interpret a measurement. Lack
649 * of support is pointless, failed retrieval/conversion
650 * is considered fatal. The routine will fill in the
651 * 'analog' details, except for channel name and sample
652 * count (assume one value per channel).
654 * Note that non-zero non-negative return codes signal
655 * that the channel's data shell get skipped in this
656 * iteration over the channels. This copes with devices
657 * or modes where channels may provide data at different
660 if (!devc->model->get_measurement) {
664 ret = devc->model->get_measurement(sdi, ch);
670 /* Send the packet that was filled in by the model's routine. */
671 info->analog[ch].num_samples = 1;
672 info->analog[ch].meaning->channels = g_slist_append(NULL, channel);
673 sr_session_send(sdi, &info->packet);
674 g_slist_free(info->analog[ch].meaning->channels);
678 sr_sw_limits_update_samples_read(&devc->limits, 1);
680 /* Stop acquisition upon communication or data errors. */
681 sr_dev_acquisition_stop(sdi);
684 if (sr_sw_limits_check(&devc->limits))
685 sr_dev_acquisition_stop(sdi);