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;
130 SR_PRIV int scpi_dmm_set_mq(const struct sr_dev_inst *sdi,
131 enum sr_mq mq, enum sr_mqflag flag)
133 struct dev_context *devc;
134 const struct mqopt_item *item;
135 const char *mode, *command;
139 item = scpi_dmm_lookup_mq_number(sdi, mq, flag);
143 mode = item->scpi_func_setup;
144 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_SETUP_FUNC);
145 scpi_dmm_cmd_delay(sdi->conn);
146 ret = sr_scpi_send(sdi->conn, command, mode);
153 SR_PRIV int scpi_dmm_get_meas_agilent(const struct sr_dev_inst *sdi, size_t ch)
155 struct sr_scpi_dev_inst *scpi;
156 struct dev_context *devc;
157 struct scpi_dmm_acq_info *info;
158 struct sr_datafeed_analog *analog;
161 enum sr_mqflag mqflag;
167 const struct mqopt_item *item;
172 int sig_digits, val_exp;
179 info = &devc->run_acq_info;
180 analog = &info->analog[ch];
183 * Get the meter's current mode, keep the response around.
184 * Skip the measurement if the mode is uncertain.
186 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
188 g_free(mode_response);
194 g_free(mode_response);
199 * Get the last comma separated field of the function query
200 * response, or fallback to the model's default precision for
201 * the current function. This copes with either of these cases:
202 * VOLT +1.00000E-01,+1.00000E-06
204 * TEMP THER,5000,+1.00000E+00,+1.00000E-01
206 p = sr_scpi_unquote_string(mode_response);
207 fields = g_strsplit(p, ",", 0);
208 count = g_strv_length(fields);
210 snprintf(prec_text, sizeof(prec_text),
211 "%s", fields[count - 1]);
215 } else if (item->default_precision == NO_DFLT_PREC) {
218 snprintf(prec_text, sizeof(prec_text),
219 "1e%d", item->default_precision);
225 * Need to extract the exponent value ourselves, since a strtod()
226 * call will "eat" the exponent, too. Strip space, strip sign,
227 * strip float number (without! exponent), check for exponent
228 * and get exponent value. Accept absence of Esnn suffixes.
230 while (p && *p && g_ascii_isspace(*p))
232 if (p && *p && (*p == '+' || *p == '-'))
234 while (p && *p && g_ascii_isdigit(*p))
236 if (p && *p && *p == '.')
238 while (p && *p && g_ascii_isdigit(*p))
243 else if (*p != 'e' && *p != 'E')
246 ret = sr_atoi(++p, &prec_exp);
247 g_free(mode_response);
252 * Get the measurement value. Make sure to strip trailing space
253 * or else number conversion may fail in fatal ways. Detect OL
254 * conditions. Determine the measurement's precision: Count the
255 * number of significant digits before the period, and get the
258 * The text presentation of values is like this:
260 * Skip space/sign, count digits before the period, skip to the
261 * exponent, get exponent value.
263 * TODO Can sr_parse_rational() return the exponent for us? In
264 * addition to providing a precise rational value instead of a
265 * float that's an approximation of the received value? Can the
266 * 'analog' struct that we fill in carry rationals?
268 * Use double precision FP here during conversion. Optionally
269 * downgrade to single precision later to reduce the amount of
270 * logged information.
272 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
273 if (!command || !*command)
275 scpi_dmm_cmd_delay(scpi);
276 ret = sr_scpi_get_string(scpi, command, &response);
279 g_strstrip(response);
280 use_double = devc->model->digits > 6;
281 ret = sr_atod_ascii(response, &info->d_value);
289 if (info->d_value > +limit) {
290 info->d_value = +INFINITY;
291 } else if (info->d_value < -limit) {
292 info->d_value = -INFINITY;
295 while (p && *p && g_ascii_isspace(*p))
297 if (p && *p && (*p == '-' || *p == '+'))
300 while (p && *p && g_ascii_isdigit(*p)) {
304 if (p && *p && *p == '.')
306 while (p && *p && g_ascii_isdigit(*p))
311 else if (*p != 'e' && *p != 'E')
314 ret = sr_atoi(++p, &val_exp);
320 * TODO Come up with the most appropriate 'digits' calculation.
321 * This implementation assumes that either the device provides
322 * the resolution with the query for the meter's function, or
323 * the driver uses a fallback text pretending the device had
324 * provided it. This works with supported Agilent devices.
326 * An alternative may be to assume a given digits count which
327 * depends on the device, and adjust that count based on the
328 * value's significant digits and exponent. But this approach
329 * fails if devices change their digits count depending on
330 * modes or user requests, and also fails when e.g. devices
331 * with "100000 counts" can provide values between 100000 and
332 * 120000 in either 4 or 5 digits modes, depending on the most
333 * recent trend of the values. This less robust approach should
334 * only be taken if the mode inquiry won't yield the resolution
335 * (as e.g. DIOD does on 34405A, though we happen to know the
336 * fixed resolution for this very mode on this very model).
338 * For now, let's keep the prepared code path for the second
339 * approach in place, should some Agilent devices need it yet
340 * benefit from re-using most of the remaining acquisition
346 digits = devc->model->digits;
347 digits -= sig_digits;
352 * Fill in the 'analog' description: value, encoding, meaning.
353 * Callers will fill in the sample count, and channel name,
354 * and will send out the packet.
357 analog->data = &info->d_value;
358 analog->encoding->unitsize = sizeof(info->d_value);
360 info->f_value = info->d_value;
361 analog->data = &info->f_value;
362 analog->encoding->unitsize = sizeof(info->f_value);
364 analog->encoding->digits = digits;
365 analog->meaning->mq = mq;
366 analog->meaning->mqflags = mqflag;
372 unit = SR_UNIT_AMPERE;
374 case SR_MQ_RESISTANCE:
375 case SR_MQ_CONTINUITY:
378 case SR_MQ_CAPACITANCE:
379 unit = SR_UNIT_FARAD;
381 case SR_MQ_TEMPERATURE:
382 unit = SR_UNIT_CELSIUS;
384 case SR_MQ_FREQUENCY:
385 unit = SR_UNIT_HERTZ;
388 unit = SR_UNIT_SECOND;
393 analog->meaning->unit = unit;
394 analog->spec->spec_digits = digits;
399 SR_PRIV int scpi_dmm_get_meas_gwinstek(const struct sr_dev_inst *sdi, size_t ch)
401 struct sr_scpi_dev_inst *scpi;
402 struct dev_context *devc;
403 struct scpi_dmm_acq_info *info;
404 struct sr_datafeed_analog *analog;
407 enum sr_mqflag mqflag;
410 const struct mqopt_item *item;
415 int sig_digits, val_exp;
422 info = &devc->run_acq_info;
423 analog = &info->analog[ch];
426 * Get the meter's current mode, keep the response around.
427 * Skip the measurement if the mode is uncertain.
429 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
431 g_free(mode_response);
437 g_free(mode_response);
440 mmode = atoi(mode_response);
441 g_free(mode_response);
444 * Get the current reading from the meter.
446 scpi_dmm_cmd_delay(scpi);
447 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
448 if (!command || !*command)
450 scpi_dmm_cmd_delay(scpi);
451 ret = sr_scpi_get_string(scpi, command, &response);
454 g_strstrip(response);
455 use_double = devc->model->digits > 6;
456 ret = sr_atod_ascii(response, &info->d_value);
464 if (devc->model->infinity_limit != 0.0)
465 limit = devc->model->infinity_limit;
466 if (info->d_value >= +limit) {
467 info->d_value = +INFINITY;
468 } else if (info->d_value <= -limit) {
469 info->d_value = -INFINITY;
472 while (p && *p && g_ascii_isspace(*p))
474 if (p && *p && (*p == '-' || *p == '+'))
477 while (p && *p && g_ascii_isdigit(*p)) {
481 if (p && *p && *p == '.')
483 while (p && *p && g_ascii_isdigit(*p))
488 else if (*p != 'e' && *p != 'E')
491 ret = sr_atoi(++p, &val_exp);
498 * Make sure we report "INFINITY" when meter displays "0L".
503 /* In resitance modes 0L reads as 1.20000E8 or 1.99999E8. */
505 if (strcmp(devc->model->model, "GDM8255A") == 0)
507 if (info->d_value >= limit)
508 info->d_value = +INFINITY;
511 /* In continuity mode 0L reads as 1.20000E3. */
512 if (info->d_value >= 1.2e3)
513 info->d_value = +INFINITY;
516 /* In diode mode 0L reads as 1.00000E0. */
517 if (info->d_value == 1.0e0)
518 info->d_value = +INFINITY;
523 * Calculate 'digits' based on the result of the optional
524 * precision reading which was done at acquisition start.
525 * The GW-Instek manual gives the following information
526 * regarding the resolution:
534 digits = devc->model->digits;
535 if (devc->precision && *devc->precision) {
536 if (g_str_has_prefix(devc->precision, "Slow"))
538 else if (g_str_has_prefix(devc->precision, "Mid"))
540 else if (g_str_has_prefix(devc->precision, "Fast"))
543 sr_info("Unknown precision: '%s'", devc->precision);
547 * Fill in the 'analog' description: value, encoding, meaning.
548 * Callers will fill in the sample count, and channel name,
549 * and will send out the packet.
552 analog->data = &info->d_value;
553 analog->encoding->unitsize = sizeof(info->d_value);
555 info->f_value = info->d_value;
556 analog->data = &info->f_value;
557 analog->encoding->unitsize = sizeof(info->f_value);
559 analog->encoding->digits = digits;
560 analog->meaning->mq = mq;
561 analog->meaning->mqflags = mqflag;
567 unit = SR_UNIT_AMPERE;
569 case SR_MQ_RESISTANCE:
570 case SR_MQ_CONTINUITY:
573 case SR_MQ_CAPACITANCE:
574 unit = SR_UNIT_FARAD;
576 case SR_MQ_TEMPERATURE:
579 unit = SR_UNIT_FAHRENHEIT;
583 unit = SR_UNIT_CELSIUS;
586 case SR_MQ_FREQUENCY:
587 unit = SR_UNIT_HERTZ;
590 unit = SR_UNIT_SECOND;
595 analog->meaning->unit = unit;
596 analog->spec->spec_digits = digits;
601 /* Strictly speaking this is a timer controlled poll routine. */
602 SR_PRIV int scpi_dmm_receive_data(int fd, int revents, void *cb_data)
604 struct sr_dev_inst *sdi;
605 struct sr_scpi_dev_inst *scpi;
606 struct dev_context *devc;
607 struct scpi_dmm_acq_info *info;
608 gboolean sent_sample;
610 struct sr_channel *channel;
623 info = &devc->run_acq_info;
627 for (ch = 0; ch < devc->num_channels; ch++) {
628 /* Check the channel's enabled status. */
629 channel = g_slist_nth_data(sdi->channels, ch);
630 if (!channel->enabled)
634 * Prepare an analog measurement value. Note that digits
635 * will get updated later.
637 info->packet.type = SR_DF_ANALOG;
638 info->packet.payload = &info->analog[ch];
639 sr_analog_init(&info->analog[ch], &info->encoding[ch],
640 &info->meaning[ch], &info->spec[ch], 0);
642 /* Just check OPC before sending another request. */
643 scpi_dmm_cmd_delay(sdi->conn);
646 * Have the model take and interpret a measurement. Lack
647 * of support is pointless, failed retrieval/conversion
648 * is considered fatal. The routine will fill in the
649 * 'analog' details, except for channel name and sample
650 * count (assume one value per channel).
652 * Note that non-zero non-negative return codes signal
653 * that the channel's data shell get skipped in this
654 * iteration over the channels. This copes with devices
655 * or modes where channels may provide data at different
658 if (!devc->model->get_measurement) {
662 ret = devc->model->get_measurement(sdi, ch);
668 /* Send the packet that was filled in by the model's routine. */
669 info->analog[ch].num_samples = 1;
670 info->analog[ch].meaning->channels = g_slist_append(NULL, channel);
671 sr_session_send(sdi, &info->packet);
672 g_slist_free(info->analog[ch].meaning->channels);
676 sr_sw_limits_update_samples_read(&devc->limits, 1);
678 /* Stop acquisition upon communication or data errors. */
679 sr_dev_acquisition_stop(sdi);
682 if (sr_sw_limits_check(&devc->limits))
683 sr_dev_acquisition_stop(sdi);