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)
75 struct dev_context *devc;
80 const struct mqopt_item *item;
90 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_FUNC);
91 if (!command || !*command)
94 ret = sr_scpi_get_string(sdi->conn, command, &response);
95 scpi_dmm_cmd_delay(sdi->conn);
98 if (!response || !*response)
105 item = scpi_dmm_lookup_mq_text(sdi, have);
110 *flag = item->mqflag;
123 SR_PRIV int scpi_dmm_set_mq(const struct sr_dev_inst *sdi,
124 enum sr_mq mq, enum sr_mqflag flag)
126 struct dev_context *devc;
127 const struct mqopt_item *item;
128 const char *mode, *command;
132 item = scpi_dmm_lookup_mq_number(sdi, mq, flag);
136 mode = item->scpi_func_setup;
137 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_SETUP_FUNC);
138 ret = sr_scpi_send(sdi->conn, command, mode);
139 scpi_dmm_cmd_delay(sdi->conn);
144 SR_PRIV int scpi_dmm_get_meas_agilent(const struct sr_dev_inst *sdi, size_t ch)
146 struct sr_scpi_dev_inst *scpi;
147 struct dev_context *devc;
148 struct scpi_dmm_acq_info *info;
149 struct sr_datafeed_analog *analog;
152 enum sr_mqflag mqflag;
158 const struct mqopt_item *item;
163 int sig_digits, val_exp;
169 info = &devc->run_acq_info;
170 analog = &info->analog[ch];
173 * Get the meter's current mode, keep the response around.
174 * Skip the measurement if the mode is uncertain.
176 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response);
178 g_free(mode_response);
184 g_free(mode_response);
189 * Get the last comma separated field of the function query
190 * response, or fallback to the model's default precision for
191 * the current function. This copes with either of these cases:
192 * VOLT +1.00000E-01,+1.00000E-06
194 * TEMP THER,5000,+1.00000E+00,+1.00000E-01
196 p = sr_scpi_unquote_string(mode_response);
197 fields = g_strsplit(p, ",", 0);
198 count = g_strv_length(fields);
200 snprintf(prec_text, sizeof(prec_text),
201 "%s", fields[count - 1]);
204 item = scpi_dmm_lookup_mq_number(sdi, mq, mqflag);
207 } else if (item->default_precision == NO_DFLT_PREC) {
210 snprintf(prec_text, sizeof(prec_text),
211 "1e%d", item->default_precision);
218 * Need to extract the exponent value ourselves, since a strtod()
219 * call will "eat" the exponent, too. Strip space, strip sign,
220 * strip float number (without! exponent), check for exponent
221 * and get exponent value. Accept absence of Esnn suffixes.
223 while (p && *p && g_ascii_isspace(*p))
225 if (p && *p && (*p == '+' || *p == '-'))
227 while (p && *p && g_ascii_isdigit(*p))
229 if (p && *p && *p == '.')
231 while (p && *p && g_ascii_isdigit(*p))
236 else if (*p != 'e' && *p != 'E')
239 ret = sr_atoi(++p, &prec_exp);
240 g_free(mode_response);
245 * Get the measurement value. Make sure to strip trailing space
246 * or else number conversion may fail in fatal ways. Detect OL
247 * conditions. Determine the measurement's precision: Count the
248 * number of significant digits before the period, and get the
251 * The text presentation of values is like this:
253 * Skip space/sign, count digits before the period, skip to the
254 * exponent, get exponent value.
256 * TODO Can sr_parse_rational() return the exponent for us? In
257 * addition to providing a precise rational value instead of a
258 * float that's an approximation of the received value? Can the
259 * 'analog' struct that we fill in carry rationals?
261 * Use double precision FP here during conversion. Optionally
262 * downgrade to single precision later to reduce the amount of
263 * logged information.
265 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
266 if (!command || !*command)
268 ret = sr_scpi_get_string(scpi, command, &response);
269 scpi_dmm_cmd_delay(scpi);
272 g_strstrip(response);
273 use_double = devc->model->digits > 6;
274 ret = sr_atod_ascii(response, &info->d_value);
281 if (info->d_value > +9e37) {
282 info->d_value = +INFINITY;
283 } else if (info->d_value < -9e37) {
284 info->d_value = -INFINITY;
287 while (p && *p && g_ascii_isspace(*p))
289 if (p && *p && (*p == '-' || *p == '+'))
292 while (p && *p && g_ascii_isdigit(*p)) {
296 if (p && *p && *p == '.')
298 while (p && *p && g_ascii_isdigit(*p))
303 else if (*p != 'e' && *p != 'E')
306 ret = sr_atoi(++p, &val_exp);
312 * TODO Come up with the most appropriate 'digits' calculation.
313 * This implementation assumes that either the device provides
314 * the resolution with the query for the meter's function, or
315 * the driver uses a fallback text pretending the device had
316 * provided it. This works with supported Agilent devices.
318 * An alternative may be to assume a given digits count which
319 * depends on the device, and adjust that count based on the
320 * value's significant digits and exponent. But this approach
321 * fails if devices change their digits count depending on
322 * modes or user requests, and also fails when e.g. devices
323 * with "100000 counts" can provide values between 100000 and
324 * 120000 in either 4 or 5 digits modes, depending on the most
325 * recent trend of the values. This less robust approach should
326 * only be taken if the mode inquiry won't yield the resolution
327 * (as e.g. DIOD does on 34405A, though we happen to know the
328 * fixed resolution for this very mode on this very model).
330 * For now, let's keep the prepared code path for the second
331 * approach in place, should some Agilent devices need it yet
332 * benefit from re-using most of the remaining acquisition
338 digits = devc->model->digits;
339 digits -= sig_digits;
344 * Fill in the 'analog' description: value, encoding, meaning.
345 * Callers will fill in the sample count, and channel name,
346 * and will send out the packet.
349 analog->data = &info->d_value;
350 analog->encoding->unitsize = sizeof(info->d_value);
352 info->f_value = info->d_value;
353 analog->data = &info->f_value;
354 analog->encoding->unitsize = sizeof(info->f_value);
356 analog->encoding->is_float = TRUE;
357 #ifdef WORDS_BIGENDIAN
358 analog->encoding->is_bigendian = TRUE;
360 analog->encoding->is_bigendian = FALSE;
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;
388 analog->meaning->unit = unit;
389 analog->spec->spec_digits = digits;
394 /* Strictly speaking this is a timer controlled poll routine. */
395 SR_PRIV int scpi_dmm_receive_data(int fd, int revents, void *cb_data)
397 struct sr_dev_inst *sdi;
398 struct sr_scpi_dev_inst *scpi;
399 struct dev_context *devc;
400 struct scpi_dmm_acq_info *info;
401 gboolean sent_sample;
403 struct sr_channel *channel;
416 info = &devc->run_acq_info;
420 for (ch = 0; ch < devc->num_channels; ch++) {
421 /* Check the channel's enabled status. */
422 channel = g_slist_nth_data(sdi->channels, ch);
423 if (!channel->enabled)
427 * Prepare an analog measurement value. Note that digits
428 * will get updated later.
430 info->packet.type = SR_DF_ANALOG;
431 info->packet.payload = &info->analog[ch];
432 sr_analog_init(&info->analog[ch], &info->encoding[ch],
433 &info->meaning[ch], &info->spec[ch], 0);
435 /* Just check OPC before sending another request. */
436 scpi_dmm_cmd_delay(sdi->conn);
439 * Have the model take and interpret a measurement. Lack
440 * of support is pointless, failed retrieval/conversion
441 * is considered fatal. The routine will fill in the
442 * 'analog' details, except for channel name and sample
443 * count (assume one value per channel).
445 * Note that non-zero non-negative return codes signal
446 * that the channel's data shell get skipped in this
447 * iteration over the channels. This copes with devices
448 * or modes where channels may provide data at different
451 if (!devc->model->get_measurement) {
455 ret = devc->model->get_measurement(sdi, ch);
461 /* Send the packet that was filled in by the model's routine. */
462 info->analog[ch].num_samples = 1;
463 info->analog[ch].meaning->channels = g_slist_append(NULL, channel);
464 sr_session_send(sdi, &info->packet);
465 g_slist_free(info->analog[ch].meaning->channels);
469 sr_sw_limits_update_samples_read(&devc->limits, 1);
471 /* Stop acquisition upon communication or data errors. */
472 sr_dev_acquisition_stop(sdi);
475 if (sr_sw_limits_check(&devc->limits))
476 sr_dev_acquisition_stop(sdi);