]>
Commit | Line | Data |
---|---|---|
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 | ||
27 | SR_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 | ||
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) | |
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 | ||
52 | SR_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 | ||
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 | { | |
75 | struct dev_context *devc; | |
76 | const char *command; | |
77 | char *response; | |
78 | const char *have; | |
79 | int ret; | |
80 | const struct mqopt_item *item; | |
81 | ||
82 | devc = sdi->priv; | |
83 | if (mq) | |
84 | *mq = 0; | |
85 | if (flag) | |
86 | *flag = 0; | |
87 | if (rsp) | |
88 | *rsp = NULL; | |
89 | ||
90 | command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_FUNC); | |
91 | if (!command || !*command) | |
92 | return SR_ERR_NA; | |
93 | response = NULL; | |
94 | ret = sr_scpi_get_string(sdi->conn, command, &response); | |
95 | scpi_dmm_cmd_delay(sdi->conn); | |
96 | if (ret != SR_OK) | |
97 | return ret; | |
98 | if (!response || !*response) | |
99 | return SR_ERR_NA; | |
100 | have = response; | |
101 | if (*have == '"') | |
102 | have++; | |
103 | ||
104 | ret = SR_ERR_NA; | |
105 | item = scpi_dmm_lookup_mq_text(sdi, have); | |
106 | if (item) { | |
107 | if (mq) | |
108 | *mq = item->mq; | |
109 | if (flag) | |
110 | *flag = item->mqflag; | |
111 | ret = SR_OK; | |
112 | } | |
113 | ||
114 | if (rsp) { | |
115 | *rsp = response; | |
116 | response = NULL; | |
117 | } | |
118 | g_free(response); | |
119 | ||
120 | return ret; | |
121 | } | |
122 | ||
123 | SR_PRIV int scpi_dmm_set_mq(const struct sr_dev_inst *sdi, | |
124 | enum sr_mq mq, enum sr_mqflag flag) | |
125 | { | |
126 | struct dev_context *devc; | |
127 | const struct mqopt_item *item; | |
128 | const char *mode, *command; | |
129 | int ret; | |
130 | ||
131 | devc = sdi->priv; | |
132 | item = scpi_dmm_lookup_mq_number(sdi, mq, flag); | |
133 | if (!item) | |
134 | return SR_ERR_NA; | |
135 | ||
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); | |
140 | ||
141 | return ret; | |
142 | } | |
143 | ||
144 | SR_PRIV int scpi_dmm_get_meas_agilent(const struct sr_dev_inst *sdi, size_t ch) | |
145 | { | |
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; | |
150 | int ret; | |
151 | enum sr_mq mq; | |
152 | enum sr_mqflag mqflag; | |
153 | char *mode_response; | |
154 | const char *p; | |
155 | char **fields; | |
156 | size_t count; | |
157 | char prec_text[20]; | |
158 | const struct mqopt_item *item; | |
159 | int prec_exp; | |
160 | const char *command; | |
161 | char *response; | |
162 | gboolean use_double; | |
163 | int sig_digits, val_exp; | |
164 | int digits; | |
165 | enum sr_unit unit; | |
166 | ||
167 | scpi = sdi->conn; | |
168 | devc = sdi->priv; | |
169 | info = &devc->run_acq_info; | |
170 | analog = &info->analog[ch]; | |
171 | ||
172 | /* | |
173 | * Get the meter's current mode, keep the response around. | |
174 | * Skip the measurement if the mode is uncertain. | |
175 | */ | |
176 | ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, &mode_response); | |
177 | if (ret != SR_OK) { | |
178 | g_free(mode_response); | |
179 | return ret; | |
180 | } | |
181 | if (!mode_response) | |
182 | return SR_ERR; | |
183 | if (!mq) { | |
184 | g_free(mode_response); | |
185 | return +1; | |
186 | } | |
187 | ||
188 | /* | |
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 | |
193 | * DIOD | |
194 | * TEMP THER,5000,+1.00000E+00,+1.00000E-01 | |
195 | */ | |
196 | p = sr_scpi_unquote_string(mode_response); | |
197 | fields = g_strsplit(p, ",", 0); | |
198 | count = g_strv_length(fields); | |
199 | if (count >= 2) { | |
200 | snprintf(prec_text, sizeof(prec_text), | |
201 | "%s", fields[count - 1]); | |
202 | p = prec_text; | |
203 | } else { | |
204 | item = scpi_dmm_lookup_mq_number(sdi, mq, mqflag); | |
205 | if (!item) { | |
206 | p = NULL; | |
207 | } else if (item->default_precision == NO_DFLT_PREC) { | |
208 | p = NULL; | |
209 | } else { | |
210 | snprintf(prec_text, sizeof(prec_text), | |
211 | "1e%d", item->default_precision); | |
212 | p = prec_text; | |
213 | } | |
214 | } | |
215 | g_strfreev(fields); | |
216 | ||
217 | /* | |
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. | |
222 | */ | |
223 | while (p && *p && g_ascii_isspace(*p)) | |
224 | p++; | |
225 | if (p && *p && (*p == '+' || *p == '-')) | |
226 | p++; | |
227 | while (p && *p && g_ascii_isdigit(*p)) | |
228 | p++; | |
229 | if (p && *p && *p == '.') | |
230 | p++; | |
231 | while (p && *p && g_ascii_isdigit(*p)) | |
232 | p++; | |
233 | ret = SR_OK; | |
234 | if (!p || !*p) | |
235 | prec_exp = 0; | |
236 | else if (*p != 'e' && *p != 'E') | |
237 | ret = SR_ERR_DATA; | |
238 | else | |
239 | ret = sr_atoi(++p, &prec_exp); | |
240 | g_free(mode_response); | |
241 | if (ret != SR_OK) | |
242 | return ret; | |
243 | ||
244 | /* | |
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 | |
249 | * exponent's value. | |
250 | * | |
251 | * The text presentation of values is like this: | |
252 | * +1.09450000E-01 | |
253 | * Skip space/sign, count digits before the period, skip to the | |
254 | * exponent, get exponent value. | |
255 | * | |
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? | |
260 | * | |
261 | * Use double precision FP here during conversion. Optionally | |
262 | * downgrade to single precision later to reduce the amount of | |
263 | * logged information. | |
264 | */ | |
265 | command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_VALUE); | |
266 | if (!command || !*command) | |
267 | return SR_ERR_NA; | |
268 | ret = sr_scpi_get_string(scpi, command, &response); | |
269 | scpi_dmm_cmd_delay(scpi); | |
270 | if (ret != SR_OK) | |
271 | return ret; | |
272 | g_strstrip(response); | |
273 | use_double = devc->model->digits > 6; | |
274 | ret = sr_atod_ascii(response, &info->d_value); | |
275 | if (ret != SR_OK) { | |
276 | g_free(response); | |
277 | return ret; | |
278 | } | |
279 | if (!response) | |
280 | return SR_ERR; | |
281 | if (info->d_value > +9e37) { | |
282 | info->d_value = +INFINITY; | |
283 | } else if (info->d_value < -9e37) { | |
284 | info->d_value = -INFINITY; | |
285 | } else { | |
286 | p = response; | |
287 | while (p && *p && g_ascii_isspace(*p)) | |
288 | p++; | |
289 | if (p && *p && (*p == '-' || *p == '+')) | |
290 | p++; | |
291 | sig_digits = 0; | |
292 | while (p && *p && g_ascii_isdigit(*p)) { | |
293 | sig_digits++; | |
294 | p++; | |
295 | } | |
296 | if (p && *p && *p == '.') | |
297 | p++; | |
298 | while (p && *p && g_ascii_isdigit(*p)) | |
299 | p++; | |
300 | ret = SR_OK; | |
301 | if (!p || !*p) | |
302 | val_exp = 0; | |
303 | else if (*p != 'e' && *p != 'E') | |
304 | ret = SR_ERR_DATA; | |
305 | else | |
306 | ret = sr_atoi(++p, &val_exp); | |
307 | } | |
308 | g_free(response); | |
309 | if (ret != SR_OK) | |
310 | return ret; | |
311 | /* | |
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. | |
317 | * | |
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). | |
329 | * | |
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 | |
333 | * routine. | |
334 | */ | |
335 | #if 1 | |
336 | digits = -prec_exp; | |
337 | #else | |
338 | digits = devc->model->digits; | |
339 | digits -= sig_digits; | |
340 | digits -= val_exp; | |
341 | #endif | |
342 | ||
343 | /* | |
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. | |
347 | */ | |
348 | if (use_double) { | |
349 | analog->data = &info->d_value; | |
350 | analog->encoding->unitsize = sizeof(info->d_value); | |
351 | } else { | |
352 | info->f_value = info->d_value; | |
353 | analog->data = &info->f_value; | |
354 | analog->encoding->unitsize = sizeof(info->f_value); | |
355 | } | |
356 | analog->encoding->is_float = TRUE; | |
357 | #ifdef WORDS_BIGENDIAN | |
358 | analog->encoding->is_bigendian = TRUE; | |
359 | #else | |
360 | analog->encoding->is_bigendian = FALSE; | |
361 | #endif | |
362 | analog->encoding->digits = digits; | |
363 | analog->meaning->mq = mq; | |
364 | analog->meaning->mqflags = mqflag; | |
365 | switch (mq) { | |
366 | case SR_MQ_VOLTAGE: | |
367 | unit = SR_UNIT_VOLT; | |
368 | break; | |
369 | case SR_MQ_CURRENT: | |
370 | unit = SR_UNIT_AMPERE; | |
371 | break; | |
372 | case SR_MQ_RESISTANCE: | |
373 | case SR_MQ_CONTINUITY: | |
374 | unit = SR_UNIT_OHM; | |
375 | break; | |
376 | case SR_MQ_CAPACITANCE: | |
377 | unit = SR_UNIT_FARAD; | |
378 | break; | |
379 | case SR_MQ_TEMPERATURE: | |
380 | unit = SR_UNIT_CELSIUS; | |
381 | break; | |
382 | case SR_MQ_FREQUENCY: | |
383 | unit = SR_UNIT_HERTZ; | |
384 | break; | |
385 | default: | |
386 | return SR_ERR_NA; | |
387 | } | |
388 | analog->meaning->unit = unit; | |
389 | analog->spec->spec_digits = digits; | |
390 | ||
391 | return SR_OK; | |
392 | } | |
393 | ||
394 | /* Strictly speaking this is a timer controlled poll routine. */ | |
7a396ff5 GS |
395 | SR_PRIV int scpi_dmm_receive_data(int fd, int revents, void *cb_data) |
396 | { | |
3cdad416 GS |
397 | struct sr_dev_inst *sdi; |
398 | struct sr_scpi_dev_inst *scpi; | |
7a396ff5 | 399 | struct dev_context *devc; |
3cdad416 GS |
400 | struct scpi_dmm_acq_info *info; |
401 | gboolean sent_sample; | |
402 | size_t ch; | |
403 | struct sr_channel *channel; | |
404 | int ret; | |
7a396ff5 GS |
405 | |
406 | (void)fd; | |
3cdad416 | 407 | (void)revents; |
7a396ff5 | 408 | |
3cdad416 GS |
409 | sdi = cb_data; |
410 | if (!sdi) | |
7a396ff5 | 411 | return TRUE; |
3cdad416 GS |
412 | scpi = sdi->conn; |
413 | devc = sdi->priv; | |
414 | if (!scpi || !devc) | |
7a396ff5 | 415 | return TRUE; |
3cdad416 GS |
416 | info = &devc->run_acq_info; |
417 | ||
418 | sent_sample = FALSE; | |
419 | ret = SR_OK; | |
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) | |
424 | continue; | |
425 | ||
426 | /* | |
427 | * Prepare an analog measurement value. Note that digits | |
428 | * will get updated later. | |
429 | */ | |
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); | |
434 | ||
435 | /* Just check OPC before sending another request. */ | |
436 | scpi_dmm_cmd_delay(sdi->conn); | |
7a396ff5 | 437 | |
3cdad416 GS |
438 | /* |
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). | |
444 | * | |
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 | |
449 | * rates. | |
450 | */ | |
451 | if (!devc->model->get_measurement) { | |
452 | ret = SR_ERR_NA; | |
453 | break; | |
454 | } | |
455 | ret = devc->model->get_measurement(sdi, ch); | |
456 | if (ret > 0) | |
457 | continue; | |
458 | if (ret != SR_OK) | |
459 | break; | |
460 | ||
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); | |
466 | sent_sample = TRUE; | |
467 | } | |
468 | if (sent_sample) | |
469 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
470 | if (ret != SR_OK) { | |
471 | /* Stop acquisition upon communication or data errors. */ | |
472 | sr_dev_acquisition_stop(sdi); | |
473 | return TRUE; | |
7a396ff5 | 474 | } |
3cdad416 GS |
475 | if (sr_sw_limits_check(&devc->limits)) |
476 | sr_dev_acquisition_stop(sdi); | |
7a396ff5 GS |
477 | |
478 | return TRUE; | |
479 | } |