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