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;
177 info = &devc->run_acq_info;
178 analog = &info->analog[ch];
181 * Get the meter's current mode, keep the response around.
182 * Skip the measurement if the mode is uncertain.
184 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
186 g_free(mode_response);
192 g_free(mode_response);
197 * Get the last comma separated field of the function query
198 * response, or fallback to the model's default precision for
199 * the current function. This copes with either of these cases:
200 * VOLT +1.00000E-01,+1.00000E-06
202 * TEMP THER,5000,+1.00000E+00,+1.00000E-01
204 p = sr_scpi_unquote_string(mode_response);
205 fields = g_strsplit(p, ",", 0);
206 count = g_strv_length(fields);
208 snprintf(prec_text, sizeof(prec_text),
209 "%s", fields[count - 1]);
213 } else if (item->default_precision == NO_DFLT_PREC) {
216 snprintf(prec_text, sizeof(prec_text),
217 "1e%d", item->default_precision);
223 * Need to extract the exponent value ourselves, since a strtod()
224 * call will "eat" the exponent, too. Strip space, strip sign,
225 * strip float number (without! exponent), check for exponent
226 * and get exponent value. Accept absence of Esnn suffixes.
228 while (p && *p && g_ascii_isspace(*p))
230 if (p && *p && (*p == '+' || *p == '-'))
232 while (p && *p && g_ascii_isdigit(*p))
234 if (p && *p && *p == '.')
236 while (p && *p && g_ascii_isdigit(*p))
241 else if (*p != 'e' && *p != 'E')
244 ret = sr_atoi(++p, &prec_exp);
245 g_free(mode_response);
250 * Get the measurement value. Make sure to strip trailing space
251 * or else number conversion may fail in fatal ways. Detect OL
252 * conditions. Determine the measurement's precision: Count the
253 * number of significant digits before the period, and get the
256 * The text presentation of values is like this:
258 * Skip space/sign, count digits before the period, skip to the
259 * exponent, get exponent value.
261 * TODO Can sr_parse_rational() return the exponent for us? In
262 * addition to providing a precise rational value instead of a
263 * float that's an approximation of the received value? Can the
264 * 'analog' struct that we fill in carry rationals?
266 * Use double precision FP here during conversion. Optionally
267 * downgrade to single precision later to reduce the amount of
268 * logged information.
270 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
271 if (!command || !*command)
273 scpi_dmm_cmd_delay(scpi);
274 ret = sr_scpi_get_string(scpi, command, &response);
277 g_strstrip(response);
278 use_double = devc->model->digits > 6;
279 ret = sr_atod_ascii(response, &info->d_value);
287 if (info->d_value > +limit) {
288 info->d_value = +INFINITY;
289 } else if (info->d_value < -limit) {
290 info->d_value = -INFINITY;
293 while (p && *p && g_ascii_isspace(*p))
295 if (p && *p && (*p == '-' || *p == '+'))
298 while (p && *p && g_ascii_isdigit(*p)) {
302 if (p && *p && *p == '.')
304 while (p && *p && g_ascii_isdigit(*p))
309 else if (*p != 'e' && *p != 'E')
312 ret = sr_atoi(++p, &val_exp);
318 * TODO Come up with the most appropriate 'digits' calculation.
319 * This implementation assumes that either the device provides
320 * the resolution with the query for the meter's function, or
321 * the driver uses a fallback text pretending the device had
322 * provided it. This works with supported Agilent devices.
324 * An alternative may be to assume a given digits count which
325 * depends on the device, and adjust that count based on the
326 * value's significant digits and exponent. But this approach
327 * fails if devices change their digits count depending on
328 * modes or user requests, and also fails when e.g. devices
329 * with "100000 counts" can provide values between 100000 and
330 * 120000 in either 4 or 5 digits modes, depending on the most
331 * recent trend of the values. This less robust approach should
332 * only be taken if the mode inquiry won't yield the resolution
333 * (as e.g. DIOD does on 34405A, though we happen to know the
334 * fixed resolution for this very mode on this very model).
336 * For now, let's keep the prepared code path for the second
337 * approach in place, should some Agilent devices need it yet
338 * benefit from re-using most of the remaining acquisition
344 digits = devc->model->digits;
345 digits -= sig_digits;
350 * Fill in the 'analog' description: value, encoding, meaning.
351 * Callers will fill in the sample count, and channel name,
352 * and will send out the packet.
355 analog->data = &info->d_value;
356 analog->encoding->unitsize = sizeof(info->d_value);
358 info->f_value = info->d_value;
359 analog->data = &info->f_value;
360 analog->encoding->unitsize = sizeof(info->f_value);
362 analog->encoding->digits = digits;
363 analog->meaning->mq = mq;
364 analog->meaning->mqflags = mqflag;
370 unit = SR_UNIT_AMPERE;
372 case SR_MQ_RESISTANCE:
373 case SR_MQ_CONTINUITY:
376 case SR_MQ_CAPACITANCE:
377 unit = SR_UNIT_FARAD;
379 case SR_MQ_TEMPERATURE:
380 unit = SR_UNIT_CELSIUS;
382 case SR_MQ_FREQUENCY:
383 unit = SR_UNIT_HERTZ;
386 unit = SR_UNIT_SECOND;
391 analog->meaning->unit = unit;
392 analog->spec->spec_digits = digits;
397 SR_PRIV int scpi_dmm_get_meas_gwinstek(const struct sr_dev_inst *sdi, size_t ch)
399 struct sr_scpi_dev_inst *scpi;
400 struct dev_context *devc;
401 struct scpi_dmm_acq_info *info;
402 struct sr_datafeed_analog *analog;
405 enum sr_mqflag mqflag;
408 const struct mqopt_item *item;
413 int sig_digits, val_exp;
420 info = &devc->run_acq_info;
421 analog = &info->analog[ch];
424 * Get the meter's current mode, keep the response around.
425 * Skip the measurement if the mode is uncertain.
427 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
429 g_free(mode_response);
435 g_free(mode_response);
438 mmode = atoi(mode_response);
439 g_free(mode_response);
442 * Get the current reading from the meter.
444 scpi_dmm_cmd_delay(scpi);
445 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
446 if (!command || !*command)
448 scpi_dmm_cmd_delay(scpi);
449 ret = sr_scpi_get_string(scpi, command, &response);
452 g_strstrip(response);
453 use_double = devc->model->digits > 6;
454 ret = sr_atod_ascii(response, &info->d_value);
462 if (info->d_value > +limit) {
463 info->d_value = +INFINITY;
464 } else if (info->d_value < -limit) {
465 info->d_value = -INFINITY;
468 while (p && *p && g_ascii_isspace(*p))
470 if (p && *p && (*p == '-' || *p == '+'))
473 while (p && *p && g_ascii_isdigit(*p)) {
477 if (p && *p && *p == '.')
479 while (p && *p && g_ascii_isdigit(*p))
484 else if (*p != 'e' && *p != 'E')
487 ret = sr_atoi(++p, &val_exp);
494 * Make sure we report "INFINITY" when meter displays "0L".
499 /* In resitance modes 0L reads as 1.20000E8 or 1.99999E8. */
501 if (strcmp(devc->model->model, "GDM8255A") == 0)
503 if (info->d_value >= limit)
504 info->d_value = +INFINITY;
507 /* In continuity mode 0L reads as 1.20000E3. */
508 if (info->d_value >= 1.2e3)
509 info->d_value = +INFINITY;
512 /* In diode mode 0L reads as 1.00000E0. */
513 if (info->d_value == 1.0e0)
514 info->d_value = +INFINITY;
519 * Calculate 'digits' based on the result of the optional
520 * precision reading which was done at acquisition start.
521 * The GW-Instek manual gives the following information
522 * regarding the resolution:
530 digits = devc->model->digits;
531 if (devc->precision && *devc->precision) {
532 if (g_str_has_prefix(devc->precision, "Slow"))
534 else if (g_str_has_prefix(devc->precision, "Mid"))
536 else if (g_str_has_prefix(devc->precision, "Fast"))
539 sr_info("Unknown precision: '%s'", devc->precision);
543 * Fill in the 'analog' description: value, encoding, meaning.
544 * Callers will fill in the sample count, and channel name,
545 * and will send out the packet.
548 analog->data = &info->d_value;
549 analog->encoding->unitsize = sizeof(info->d_value);
551 info->f_value = info->d_value;
552 analog->data = &info->f_value;
553 analog->encoding->unitsize = sizeof(info->f_value);
555 analog->encoding->digits = digits;
556 analog->meaning->mq = mq;
557 analog->meaning->mqflags = mqflag;
563 unit = SR_UNIT_AMPERE;
565 case SR_MQ_RESISTANCE:
566 case SR_MQ_CONTINUITY:
569 case SR_MQ_CAPACITANCE:
570 unit = SR_UNIT_FARAD;
572 case SR_MQ_TEMPERATURE:
575 unit = SR_UNIT_FAHRENHEIT;
579 unit = SR_UNIT_CELSIUS;
582 case SR_MQ_FREQUENCY:
583 unit = SR_UNIT_HERTZ;
586 unit = SR_UNIT_SECOND;
591 analog->meaning->unit = unit;
592 analog->spec->spec_digits = digits;
597 /* Strictly speaking this is a timer controlled poll routine. */
598 SR_PRIV int scpi_dmm_receive_data(int fd, int revents, void *cb_data)
600 struct sr_dev_inst *sdi;
601 struct sr_scpi_dev_inst *scpi;
602 struct dev_context *devc;
603 struct scpi_dmm_acq_info *info;
604 gboolean sent_sample;
606 struct sr_channel *channel;
619 info = &devc->run_acq_info;
623 for (ch = 0; ch < devc->num_channels; ch++) {
624 /* Check the channel's enabled status. */
625 channel = g_slist_nth_data(sdi->channels, ch);
626 if (!channel->enabled)
630 * Prepare an analog measurement value. Note that digits
631 * will get updated later.
633 info->packet.type = SR_DF_ANALOG;
634 info->packet.payload = &info->analog[ch];
635 sr_analog_init(&info->analog[ch], &info->encoding[ch],
636 &info->meaning[ch], &info->spec[ch], 0);
638 /* Just check OPC before sending another request. */
639 scpi_dmm_cmd_delay(sdi->conn);
642 * Have the model take and interpret a measurement. Lack
643 * of support is pointless, failed retrieval/conversion
644 * is considered fatal. The routine will fill in the
645 * 'analog' details, except for channel name and sample
646 * count (assume one value per channel).
648 * Note that non-zero non-negative return codes signal
649 * that the channel's data shell get skipped in this
650 * iteration over the channels. This copes with devices
651 * or modes where channels may provide data at different
654 if (!devc->model->get_measurement) {
658 ret = devc->model->get_measurement(sdi, ch);
664 /* Send the packet that was filled in by the model's routine. */
665 info->analog[ch].num_samples = 1;
666 info->analog[ch].meaning->channels = g_slist_append(NULL, channel);
667 sr_session_send(sdi, &info->packet);
668 g_slist_free(info->analog[ch].meaning->channels);
672 sr_sw_limits_update_samples_read(&devc->limits, 1);
674 /* Stop acquisition upon communication or data errors. */
675 sr_dev_acquisition_stop(sdi);
678 if (sr_sw_limits_check(&devc->limits))
679 sr_dev_acquisition_stop(sdi);