]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-dmm/protocol.c
scpi-dmm: return MQ table entry to "get MQ" routine callers
[libsigrok.git] / src / hardware / scpi-dmm / protocol.c
CommitLineData
7a396ff5
GS
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2018 Gerhard Sittig <gerhard.sittig@gmx.net>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <config.h>
3cdad416
GS
21#include <math.h>
22#include <string.h>
7a396ff5
GS
23#include "protocol.h"
24
3cdad416
GS
25#define WITH_CMD_DELAY 0 /* TODO See which devices need delays. */
26
27SR_PRIV void scpi_dmm_cmd_delay(struct sr_scpi_dev_inst *scpi)
28{
29 if (WITH_CMD_DELAY)
30 g_usleep(WITH_CMD_DELAY * 1000);
31 sr_scpi_get_opc(scpi);
32}
33
34SR_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)
36{
37 struct dev_context *devc;
38 size_t i;
39 const struct mqopt_item *item;
40
41 devc = sdi->priv;
42 for (i = 0; i < devc->model->mqopt_size; i++) {
43 item = &devc->model->mqopts[i];
44 if (item->mq != mq || item->mqflag != flag)
45 continue;
46 return item;
47 }
48
49 return NULL;
50}
51
52SR_PRIV const struct mqopt_item *scpi_dmm_lookup_mq_text(
53 const struct sr_dev_inst *sdi, const char *text)
54{
55 struct dev_context *devc;
56 size_t i;
57 const struct mqopt_item *item;
58
59 devc = sdi->priv;
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])
63 continue;
64 if (!g_str_has_prefix(text, item->scpi_func_query))
65 continue;
66 return item;
67 }
68
69 return NULL;
70}
71
72SR_PRIV int scpi_dmm_get_mq(const struct sr_dev_inst *sdi,
08f3b427
GS
73 enum sr_mq *mq, enum sr_mqflag *flag, char **rsp,
74 const struct mqopt_item **mqitem)
3cdad416
GS
75{
76 struct dev_context *devc;
77 const char *command;
78 char *response;
79 const char *have;
80 int ret;
81 const struct mqopt_item *item;
82
83 devc = sdi->priv;
84 if (mq)
85 *mq = 0;
86 if (flag)
87 *flag = 0;
88 if (rsp)
89 *rsp = NULL;
08f3b427
GS
90 if (mqitem)
91 *mqitem = NULL;
3cdad416
GS
92
93 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_FUNC);
94 if (!command || !*command)
95 return SR_ERR_NA;
96 response = NULL;
97 ret = sr_scpi_get_string(sdi->conn, command, &response);
98 scpi_dmm_cmd_delay(sdi->conn);
99 if (ret != SR_OK)
100 return ret;
101 if (!response || !*response)
102 return SR_ERR_NA;
103 have = response;
104 if (*have == '"')
105 have++;
106
107 ret = SR_ERR_NA;
108 item = scpi_dmm_lookup_mq_text(sdi, have);
109 if (item) {
110 if (mq)
111 *mq = item->mq;
112 if (flag)
113 *flag = item->mqflag;
08f3b427
GS
114 if (mqitem)
115 *mqitem = item;
3cdad416
GS
116 ret = SR_OK;
117 }
118
119 if (rsp) {
120 *rsp = response;
121 response = NULL;
122 }
123 g_free(response);
124
125 return ret;
126}
127
128SR_PRIV int scpi_dmm_set_mq(const struct sr_dev_inst *sdi,
129 enum sr_mq mq, enum sr_mqflag flag)
130{
131 struct dev_context *devc;
132 const struct mqopt_item *item;
133 const char *mode, *command;
134 int ret;
135
136 devc = sdi->priv;
137 item = scpi_dmm_lookup_mq_number(sdi, mq, flag);
138 if (!item)
139 return SR_ERR_NA;
140
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);
145
146 return ret;
147}
148
149SR_PRIV int scpi_dmm_get_meas_agilent(const struct sr_dev_inst *sdi, size_t ch)
150{
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;
155 int ret;
156 enum sr_mq mq;
157 enum sr_mqflag mqflag;
158 char *mode_response;
159 const char *p;
160 char **fields;
161 size_t count;
162 char prec_text[20];
163 const struct mqopt_item *item;
164 int prec_exp;
165 const char *command;
166 char *response;
167 gboolean use_double;
168 int sig_digits, val_exp;
169 int digits;
170 enum sr_unit unit;
171
172 scpi = sdi->conn;
173 devc = sdi->priv;
174 info = &devc->run_acq_info;
175 analog = &info->analog[ch];
176
177 /*
178 * Get the meter's current mode, keep the response around.
179 * Skip the measurement if the mode is uncertain.
180 */
08f3b427 181 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
3cdad416
GS
182 if (ret != SR_OK) {
183 g_free(mode_response);
184 return ret;
185 }
186 if (!mode_response)
187 return SR_ERR;
188 if (!mq) {
189 g_free(mode_response);
190 return +1;
191 }
192
193 /*
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
198 * DIOD
199 * TEMP THER,5000,+1.00000E+00,+1.00000E-01
200 */
201 p = sr_scpi_unquote_string(mode_response);
202 fields = g_strsplit(p, ",", 0);
203 count = g_strv_length(fields);
204 if (count >= 2) {
205 snprintf(prec_text, sizeof(prec_text),
206 "%s", fields[count - 1]);
207 p = prec_text;
08f3b427
GS
208 } else if (!item) {
209 p = NULL;
210 } else if (item->default_precision == NO_DFLT_PREC) {
211 p = NULL;
3cdad416 212 } else {
08f3b427
GS
213 snprintf(prec_text, sizeof(prec_text),
214 "1e%d", item->default_precision);
215 p = prec_text;
3cdad416
GS
216 }
217 g_strfreev(fields);
218
219 /*
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.
224 */
225 while (p && *p && g_ascii_isspace(*p))
226 p++;
227 if (p && *p && (*p == '+' || *p == '-'))
228 p++;
229 while (p && *p && g_ascii_isdigit(*p))
230 p++;
231 if (p && *p && *p == '.')
232 p++;
233 while (p && *p && g_ascii_isdigit(*p))
234 p++;
235 ret = SR_OK;
236 if (!p || !*p)
237 prec_exp = 0;
238 else if (*p != 'e' && *p != 'E')
239 ret = SR_ERR_DATA;
240 else
241 ret = sr_atoi(++p, &prec_exp);
242 g_free(mode_response);
243 if (ret != SR_OK)
244 return ret;
245
246 /*
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
251 * exponent's value.
252 *
253 * The text presentation of values is like this:
254 * +1.09450000E-01
255 * Skip space/sign, count digits before the period, skip to the
256 * exponent, get exponent value.
257 *
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?
262 *
263 * Use double precision FP here during conversion. Optionally
264 * downgrade to single precision later to reduce the amount of
265 * logged information.
266 */
267 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
268 if (!command || !*command)
269 return SR_ERR_NA;
270 ret = sr_scpi_get_string(scpi, command, &response);
271 scpi_dmm_cmd_delay(scpi);
272 if (ret != SR_OK)
273 return ret;
274 g_strstrip(response);
275 use_double = devc->model->digits > 6;
276 ret = sr_atod_ascii(response, &info->d_value);
277 if (ret != SR_OK) {
278 g_free(response);
279 return ret;
280 }
281 if (!response)
282 return SR_ERR;
283 if (info->d_value > +9e37) {
284 info->d_value = +INFINITY;
285 } else if (info->d_value < -9e37) {
286 info->d_value = -INFINITY;
287 } else {
288 p = response;
289 while (p && *p && g_ascii_isspace(*p))
290 p++;
291 if (p && *p && (*p == '-' || *p == '+'))
292 p++;
293 sig_digits = 0;
294 while (p && *p && g_ascii_isdigit(*p)) {
295 sig_digits++;
296 p++;
297 }
298 if (p && *p && *p == '.')
299 p++;
300 while (p && *p && g_ascii_isdigit(*p))
301 p++;
302 ret = SR_OK;
303 if (!p || !*p)
304 val_exp = 0;
305 else if (*p != 'e' && *p != 'E')
306 ret = SR_ERR_DATA;
307 else
308 ret = sr_atoi(++p, &val_exp);
309 }
310 g_free(response);
311 if (ret != SR_OK)
312 return ret;
313 /*
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.
319 *
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).
331 *
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
335 * routine.
336 */
337#if 1
338 digits = -prec_exp;
339#else
340 digits = devc->model->digits;
341 digits -= sig_digits;
342 digits -= val_exp;
343#endif
344
345 /*
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.
349 */
350 if (use_double) {
351 analog->data = &info->d_value;
352 analog->encoding->unitsize = sizeof(info->d_value);
353 } else {
354 info->f_value = info->d_value;
355 analog->data = &info->f_value;
356 analog->encoding->unitsize = sizeof(info->f_value);
357 }
358 analog->encoding->is_float = TRUE;
359#ifdef WORDS_BIGENDIAN
360 analog->encoding->is_bigendian = TRUE;
361#else
362 analog->encoding->is_bigendian = FALSE;
363#endif
364 analog->encoding->digits = digits;
365 analog->meaning->mq = mq;
366 analog->meaning->mqflags = mqflag;
367 switch (mq) {
368 case SR_MQ_VOLTAGE:
369 unit = SR_UNIT_VOLT;
370 break;
371 case SR_MQ_CURRENT:
372 unit = SR_UNIT_AMPERE;
373 break;
374 case SR_MQ_RESISTANCE:
375 case SR_MQ_CONTINUITY:
376 unit = SR_UNIT_OHM;
377 break;
378 case SR_MQ_CAPACITANCE:
379 unit = SR_UNIT_FARAD;
380 break;
381 case SR_MQ_TEMPERATURE:
382 unit = SR_UNIT_CELSIUS;
383 break;
384 case SR_MQ_FREQUENCY:
385 unit = SR_UNIT_HERTZ;
386 break;
387 default:
388 return SR_ERR_NA;
389 }
390 analog->meaning->unit = unit;
391 analog->spec->spec_digits = digits;
392
393 return SR_OK;
394}
395
396/* Strictly speaking this is a timer controlled poll routine. */
7a396ff5
GS
397SR_PRIV int scpi_dmm_receive_data(int fd, int revents, void *cb_data)
398{
3cdad416
GS
399 struct sr_dev_inst *sdi;
400 struct sr_scpi_dev_inst *scpi;
7a396ff5 401 struct dev_context *devc;
3cdad416
GS
402 struct scpi_dmm_acq_info *info;
403 gboolean sent_sample;
404 size_t ch;
405 struct sr_channel *channel;
406 int ret;
7a396ff5
GS
407
408 (void)fd;
3cdad416 409 (void)revents;
7a396ff5 410
3cdad416
GS
411 sdi = cb_data;
412 if (!sdi)
7a396ff5 413 return TRUE;
3cdad416
GS
414 scpi = sdi->conn;
415 devc = sdi->priv;
416 if (!scpi || !devc)
7a396ff5 417 return TRUE;
3cdad416
GS
418 info = &devc->run_acq_info;
419
420 sent_sample = FALSE;
421 ret = SR_OK;
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)
426 continue;
427
428 /*
429 * Prepare an analog measurement value. Note that digits
430 * will get updated later.
431 */
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);
436
437 /* Just check OPC before sending another request. */
438 scpi_dmm_cmd_delay(sdi->conn);
7a396ff5 439
3cdad416
GS
440 /*
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).
446 *
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
451 * rates.
452 */
453 if (!devc->model->get_measurement) {
454 ret = SR_ERR_NA;
455 break;
456 }
457 ret = devc->model->get_measurement(sdi, ch);
458 if (ret > 0)
459 continue;
460 if (ret != SR_OK)
461 break;
462
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);
468 sent_sample = TRUE;
469 }
470 if (sent_sample)
471 sr_sw_limits_update_samples_read(&devc->limits, 1);
472 if (ret != SR_OK) {
473 /* Stop acquisition upon communication or data errors. */
474 sr_dev_acquisition_stop(sdi);
475 return TRUE;
7a396ff5 476 }
3cdad416
GS
477 if (sr_sw_limits_check(&devc->limits))
478 sr_dev_acquisition_stop(sdi);
7a396ff5
GS
479
480 return TRUE;
481}