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 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_FUNC);
94 if (!command || !*command)
97 ret = sr_scpi_get_string(sdi->conn, command, &response);
98 scpi_dmm_cmd_delay(sdi->conn);
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 ret = sr_scpi_send(sdi->conn, command, mode);
144 scpi_dmm_cmd_delay(sdi->conn);
149 SR_PRIV int scpi_dmm_get_meas_agilent(const struct sr_dev_inst *sdi, size_t ch)
151 struct sr_scpi_dev_inst *scpi;
152 struct dev_context *devc;
153 struct scpi_dmm_acq_info *info;
154 struct sr_datafeed_analog *analog;
157 enum sr_mqflag mqflag;
163 const struct mqopt_item *item;
168 int sig_digits, val_exp;
174 info = &devc->run_acq_info;
175 analog = &info->analog[ch];
178 * Get the meter's current mode, keep the response around.
179 * Skip the measurement if the mode is uncertain.
181 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
183 g_free(mode_response);
189 g_free(mode_response);
194 * Get the last comma separated field of the function query
195 * response, or fallback to the model's default precision for
196 * the current function. This copes with either of these cases:
197 * VOLT +1.00000E-01,+1.00000E-06
199 * TEMP THER,5000,+1.00000E+00,+1.00000E-01
201 p = sr_scpi_unquote_string(mode_response);
202 fields = g_strsplit(p, ",", 0);
203 count = g_strv_length(fields);
205 snprintf(prec_text, sizeof(prec_text),
206 "%s", fields[count - 1]);
210 } else if (item->default_precision == NO_DFLT_PREC) {
213 snprintf(prec_text, sizeof(prec_text),
214 "1e%d", item->default_precision);
220 * Need to extract the exponent value ourselves, since a strtod()
221 * call will "eat" the exponent, too. Strip space, strip sign,
222 * strip float number (without! exponent), check for exponent
223 * and get exponent value. Accept absence of Esnn suffixes.
225 while (p && *p && g_ascii_isspace(*p))
227 if (p && *p && (*p == '+' || *p == '-'))
229 while (p && *p && g_ascii_isdigit(*p))
231 if (p && *p && *p == '.')
233 while (p && *p && g_ascii_isdigit(*p))
238 else if (*p != 'e' && *p != 'E')
241 ret = sr_atoi(++p, &prec_exp);
242 g_free(mode_response);
247 * Get the measurement value. Make sure to strip trailing space
248 * or else number conversion may fail in fatal ways. Detect OL
249 * conditions. Determine the measurement's precision: Count the
250 * number of significant digits before the period, and get the
253 * The text presentation of values is like this:
255 * Skip space/sign, count digits before the period, skip to the
256 * exponent, get exponent value.
258 * TODO Can sr_parse_rational() return the exponent for us? In
259 * addition to providing a precise rational value instead of a
260 * float that's an approximation of the received value? Can the
261 * 'analog' struct that we fill in carry rationals?
263 * Use double precision FP here during conversion. Optionally
264 * downgrade to single precision later to reduce the amount of
265 * logged information.
267 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
268 if (!command || !*command)
270 ret = sr_scpi_get_string(scpi, command, &response);
271 scpi_dmm_cmd_delay(scpi);
274 g_strstrip(response);
275 use_double = devc->model->digits > 6;
276 ret = sr_atod_ascii(response, &info->d_value);
283 if (info->d_value > +9e37) {
284 info->d_value = +INFINITY;
285 } else if (info->d_value < -9e37) {
286 info->d_value = -INFINITY;
289 while (p && *p && g_ascii_isspace(*p))
291 if (p && *p && (*p == '-' || *p == '+'))
294 while (p && *p && g_ascii_isdigit(*p)) {
298 if (p && *p && *p == '.')
300 while (p && *p && g_ascii_isdigit(*p))
305 else if (*p != 'e' && *p != 'E')
308 ret = sr_atoi(++p, &val_exp);
314 * TODO Come up with the most appropriate 'digits' calculation.
315 * This implementation assumes that either the device provides
316 * the resolution with the query for the meter's function, or
317 * the driver uses a fallback text pretending the device had
318 * provided it. This works with supported Agilent devices.
320 * An alternative may be to assume a given digits count which
321 * depends on the device, and adjust that count based on the
322 * value's significant digits and exponent. But this approach
323 * fails if devices change their digits count depending on
324 * modes or user requests, and also fails when e.g. devices
325 * with "100000 counts" can provide values between 100000 and
326 * 120000 in either 4 or 5 digits modes, depending on the most
327 * recent trend of the values. This less robust approach should
328 * only be taken if the mode inquiry won't yield the resolution
329 * (as e.g. DIOD does on 34405A, though we happen to know the
330 * fixed resolution for this very mode on this very model).
332 * For now, let's keep the prepared code path for the second
333 * approach in place, should some Agilent devices need it yet
334 * benefit from re-using most of the remaining acquisition
340 digits = devc->model->digits;
341 digits -= sig_digits;
346 * Fill in the 'analog' description: value, encoding, meaning.
347 * Callers will fill in the sample count, and channel name,
348 * and will send out the packet.
351 analog->data = &info->d_value;
352 analog->encoding->unitsize = sizeof(info->d_value);
354 info->f_value = info->d_value;
355 analog->data = &info->f_value;
356 analog->encoding->unitsize = sizeof(info->f_value);
358 analog->encoding->is_float = TRUE;
359 #ifdef WORDS_BIGENDIAN
360 analog->encoding->is_bigendian = TRUE;
362 analog->encoding->is_bigendian = FALSE;
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;
390 analog->meaning->unit = unit;
391 analog->spec->spec_digits = digits;
396 /* Strictly speaking this is a timer controlled poll routine. */
397 SR_PRIV int scpi_dmm_receive_data(int fd, int revents, void *cb_data)
399 struct sr_dev_inst *sdi;
400 struct sr_scpi_dev_inst *scpi;
401 struct dev_context *devc;
402 struct scpi_dmm_acq_info *info;
403 gboolean sent_sample;
405 struct sr_channel *channel;
418 info = &devc->run_acq_info;
422 for (ch = 0; ch < devc->num_channels; ch++) {
423 /* Check the channel's enabled status. */
424 channel = g_slist_nth_data(sdi->channels, ch);
425 if (!channel->enabled)
429 * Prepare an analog measurement value. Note that digits
430 * will get updated later.
432 info->packet.type = SR_DF_ANALOG;
433 info->packet.payload = &info->analog[ch];
434 sr_analog_init(&info->analog[ch], &info->encoding[ch],
435 &info->meaning[ch], &info->spec[ch], 0);
437 /* Just check OPC before sending another request. */
438 scpi_dmm_cmd_delay(sdi->conn);
441 * Have the model take and interpret a measurement. Lack
442 * of support is pointless, failed retrieval/conversion
443 * is considered fatal. The routine will fill in the
444 * 'analog' details, except for channel name and sample
445 * count (assume one value per channel).
447 * Note that non-zero non-negative return codes signal
448 * that the channel's data shell get skipped in this
449 * iteration over the channels. This copes with devices
450 * or modes where channels may provide data at different
453 if (!devc->model->get_measurement) {
457 ret = devc->model->get_measurement(sdi, ch);
463 /* Send the packet that was filled in by the model's routine. */
464 info->analog[ch].num_samples = 1;
465 info->analog[ch].meaning->channels = g_slist_append(NULL, channel);
466 sr_session_send(sdi, &info->packet);
467 g_slist_free(info->analog[ch].meaning->channels);
471 sr_sw_limits_update_samples_read(&devc->limits, 1);
473 /* Stop acquisition upon communication or data errors. */
474 sr_dev_acquisition_stop(sdi);
477 if (sr_sw_limits_check(&devc->limits))
478 sr_dev_acquisition_stop(sdi);