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;
215 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK) {
216 sr_err("Invalid float '%s'.", tokens[0]);
219 digits = count_digits(tokens[0]);
221 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
222 analog.data = &fvalue;
223 analog.meaning->channels = sdi->channels;
224 analog.num_samples = 1;
225 analog.meaning->mq = 0;
227 if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) {
228 analog.meaning->mq = SR_MQ_VOLTAGE;
229 analog.meaning->unit = SR_UNIT_VOLT;
230 if (!strcmp(tokens[2], "NORMAL")) {
231 if (tokens[1][1] == 'A') {
232 analog.meaning->mqflags |= SR_MQFLAG_AC;
233 analog.meaning->mqflags |= SR_MQFLAG_RMS;
235 analog.meaning->mqflags |= SR_MQFLAG_DC;
236 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
239 analog.meaning->mq = 0;
240 } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
241 analog.meaning->mq = SR_MQ_VOLTAGE;
242 if (tokens[1][2] == 'm')
243 analog.meaning->unit = SR_UNIT_DECIBEL_MW;
245 analog.meaning->unit = SR_UNIT_DECIBEL_VOLT;
246 analog.meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
247 } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
248 if (!strcmp(tokens[2], "NORMAL")) {
249 analog.meaning->mq = SR_MQ_TEMPERATURE;
250 if (tokens[1][0] == 'C')
251 analog.meaning->unit = SR_UNIT_CELSIUS;
253 analog.meaning->unit = SR_UNIT_FAHRENHEIT;
255 } else if (!strcmp(tokens[1], "OHM")) {
256 if (!strcmp(tokens[3], "NONE")) {
257 analog.meaning->mq = SR_MQ_RESISTANCE;
258 analog.meaning->unit = SR_UNIT_OHM;
259 if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
261 } else if (strcmp(tokens[2], "NORMAL"))
262 analog.meaning->mq = 0;
263 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
264 analog.meaning->mq = SR_MQ_CONTINUITY;
265 analog.meaning->unit = SR_UNIT_BOOLEAN;
267 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
268 analog.meaning->mq = SR_MQ_CONTINUITY;
269 analog.meaning->unit = SR_UNIT_BOOLEAN;
272 } else if (!strcmp(tokens[1], "F")
273 && !strcmp(tokens[2], "NORMAL")
274 && !strcmp(tokens[3], "NONE")) {
275 analog.meaning->mq = SR_MQ_CAPACITANCE;
276 analog.meaning->unit = SR_UNIT_FARAD;
277 } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
278 analog.meaning->mq = SR_MQ_CURRENT;
279 analog.meaning->unit = SR_UNIT_AMPERE;
280 if (!strcmp(tokens[2], "NORMAL")) {
281 if (tokens[1][1] == 'A') {
282 analog.meaning->mqflags |= SR_MQFLAG_AC;
283 analog.meaning->mqflags |= SR_MQFLAG_RMS;
285 analog.meaning->mqflags |= SR_MQFLAG_DC;
286 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
289 analog.meaning->mq = 0;
290 } else if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
291 analog.meaning->mq = SR_MQ_FREQUENCY;
292 analog.meaning->unit = SR_UNIT_HERTZ;
293 } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
294 analog.meaning->mq = SR_MQ_DUTY_CYCLE;
295 analog.meaning->unit = SR_UNIT_PERCENTAGE;
296 } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
297 analog.meaning->mq = SR_MQ_PULSE_WIDTH;
298 analog.meaning->unit = SR_UNIT_SECOND;
299 } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
300 analog.meaning->mq = SR_MQ_CONDUCTANCE;
301 analog.meaning->unit = SR_UNIT_SIEMENS;
304 if (analog.meaning->mq != 0) {
305 /* Got a measurement. */
306 packet.type = SR_DF_ANALOG;
307 packet.payload = &analog;
308 sr_session_send(sdi, &packet);
309 sr_sw_limits_update_samples_read(&devc->limits, 1);
313 static void handle_qm_19x_meta(const struct sr_dev_inst *sdi, char **tokens)
315 struct dev_context *devc;
316 int meas_type, meas_unit, meas_char, i;
318 /* Make sure we have 7 valid tokens. */
319 for (i = 0; tokens[i] && i < 7; i++);
323 if (strcmp(tokens[1], "1"))
324 /* Invalid measurement. */
327 if (strcmp(tokens[2], "3"))
328 /* Only interested in input from the meter mode source. */
333 /* Measurement type 11 == absolute, 19 = relative */
334 meas_type = strtol(tokens[0], NULL, 10);
335 if (meas_type != 11 && meas_type != 19)
336 /* Device is in some mode we don't support. */
339 /* We might get metadata for absolute and relative mode (if the device
340 * is in relative mode). In that case, relative takes precedence. */
341 if (meas_type == 11 && devc->meas_type == 19)
344 meas_unit = strtol(tokens[3], NULL, 10);
346 /* Device is turned off. Really. */
348 meas_char = strtol(tokens[4], NULL, 10);
356 devc->mq = SR_MQ_VOLTAGE;
357 devc->unit = SR_UNIT_VOLT;
359 devc->mqflags |= SR_MQFLAG_DC;
360 else if (meas_char == 2)
361 devc->mqflags |= SR_MQFLAG_AC;
362 else if (meas_char == 3)
363 devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
364 else if (meas_char == 15)
365 devc->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
368 devc->mq = SR_MQ_CURRENT;
369 devc->unit = SR_UNIT_AMPERE;
371 devc->mqflags |= SR_MQFLAG_DC;
372 else if (meas_char == 2)
373 devc->mqflags |= SR_MQFLAG_AC;
374 else if (meas_char == 3)
375 devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
378 if (meas_char == 1) {
379 devc->mq = SR_MQ_RESISTANCE;
380 devc->unit = SR_UNIT_OHM;
381 } else if (meas_char == 16) {
382 devc->mq = SR_MQ_CONTINUITY;
383 devc->unit = SR_UNIT_BOOLEAN;
387 devc->mq = SR_MQ_TEMPERATURE;
388 devc->unit = SR_UNIT_CELSIUS;
391 devc->mq = SR_MQ_TEMPERATURE;
392 devc->unit = SR_UNIT_FAHRENHEIT;
395 sr_dbg("unknown unit: %d", meas_unit);
397 if (devc->mq == 0 && devc->unit == 0)
400 /* If we got here, we know how to interpret the measurement. */
401 devc->meas_type = meas_type;
403 /* Absolute meter reading. */
404 devc->is_relative = FALSE;
405 else if (!strcmp(tokens[0], "19"))
406 /* Relative meter reading. */
407 devc->is_relative = TRUE;
411 static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens)
413 struct dev_context *devc;
414 struct sr_datafeed_packet packet;
415 struct sr_datafeed_analog analog;
416 struct sr_analog_encoding encoding;
417 struct sr_analog_meaning meaning;
418 struct sr_analog_spec spec;
423 if (!strcmp(tokens[0], "9.9E+37")) {
424 /* An invalid measurement shows up on the display as "OL", but
425 * comes through like this. Since comparing 38-digit floats
426 * is rather problematic, we'll cut through this here. */
429 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
430 sr_err("Invalid float '%s'.", tokens[0]);
433 digits = count_digits(tokens[0]);
437 if (devc->mq == 0 || devc->unit == 0)
438 /* Don't have valid metadata yet. */
441 if (devc->mq == SR_MQ_RESISTANCE && isnan(fvalue))
443 else if (devc->mq == SR_MQ_CONTINUITY) {
450 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
451 analog.meaning->channels = sdi->channels;
452 analog.num_samples = 1;
453 analog.data = &fvalue;
454 analog.meaning->mq = devc->mq;
455 analog.meaning->unit = devc->unit;
456 analog.meaning->mqflags = 0;
457 packet.type = SR_DF_ANALOG;
458 packet.payload = &analog;
459 sr_session_send(sdi, &packet);
461 sr_sw_limits_update_samples_read(&devc->limits, 1);
464 static void handle_line(const struct sr_dev_inst *sdi)
466 struct dev_context *devc;
467 struct sr_serial_dev_inst *serial;
468 int num_tokens, n, i;
469 char cmd[16], **tokens;
474 sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
476 if (devc->buflen == 1) {
477 if (devc->buf[0] != '0') {
478 /* Not just a CMD_ACK from the query command. */
479 sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
480 devc->expect_response = FALSE;
486 tokens = g_strsplit(devc->buf, ",", 0);
488 switch (devc->profile->model) {
493 devc->expect_response = FALSE;
494 handle_qm_18x(sdi, tokens);
497 devc->expect_response = FALSE;
498 num_tokens = g_strv_length(tokens);
499 if (num_tokens < 7) {
500 /* Response to QM <n> measurement request. */
501 handle_qm_19x_data(sdi, tokens);
505 * Response to QM: This is a comma-separated list of
506 * fields with metadata about the measurement. This
507 * format can return multiple sets of metadata,
508 * split into sets of 7 tokens each.
511 for (i = 0; i < num_tokens; i += 7)
512 handle_qm_19x_meta(sdi, tokens + i);
513 if (devc->meas_type) {
515 * Slip the request in now, before the main
516 * timer loop asks for metadata again.
518 n = sprintf(cmd, "QM %d\r", devc->meas_type);
519 ret = serial_write_blocking(serial,
520 cmd, n, SERIAL_WRITE_TIMEOUT_MS);
522 sr_err("Cannot send QM (measurement).");
527 devc->expect_response = FALSE;
528 handle_qm_28x(sdi, tokens);
536 SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
538 struct sr_dev_inst *sdi;
539 struct dev_context *devc;
540 struct sr_serial_dev_inst *serial;
542 int64_t now, elapsed;
546 if (!(sdi = cb_data))
549 if (!(devc = sdi->priv))
553 if (revents == G_IO_IN) {
554 /* Serial data arrived. */
555 while (FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
556 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
560 *(devc->buf + devc->buflen) = '\0';
561 if (*(devc->buf + devc->buflen - 1) == '\r') {
562 *(devc->buf + --devc->buflen) = '\0';
569 if (sr_sw_limits_check(&devc->limits)) {
570 sr_dev_acquisition_stop(sdi);
574 now = g_get_monotonic_time() / 1000;
575 elapsed = now - devc->cmd_sent_at;
576 /* Send query command at poll_period interval, or after 1 second
577 * has elapsed. This will make it easier to recover from any
578 * out-of-sync or temporary disconnect issues. */
579 if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
580 || elapsed > devc->profile->timeout) {
581 if (serial_write_blocking(serial, "QM\r", 3, SERIAL_WRITE_TIMEOUT_MS) < 0)
582 sr_err("Unable to send QM.");
583 devc->cmd_sent_at = now;
584 devc->expect_response = TRUE;