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