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