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"
29 static int count_digits(const char *str)
33 if (*str == '-' || *str == '+')
36 while (*str >= '0' && *str <= '9')
42 while (*str >= '0' && *str <= '9') {
51 static void handle_qm_18x(const struct sr_dev_inst *sdi, char **tokens)
53 struct dev_context *devc;
54 struct sr_datafeed_packet packet;
55 struct sr_datafeed_analog analog;
56 struct sr_analog_encoding encoding;
57 struct sr_analog_meaning meaning;
58 struct sr_analog_spec spec;
66 enum sr_mqflag mqflags;
70 if (strcmp(tokens[0], "QM") || !tokens[1])
73 if ((e = strstr(tokens[1], "Out of range"))) {
77 while (*e && *e != '.')
81 /* Delimit the float, since sr_atof_ascii() wants only
82 * a valid float here. */
84 while (*e && *e != ' ')
87 if (sr_atof_ascii(tokens[1], &fvalue) != SR_OK) {
88 /* Happens all the time, when switching modes. */
89 sr_dbg("Invalid float: '%s'", tokens[1]);
92 digits = count_digits(tokens[1]);
94 while (*e && *e == ' ')
104 if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) {
107 if (!is_oor && e[0] == 'm')
109 /* This catches "V AC", "V DC" and "V AC+DC". */
111 mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
113 mqflags |= SR_MQFLAG_DC;
114 } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
117 unit = SR_UNIT_DECIBEL_MW;
119 unit = SR_UNIT_DECIBEL_VOLT;
120 mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
121 } else if ((u = strstr(e, "Ohms"))) {
122 mq = SR_MQ_RESISTANCE;
126 else if (e[0] == 'k')
128 else if (e[0] == 'M')
130 } else if (!strcmp(e, "nS")) {
131 mq = SR_MQ_CONDUCTANCE;
132 unit = SR_UNIT_SIEMENS;
134 } else if ((u = strstr(e, "Farads"))) {
135 mq = SR_MQ_CAPACITANCE;
136 unit = SR_UNIT_FARAD;
140 else if (e[0] == 'u')
142 else if (e[0] == 'n')
145 } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
146 mq = SR_MQ_TEMPERATURE;
148 unit = SR_UNIT_CELSIUS;
150 unit = SR_UNIT_FAHRENHEIT;
151 } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) {
153 unit = SR_UNIT_AMPERE;
154 /* This catches "A AC", "A DC" and "A AC+DC". */
156 mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
158 mqflags |= SR_MQFLAG_DC;
162 else if (e[0] == 'u')
165 } else if ((u = strstr(e, "Hz"))) {
166 mq = SR_MQ_FREQUENCY;
167 unit = SR_UNIT_HERTZ;
170 } else if (!strcmp(e, "%")) {
171 mq = SR_MQ_DUTY_CYCLE;
172 unit = SR_UNIT_PERCENTAGE;
173 } else if ((u = strstr(e, "ms"))) {
174 mq = SR_MQ_PULSE_WIDTH;
175 unit = SR_UNIT_SECOND;
180 /* Got a measurement. */
182 fvalue *= pow(10.0f, exponent);
184 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
185 analog.data = &fvalue;
186 analog.num_samples = 1;
187 analog.meaning->unit = unit;
188 analog.meaning->mq = mq;
189 analog.meaning->mqflags = mqflags;
190 analog.meaning->channels = sdi->channels;
192 packet.type = SR_DF_ANALOG;
193 packet.payload = &analog;
194 sr_session_send(sdi, &packet);
195 sr_sw_limits_update_samples_read(&devc->limits, 1);
199 static void handle_qm_28x(const struct sr_dev_inst *sdi, char **tokens)
201 struct dev_context *devc;
202 struct sr_datafeed_packet packet;
203 struct sr_datafeed_analog analog;
204 struct sr_analog_encoding encoding;
205 struct sr_analog_meaning meaning;
206 struct sr_analog_spec spec;
217 /* Split measurement into mantissa / exponent */
220 if (*e == 'e' || *e == 'E') {
228 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK) {
229 sr_err("Invalid mantissa '%s'.", tokens[0]);
233 if (sr_atoi(e, &exponent) != SR_OK) {
234 sr_err("Invalid exponent '%s'.", e);
238 digits = count_digits(tokens[0]) - exponent;
239 fvalue *= pow(10.0f, exponent);
241 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
242 analog.data = &fvalue;
243 analog.meaning->channels = sdi->channels;
244 analog.num_samples = 1;
245 analog.meaning->mq = 0;
247 if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) {
248 analog.meaning->mq = SR_MQ_VOLTAGE;
249 analog.meaning->unit = SR_UNIT_VOLT;
250 if (!strcmp(tokens[2], "NORMAL")) {
251 if (tokens[1][1] == 'A') {
252 analog.meaning->mqflags |= SR_MQFLAG_AC;
253 analog.meaning->mqflags |= SR_MQFLAG_RMS;
255 analog.meaning->mqflags |= SR_MQFLAG_DC;
256 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
259 analog.meaning->mq = 0;
260 } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
261 analog.meaning->mq = SR_MQ_VOLTAGE;
262 if (tokens[1][2] == 'm')
263 analog.meaning->unit = SR_UNIT_DECIBEL_MW;
265 analog.meaning->unit = SR_UNIT_DECIBEL_VOLT;
266 analog.meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
267 } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
268 if (!strcmp(tokens[2], "NORMAL")) {
269 analog.meaning->mq = SR_MQ_TEMPERATURE;
270 if (tokens[1][0] == 'C')
271 analog.meaning->unit = SR_UNIT_CELSIUS;
273 analog.meaning->unit = SR_UNIT_FAHRENHEIT;
275 } else if (!strcmp(tokens[1], "OHM")) {
276 if (!strcmp(tokens[3], "NONE")) {
277 analog.meaning->mq = SR_MQ_RESISTANCE;
278 analog.meaning->unit = SR_UNIT_OHM;
279 if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
281 } else if (strcmp(tokens[2], "NORMAL"))
282 analog.meaning->mq = 0;
283 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
284 analog.meaning->mq = SR_MQ_CONTINUITY;
285 analog.meaning->unit = SR_UNIT_BOOLEAN;
287 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
288 analog.meaning->mq = SR_MQ_CONTINUITY;
289 analog.meaning->unit = SR_UNIT_BOOLEAN;
292 } else if (!strcmp(tokens[1], "F")
293 && !strcmp(tokens[2], "NORMAL")
294 && !strcmp(tokens[3], "NONE")) {
295 analog.meaning->mq = SR_MQ_CAPACITANCE;
296 analog.meaning->unit = SR_UNIT_FARAD;
297 } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
298 analog.meaning->mq = SR_MQ_CURRENT;
299 analog.meaning->unit = SR_UNIT_AMPERE;
300 if (!strcmp(tokens[2], "NORMAL")) {
301 if (tokens[1][1] == 'A') {
302 analog.meaning->mqflags |= SR_MQFLAG_AC;
303 analog.meaning->mqflags |= SR_MQFLAG_RMS;
305 analog.meaning->mqflags |= SR_MQFLAG_DC;
306 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
309 analog.meaning->mq = 0;
310 } else if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
311 analog.meaning->mq = SR_MQ_FREQUENCY;
312 analog.meaning->unit = SR_UNIT_HERTZ;
313 } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
314 analog.meaning->mq = SR_MQ_DUTY_CYCLE;
315 analog.meaning->unit = SR_UNIT_PERCENTAGE;
316 } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
317 analog.meaning->mq = SR_MQ_PULSE_WIDTH;
318 analog.meaning->unit = SR_UNIT_SECOND;
319 } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
320 analog.meaning->mq = SR_MQ_CONDUCTANCE;
321 analog.meaning->unit = SR_UNIT_SIEMENS;
324 if (analog.meaning->mq != 0) {
325 /* Got a measurement. */
326 packet.type = SR_DF_ANALOG;
327 packet.payload = &analog;
328 sr_session_send(sdi, &packet);
329 sr_sw_limits_update_samples_read(&devc->limits, 1);
333 static void handle_qm_19x_meta(const struct sr_dev_inst *sdi, char **tokens)
335 struct dev_context *devc;
336 int meas_type, meas_unit, meas_char, i;
338 /* Make sure we have 7 valid tokens. */
339 for (i = 0; tokens[i] && i < 7; i++);
343 if (strcmp(tokens[1], "1"))
344 /* Invalid measurement. */
347 if (strcmp(tokens[2], "3"))
348 /* Only interested in input from the meter mode source. */
353 /* Measurement type 11 == absolute, 19 = relative */
354 meas_type = strtol(tokens[0], NULL, 10);
355 if (meas_type != 11 && meas_type != 19)
356 /* Device is in some mode we don't support. */
359 /* We might get metadata for absolute and relative mode (if the device
360 * is in relative mode). In that case, relative takes precedence. */
361 if (meas_type == 11 && devc->meas_type == 19)
364 meas_unit = strtol(tokens[3], NULL, 10);
366 /* Device is turned off. Really. */
368 meas_char = strtol(tokens[4], NULL, 10);
376 devc->mq = SR_MQ_VOLTAGE;
377 devc->unit = SR_UNIT_VOLT;
379 devc->mqflags |= SR_MQFLAG_DC;
380 else if (meas_char == 2)
381 devc->mqflags |= SR_MQFLAG_AC;
382 else if (meas_char == 3)
383 devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
384 else if (meas_char == 15)
385 devc->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
388 devc->mq = SR_MQ_CURRENT;
389 devc->unit = SR_UNIT_AMPERE;
391 devc->mqflags |= SR_MQFLAG_DC;
392 else if (meas_char == 2)
393 devc->mqflags |= SR_MQFLAG_AC;
394 else if (meas_char == 3)
395 devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
398 if (meas_char == 1) {
399 devc->mq = SR_MQ_RESISTANCE;
400 devc->unit = SR_UNIT_OHM;
401 } else if (meas_char == 16) {
402 devc->mq = SR_MQ_CONTINUITY;
403 devc->unit = SR_UNIT_BOOLEAN;
407 devc->mq = SR_MQ_TEMPERATURE;
408 devc->unit = SR_UNIT_CELSIUS;
411 devc->mq = SR_MQ_TEMPERATURE;
412 devc->unit = SR_UNIT_FAHRENHEIT;
415 sr_dbg("unknown unit: %d", meas_unit);
417 if (devc->mq == 0 && devc->unit == 0)
420 /* If we got here, we know how to interpret the measurement. */
421 devc->meas_type = meas_type;
423 /* Absolute meter reading. */
424 devc->is_relative = FALSE;
425 else if (!strcmp(tokens[0], "19"))
426 /* Relative meter reading. */
427 devc->is_relative = TRUE;
431 static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens)
433 struct dev_context *devc;
434 struct sr_datafeed_packet packet;
435 struct sr_datafeed_analog analog;
436 struct sr_analog_encoding encoding;
437 struct sr_analog_meaning meaning;
438 struct sr_analog_spec spec;
443 if (!strcmp(tokens[0], "9.9E+37")) {
444 /* An invalid measurement shows up on the display as "OL", but
445 * comes through like this. Since comparing 38-digit floats
446 * is rather problematic, we'll cut through this here. */
449 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
450 sr_err("Invalid float '%s'.", tokens[0]);
453 digits = count_digits(tokens[0]);
457 if (devc->mq == 0 || devc->unit == 0)
458 /* Don't have valid metadata yet. */
461 if (devc->mq == SR_MQ_RESISTANCE && isnan(fvalue))
463 else if (devc->mq == SR_MQ_CONTINUITY) {
470 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
471 analog.meaning->channels = sdi->channels;
472 analog.num_samples = 1;
473 analog.data = &fvalue;
474 analog.meaning->mq = devc->mq;
475 analog.meaning->unit = devc->unit;
476 analog.meaning->mqflags = 0;
477 packet.type = SR_DF_ANALOG;
478 packet.payload = &analog;
479 sr_session_send(sdi, &packet);
481 sr_sw_limits_update_samples_read(&devc->limits, 1);
484 static void handle_line(const struct sr_dev_inst *sdi)
486 struct dev_context *devc;
487 struct sr_serial_dev_inst *serial;
488 int num_tokens, n, i;
489 char cmd[16], **tokens;
494 sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
496 if (devc->buflen == 1) {
497 if (devc->buf[0] != '0') {
498 /* Not just a CMD_ACK from the query command. */
499 sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
500 devc->expect_response = FALSE;
506 tokens = g_strsplit(devc->buf, ",", 0);
508 switch (devc->profile->model) {
513 devc->expect_response = FALSE;
514 handle_qm_18x(sdi, tokens);
517 devc->expect_response = FALSE;
518 num_tokens = g_strv_length(tokens);
519 if (num_tokens < 7) {
520 /* Response to QM <n> measurement request. */
521 handle_qm_19x_data(sdi, tokens);
525 * Response to QM: This is a comma-separated list of
526 * fields with metadata about the measurement. This
527 * format can return multiple sets of metadata,
528 * split into sets of 7 tokens each.
531 for (i = 0; i < num_tokens; i += 7)
532 handle_qm_19x_meta(sdi, tokens + i);
533 if (devc->meas_type) {
535 * Slip the request in now, before the main
536 * timer loop asks for metadata again.
538 n = sprintf(cmd, "QM %d\r", devc->meas_type);
539 ret = serial_write_blocking(serial,
540 cmd, n, SERIAL_WRITE_TIMEOUT_MS);
542 sr_err("Cannot send QM (measurement).");
547 devc->expect_response = FALSE;
548 handle_qm_28x(sdi, tokens);
556 SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
558 struct sr_dev_inst *sdi;
559 struct dev_context *devc;
560 struct sr_serial_dev_inst *serial;
562 int64_t now, elapsed;
566 if (!(sdi = cb_data))
569 if (!(devc = sdi->priv))
573 if (revents == G_IO_IN) {
574 /* Serial data arrived. */
575 while (FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
576 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
580 *(devc->buf + devc->buflen) = '\0';
581 if (*(devc->buf + devc->buflen - 1) == '\r') {
582 *(devc->buf + --devc->buflen) = '\0';
589 if (sr_sw_limits_check(&devc->limits)) {
590 sr_dev_acquisition_stop(sdi);
594 now = g_get_monotonic_time() / 1000;
595 elapsed = now - devc->cmd_sent_at;
596 /* Send query command at poll_period interval, or after 1 second
597 * has elapsed. This will make it easier to recover from any
598 * out-of-sync or temporary disconnect issues. */
599 if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
600 || elapsed > devc->profile->timeout) {
601 if (serial_write_blocking(serial, "QM\r", 3, SERIAL_WRITE_TIMEOUT_MS) < 0)
602 sr_err("Unable to send QM.");
603 devc->cmd_sent_at = now;
604 devc->expect_response = TRUE;