]> sigrok.org Git - libsigrok.git/blame - src/hardware/fluke-dmm/fluke.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / 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>
4cea0ff7 23#include <glib.h>
c1aae900 24#include <libsigrok/libsigrok.h>
4cea0ff7
BV
25#include "libsigrok-internal.h"
26#include "fluke-dmm.h"
d38d2ef0 27
4cea0ff7 28static struct sr_datafeed_analog *handle_qm_18x(const struct sr_dev_inst *sdi,
fe31f8b9
BV
29 char **tokens)
30{
31 struct sr_datafeed_analog *analog;
32 float fvalue;
33 char *e, *u;
34 gboolean is_oor;
35
4cea0ff7 36 if (strcmp(tokens[0], "QM") || !tokens[1])
fe31f8b9
BV
37 return NULL;
38
39 if ((e = strstr(tokens[1], "Out of range"))) {
40 is_oor = TRUE;
41 fvalue = -1;
0c5f2abc 42 while (*e && *e != '.')
357e341d 43 e++;
fe31f8b9
BV
44 } else {
45 is_oor = FALSE;
357e341d
BV
46 /* Delimit the float, since sr_atof_ascii() wants only
47 * a valid float here. */
48 e = tokens[1];
0c5f2abc 49 while (*e && *e != ' ')
357e341d
BV
50 e++;
51 *e++ = '\0';
52 if (sr_atof_ascii(tokens[1], &fvalue) != SR_OK || fvalue == 0.0) {
fe31f8b9 53 /* Happens all the time, when switching modes. */
31d84da3 54 sr_dbg("Invalid float.");
fe31f8b9
BV
55 return NULL;
56 }
57 }
0c5f2abc 58 while (*e && *e == ' ')
fe31f8b9
BV
59 e++;
60
a95f142e
UH
61 analog = g_malloc0(sizeof(struct sr_datafeed_analog));
62 analog->data = g_malloc(sizeof(float));
ba7dd8bb 63 analog->channels = sdi->channels;
fe31f8b9 64 analog->num_samples = 1;
fe31f8b9
BV
65 if (is_oor)
66 *analog->data = NAN;
67 else
68 *analog->data = fvalue;
69 analog->mq = -1;
70
71 if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) {
72 analog->mq = SR_MQ_VOLTAGE;
73 analog->unit = SR_UNIT_VOLT;
74 if (!is_oor && e[0] == 'm')
75 *analog->data /= 1000;
76 /* This catches "V AC", "V DC" and "V AC+DC". */
77 if (strstr(u, "AC"))
78 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
79 if (strstr(u, "DC"))
80 analog->mqflags |= SR_MQFLAG_DC;
81 } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
82 analog->mq = SR_MQ_VOLTAGE;
83 if (u[2] == 'm')
84 analog->unit = SR_UNIT_DECIBEL_MW;
85 else
86 analog->unit = SR_UNIT_DECIBEL_VOLT;
87 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
88 } else if ((u = strstr(e, "Ohms"))) {
89 analog->mq = SR_MQ_RESISTANCE;
90 analog->unit = SR_UNIT_OHM;
91 if (is_oor)
92 *analog->data = INFINITY;
93 else if (e[0] == 'k')
94 *analog->data *= 1000;
95 else if (e[0] == 'M')
96 *analog->data *= 1000000;
97 } else if (!strcmp(e, "nS")) {
98 analog->mq = SR_MQ_CONDUCTANCE;
99 analog->unit = SR_UNIT_SIEMENS;
100 *analog->data /= 1e+9;
101 } else if ((u = strstr(e, "Farads"))) {
102 analog->mq = SR_MQ_CAPACITANCE;
103 analog->unit = SR_UNIT_FARAD;
104 if (!is_oor) {
105 if (e[0] == 'm')
106 *analog->data /= 1e+3;
107 else if (e[0] == 'u')
108 *analog->data /= 1e+6;
109 else if (e[0] == 'n')
110 *analog->data /= 1e+9;
111 }
112 } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
113 analog->mq = SR_MQ_TEMPERATURE;
114 if (u[4] == 'C')
115 analog->unit = SR_UNIT_CELSIUS;
116 else
117 analog->unit = SR_UNIT_FAHRENHEIT;
118 } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) {
119 analog->mq = SR_MQ_CURRENT;
120 analog->unit = SR_UNIT_AMPERE;
121 /* This catches "A AC", "A DC" and "A AC+DC". */
122 if (strstr(u, "AC"))
123 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
124 if (strstr(u, "DC"))
125 analog->mqflags |= SR_MQFLAG_DC;
126 if (!is_oor) {
127 if (e[0] == 'm')
128 *analog->data /= 1e+3;
129 else if (e[0] == 'u')
130 *analog->data /= 1e+6;
131 }
132 } else if ((u = strstr(e, "Hz"))) {
133 analog->mq = SR_MQ_FREQUENCY;
134 analog->unit = SR_UNIT_HERTZ;
135 if (e[0] == 'k')
136 *analog->data *= 1e+3;
137 } else if (!strcmp(e, "%")) {
138 analog->mq = SR_MQ_DUTY_CYCLE;
139 analog->unit = SR_UNIT_PERCENTAGE;
140 } else if ((u = strstr(e, "ms"))) {
141 analog->mq = SR_MQ_PULSE_WIDTH;
142 analog->unit = SR_UNIT_SECOND;
143 *analog->data /= 1e+3;
144 }
145
146 if (analog->mq == -1) {
147 /* Not a valid measurement. */
148 g_free(analog->data);
149 g_free(analog);
150 analog = NULL;
151 }
152
153 return analog;
154}
155
4cea0ff7 156static struct sr_datafeed_analog *handle_qm_28x(const struct sr_dev_inst *sdi,
d38d2ef0
BV
157 char **tokens)
158{
159 struct sr_datafeed_analog *analog;
160 float fvalue;
d38d2ef0 161
4cea0ff7
BV
162 if (!tokens[1])
163 return NULL;
164
357e341d
BV
165 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
166 sr_err("Invalid float '%s'.", tokens[0]);
d38d2ef0
BV
167 return NULL;
168 }
169
a95f142e
UH
170 analog = g_malloc0(sizeof(struct sr_datafeed_analog));
171 analog->data = g_malloc(sizeof(float));
ba7dd8bb 172 analog->channels = sdi->channels;
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;
11fb7110
BV
366
367 if (!strcmp(tokens[0], "9.9E+37")) {
368 /* An invalid measurement shows up on the display as "OL", but
369 * comes through like this. Since comparing 38-digit floats
370 * is rather problematic, we'll cut through this here. */
371 fvalue = NAN;
372 } else {
357e341d 373 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
11fb7110
BV
374 sr_err("Invalid float '%s'.", tokens[0]);
375 return;
376 }
377 }
378
379 devc = sdi->priv;
380 if (devc->mq == -1 || devc->unit == -1)
381 /* Don't have valid metadata yet. */
382 return;
383
11fb7110
BV
384 if (devc->mq == SR_MQ_RESISTANCE && isnan(fvalue))
385 fvalue = INFINITY;
386 else if (devc->mq == SR_MQ_CONTINUITY) {
387 if (isnan(fvalue))
388 fvalue = 0.0;
389 else
390 fvalue = 1.0;
391 }
392
ba7dd8bb 393 analog.channels = sdi->channels;
11fb7110
BV
394 analog.num_samples = 1;
395 analog.data = &fvalue;
396 analog.mq = devc->mq;
397 analog.unit = devc->unit;
398 analog.mqflags = 0;
399 packet.type = SR_DF_ANALOG;
400 packet.payload = &analog;
401 sr_session_send(devc->cb_data, &packet);
402 devc->num_samples++;
403
404}
405
d38d2ef0
BV
406static void handle_line(const struct sr_dev_inst *sdi)
407{
408 struct dev_context *devc;
919681f0 409 struct sr_serial_dev_inst *serial;
d38d2ef0
BV
410 struct sr_datafeed_packet packet;
411 struct sr_datafeed_analog *analog;
11fb7110
BV
412 int num_tokens, n, i;
413 char cmd[16], **tokens;
d38d2ef0
BV
414
415 devc = sdi->priv;
919681f0 416 serial = sdi->conn;
31d84da3 417 sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
d38d2ef0
BV
418
419 if (devc->buflen == 1) {
420 if (devc->buf[0] != '0') {
421 /* Not just a CMD_ACK from the query command. */
31d84da3 422 sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
d38d2ef0
BV
423 devc->expect_response = FALSE;
424 }
425 devc->buflen = 0;
426 return;
427 }
428
429 analog = NULL;
430 tokens = g_strsplit(devc->buf, ",", 0);
4cea0ff7 431 if (tokens[0]) {
0bc3ab92 432 if (devc->profile->model == FLUKE_187 || devc->profile->model == FLUKE_189) {
fe31f8b9 433 devc->expect_response = FALSE;
4cea0ff7 434 analog = handle_qm_18x(sdi, tokens);
fe31f8b9 435 } else if (devc->profile->model == FLUKE_287) {
d38d2ef0 436 devc->expect_response = FALSE;
4cea0ff7 437 analog = handle_qm_28x(sdi, tokens);
11fb7110
BV
438 } else if (devc->profile->model == FLUKE_190) {
439 devc->expect_response = FALSE;
440 for (num_tokens = 0; tokens[num_tokens]; num_tokens++);
441 if (num_tokens >= 7) {
442 /* Response to QM: this is a comma-separated list of
443 * fields with metadata about the measurement. This
444 * format can return multiple sets of metadata,
445 * split into sets of 7 tokens each. */
446 devc->meas_type = 0;
447 for (i = 0; i < num_tokens; i += 7)
448 handle_qm_19x_meta(sdi, tokens + i);
449 if (devc->meas_type) {
450 /* Slip the request in now, before the main
451 * timer loop asks for metadata again. */
452 n = sprintf(cmd, "QM %d\r", devc->meas_type);
20401400 453 if (serial_write_blocking(serial, cmd, n, SERIAL_WRITE_TIMEOUT_MS) < 0)
bb27c765 454 sr_err("Unable to send QM (measurement).");
11fb7110
BV
455 }
456 } else {
457 /* Response to QM <n> measurement request. */
458 handle_qm_19x_data(sdi, tokens);
459 }
d38d2ef0 460 }
d38d2ef0
BV
461 }
462 g_strfreev(tokens);
463 devc->buflen = 0;
464
465 if (analog) {
466 /* Got a measurement. */
d38d2ef0
BV
467 packet.type = SR_DF_ANALOG;
468 packet.payload = analog;
469 sr_session_send(devc->cb_data, &packet);
470 devc->num_samples++;
471 g_free(analog->data);
472 g_free(analog);
473 }
474
475}
476
477SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
478{
642e9d62 479 struct sr_dev_inst *sdi;
d38d2ef0 480 struct dev_context *devc;
919681f0 481 struct sr_serial_dev_inst *serial;
d38d2ef0
BV
482 int len;
483 int64_t now, elapsed;
484
58d03f03
BV
485 (void)fd;
486
d38d2ef0
BV
487 if (!(sdi = cb_data))
488 return TRUE;
489
490 if (!(devc = sdi->priv))
491 return TRUE;
492
919681f0 493 serial = sdi->conn;
d38d2ef0
BV
494 if (revents == G_IO_IN) {
495 /* Serial data arrived. */
0c5f2abc 496 while (FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
707fa85a 497 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
d38d2ef0
BV
498 if (len < 1)
499 break;
500 devc->buflen++;
501 *(devc->buf + devc->buflen) = '\0';
502 if (*(devc->buf + devc->buflen - 1) == '\r') {
503 *(devc->buf + --devc->buflen) = '\0';
504 handle_line(sdi);
505 break;
506 }
507 }
508 }
509
2f1e5c7c 510 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
d38d2ef0
BV
511 sdi->driver->dev_acquisition_stop(sdi, cb_data);
512 return TRUE;
513 }
514
515 now = g_get_monotonic_time() / 1000;
516 elapsed = now - devc->cmd_sent_at;
517 /* Send query command at poll_period interval, or after 1 second
d4b11de0
BV
518 * has elapsed. This will make it easier to recover from any
519 * out-of-sync or temporary disconnect issues. */
d38d2ef0 520 if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
d4b11de0 521 || elapsed > devc->profile->timeout) {
20401400 522 if (serial_write_blocking(serial, "QM\r", 3, SERIAL_WRITE_TIMEOUT_MS) < 0)
bb27c765 523 sr_err("Unable to send QM.");
d38d2ef0
BV
524 devc->cmd_sent_at = now;
525 devc->expect_response = TRUE;
526 }
527
528 return TRUE;
529}