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) {
32 while (*str && *str != ' ' && *str != ',' && *str != '.')
38 while (*str && *str != ' ' && *str != ',') {
47 static void handle_qm_18x(const struct sr_dev_inst *sdi, char **tokens)
49 struct dev_context *devc;
50 struct sr_datafeed_packet packet;
51 struct sr_datafeed_analog analog;
52 struct sr_analog_encoding encoding;
53 struct sr_analog_meaning meaning;
54 struct sr_analog_spec spec;
62 enum sr_mqflag mqflags;
66 if (strcmp(tokens[0], "QM") || !tokens[1])
69 if ((e = strstr(tokens[1], "Out of range"))) {
73 while (*e && *e != '.')
77 /* Delimit the float, since sr_atof_ascii() wants only
78 * a valid float here. */
80 while (*e && *e != ' ')
83 if (sr_atof_ascii(tokens[1], &fvalue) != SR_OK) {
84 /* Happens all the time, when switching modes. */
85 sr_dbg("Invalid float: '%s'", tokens[1]);
88 digits = count_digits(tokens[1]);
90 while (*e && *e == ' ')
100 if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) {
103 if (!is_oor && e[0] == 'm')
105 /* This catches "V AC", "V DC" and "V AC+DC". */
107 mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
109 mqflags |= SR_MQFLAG_DC;
110 } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
113 unit = SR_UNIT_DECIBEL_MW;
115 unit = SR_UNIT_DECIBEL_VOLT;
116 mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
117 } else if ((u = strstr(e, "Ohms"))) {
118 mq = SR_MQ_RESISTANCE;
122 else if (e[0] == 'k')
124 else if (e[0] == 'M')
126 } else if (!strcmp(e, "nS")) {
127 mq = SR_MQ_CONDUCTANCE;
128 unit = SR_UNIT_SIEMENS;
130 } else if ((u = strstr(e, "Farads"))) {
131 mq = SR_MQ_CAPACITANCE;
132 unit = SR_UNIT_FARAD;
136 else if (e[0] == 'u')
138 else if (e[0] == 'n')
141 } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
142 mq = SR_MQ_TEMPERATURE;
144 unit = SR_UNIT_CELSIUS;
146 unit = SR_UNIT_FAHRENHEIT;
147 } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) {
149 unit = SR_UNIT_AMPERE;
150 /* This catches "A AC", "A DC" and "A AC+DC". */
152 mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
154 mqflags |= SR_MQFLAG_DC;
158 else if (e[0] == 'u')
161 } else if ((u = strstr(e, "Hz"))) {
162 mq = SR_MQ_FREQUENCY;
163 unit = SR_UNIT_HERTZ;
166 } else if (!strcmp(e, "%")) {
167 mq = SR_MQ_DUTY_CYCLE;
168 unit = SR_UNIT_PERCENTAGE;
169 } else if ((u = strstr(e, "ms"))) {
170 mq = SR_MQ_PULSE_WIDTH;
171 unit = SR_UNIT_SECOND;
176 /* Got a measurement. */
178 fvalue *= pow(10.0f, exponent);
180 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
181 analog.data = &fvalue;
182 analog.num_samples = 1;
183 analog.meaning->unit = unit;
184 analog.meaning->mq = mq;
185 analog.meaning->mqflags = mqflags;
186 analog.meaning->channels = sdi->channels;
188 packet.type = SR_DF_ANALOG;
189 packet.payload = &analog;
190 sr_session_send(sdi, &packet);
191 sr_sw_limits_update_samples_read(&devc->limits, 1);
195 static void handle_qm_28x(const struct sr_dev_inst *sdi, char **tokens)
197 struct dev_context *devc;
198 struct sr_datafeed_packet packet;
199 struct sr_datafeed_analog analog;
200 struct sr_analog_encoding encoding;
201 struct sr_analog_meaning meaning;
202 struct sr_analog_spec spec;
211 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK) {
212 sr_err("Invalid float '%s'.", tokens[0]);
215 digits = count_digits(tokens[0]);
217 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
218 analog.data = &fvalue;
219 analog.meaning->channels = sdi->channels;
220 analog.num_samples = 1;
221 analog.meaning->mq = 0;
223 if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) {
224 analog.meaning->mq = SR_MQ_VOLTAGE;
225 analog.meaning->unit = SR_UNIT_VOLT;
226 if (!strcmp(tokens[2], "NORMAL")) {
227 if (tokens[1][1] == 'A') {
228 analog.meaning->mqflags |= SR_MQFLAG_AC;
229 analog.meaning->mqflags |= SR_MQFLAG_RMS;
231 analog.meaning->mqflags |= SR_MQFLAG_DC;
232 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
235 analog.meaning->mq = 0;
236 } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
237 analog.meaning->mq = SR_MQ_VOLTAGE;
238 if (tokens[1][2] == 'm')
239 analog.meaning->unit = SR_UNIT_DECIBEL_MW;
241 analog.meaning->unit = SR_UNIT_DECIBEL_VOLT;
242 analog.meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
243 } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
244 if (!strcmp(tokens[2], "NORMAL")) {
245 analog.meaning->mq = SR_MQ_TEMPERATURE;
246 if (tokens[1][0] == 'C')
247 analog.meaning->unit = SR_UNIT_CELSIUS;
249 analog.meaning->unit = SR_UNIT_FAHRENHEIT;
251 } else if (!strcmp(tokens[1], "OHM")) {
252 if (!strcmp(tokens[3], "NONE")) {
253 analog.meaning->mq = SR_MQ_RESISTANCE;
254 analog.meaning->unit = SR_UNIT_OHM;
255 if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
257 } else if (strcmp(tokens[2], "NORMAL"))
258 analog.meaning->mq = 0;
259 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
260 analog.meaning->mq = SR_MQ_CONTINUITY;
261 analog.meaning->unit = SR_UNIT_BOOLEAN;
263 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
264 analog.meaning->mq = SR_MQ_CONTINUITY;
265 analog.meaning->unit = SR_UNIT_BOOLEAN;
268 } else if (!strcmp(tokens[1], "F")
269 && !strcmp(tokens[2], "NORMAL")
270 && !strcmp(tokens[3], "NONE")) {
271 analog.meaning->mq = SR_MQ_CAPACITANCE;
272 analog.meaning->unit = SR_UNIT_FARAD;
273 } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
274 analog.meaning->mq = SR_MQ_CURRENT;
275 analog.meaning->unit = SR_UNIT_AMPERE;
276 if (!strcmp(tokens[2], "NORMAL")) {
277 if (tokens[1][1] == 'A') {
278 analog.meaning->mqflags |= SR_MQFLAG_AC;
279 analog.meaning->mqflags |= SR_MQFLAG_RMS;
281 analog.meaning->mqflags |= SR_MQFLAG_DC;
282 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
285 analog.meaning->mq = 0;
286 } else if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
287 analog.meaning->mq = SR_MQ_FREQUENCY;
288 analog.meaning->unit = SR_UNIT_HERTZ;
289 } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
290 analog.meaning->mq = SR_MQ_DUTY_CYCLE;
291 analog.meaning->unit = SR_UNIT_PERCENTAGE;
292 } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
293 analog.meaning->mq = SR_MQ_PULSE_WIDTH;
294 analog.meaning->unit = SR_UNIT_SECOND;
295 } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
296 analog.meaning->mq = SR_MQ_CONDUCTANCE;
297 analog.meaning->unit = SR_UNIT_SIEMENS;
300 if (analog.meaning->mq != 0) {
301 /* Got a measurement. */
302 packet.type = SR_DF_ANALOG;
303 packet.payload = &analog;
304 sr_session_send(sdi, &packet);
305 sr_sw_limits_update_samples_read(&devc->limits, 1);
309 static void handle_qm_19x_meta(const struct sr_dev_inst *sdi, char **tokens)
311 struct dev_context *devc;
312 int meas_type, meas_unit, meas_char, i;
314 /* Make sure we have 7 valid tokens. */
315 for (i = 0; tokens[i] && i < 7; i++);
319 if (strcmp(tokens[1], "1"))
320 /* Invalid measurement. */
323 if (strcmp(tokens[2], "3"))
324 /* Only interested in input from the meter mode source. */
329 /* Measurement type 11 == absolute, 19 = relative */
330 meas_type = strtol(tokens[0], NULL, 10);
331 if (meas_type != 11 && meas_type != 19)
332 /* Device is in some mode we don't support. */
335 /* We might get metadata for absolute and relative mode (if the device
336 * is in relative mode). In that case, relative takes precedence. */
337 if (meas_type == 11 && devc->meas_type == 19)
340 meas_unit = strtol(tokens[3], NULL, 10);
342 /* Device is turned off. Really. */
344 meas_char = strtol(tokens[4], NULL, 10);
352 devc->mq = SR_MQ_VOLTAGE;
353 devc->unit = SR_UNIT_VOLT;
355 devc->mqflags |= SR_MQFLAG_DC;
356 else if (meas_char == 2)
357 devc->mqflags |= SR_MQFLAG_AC;
358 else if (meas_char == 3)
359 devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
360 else if (meas_char == 15)
361 devc->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
364 devc->mq = SR_MQ_CURRENT;
365 devc->unit = SR_UNIT_AMPERE;
367 devc->mqflags |= SR_MQFLAG_DC;
368 else if (meas_char == 2)
369 devc->mqflags |= SR_MQFLAG_AC;
370 else if (meas_char == 3)
371 devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
374 if (meas_char == 1) {
375 devc->mq = SR_MQ_RESISTANCE;
376 devc->unit = SR_UNIT_OHM;
377 } else if (meas_char == 16) {
378 devc->mq = SR_MQ_CONTINUITY;
379 devc->unit = SR_UNIT_BOOLEAN;
383 devc->mq = SR_MQ_TEMPERATURE;
384 devc->unit = SR_UNIT_CELSIUS;
387 devc->mq = SR_MQ_TEMPERATURE;
388 devc->unit = SR_UNIT_FAHRENHEIT;
391 sr_dbg("unknown unit: %d", meas_unit);
393 if (devc->mq == 0 && devc->unit == 0)
396 /* If we got here, we know how to interpret the measurement. */
397 devc->meas_type = meas_type;
399 /* Absolute meter reading. */
400 devc->is_relative = FALSE;
401 else if (!strcmp(tokens[0], "19"))
402 /* Relative meter reading. */
403 devc->is_relative = TRUE;
407 static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens)
409 struct dev_context *devc;
410 struct sr_datafeed_packet packet;
411 struct sr_datafeed_analog analog;
412 struct sr_analog_encoding encoding;
413 struct sr_analog_meaning meaning;
414 struct sr_analog_spec spec;
419 if (!strcmp(tokens[0], "9.9E+37")) {
420 /* An invalid measurement shows up on the display as "OL", but
421 * comes through like this. Since comparing 38-digit floats
422 * is rather problematic, we'll cut through this here. */
425 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
426 sr_err("Invalid float '%s'.", tokens[0]);
429 digits = count_digits(tokens[0]);
433 if (devc->mq == 0 || devc->unit == 0)
434 /* Don't have valid metadata yet. */
437 if (devc->mq == SR_MQ_RESISTANCE && isnan(fvalue))
439 else if (devc->mq == SR_MQ_CONTINUITY) {
446 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
447 analog.meaning->channels = sdi->channels;
448 analog.num_samples = 1;
449 analog.data = &fvalue;
450 analog.meaning->mq = devc->mq;
451 analog.meaning->unit = devc->unit;
452 analog.meaning->mqflags = 0;
453 packet.type = SR_DF_ANALOG;
454 packet.payload = &analog;
455 sr_session_send(sdi, &packet);
457 sr_sw_limits_update_samples_read(&devc->limits, 1);
460 static void handle_line(const struct sr_dev_inst *sdi)
462 struct dev_context *devc;
463 struct sr_serial_dev_inst *serial;
464 int num_tokens, n, i;
465 char cmd[16], **tokens;
470 sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
472 if (devc->buflen == 1) {
473 if (devc->buf[0] != '0') {
474 /* Not just a CMD_ACK from the query command. */
475 sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
476 devc->expect_response = FALSE;
482 tokens = g_strsplit(devc->buf, ",", 0);
484 switch (devc->profile->model) {
489 devc->expect_response = FALSE;
490 handle_qm_18x(sdi, tokens);
493 devc->expect_response = FALSE;
494 num_tokens = g_strv_length(tokens);
495 if (num_tokens < 7) {
496 /* Response to QM <n> measurement request. */
497 handle_qm_19x_data(sdi, tokens);
501 * Response to QM: This is a comma-separated list of
502 * fields with metadata about the measurement. This
503 * format can return multiple sets of metadata,
504 * split into sets of 7 tokens each.
507 for (i = 0; i < num_tokens; i += 7)
508 handle_qm_19x_meta(sdi, tokens + i);
509 if (devc->meas_type) {
511 * Slip the request in now, before the main
512 * timer loop asks for metadata again.
514 n = sprintf(cmd, "QM %d\r", devc->meas_type);
515 ret = serial_write_blocking(serial,
516 cmd, n, SERIAL_WRITE_TIMEOUT_MS);
518 sr_err("Cannot send QM (measurement).");
523 devc->expect_response = FALSE;
524 handle_qm_28x(sdi, tokens);
532 SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
534 struct sr_dev_inst *sdi;
535 struct dev_context *devc;
536 struct sr_serial_dev_inst *serial;
538 int64_t now, elapsed;
542 if (!(sdi = cb_data))
545 if (!(devc = sdi->priv))
549 if (revents == G_IO_IN) {
550 /* Serial data arrived. */
551 while (FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
552 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
556 *(devc->buf + devc->buflen) = '\0';
557 if (*(devc->buf + devc->buflen - 1) == '\r') {
558 *(devc->buf + --devc->buflen) = '\0';
565 if (sr_sw_limits_check(&devc->limits)) {
566 sr_dev_acquisition_stop(sdi);
570 now = g_get_monotonic_time() / 1000;
571 elapsed = now - devc->cmd_sent_at;
572 /* Send query command at poll_period interval, or after 1 second
573 * has elapsed. This will make it easier to recover from any
574 * out-of-sync or temporary disconnect issues. */
575 if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
576 || elapsed > devc->profile->timeout) {
577 if (serial_write_blocking(serial, "QM\r", 3, SERIAL_WRITE_TIMEOUT_MS) < 0)
578 sr_err("Unable to send QM.");
579 devc->cmd_sent_at = now;
580 devc->expect_response = TRUE;