]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/fluke-dmm/fluke.c
Have remaining drivers default to digits=2 for analog values.
[libsigrok.git] / src / hardware / fluke-dmm / fluke.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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>
21#include <stdlib.h>
22#include <math.h>
23#include <string.h>
24#include <glib.h>
25#include <libsigrok/libsigrok.h>
26#include "libsigrok-internal.h"
27#include "fluke-dmm.h"
28
29static struct sr_datafeed_analog *handle_qm_18x(const struct sr_dev_inst *sdi,
30 char **tokens)
31{
32 struct sr_datafeed_analog *analog;
33 struct sr_analog_encoding encoding;
34 struct sr_analog_meaning meaning;
35 struct sr_analog_spec spec;
36 float fvalue;
37 char *e, *u;
38 gboolean is_oor;
39
40 if (strcmp(tokens[0], "QM") || !tokens[1])
41 return NULL;
42
43 if ((e = strstr(tokens[1], "Out of range"))) {
44 is_oor = TRUE;
45 fvalue = -1;
46 while (*e && *e != '.')
47 e++;
48 } else {
49 is_oor = FALSE;
50 /* Delimit the float, since sr_atof_ascii() wants only
51 * a valid float here. */
52 e = tokens[1];
53 while (*e && *e != ' ')
54 e++;
55 *e++ = '\0';
56 if (sr_atof_ascii(tokens[1], &fvalue) != SR_OK || fvalue == 0.0) {
57 /* Happens all the time, when switching modes. */
58 sr_dbg("Invalid float.");
59 return NULL;
60 }
61 }
62 while (*e && *e == ' ')
63 e++;
64
65 analog = g_malloc0(sizeof(struct sr_datafeed_analog));
66 /* TODO: Use proper 'digits' value for this device (and its modes). */
67 sr_analog_init(analog, &encoding, &meaning, &spec, 2);
68 analog->data = g_malloc(sizeof(float));
69 analog->meaning->channels = sdi->channels;
70 analog->num_samples = 1;
71 if (is_oor)
72 *((float *)analog->data) = NAN;
73 else
74 *((float *)analog->data) = fvalue;
75 analog->meaning->mq = 0;
76
77 if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) {
78 analog->meaning->mq = SR_MQ_VOLTAGE;
79 analog->meaning->unit = SR_UNIT_VOLT;
80 if (!is_oor && e[0] == 'm')
81 *((float *)analog->data) /= 1000;
82 /* This catches "V AC", "V DC" and "V AC+DC". */
83 if (strstr(u, "AC"))
84 analog->meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
85 if (strstr(u, "DC"))
86 analog->meaning->mqflags |= SR_MQFLAG_DC;
87 } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
88 analog->meaning->mq = SR_MQ_VOLTAGE;
89 if (u[2] == 'm')
90 analog->meaning->unit = SR_UNIT_DECIBEL_MW;
91 else
92 analog->meaning->unit = SR_UNIT_DECIBEL_VOLT;
93 analog->meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
94 } else if ((u = strstr(e, "Ohms"))) {
95 analog->meaning->mq = SR_MQ_RESISTANCE;
96 analog->meaning->unit = SR_UNIT_OHM;
97 if (is_oor)
98 *((float *)analog->data) = INFINITY;
99 else if (e[0] == 'k')
100 *((float *)analog->data) *= 1000;
101 else if (e[0] == 'M')
102 *((float *)analog->data) *= 1000000;
103 } else if (!strcmp(e, "nS")) {
104 analog->meaning->mq = SR_MQ_CONDUCTANCE;
105 analog->meaning->unit = SR_UNIT_SIEMENS;
106 *((float *)analog->data) /= 1e+9;
107 } else if ((u = strstr(e, "Farads"))) {
108 analog->meaning->mq = SR_MQ_CAPACITANCE;
109 analog->meaning->unit = SR_UNIT_FARAD;
110 if (!is_oor) {
111 if (e[0] == 'm')
112 *((float *)analog->data) /= 1e+3;
113 else if (e[0] == 'u')
114 *((float *)analog->data) /= 1e+6;
115 else if (e[0] == 'n')
116 *((float *)analog->data) /= 1e+9;
117 }
118 } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
119 analog->meaning->mq = SR_MQ_TEMPERATURE;
120 if (u[4] == 'C')
121 analog->meaning->unit = SR_UNIT_CELSIUS;
122 else
123 analog->meaning->unit = SR_UNIT_FAHRENHEIT;
124 } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) {
125 analog->meaning->mq = SR_MQ_CURRENT;
126 analog->meaning->unit = SR_UNIT_AMPERE;
127 /* This catches "A AC", "A DC" and "A AC+DC". */
128 if (strstr(u, "AC"))
129 analog->meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
130 if (strstr(u, "DC"))
131 analog->meaning->mqflags |= SR_MQFLAG_DC;
132 if (!is_oor) {
133 if (e[0] == 'm')
134 *((float *)analog->data) /= 1e+3;
135 else if (e[0] == 'u')
136 *((float *)analog->data) /= 1e+6;
137 }
138 } else if ((u = strstr(e, "Hz"))) {
139 analog->meaning->mq = SR_MQ_FREQUENCY;
140 analog->meaning->unit = SR_UNIT_HERTZ;
141 if (e[0] == 'k')
142 *((float *)analog->data) *= 1e+3;
143 } else if (!strcmp(e, "%")) {
144 analog->meaning->mq = SR_MQ_DUTY_CYCLE;
145 analog->meaning->unit = SR_UNIT_PERCENTAGE;
146 } else if ((u = strstr(e, "ms"))) {
147 analog->meaning->mq = SR_MQ_PULSE_WIDTH;
148 analog->meaning->unit = SR_UNIT_SECOND;
149 *((float *)analog->data) /= 1e+3;
150 }
151
152 if (analog->meaning->mq == 0) {
153 /* Not a valid measurement. */
154 g_free(analog->data);
155 g_free(analog);
156 analog = NULL;
157 }
158
159 return analog;
160}
161
162static struct sr_datafeed_analog *handle_qm_28x(const struct sr_dev_inst *sdi,
163 char **tokens)
164{
165 struct sr_datafeed_analog *analog;
166 struct sr_analog_encoding encoding;
167 struct sr_analog_meaning meaning;
168 struct sr_analog_spec spec;
169 float fvalue;
170
171 if (!tokens[1])
172 return NULL;
173
174 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
175 sr_err("Invalid float '%s'.", tokens[0]);
176 return NULL;
177 }
178
179 analog = g_malloc0(sizeof(struct sr_datafeed_analog));
180 /* TODO: Use proper 'digits' value for this device (and its modes). */
181 sr_analog_init(analog, &encoding, &meaning, &spec, 2);
182 analog->data = g_malloc(sizeof(float));
183 analog->meaning->channels = sdi->channels;
184 analog->num_samples = 1;
185 *((float *)analog->data) = fvalue;
186 analog->meaning->mq = 0;
187
188 if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) {
189 analog->meaning->mq = SR_MQ_VOLTAGE;
190 analog->meaning->unit = SR_UNIT_VOLT;
191 if (!strcmp(tokens[2], "NORMAL")) {
192 if (tokens[1][1] == 'A') {
193 analog->meaning->mqflags |= SR_MQFLAG_AC;
194 analog->meaning->mqflags |= SR_MQFLAG_RMS;
195 } else
196 analog->meaning->mqflags |= SR_MQFLAG_DC;
197 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
198 *((float *)analog->data) = NAN;
199 } else
200 analog->meaning->mq = 0;
201 } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
202 analog->meaning->mq = SR_MQ_VOLTAGE;
203 if (tokens[1][2] == 'm')
204 analog->meaning->unit = SR_UNIT_DECIBEL_MW;
205 else
206 analog->meaning->unit = SR_UNIT_DECIBEL_VOLT;
207 analog->meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
208 } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
209 if (!strcmp(tokens[2], "NORMAL")) {
210 analog->meaning->mq = SR_MQ_TEMPERATURE;
211 if (tokens[1][0] == 'C')
212 analog->meaning->unit = SR_UNIT_CELSIUS;
213 else
214 analog->meaning->unit = SR_UNIT_FAHRENHEIT;
215 }
216 } else if (!strcmp(tokens[1], "OHM")) {
217 if (!strcmp(tokens[3], "NONE")) {
218 analog->meaning->mq = SR_MQ_RESISTANCE;
219 analog->meaning->unit = SR_UNIT_OHM;
220 if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
221 *((float *)analog->data) = INFINITY;
222 } else if (strcmp(tokens[2], "NORMAL"))
223 analog->meaning->mq = 0;
224 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
225 analog->meaning->mq = SR_MQ_CONTINUITY;
226 analog->meaning->unit = SR_UNIT_BOOLEAN;
227 *((float *)analog->data) = 0.0;
228 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
229 analog->meaning->mq = SR_MQ_CONTINUITY;
230 analog->meaning->unit = SR_UNIT_BOOLEAN;
231 *((float *)analog->data) = 1.0;
232 }
233 } else if (!strcmp(tokens[1], "F")
234 && !strcmp(tokens[2], "NORMAL")
235 && !strcmp(tokens[3], "NONE")) {
236 analog->meaning->mq = SR_MQ_CAPACITANCE;
237 analog->meaning->unit = SR_UNIT_FARAD;
238 } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
239 analog->meaning->mq = SR_MQ_CURRENT;
240 analog->meaning->unit = SR_UNIT_AMPERE;
241 if (!strcmp(tokens[2], "NORMAL")) {
242 if (tokens[1][1] == 'A') {
243 analog->meaning->mqflags |= SR_MQFLAG_AC;
244 analog->meaning->mqflags |= SR_MQFLAG_RMS;
245 } else
246 analog->meaning->mqflags |= SR_MQFLAG_DC;
247 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
248 *((float *)analog->data) = NAN;
249 } else
250 analog->meaning->mq = 0;
251 } if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
252 analog->meaning->mq = SR_MQ_FREQUENCY;
253 analog->meaning->unit = SR_UNIT_HERTZ;
254 } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
255 analog->meaning->mq = SR_MQ_DUTY_CYCLE;
256 analog->meaning->unit = SR_UNIT_PERCENTAGE;
257 } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
258 analog->meaning->mq = SR_MQ_PULSE_WIDTH;
259 analog->meaning->unit = SR_UNIT_SECOND;
260 } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
261 analog->meaning->mq = SR_MQ_CONDUCTANCE;
262 analog->meaning->unit = SR_UNIT_SIEMENS;
263 }
264
265 if (analog->meaning->mq == 0) {
266 /* Not a valid measurement. */
267 g_free(analog->data);
268 g_free(analog);
269 analog = NULL;
270 }
271
272 return analog;
273}
274
275static void handle_qm_19x_meta(const struct sr_dev_inst *sdi, char **tokens)
276{
277 struct dev_context *devc;
278 int meas_type, meas_unit, meas_char, i;
279
280 /* Make sure we have 7 valid tokens. */
281 for (i = 0; tokens[i] && i < 7; i++);
282 if (i != 7)
283 return;
284
285 if (strcmp(tokens[1], "1"))
286 /* Invalid measurement. */
287 return;
288
289 if (strcmp(tokens[2], "3"))
290 /* Only interested in input from the meter mode source. */
291 return;
292
293 devc = sdi->priv;
294
295 /* Measurement type 11 == absolute, 19 = relative */
296 meas_type = strtol(tokens[0], NULL, 10);
297 if (meas_type != 11 && meas_type != 19)
298 /* Device is in some mode we don't support. */
299 return;
300
301 /* We might get metadata for absolute and relative mode (if the device
302 * is in relative mode). In that case, relative takes precedence. */
303 if (meas_type == 11 && devc->meas_type == 19)
304 return;
305
306 meas_unit = strtol(tokens[3], NULL, 10);
307 if (meas_unit == 0)
308 /* Device is turned off. Really. */
309 return;
310 meas_char = strtol(tokens[4], NULL, 10);
311
312 devc->mq = devc->unit = devc->mqflags = 0;
313 switch (meas_unit) {
314 case 1:
315 devc->mq = SR_MQ_VOLTAGE;
316 devc->unit = SR_UNIT_VOLT;
317 if (meas_char == 1)
318 devc->mqflags |= SR_MQFLAG_DC;
319 else if (meas_char == 2)
320 devc->mqflags |= SR_MQFLAG_AC;
321 else if (meas_char == 3)
322 devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
323 else if (meas_char == 15)
324 devc->mqflags |= SR_MQFLAG_DIODE;
325 break;
326 case 2:
327 devc->mq = SR_MQ_CURRENT;
328 devc->unit = SR_UNIT_AMPERE;
329 if (meas_char == 1)
330 devc->mqflags |= SR_MQFLAG_DC;
331 else if (meas_char == 2)
332 devc->mqflags |= SR_MQFLAG_AC;
333 else if (meas_char == 3)
334 devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
335 break;
336 case 3:
337 if (meas_char == 1) {
338 devc->mq = SR_MQ_RESISTANCE;
339 devc->unit = SR_UNIT_OHM;
340 } else if (meas_char == 16) {
341 devc->mq = SR_MQ_CONTINUITY;
342 devc->unit = SR_UNIT_BOOLEAN;
343 }
344 break;
345 case 12:
346 devc->mq = SR_MQ_TEMPERATURE;
347 devc->unit = SR_UNIT_CELSIUS;
348 break;
349 case 13:
350 devc->mq = SR_MQ_TEMPERATURE;
351 devc->unit = SR_UNIT_FAHRENHEIT;
352 break;
353 default:
354 sr_dbg("unknown unit: %d", meas_unit);
355 }
356 if (devc->mq == 0 && devc->unit == 0)
357 return;
358
359 /* If we got here, we know how to interpret the measurement. */
360 devc->meas_type = meas_type;
361 if (meas_type == 11)
362 /* Absolute meter reading. */
363 devc->is_relative = FALSE;
364 else if (!strcmp(tokens[0], "19"))
365 /* Relative meter reading. */
366 devc->is_relative = TRUE;
367
368}
369
370static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens)
371{
372 struct dev_context *devc;
373 struct sr_datafeed_packet packet;
374 struct sr_datafeed_analog analog;
375 struct sr_analog_encoding encoding;
376 struct sr_analog_meaning meaning;
377 struct sr_analog_spec spec;
378 float fvalue;
379
380 if (!strcmp(tokens[0], "9.9E+37")) {
381 /* An invalid measurement shows up on the display as "OL", but
382 * comes through like this. Since comparing 38-digit floats
383 * is rather problematic, we'll cut through this here. */
384 fvalue = NAN;
385 } else {
386 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
387 sr_err("Invalid float '%s'.", tokens[0]);
388 return;
389 }
390 }
391
392 devc = sdi->priv;
393 if (devc->mq == 0 || devc->unit == 0)
394 /* Don't have valid metadata yet. */
395 return;
396
397 if (devc->mq == SR_MQ_RESISTANCE && isnan(fvalue))
398 fvalue = INFINITY;
399 else if (devc->mq == SR_MQ_CONTINUITY) {
400 if (isnan(fvalue))
401 fvalue = 0.0;
402 else
403 fvalue = 1.0;
404 }
405
406 /* TODO: Use proper 'digits' value for this device (and its modes). */
407 sr_analog_init(&analog, &encoding, &meaning, &spec, 2);
408 analog.meaning->channels = sdi->channels;
409 analog.num_samples = 1;
410 analog.data = &fvalue;
411 analog.meaning->mq = devc->mq;
412 analog.meaning->unit = devc->unit;
413 analog.meaning->mqflags = 0;
414 packet.type = SR_DF_ANALOG;
415 packet.payload = &analog;
416 sr_session_send(sdi, &packet);
417
418 sr_sw_limits_update_samples_read(&devc->limits, 1);
419}
420
421static void handle_line(const struct sr_dev_inst *sdi)
422{
423 struct dev_context *devc;
424 struct sr_serial_dev_inst *serial;
425 struct sr_datafeed_packet packet;
426 struct sr_datafeed_analog *analog;
427 int num_tokens, n, i;
428 char cmd[16], **tokens;
429
430 devc = sdi->priv;
431 serial = sdi->conn;
432 sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
433
434 if (devc->buflen == 1) {
435 if (devc->buf[0] != '0') {
436 /* Not just a CMD_ACK from the query command. */
437 sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
438 devc->expect_response = FALSE;
439 }
440 devc->buflen = 0;
441 return;
442 }
443
444 analog = NULL;
445 tokens = g_strsplit(devc->buf, ",", 0);
446 if (tokens[0]) {
447 if (devc->profile->model == FLUKE_187 || devc->profile->model == FLUKE_189) {
448 devc->expect_response = FALSE;
449 analog = handle_qm_18x(sdi, tokens);
450 } else if (devc->profile->model == FLUKE_287 || devc->profile->model == FLUKE_289) {
451 devc->expect_response = FALSE;
452 analog = handle_qm_28x(sdi, tokens);
453 } else if (devc->profile->model == FLUKE_190) {
454 devc->expect_response = FALSE;
455 for (num_tokens = 0; tokens[num_tokens]; num_tokens++);
456 if (num_tokens >= 7) {
457 /* Response to QM: this is a comma-separated list of
458 * fields with metadata about the measurement. This
459 * format can return multiple sets of metadata,
460 * split into sets of 7 tokens each. */
461 devc->meas_type = 0;
462 for (i = 0; i < num_tokens; i += 7)
463 handle_qm_19x_meta(sdi, tokens + i);
464 if (devc->meas_type) {
465 /* Slip the request in now, before the main
466 * timer loop asks for metadata again. */
467 n = sprintf(cmd, "QM %d\r", devc->meas_type);
468 if (serial_write_blocking(serial, cmd, n, SERIAL_WRITE_TIMEOUT_MS) < 0)
469 sr_err("Unable to send QM (measurement).");
470 }
471 } else {
472 /* Response to QM <n> measurement request. */
473 handle_qm_19x_data(sdi, tokens);
474 }
475 }
476 }
477 g_strfreev(tokens);
478 devc->buflen = 0;
479
480 if (analog) {
481 /* Got a measurement. */
482 packet.type = SR_DF_ANALOG;
483 packet.payload = analog;
484 sr_session_send(sdi, &packet);
485 sr_sw_limits_update_samples_read(&devc->limits, 1);
486 g_free(analog->data);
487 g_free(analog);
488 }
489
490}
491
492SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
493{
494 struct sr_dev_inst *sdi;
495 struct dev_context *devc;
496 struct sr_serial_dev_inst *serial;
497 int len;
498 int64_t now, elapsed;
499
500 (void)fd;
501
502 if (!(sdi = cb_data))
503 return TRUE;
504
505 if (!(devc = sdi->priv))
506 return TRUE;
507
508 serial = sdi->conn;
509 if (revents == G_IO_IN) {
510 /* Serial data arrived. */
511 while (FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
512 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
513 if (len < 1)
514 break;
515 devc->buflen++;
516 *(devc->buf + devc->buflen) = '\0';
517 if (*(devc->buf + devc->buflen - 1) == '\r') {
518 *(devc->buf + --devc->buflen) = '\0';
519 handle_line(sdi);
520 break;
521 }
522 }
523 }
524
525 if (sr_sw_limits_check(&devc->limits)) {
526 sdi->driver->dev_acquisition_stop(sdi);
527 return TRUE;
528 }
529
530 now = g_get_monotonic_time() / 1000;
531 elapsed = now - devc->cmd_sent_at;
532 /* Send query command at poll_period interval, or after 1 second
533 * has elapsed. This will make it easier to recover from any
534 * out-of-sync or temporary disconnect issues. */
535 if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
536 || elapsed > devc->profile->timeout) {
537 if (serial_write_blocking(serial, "QM\r", 3, SERIAL_WRITE_TIMEOUT_MS) < 0)
538 sr_err("Unable to send QM.");
539 devc->cmd_sent_at = now;
540 devc->expect_response = TRUE;
541 }
542
543 return TRUE;
544}