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 (devc->model->infinity_limit != 0.0)
463 limit = devc->model->infinity_limit;
464 if (info->d_value >= +limit) {
465 info->d_value = +INFINITY;
466 } else if (info->d_value <= -limit) {
467 info->d_value = -INFINITY;
470 while (p && *p && g_ascii_isspace(*p))
472 if (p && *p && (*p == '-' || *p == '+'))
475 while (p && *p && g_ascii_isdigit(*p)) {
479 if (p && *p && *p == '.')
481 while (p && *p && g_ascii_isdigit(*p))
486 else if (*p != 'e' && *p != 'E')
489 ret = sr_atoi(++p, &val_exp);
496 * Make sure we report "INFINITY" when meter displays "0L".
501 /* In resitance modes 0L reads as 1.20000E8 or 1.99999E8. */
503 if (strcmp(devc->model->model, "GDM8255A") == 0)
505 if (info->d_value >= limit)
506 info->d_value = +INFINITY;
509 /* In continuity mode 0L reads as 1.20000E3. */
510 if (info->d_value >= 1.2e3)
511 info->d_value = +INFINITY;
514 /* In diode mode 0L reads as 1.00000E0. */
515 if (info->d_value == 1.0e0)
516 info->d_value = +INFINITY;
521 * Calculate 'digits' based on the result of the optional
522 * precision reading which was done at acquisition start.
523 * The GW-Instek manual gives the following information
524 * regarding the resolution:
532 digits = devc->model->digits;
533 if (devc->precision && *devc->precision) {
534 if (g_str_has_prefix(devc->precision, "Slow"))
536 else if (g_str_has_prefix(devc->precision, "Mid"))
538 else if (g_str_has_prefix(devc->precision, "Fast"))
541 sr_info("Unknown precision: '%s'", devc->precision);
545 * Fill in the 'analog' description: value, encoding, meaning.
546 * Callers will fill in the sample count, and channel name,
547 * and will send out the packet.
550 analog->data = &info->d_value;
551 analog->encoding->unitsize = sizeof(info->d_value);
553 info->f_value = info->d_value;
554 analog->data = &info->f_value;
555 analog->encoding->unitsize = sizeof(info->f_value);
557 analog->encoding->digits = digits;
558 analog->meaning->mq = mq;
559 analog->meaning->mqflags = mqflag;
565 unit = SR_UNIT_AMPERE;
567 case SR_MQ_RESISTANCE:
568 case SR_MQ_CONTINUITY:
571 case SR_MQ_CAPACITANCE:
572 unit = SR_UNIT_FARAD;
574 case SR_MQ_TEMPERATURE:
577 unit = SR_UNIT_FAHRENHEIT;
581 unit = SR_UNIT_CELSIUS;
584 case SR_MQ_FREQUENCY:
585 unit = SR_UNIT_HERTZ;
588 unit = SR_UNIT_SECOND;
593 analog->meaning->unit = unit;
594 analog->spec->spec_digits = digits;
599 /* Strictly speaking this is a timer controlled poll routine. */
600 SR_PRIV int scpi_dmm_receive_data(int fd, int revents, void *cb_data)
602 struct sr_dev_inst *sdi;
603 struct sr_scpi_dev_inst *scpi;
604 struct dev_context *devc;
605 struct scpi_dmm_acq_info *info;
606 gboolean sent_sample;
608 struct sr_channel *channel;
621 info = &devc->run_acq_info;
625 for (ch = 0; ch < devc->num_channels; ch++) {
626 /* Check the channel's enabled status. */
627 channel = g_slist_nth_data(sdi->channels, ch);
628 if (!channel->enabled)
632 * Prepare an analog measurement value. Note that digits
633 * will get updated later.
635 info->packet.type = SR_DF_ANALOG;
636 info->packet.payload = &info->analog[ch];
637 sr_analog_init(&info->analog[ch], &info->encoding[ch],
638 &info->meaning[ch], &info->spec[ch], 0);
640 /* Just check OPC before sending another request. */
641 scpi_dmm_cmd_delay(sdi->conn);
644 * Have the model take and interpret a measurement. Lack
645 * of support is pointless, failed retrieval/conversion
646 * is considered fatal. The routine will fill in the
647 * 'analog' details, except for channel name and sample
648 * count (assume one value per channel).
650 * Note that non-zero non-negative return codes signal
651 * that the channel's data shell get skipped in this
652 * iteration over the channels. This copes with devices
653 * or modes where channels may provide data at different
656 if (!devc->model->get_measurement) {
660 ret = devc->model->get_measurement(sdi, ch);
666 /* Send the packet that was filled in by the model's routine. */
667 info->analog[ch].num_samples = 1;
668 info->analog[ch].meaning->channels = g_slist_append(NULL, channel);
669 sr_session_send(sdi, &info->packet);
670 g_slist_free(info->analog[ch].meaning->channels);
674 sr_sw_limits_update_samples_read(&devc->limits, 1);
676 /* Stop acquisition upon communication or data errors. */
677 sr_dev_acquisition_stop(sdi);
680 if (sr_sw_limits_check(&devc->limits))
681 sr_dev_acquisition_stop(sdi);