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 *handle_qm_18x(const struct sr_dev_inst *sdi,
32 struct sr_datafeed_analog *analog;
33 struct sr_analog_encoding encoding;
34 struct sr_analog_meaning meaning;
35 struct sr_analog_spec spec;
40 if (strcmp(tokens[0], "QM") || !tokens[1])
43 if ((e = strstr(tokens[1], "Out of range"))) {
46 while (*e && *e != '.')
50 /* Delimit the float, since sr_atof_ascii() wants only
51 * a valid float here. */
53 while (*e && *e != ' ')
56 if (sr_atof_ascii(tokens[1], &fvalue) != SR_OK || fvalue == 0.0) {
57 /* Happens all the time, when switching modes. */
58 sr_dbg("Invalid float.");
62 while (*e && *e == ' ')
65 analog = g_malloc0(sizeof(struct sr_datafeed_analog));
66 sr_analog_init(analog, &encoding, &meaning, &spec, 0);
67 analog->data = g_malloc(sizeof(float));
68 analog->meaning->channels = sdi->channels;
69 analog->num_samples = 1;
71 *((float *)analog->data) = NAN;
73 *((float *)analog->data) = fvalue;
74 analog->meaning->mq = 0;
76 if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) {
77 analog->meaning->mq = SR_MQ_VOLTAGE;
78 analog->meaning->unit = SR_UNIT_VOLT;
79 if (!is_oor && e[0] == 'm')
80 *((float *)analog->data) /= 1000;
81 /* This catches "V AC", "V DC" and "V AC+DC". */
83 analog->meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
85 analog->meaning->mqflags |= SR_MQFLAG_DC;
86 } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
87 analog->meaning->mq = SR_MQ_VOLTAGE;
89 analog->meaning->unit = SR_UNIT_DECIBEL_MW;
91 analog->meaning->unit = SR_UNIT_DECIBEL_VOLT;
92 analog->meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
93 } else if ((u = strstr(e, "Ohms"))) {
94 analog->meaning->mq = SR_MQ_RESISTANCE;
95 analog->meaning->unit = SR_UNIT_OHM;
97 *((float *)analog->data) = INFINITY;
99 *((float *)analog->data) *= 1000;
100 else if (e[0] == 'M')
101 *((float *)analog->data) *= 1000000;
102 } else if (!strcmp(e, "nS")) {
103 analog->meaning->mq = SR_MQ_CONDUCTANCE;
104 analog->meaning->unit = SR_UNIT_SIEMENS;
105 *((float *)analog->data) /= 1e+9;
106 } else if ((u = strstr(e, "Farads"))) {
107 analog->meaning->mq = SR_MQ_CAPACITANCE;
108 analog->meaning->unit = SR_UNIT_FARAD;
111 *((float *)analog->data) /= 1e+3;
112 else if (e[0] == 'u')
113 *((float *)analog->data) /= 1e+6;
114 else if (e[0] == 'n')
115 *((float *)analog->data) /= 1e+9;
117 } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
118 analog->meaning->mq = SR_MQ_TEMPERATURE;
120 analog->meaning->unit = SR_UNIT_CELSIUS;
122 analog->meaning->unit = SR_UNIT_FAHRENHEIT;
123 } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) {
124 analog->meaning->mq = SR_MQ_CURRENT;
125 analog->meaning->unit = SR_UNIT_AMPERE;
126 /* This catches "A AC", "A DC" and "A AC+DC". */
128 analog->meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
130 analog->meaning->mqflags |= SR_MQFLAG_DC;
133 *((float *)analog->data) /= 1e+3;
134 else if (e[0] == 'u')
135 *((float *)analog->data) /= 1e+6;
137 } else if ((u = strstr(e, "Hz"))) {
138 analog->meaning->mq = SR_MQ_FREQUENCY;
139 analog->meaning->unit = SR_UNIT_HERTZ;
141 *((float *)analog->data) *= 1e+3;
142 } else if (!strcmp(e, "%")) {
143 analog->meaning->mq = SR_MQ_DUTY_CYCLE;
144 analog->meaning->unit = SR_UNIT_PERCENTAGE;
145 } else if ((u = strstr(e, "ms"))) {
146 analog->meaning->mq = SR_MQ_PULSE_WIDTH;
147 analog->meaning->unit = SR_UNIT_SECOND;
148 *((float *)analog->data) /= 1e+3;
151 if (analog->meaning->mq == 0) {
152 /* Not a valid measurement. */
153 g_free(analog->data);
161 static struct sr_datafeed_analog *handle_qm_28x(const struct sr_dev_inst *sdi,
164 struct sr_datafeed_analog *analog;
165 struct sr_analog_encoding encoding;
166 struct sr_analog_meaning meaning;
167 struct sr_analog_spec spec;
173 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
174 sr_err("Invalid float '%s'.", tokens[0]);
178 analog = g_malloc0(sizeof(struct sr_datafeed_analog));
179 sr_analog_init(analog, &encoding, &meaning, &spec, 0);
180 analog->data = g_malloc(sizeof(float));
181 analog->meaning->channels = sdi->channels;
182 analog->num_samples = 1;
183 *((float *)analog->data) = fvalue;
184 analog->meaning->mq = 0;
186 if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) {
187 analog->meaning->mq = SR_MQ_VOLTAGE;
188 analog->meaning->unit = SR_UNIT_VOLT;
189 if (!strcmp(tokens[2], "NORMAL")) {
190 if (tokens[1][1] == 'A') {
191 analog->meaning->mqflags |= SR_MQFLAG_AC;
192 analog->meaning->mqflags |= SR_MQFLAG_RMS;
194 analog->meaning->mqflags |= SR_MQFLAG_DC;
195 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
196 *((float *)analog->data) = NAN;
198 analog->meaning->mq = 0;
199 } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
200 analog->meaning->mq = SR_MQ_VOLTAGE;
201 if (tokens[1][2] == 'm')
202 analog->meaning->unit = SR_UNIT_DECIBEL_MW;
204 analog->meaning->unit = SR_UNIT_DECIBEL_VOLT;
205 analog->meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
206 } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
207 if (!strcmp(tokens[2], "NORMAL")) {
208 analog->meaning->mq = SR_MQ_TEMPERATURE;
209 if (tokens[1][0] == 'C')
210 analog->meaning->unit = SR_UNIT_CELSIUS;
212 analog->meaning->unit = SR_UNIT_FAHRENHEIT;
214 } else if (!strcmp(tokens[1], "OHM")) {
215 if (!strcmp(tokens[3], "NONE")) {
216 analog->meaning->mq = SR_MQ_RESISTANCE;
217 analog->meaning->unit = SR_UNIT_OHM;
218 if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
219 *((float *)analog->data) = INFINITY;
220 } else if (strcmp(tokens[2], "NORMAL"))
221 analog->meaning->mq = 0;
222 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
223 analog->meaning->mq = SR_MQ_CONTINUITY;
224 analog->meaning->unit = SR_UNIT_BOOLEAN;
225 *((float *)analog->data) = 0.0;
226 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
227 analog->meaning->mq = SR_MQ_CONTINUITY;
228 analog->meaning->unit = SR_UNIT_BOOLEAN;
229 *((float *)analog->data) = 1.0;
231 } else if (!strcmp(tokens[1], "F")
232 && !strcmp(tokens[2], "NORMAL")
233 && !strcmp(tokens[3], "NONE")) {
234 analog->meaning->mq = SR_MQ_CAPACITANCE;
235 analog->meaning->unit = SR_UNIT_FARAD;
236 } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
237 analog->meaning->mq = SR_MQ_CURRENT;
238 analog->meaning->unit = SR_UNIT_AMPERE;
239 if (!strcmp(tokens[2], "NORMAL")) {
240 if (tokens[1][1] == 'A') {
241 analog->meaning->mqflags |= SR_MQFLAG_AC;
242 analog->meaning->mqflags |= SR_MQFLAG_RMS;
244 analog->meaning->mqflags |= SR_MQFLAG_DC;
245 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
246 *((float *)analog->data) = NAN;
248 analog->meaning->mq = 0;
249 } if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
250 analog->meaning->mq = SR_MQ_FREQUENCY;
251 analog->meaning->unit = SR_UNIT_HERTZ;
252 } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
253 analog->meaning->mq = SR_MQ_DUTY_CYCLE;
254 analog->meaning->unit = SR_UNIT_PERCENTAGE;
255 } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
256 analog->meaning->mq = SR_MQ_PULSE_WIDTH;
257 analog->meaning->unit = SR_UNIT_SECOND;
258 } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
259 analog->meaning->mq = SR_MQ_CONDUCTANCE;
260 analog->meaning->unit = SR_UNIT_SIEMENS;
263 if (analog->meaning->mq == 0) {
264 /* Not a valid measurement. */
265 g_free(analog->data);
273 static void handle_qm_19x_meta(const struct sr_dev_inst *sdi, char **tokens)
275 struct dev_context *devc;
276 int meas_type, meas_unit, meas_char, i;
278 /* Make sure we have 7 valid tokens. */
279 for (i = 0; tokens[i] && i < 7; i++);
283 if (strcmp(tokens[1], "1"))
284 /* Invalid measurement. */
287 if (strcmp(tokens[2], "3"))
288 /* Only interested in input from the meter mode source. */
293 /* Measurement type 11 == absolute, 19 = relative */
294 meas_type = strtol(tokens[0], NULL, 10);
295 if (meas_type != 11 && meas_type != 19)
296 /* Device is in some mode we don't support. */
299 /* We might get metadata for absolute and relative mode (if the device
300 * is in relative mode). In that case, relative takes precedence. */
301 if (meas_type == 11 && devc->meas_type == 19)
304 meas_unit = strtol(tokens[3], NULL, 10);
306 /* Device is turned off. Really. */
308 meas_char = strtol(tokens[4], NULL, 10);
310 devc->mq = devc->unit = devc->mqflags = 0;
313 devc->mq = SR_MQ_VOLTAGE;
314 devc->unit = SR_UNIT_VOLT;
316 devc->mqflags |= SR_MQFLAG_DC;
317 else if (meas_char == 2)
318 devc->mqflags |= SR_MQFLAG_AC;
319 else if (meas_char == 3)
320 devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
321 else if (meas_char == 15)
322 devc->mqflags |= SR_MQFLAG_DIODE;
325 devc->mq = SR_MQ_CURRENT;
326 devc->unit = SR_UNIT_AMPERE;
328 devc->mqflags |= SR_MQFLAG_DC;
329 else if (meas_char == 2)
330 devc->mqflags |= SR_MQFLAG_AC;
331 else if (meas_char == 3)
332 devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
335 if (meas_char == 1) {
336 devc->mq = SR_MQ_RESISTANCE;
337 devc->unit = SR_UNIT_OHM;
338 } else if (meas_char == 16) {
339 devc->mq = SR_MQ_CONTINUITY;
340 devc->unit = SR_UNIT_BOOLEAN;
344 devc->mq = SR_MQ_TEMPERATURE;
345 devc->unit = SR_UNIT_CELSIUS;
348 devc->mq = SR_MQ_TEMPERATURE;
349 devc->unit = SR_UNIT_FAHRENHEIT;
352 sr_dbg("unknown unit: %d", meas_unit);
354 if (devc->mq == 0 && devc->unit == 0)
357 /* If we got here, we know how to interpret the measurement. */
358 devc->meas_type = meas_type;
360 /* Absolute meter reading. */
361 devc->is_relative = FALSE;
362 else if (!strcmp(tokens[0], "19"))
363 /* Relative meter reading. */
364 devc->is_relative = TRUE;
368 static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens)
370 struct dev_context *devc;
371 struct sr_datafeed_packet packet;
372 struct sr_datafeed_analog analog;
373 struct sr_analog_encoding encoding;
374 struct sr_analog_meaning meaning;
375 struct sr_analog_spec spec;
378 if (!strcmp(tokens[0], "9.9E+37")) {
379 /* An invalid measurement shows up on the display as "OL", but
380 * comes through like this. Since comparing 38-digit floats
381 * is rather problematic, we'll cut through this here. */
384 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
385 sr_err("Invalid float '%s'.", tokens[0]);
391 if (devc->mq == 0 || devc->unit == 0)
392 /* Don't have valid metadata yet. */
395 if (devc->mq == SR_MQ_RESISTANCE && isnan(fvalue))
397 else if (devc->mq == SR_MQ_CONTINUITY) {
404 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
405 analog.meaning->channels = sdi->channels;
406 analog.num_samples = 1;
407 analog.data = &fvalue;
408 analog.meaning->mq = devc->mq;
409 analog.meaning->unit = devc->unit;
410 analog.meaning->mqflags = 0;
411 packet.type = SR_DF_ANALOG;
412 packet.payload = &analog;
413 sr_session_send(sdi, &packet);
415 sr_sw_limits_update_samples_read(&devc->limits, 1);
418 static void handle_line(const struct sr_dev_inst *sdi)
420 struct dev_context *devc;
421 struct sr_serial_dev_inst *serial;
422 struct sr_datafeed_packet packet;
423 struct sr_datafeed_analog *analog;
424 int num_tokens, n, i;
425 char cmd[16], **tokens;
429 sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
431 if (devc->buflen == 1) {
432 if (devc->buf[0] != '0') {
433 /* Not just a CMD_ACK from the query command. */
434 sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
435 devc->expect_response = FALSE;
442 tokens = g_strsplit(devc->buf, ",", 0);
444 if (devc->profile->model == FLUKE_187 || devc->profile->model == FLUKE_189) {
445 devc->expect_response = FALSE;
446 analog = handle_qm_18x(sdi, tokens);
447 } else if (devc->profile->model == FLUKE_287 || devc->profile->model == FLUKE_289) {
448 devc->expect_response = FALSE;
449 analog = handle_qm_28x(sdi, tokens);
450 } else if (devc->profile->model == FLUKE_190) {
451 devc->expect_response = FALSE;
452 for (num_tokens = 0; tokens[num_tokens]; num_tokens++);
453 if (num_tokens >= 7) {
454 /* Response to QM: this is a comma-separated list of
455 * fields with metadata about the measurement. This
456 * format can return multiple sets of metadata,
457 * split into sets of 7 tokens each. */
459 for (i = 0; i < num_tokens; i += 7)
460 handle_qm_19x_meta(sdi, tokens + i);
461 if (devc->meas_type) {
462 /* Slip the request in now, before the main
463 * timer loop asks for metadata again. */
464 n = sprintf(cmd, "QM %d\r", devc->meas_type);
465 if (serial_write_blocking(serial, cmd, n, SERIAL_WRITE_TIMEOUT_MS) < 0)
466 sr_err("Unable to send QM (measurement).");
469 /* Response to QM <n> measurement request. */
470 handle_qm_19x_data(sdi, tokens);
478 /* Got a measurement. */
479 packet.type = SR_DF_ANALOG;
480 packet.payload = analog;
481 sr_session_send(sdi, &packet);
482 sr_sw_limits_update_samples_read(&devc->limits, 1);
483 g_free(analog->data);
489 SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
491 struct sr_dev_inst *sdi;
492 struct dev_context *devc;
493 struct sr_serial_dev_inst *serial;
495 int64_t now, elapsed;
499 if (!(sdi = cb_data))
502 if (!(devc = sdi->priv))
506 if (revents == G_IO_IN) {
507 /* Serial data arrived. */
508 while (FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
509 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
513 *(devc->buf + devc->buflen) = '\0';
514 if (*(devc->buf + devc->buflen - 1) == '\r') {
515 *(devc->buf + --devc->buflen) = '\0';
522 if (sr_sw_limits_check(&devc->limits)) {
523 sdi->driver->dev_acquisition_stop(sdi);
527 now = g_get_monotonic_time() / 1000;
528 elapsed = now - devc->cmd_sent_at;
529 /* Send query command at poll_period interval, or after 1 second
530 * has elapsed. This will make it easier to recover from any
531 * out-of-sync or temporary disconnect issues. */
532 if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
533 || elapsed > devc->profile->timeout) {
534 if (serial_write_blocking(serial, "QM\r", 3, SERIAL_WRITE_TIMEOUT_MS) < 0)
535 sr_err("Unable to send QM.");
536 devc->cmd_sent_at = now;
537 devc->expect_response = TRUE;