]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-dmm/protocol.c
scpi-dmm: add SR_CONF_CONN getter
[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 92
28877994 93 scpi_dmm_cmd_delay(sdi->conn);
3cdad416
GS
94 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_FUNC);
95 if (!command || !*command)
96 return SR_ERR_NA;
97 response = NULL;
98 ret = sr_scpi_get_string(sdi->conn, command, &response);
3cdad416
GS
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);
3cdad416 143 scpi_dmm_cmd_delay(sdi->conn);
28877994
GS
144 ret = sr_scpi_send(sdi->conn, command, mode);
145 if (ret != SR_OK)
146 return ret;
3cdad416 147
28877994 148 return SR_OK;
3cdad416
GS
149}
150
151SR_PRIV int scpi_dmm_get_meas_agilent(const struct sr_dev_inst *sdi, size_t ch)
152{
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;
157 int ret;
158 enum sr_mq mq;
159 enum sr_mqflag mqflag;
160 char *mode_response;
161 const char *p;
162 char **fields;
163 size_t count;
164 char prec_text[20];
165 const struct mqopt_item *item;
166 int prec_exp;
167 const char *command;
168 char *response;
169 gboolean use_double;
170 int sig_digits, val_exp;
171 int digits;
172 enum sr_unit unit;
173
174 scpi = sdi->conn;
175 devc = sdi->priv;
176 info = &devc->run_acq_info;
177 analog = &info->analog[ch];
178
179 /*
180 * Get the meter's current mode, keep the response around.
181 * Skip the measurement if the mode is uncertain.
182 */
08f3b427 183 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
3cdad416
GS
184 if (ret != SR_OK) {
185 g_free(mode_response);
186 return ret;
187 }
188 if (!mode_response)
189 return SR_ERR;
190 if (!mq) {
191 g_free(mode_response);
192 return +1;
193 }
194
195 /*
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
200 * DIOD
201 * TEMP THER,5000,+1.00000E+00,+1.00000E-01
202 */
203 p = sr_scpi_unquote_string(mode_response);
204 fields = g_strsplit(p, ",", 0);
205 count = g_strv_length(fields);
206 if (count >= 2) {
207 snprintf(prec_text, sizeof(prec_text),
208 "%s", fields[count - 1]);
209 p = prec_text;
08f3b427
GS
210 } else if (!item) {
211 p = NULL;
212 } else if (item->default_precision == NO_DFLT_PREC) {
213 p = NULL;
3cdad416 214 } else {
08f3b427
GS
215 snprintf(prec_text, sizeof(prec_text),
216 "1e%d", item->default_precision);
217 p = prec_text;
3cdad416
GS
218 }
219 g_strfreev(fields);
220
221 /*
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.
226 */
227 while (p && *p && g_ascii_isspace(*p))
228 p++;
229 if (p && *p && (*p == '+' || *p == '-'))
230 p++;
231 while (p && *p && g_ascii_isdigit(*p))
232 p++;
233 if (p && *p && *p == '.')
234 p++;
235 while (p && *p && g_ascii_isdigit(*p))
236 p++;
237 ret = SR_OK;
238 if (!p || !*p)
239 prec_exp = 0;
240 else if (*p != 'e' && *p != 'E')
241 ret = SR_ERR_DATA;
242 else
243 ret = sr_atoi(++p, &prec_exp);
244 g_free(mode_response);
245 if (ret != SR_OK)
246 return ret;
247
248 /*
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
253 * exponent's value.
254 *
255 * The text presentation of values is like this:
256 * +1.09450000E-01
257 * Skip space/sign, count digits before the period, skip to the
258 * exponent, get exponent value.
259 *
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?
264 *
265 * Use double precision FP here during conversion. Optionally
266 * downgrade to single precision later to reduce the amount of
267 * logged information.
268 */
269 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
270 if (!command || !*command)
271 return SR_ERR_NA;
3cdad416 272 scpi_dmm_cmd_delay(scpi);
28877994 273 ret = sr_scpi_get_string(scpi, command, &response);
3cdad416
GS
274 if (ret != SR_OK)
275 return ret;
276 g_strstrip(response);
277 use_double = devc->model->digits > 6;
278 ret = sr_atod_ascii(response, &info->d_value);
279 if (ret != SR_OK) {
280 g_free(response);
281 return ret;
282 }
283 if (!response)
284 return SR_ERR;
285 if (info->d_value > +9e37) {
286 info->d_value = +INFINITY;
287 } else if (info->d_value < -9e37) {
288 info->d_value = -INFINITY;
289 } else {
290 p = response;
291 while (p && *p && g_ascii_isspace(*p))
292 p++;
293 if (p && *p && (*p == '-' || *p == '+'))
294 p++;
295 sig_digits = 0;
296 while (p && *p && g_ascii_isdigit(*p)) {
297 sig_digits++;
298 p++;
299 }
300 if (p && *p && *p == '.')
301 p++;
302 while (p && *p && g_ascii_isdigit(*p))
303 p++;
304 ret = SR_OK;
305 if (!p || !*p)
306 val_exp = 0;
307 else if (*p != 'e' && *p != 'E')
308 ret = SR_ERR_DATA;
309 else
310 ret = sr_atoi(++p, &val_exp);
311 }
312 g_free(response);
313 if (ret != SR_OK)
314 return ret;
315 /*
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.
321 *
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).
333 *
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
337 * routine.
338 */
339#if 1
340 digits = -prec_exp;
341#else
342 digits = devc->model->digits;
343 digits -= sig_digits;
344 digits -= val_exp;
345#endif
346
347 /*
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.
351 */
352 if (use_double) {
353 analog->data = &info->d_value;
354 analog->encoding->unitsize = sizeof(info->d_value);
355 } else {
356 info->f_value = info->d_value;
357 analog->data = &info->f_value;
358 analog->encoding->unitsize = sizeof(info->f_value);
359 }
3cdad416
GS
360 analog->encoding->digits = digits;
361 analog->meaning->mq = mq;
362 analog->meaning->mqflags = mqflag;
363 switch (mq) {
364 case SR_MQ_VOLTAGE:
365 unit = SR_UNIT_VOLT;
366 break;
367 case SR_MQ_CURRENT:
368 unit = SR_UNIT_AMPERE;
369 break;
370 case SR_MQ_RESISTANCE:
371 case SR_MQ_CONTINUITY:
372 unit = SR_UNIT_OHM;
373 break;
374 case SR_MQ_CAPACITANCE:
375 unit = SR_UNIT_FARAD;
376 break;
377 case SR_MQ_TEMPERATURE:
378 unit = SR_UNIT_CELSIUS;
379 break;
380 case SR_MQ_FREQUENCY:
381 unit = SR_UNIT_HERTZ;
382 break;
a1619831
AG
383 case SR_MQ_TIME:
384 unit = SR_UNIT_SECOND;
385 break;
3cdad416
GS
386 default:
387 return SR_ERR_NA;
388 }
389 analog->meaning->unit = unit;
390 analog->spec->spec_digits = digits;
391
392 return SR_OK;
393}
394
d0b602f0
TK
395SR_PRIV int scpi_dmm_get_meas_gwinstek(const struct sr_dev_inst *sdi, size_t ch)
396{
397 struct sr_scpi_dev_inst *scpi;
398 struct dev_context *devc;
399 struct scpi_dmm_acq_info *info;
400 struct sr_datafeed_analog *analog;
401 int ret;
402 enum sr_mq mq;
403 enum sr_mqflag mqflag;
404 char *mode_response;
405 const char *p;
406 const struct mqopt_item *item;
407 const char *command;
408 char *response;
409 gboolean use_double;
33aa8117 410 double limit;
d0b602f0
TK
411 int sig_digits, val_exp;
412 int digits;
413 enum sr_unit unit;
414 int mmode;
415
416 scpi = sdi->conn;
417 devc = sdi->priv;
418 info = &devc->run_acq_info;
419 analog = &info->analog[ch];
420
421 /*
422 * Get the meter's current mode, keep the response around.
423 * Skip the measurement if the mode is uncertain.
424 */
425 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response, &item);
426 if (ret != SR_OK) {
427 g_free(mode_response);
428 return ret;
429 }
430 if (!mode_response)
431 return SR_ERR;
432 if (!mq) {
433 g_free(mode_response);
434 return +1;
435 }
436 mmode = atoi(mode_response);
437 g_free(mode_response);
438
439 /*
440 * Get the current reading from the meter.
441 */
442 scpi_dmm_cmd_delay(scpi);
443 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE);
444 if (!command || !*command)
445 return SR_ERR_NA;
446 scpi_dmm_cmd_delay(scpi);
447 ret = sr_scpi_get_string(scpi, command, &response);
448 if (ret != SR_OK)
449 return ret;
450 g_strstrip(response);
451 use_double = devc->model->digits > 6;
452 ret = sr_atod_ascii(response, &info->d_value);
453 if (ret != SR_OK) {
454 g_free(response);
455 return ret;
456 }
457 if (!response)
458 return SR_ERR;
459 if (info->d_value > +9e37) {
460 info->d_value = +INFINITY;
461 } else if (info->d_value < -9e37) {
462 info->d_value = -INFINITY;
463 } else {
464 p = response;
465 while (p && *p && g_ascii_isspace(*p))
466 p++;
467 if (p && *p && (*p == '-' || *p == '+'))
468 p++;
469 sig_digits = 0;
470 while (p && *p && g_ascii_isdigit(*p)) {
471 sig_digits++;
472 p++;
473 }
474 if (p && *p && *p == '.')
475 p++;
476 while (p && *p && g_ascii_isdigit(*p))
477 p++;
478 ret = SR_OK;
479 if (!p || !*p)
480 val_exp = 0;
481 else if (*p != 'e' && *p != 'E')
482 ret = SR_ERR_DATA;
483 else
484 ret = sr_atoi(++p, &val_exp);
485 }
486 g_free(response);
487 if (ret != SR_OK)
488 return ret;
489
490 /*
33aa8117 491 * Make sure we report "INFINITY" when meter displays "0L".
d0b602f0
TK
492 */
493 switch (mmode) {
494 case 7:
495 case 16:
33aa8117
GS
496 /* In resitance modes 0L reads as 1.20000E8 or 1.99999E8. */
497 if (strcmp(devc->model->model, "GDM8255A") == 0)
498 limit = 1.99999e8;
499 else
500 limit = 1.2e8;
501 if (info->d_value >= limit)
502 info->d_value = +INFINITY;
d0b602f0
TK
503 break;
504 case 13:
33aa8117 505 /* In continuity mode 0L reads as 1.20000E3. */
d0b602f0
TK
506 if (info->d_value >= 1.2e3)
507 info->d_value = +INFINITY;
508 break;
509 case 17:
33aa8117 510 /* In diode mode 0L reads as 1.00000E0. */
d0b602f0
TK
511 if (info->d_value == 1.0e0)
512 info->d_value = +INFINITY;
513 break;
514 }
515
516 /*
33aa8117
GS
517 * Calculate 'digits' based on the result of the optional
518 * precision reading which was done at acquisition start.
519 * The GW-Instek manual gives the following information
520 * regarding the resolution:
d0b602f0 521 *
33aa8117
GS
522 * Type Digit
523 * -------- ------
524 * Slow 5 1/2
525 * Medium 4 1/2
526 * Fast 3 1/2
d0b602f0 527 */
d0b602f0
TK
528 digits = devc->model->digits;
529 if (devc->precision && *devc->precision) {
33aa8117 530 if (g_str_has_prefix(devc->precision, "Slow"))
d0b602f0 531 digits = 6;
33aa8117 532 else if (g_str_has_prefix(devc->precision, "Mid"))
d0b602f0 533 digits = 5;
33aa8117 534 else if (g_str_has_prefix(devc->precision, "Fast"))
d0b602f0
TK
535 digits = 4;
536 else
33aa8117 537 sr_info("Unknown precision: '%s'", devc->precision);
d0b602f0
TK
538 }
539
540 /*
541 * Fill in the 'analog' description: value, encoding, meaning.
542 * Callers will fill in the sample count, and channel name,
543 * and will send out the packet.
544 */
545 if (use_double) {
546 analog->data = &info->d_value;
547 analog->encoding->unitsize = sizeof(info->d_value);
548 } else {
549 info->f_value = info->d_value;
550 analog->data = &info->f_value;
551 analog->encoding->unitsize = sizeof(info->f_value);
552 }
d0b602f0
TK
553 analog->encoding->digits = digits;
554 analog->meaning->mq = mq;
555 analog->meaning->mqflags = mqflag;
556 switch (mq) {
557 case SR_MQ_VOLTAGE:
558 unit = SR_UNIT_VOLT;
559 break;
560 case SR_MQ_CURRENT:
561 unit = SR_UNIT_AMPERE;
562 break;
563 case SR_MQ_RESISTANCE:
564 case SR_MQ_CONTINUITY:
565 unit = SR_UNIT_OHM;
566 break;
567 case SR_MQ_CAPACITANCE:
568 unit = SR_UNIT_FARAD;
569 break;
570 case SR_MQ_TEMPERATURE:
571 switch (mmode) {
572 case 15:
573 unit = SR_UNIT_FAHRENHEIT;
574 break;
575 case 9:
576 default:
577 unit = SR_UNIT_CELSIUS;
578 }
579 break;
580 case SR_MQ_FREQUENCY:
581 unit = SR_UNIT_HERTZ;
582 break;
583 case SR_MQ_TIME:
584 unit = SR_UNIT_SECOND;
585 break;
586 default:
587 return SR_ERR_NA;
588 }
589 analog->meaning->unit = unit;
590 analog->spec->spec_digits = digits;
591
592 return SR_OK;
593}
594
3cdad416 595/* Strictly speaking this is a timer controlled poll routine. */
7a396ff5
GS
596SR_PRIV int scpi_dmm_receive_data(int fd, int revents, void *cb_data)
597{
3cdad416
GS
598 struct sr_dev_inst *sdi;
599 struct sr_scpi_dev_inst *scpi;
7a396ff5 600 struct dev_context *devc;
3cdad416
GS
601 struct scpi_dmm_acq_info *info;
602 gboolean sent_sample;
603 size_t ch;
604 struct sr_channel *channel;
605 int ret;
7a396ff5
GS
606
607 (void)fd;
3cdad416 608 (void)revents;
7a396ff5 609
3cdad416
GS
610 sdi = cb_data;
611 if (!sdi)
7a396ff5 612 return TRUE;
3cdad416
GS
613 scpi = sdi->conn;
614 devc = sdi->priv;
615 if (!scpi || !devc)
7a396ff5 616 return TRUE;
3cdad416
GS
617 info = &devc->run_acq_info;
618
619 sent_sample = FALSE;
620 ret = SR_OK;
621 for (ch = 0; ch < devc->num_channels; ch++) {
622 /* Check the channel's enabled status. */
623 channel = g_slist_nth_data(sdi->channels, ch);
624 if (!channel->enabled)
625 continue;
626
627 /*
628 * Prepare an analog measurement value. Note that digits
629 * will get updated later.
630 */
631 info->packet.type = SR_DF_ANALOG;
632 info->packet.payload = &info->analog[ch];
633 sr_analog_init(&info->analog[ch], &info->encoding[ch],
634 &info->meaning[ch], &info->spec[ch], 0);
635
636 /* Just check OPC before sending another request. */
637 scpi_dmm_cmd_delay(sdi->conn);
7a396ff5 638
3cdad416
GS
639 /*
640 * Have the model take and interpret a measurement. Lack
641 * of support is pointless, failed retrieval/conversion
642 * is considered fatal. The routine will fill in the
643 * 'analog' details, except for channel name and sample
644 * count (assume one value per channel).
645 *
646 * Note that non-zero non-negative return codes signal
647 * that the channel's data shell get skipped in this
648 * iteration over the channels. This copes with devices
649 * or modes where channels may provide data at different
650 * rates.
651 */
652 if (!devc->model->get_measurement) {
653 ret = SR_ERR_NA;
654 break;
655 }
656 ret = devc->model->get_measurement(sdi, ch);
657 if (ret > 0)
658 continue;
659 if (ret != SR_OK)
660 break;
661
662 /* Send the packet that was filled in by the model's routine. */
663 info->analog[ch].num_samples = 1;
664 info->analog[ch].meaning->channels = g_slist_append(NULL, channel);
665 sr_session_send(sdi, &info->packet);
666 g_slist_free(info->analog[ch].meaning->channels);
667 sent_sample = TRUE;
668 }
669 if (sent_sample)
670 sr_sw_limits_update_samples_read(&devc->limits, 1);
671 if (ret != SR_OK) {
672 /* Stop acquisition upon communication or data errors. */
673 sr_dev_acquisition_stop(sdi);
674 return TRUE;
7a396ff5 675 }
3cdad416
GS
676 if (sr_sw_limits_check(&devc->limits))
677 sr_dev_acquisition_stop(sdi);
7a396ff5
GS
678
679 return TRUE;
680}