2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
27 #include "fluke-dmm.h"
29 static struct sr_datafeed_analog_old *handle_qm_18x(const struct sr_dev_inst *sdi,
32 struct sr_datafeed_analog_old *analog;
37 if (strcmp(tokens[0], "QM") || !tokens[1])
40 if ((e = strstr(tokens[1], "Out of range"))) {
43 while (*e && *e != '.')
47 /* Delimit the float, since sr_atof_ascii() wants only
48 * a valid float here. */
50 while (*e && *e != ' ')
53 if (sr_atof_ascii(tokens[1], &fvalue) != SR_OK || fvalue == 0.0) {
54 /* Happens all the time, when switching modes. */
55 sr_dbg("Invalid float.");
59 while (*e && *e == ' ')
62 analog = g_malloc0(sizeof(struct sr_datafeed_analog_old));
63 analog->data = g_malloc(sizeof(float));
64 analog->channels = sdi->channels;
65 analog->num_samples = 1;
69 *analog->data = fvalue;
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". */
79 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
81 analog->mqflags |= SR_MQFLAG_DC;
82 } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
83 analog->mq = SR_MQ_VOLTAGE;
85 analog->unit = SR_UNIT_DECIBEL_MW;
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;
93 *analog->data = INFINITY;
95 *analog->data *= 1000;
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;
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;
113 } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
114 analog->mq = SR_MQ_TEMPERATURE;
116 analog->unit = SR_UNIT_CELSIUS;
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". */
124 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
126 analog->mqflags |= SR_MQFLAG_DC;
129 *analog->data /= 1e+3;
130 else if (e[0] == 'u')
131 *analog->data /= 1e+6;
133 } else if ((u = strstr(e, "Hz"))) {
134 analog->mq = SR_MQ_FREQUENCY;
135 analog->unit = SR_UNIT_HERTZ;
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;
147 if (analog->mq == -1) {
148 /* Not a valid measurement. */
149 g_free(analog->data);
157 static struct sr_datafeed_analog_old *handle_qm_28x(const struct sr_dev_inst *sdi,
160 struct sr_datafeed_analog_old *analog;
166 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
167 sr_err("Invalid float '%s'.", tokens[0]);
171 analog = g_malloc0(sizeof(struct sr_datafeed_analog_old));
172 analog->data = g_malloc(sizeof(float));
173 analog->channels = sdi->channels;
174 analog->num_samples = 1;
175 *analog->data = fvalue;
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;
186 analog->mqflags |= SR_MQFLAG_DC;
187 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
191 } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
192 analog->mq = SR_MQ_VOLTAGE;
193 if (tokens[1][2] == 'm')
194 analog->unit = SR_UNIT_DECIBEL_MW;
196 analog->unit = SR_UNIT_DECIBEL_VOLT;
197 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
198 } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
199 if (!strcmp(tokens[2], "NORMAL")) {
200 analog->mq = SR_MQ_TEMPERATURE;
201 if (tokens[1][0] == 'C')
202 analog->unit = SR_UNIT_CELSIUS;
204 analog->unit = SR_UNIT_FAHRENHEIT;
206 } else if (!strcmp(tokens[1], "OHM")) {
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"))
214 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
215 analog->mq = SR_MQ_CONTINUITY;
216 analog->unit = SR_UNIT_BOOLEAN;
218 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
219 analog->mq = SR_MQ_CONTINUITY;
220 analog->unit = SR_UNIT_BOOLEAN;
223 } else if (!strcmp(tokens[1], "F")
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;
231 if (!strcmp(tokens[2], "NORMAL")) {
232 if (tokens[1][1] == 'A') {
233 analog->mqflags |= SR_MQFLAG_AC;
234 analog->mqflags |= SR_MQFLAG_RMS;
236 analog->mqflags |= SR_MQFLAG_DC;
237 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
241 } if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
242 analog->mq = SR_MQ_FREQUENCY;
243 analog->unit = SR_UNIT_HERTZ;
244 } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
245 analog->mq = SR_MQ_DUTY_CYCLE;
246 analog->unit = SR_UNIT_PERCENTAGE;
247 } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
248 analog->mq = SR_MQ_PULSE_WIDTH;
249 analog->unit = SR_UNIT_SECOND;
250 } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
251 analog->mq = SR_MQ_CONDUCTANCE;
252 analog->unit = SR_UNIT_SIEMENS;
255 if (analog->mq == -1) {
256 /* Not a valid measurement. */
257 g_free(analog->data);
265 static void handle_qm_19x_meta(const struct sr_dev_inst *sdi, char **tokens)
267 struct dev_context *devc;
268 int meas_type, meas_unit, meas_char, i;
270 /* Make sure we have 7 valid tokens. */
271 for (i = 0; tokens[i] && i < 7; i++);
275 if (strcmp(tokens[1], "1"))
276 /* Invalid measurement. */
279 if (strcmp(tokens[2], "3"))
280 /* Only interested in input from the meter mode source. */
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. */
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)
296 meas_unit = strtol(tokens[3], NULL, 10);
298 /* Device is turned off. Really. */
300 meas_char = strtol(tokens[4], NULL, 10);
302 devc->mq = devc->unit = -1;
306 devc->mq = SR_MQ_VOLTAGE;
307 devc->unit = SR_UNIT_VOLT;
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;
318 devc->mq = SR_MQ_CURRENT;
319 devc->unit = SR_UNIT_AMPERE;
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;
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;
337 devc->mq = SR_MQ_TEMPERATURE;
338 devc->unit = SR_UNIT_CELSIUS;
341 devc->mq = SR_MQ_TEMPERATURE;
342 devc->unit = SR_UNIT_FAHRENHEIT;
345 sr_dbg("unknown unit: %d", meas_unit);
347 if (devc->mq == -1 && devc->unit == -1)
350 /* If we got here, we know how to interpret the measurement. */
351 devc->meas_type = meas_type;
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;
361 static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens)
363 struct dev_context *devc;
364 struct sr_datafeed_packet packet;
365 struct sr_datafeed_analog_old analog;
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. */
374 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
375 sr_err("Invalid float '%s'.", tokens[0]);
381 if (devc->mq == -1 || devc->unit == -1)
382 /* Don't have valid metadata yet. */
385 if (devc->mq == SR_MQ_RESISTANCE && isnan(fvalue))
387 else if (devc->mq == SR_MQ_CONTINUITY) {
394 analog.channels = sdi->channels;
395 analog.num_samples = 1;
396 analog.data = &fvalue;
397 analog.mq = devc->mq;
398 analog.unit = devc->unit;
400 packet.type = SR_DF_ANALOG_OLD;
401 packet.payload = &analog;
402 sr_session_send(devc->cb_data, &packet);
407 static void handle_line(const struct sr_dev_inst *sdi)
409 struct dev_context *devc;
410 struct sr_serial_dev_inst *serial;
411 struct sr_datafeed_packet packet;
412 struct sr_datafeed_analog_old *analog;
413 int num_tokens, n, i;
414 char cmd[16], **tokens;
418 sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
420 if (devc->buflen == 1) {
421 if (devc->buf[0] != '0') {
422 /* Not just a CMD_ACK from the query command. */
423 sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
424 devc->expect_response = FALSE;
431 tokens = g_strsplit(devc->buf, ",", 0);
433 if (devc->profile->model == FLUKE_187 || devc->profile->model == FLUKE_189) {
434 devc->expect_response = FALSE;
435 analog = handle_qm_18x(sdi, tokens);
436 } else if (devc->profile->model == FLUKE_287 || devc->profile->model == FLUKE_289) {
437 devc->expect_response = FALSE;
438 analog = handle_qm_28x(sdi, tokens);
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. */
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);
454 if (serial_write_blocking(serial, cmd, n, SERIAL_WRITE_TIMEOUT_MS) < 0)
455 sr_err("Unable to send QM (measurement).");
458 /* Response to QM <n> measurement request. */
459 handle_qm_19x_data(sdi, tokens);
467 /* Got a measurement. */
468 packet.type = SR_DF_ANALOG_OLD;
469 packet.payload = analog;
470 sr_session_send(devc->cb_data, &packet);
472 g_free(analog->data);
478 SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
480 struct sr_dev_inst *sdi;
481 struct dev_context *devc;
482 struct sr_serial_dev_inst *serial;
484 int64_t now, elapsed;
488 if (!(sdi = cb_data))
491 if (!(devc = sdi->priv))
495 if (revents == G_IO_IN) {
496 /* Serial data arrived. */
497 while (FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
498 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
502 *(devc->buf + devc->buflen) = '\0';
503 if (*(devc->buf + devc->buflen - 1) == '\r') {
504 *(devc->buf + --devc->buflen) = '\0';
511 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
512 sdi->driver->dev_acquisition_stop(sdi, cb_data);
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
519 * has elapsed. This will make it easier to recover from any
520 * out-of-sync or temporary disconnect issues. */
521 if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
522 || elapsed > devc->profile->timeout) {
523 if (serial_write_blocking(serial, "QM\r", 3, SERIAL_WRITE_TIMEOUT_MS) < 0)
524 sr_err("Unable to send QM.");
525 devc->cmd_sent_at = now;
526 devc->expect_response = TRUE;