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->is_float = TRUE;
361 #ifdef WORDS_BIGENDIAN
362 analog->encoding->is_bigendian = TRUE;
364 analog->encoding->is_bigendian = FALSE;
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 /* Strictly speaking this is a timer controlled poll routine. */
402 SR_PRIV int scpi_dmm_receive_data(int fd, int revents, void *cb_data)
404 struct sr_dev_inst *sdi;
405 struct sr_scpi_dev_inst *scpi;
406 struct dev_context *devc;
407 struct scpi_dmm_acq_info *info;
408 gboolean sent_sample;
410 struct sr_channel *channel;
423 info = &devc->run_acq_info;
427 for (ch = 0; ch < devc->num_channels; ch++) {
428 /* Check the channel's enabled status. */
429 channel = g_slist_nth_data(sdi->channels, ch);
430 if (!channel->enabled)
434 * Prepare an analog measurement value. Note that digits
435 * will get updated later.
437 info->packet.type = SR_DF_ANALOG;
438 info->packet.payload = &info->analog[ch];
439 sr_analog_init(&info->analog[ch], &info->encoding[ch],
440 &info->meaning[ch], &info->spec[ch], 0);
442 /* Just check OPC before sending another request. */
443 scpi_dmm_cmd_delay(sdi->conn);
446 * Have the model take and interpret a measurement. Lack
447 * of support is pointless, failed retrieval/conversion
448 * is considered fatal. The routine will fill in the
449 * 'analog' details, except for channel name and sample
450 * count (assume one value per channel).
452 * Note that non-zero non-negative return codes signal
453 * that the channel's data shell get skipped in this
454 * iteration over the channels. This copes with devices
455 * or modes where channels may provide data at different
458 if (!devc->model->get_measurement) {
462 ret = devc->model->get_measurement(sdi, ch);
468 /* Send the packet that was filled in by the model's routine. */
469 info->analog[ch].num_samples = 1;
470 info->analog[ch].meaning->channels = g_slist_append(NULL, channel);
471 sr_session_send(sdi, &info->packet);
472 g_slist_free(info->analog[ch].meaning->channels);
476 sr_sw_limits_update_samples_read(&devc->limits, 1);
478 /* Stop acquisition upon communication or data errors. */
479 sr_dev_acquisition_stop(sdi);
482 if (sr_sw_limits_check(&devc->limits))
483 sr_dev_acquisition_stop(sdi);