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