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 if (strcmp(tokens[0], "QM") || !tokens[1])
65 if ((e = strstr(tokens[1], "Out of range"))) {
69 while (*e && *e != '.')
73 /* Delimit the float, since sr_atof_ascii() wants only
74 * a valid float here. */
76 while (*e && *e != ' ')
79 if (sr_atof_ascii(tokens[1], &fvalue) != SR_OK) {
80 /* Happens all the time, when switching modes. */
81 sr_dbg("Invalid float: '%s'", tokens[1]);
84 digits = count_digits(tokens[1]);
86 while (*e && *e == ' ')
89 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
90 analog.data = &fvalue;
91 analog.meaning->channels = sdi->channels;
92 analog.num_samples = 1;
95 analog.meaning->mq = 0;
97 if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) {
98 analog.meaning->mq = SR_MQ_VOLTAGE;
99 analog.meaning->unit = SR_UNIT_VOLT;
100 if (!is_oor && e[0] == 'm')
102 /* This catches "V AC", "V DC" and "V AC+DC". */
104 analog.meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
106 analog.meaning->mqflags |= SR_MQFLAG_DC;
107 } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
108 analog.meaning->mq = SR_MQ_VOLTAGE;
110 analog.meaning->unit = SR_UNIT_DECIBEL_MW;
112 analog.meaning->unit = SR_UNIT_DECIBEL_VOLT;
113 analog.meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
114 } else if ((u = strstr(e, "Ohms"))) {
115 analog.meaning->mq = SR_MQ_RESISTANCE;
116 analog.meaning->unit = SR_UNIT_OHM;
119 else if (e[0] == 'k')
121 else if (e[0] == 'M')
123 } else if (!strcmp(e, "nS")) {
124 analog.meaning->mq = SR_MQ_CONDUCTANCE;
125 analog.meaning->unit = SR_UNIT_SIEMENS;
126 *((float *)analog.data) /= 1e+9;
127 } else if ((u = strstr(e, "Farads"))) {
128 analog.meaning->mq = SR_MQ_CAPACITANCE;
129 analog.meaning->unit = SR_UNIT_FARAD;
133 else if (e[0] == 'u')
135 else if (e[0] == 'n')
138 } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
139 analog.meaning->mq = SR_MQ_TEMPERATURE;
141 analog.meaning->unit = SR_UNIT_CELSIUS;
143 analog.meaning->unit = SR_UNIT_FAHRENHEIT;
144 } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) {
145 analog.meaning->mq = SR_MQ_CURRENT;
146 analog.meaning->unit = SR_UNIT_AMPERE;
147 /* This catches "A AC", "A DC" and "A AC+DC". */
149 analog.meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
151 analog.meaning->mqflags |= SR_MQFLAG_DC;
155 else if (e[0] == 'u')
158 } else if ((u = strstr(e, "Hz"))) {
159 analog.meaning->mq = SR_MQ_FREQUENCY;
160 analog.meaning->unit = SR_UNIT_HERTZ;
163 } else if (!strcmp(e, "%")) {
164 analog.meaning->mq = SR_MQ_DUTY_CYCLE;
165 analog.meaning->unit = SR_UNIT_PERCENTAGE;
166 } else if ((u = strstr(e, "ms"))) {
167 analog.meaning->mq = SR_MQ_PULSE_WIDTH;
168 analog.meaning->unit = SR_UNIT_SECOND;
172 if (analog.meaning->mq != 0) {
173 /* Got a measurement. */
174 packet.type = SR_DF_ANALOG;
175 packet.payload = &analog;
176 sr_session_send(sdi, &packet);
177 sr_sw_limits_update_samples_read(&devc->limits, 1);
181 static void handle_qm_28x(const struct sr_dev_inst *sdi, char **tokens)
183 struct dev_context *devc;
184 struct sr_datafeed_packet packet;
185 struct sr_datafeed_analog analog;
186 struct sr_analog_encoding encoding;
187 struct sr_analog_meaning meaning;
188 struct sr_analog_spec spec;
197 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK) {
198 sr_err("Invalid float '%s'.", tokens[0]);
201 digits = count_digits(tokens[0]);
203 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
204 analog.data = &fvalue;
205 analog.meaning->channels = sdi->channels;
206 analog.num_samples = 1;
207 analog.meaning->mq = 0;
209 if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) {
210 analog.meaning->mq = SR_MQ_VOLTAGE;
211 analog.meaning->unit = SR_UNIT_VOLT;
212 if (!strcmp(tokens[2], "NORMAL")) {
213 if (tokens[1][1] == 'A') {
214 analog.meaning->mqflags |= SR_MQFLAG_AC;
215 analog.meaning->mqflags |= SR_MQFLAG_RMS;
217 analog.meaning->mqflags |= SR_MQFLAG_DC;
218 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
221 analog.meaning->mq = 0;
222 } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
223 analog.meaning->mq = SR_MQ_VOLTAGE;
224 if (tokens[1][2] == 'm')
225 analog.meaning->unit = SR_UNIT_DECIBEL_MW;
227 analog.meaning->unit = SR_UNIT_DECIBEL_VOLT;
228 analog.meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
229 } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
230 if (!strcmp(tokens[2], "NORMAL")) {
231 analog.meaning->mq = SR_MQ_TEMPERATURE;
232 if (tokens[1][0] == 'C')
233 analog.meaning->unit = SR_UNIT_CELSIUS;
235 analog.meaning->unit = SR_UNIT_FAHRENHEIT;
237 } else if (!strcmp(tokens[1], "OHM")) {
238 if (!strcmp(tokens[3], "NONE")) {
239 analog.meaning->mq = SR_MQ_RESISTANCE;
240 analog.meaning->unit = SR_UNIT_OHM;
241 if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
243 } else if (strcmp(tokens[2], "NORMAL"))
244 analog.meaning->mq = 0;
245 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
246 analog.meaning->mq = SR_MQ_CONTINUITY;
247 analog.meaning->unit = SR_UNIT_BOOLEAN;
249 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
250 analog.meaning->mq = SR_MQ_CONTINUITY;
251 analog.meaning->unit = SR_UNIT_BOOLEAN;
254 } else if (!strcmp(tokens[1], "F")
255 && !strcmp(tokens[2], "NORMAL")
256 && !strcmp(tokens[3], "NONE")) {
257 analog.meaning->mq = SR_MQ_CAPACITANCE;
258 analog.meaning->unit = SR_UNIT_FARAD;
259 } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
260 analog.meaning->mq = SR_MQ_CURRENT;
261 analog.meaning->unit = SR_UNIT_AMPERE;
262 if (!strcmp(tokens[2], "NORMAL")) {
263 if (tokens[1][1] == 'A') {
264 analog.meaning->mqflags |= SR_MQFLAG_AC;
265 analog.meaning->mqflags |= SR_MQFLAG_RMS;
267 analog.meaning->mqflags |= SR_MQFLAG_DC;
268 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
271 analog.meaning->mq = 0;
272 } else if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
273 analog.meaning->mq = SR_MQ_FREQUENCY;
274 analog.meaning->unit = SR_UNIT_HERTZ;
275 } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
276 analog.meaning->mq = SR_MQ_DUTY_CYCLE;
277 analog.meaning->unit = SR_UNIT_PERCENTAGE;
278 } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
279 analog.meaning->mq = SR_MQ_PULSE_WIDTH;
280 analog.meaning->unit = SR_UNIT_SECOND;
281 } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
282 analog.meaning->mq = SR_MQ_CONDUCTANCE;
283 analog.meaning->unit = SR_UNIT_SIEMENS;
286 if (analog.meaning->mq != 0) {
287 /* Got a measurement. */
288 packet.type = SR_DF_ANALOG;
289 packet.payload = &analog;
290 sr_session_send(sdi, &packet);
291 sr_sw_limits_update_samples_read(&devc->limits, 1);
295 static void handle_qm_19x_meta(const struct sr_dev_inst *sdi, char **tokens)
297 struct dev_context *devc;
298 int meas_type, meas_unit, meas_char, i;
300 /* Make sure we have 7 valid tokens. */
301 for (i = 0; tokens[i] && i < 7; i++);
305 if (strcmp(tokens[1], "1"))
306 /* Invalid measurement. */
309 if (strcmp(tokens[2], "3"))
310 /* Only interested in input from the meter mode source. */
315 /* Measurement type 11 == absolute, 19 = relative */
316 meas_type = strtol(tokens[0], NULL, 10);
317 if (meas_type != 11 && meas_type != 19)
318 /* Device is in some mode we don't support. */
321 /* We might get metadata for absolute and relative mode (if the device
322 * is in relative mode). In that case, relative takes precedence. */
323 if (meas_type == 11 && devc->meas_type == 19)
326 meas_unit = strtol(tokens[3], NULL, 10);
328 /* Device is turned off. Really. */
330 meas_char = strtol(tokens[4], NULL, 10);
338 devc->mq = SR_MQ_VOLTAGE;
339 devc->unit = SR_UNIT_VOLT;
341 devc->mqflags |= SR_MQFLAG_DC;
342 else if (meas_char == 2)
343 devc->mqflags |= SR_MQFLAG_AC;
344 else if (meas_char == 3)
345 devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
346 else if (meas_char == 15)
347 devc->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
350 devc->mq = SR_MQ_CURRENT;
351 devc->unit = SR_UNIT_AMPERE;
353 devc->mqflags |= SR_MQFLAG_DC;
354 else if (meas_char == 2)
355 devc->mqflags |= SR_MQFLAG_AC;
356 else if (meas_char == 3)
357 devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
360 if (meas_char == 1) {
361 devc->mq = SR_MQ_RESISTANCE;
362 devc->unit = SR_UNIT_OHM;
363 } else if (meas_char == 16) {
364 devc->mq = SR_MQ_CONTINUITY;
365 devc->unit = SR_UNIT_BOOLEAN;
369 devc->mq = SR_MQ_TEMPERATURE;
370 devc->unit = SR_UNIT_CELSIUS;
373 devc->mq = SR_MQ_TEMPERATURE;
374 devc->unit = SR_UNIT_FAHRENHEIT;
377 sr_dbg("unknown unit: %d", meas_unit);
379 if (devc->mq == 0 && devc->unit == 0)
382 /* If we got here, we know how to interpret the measurement. */
383 devc->meas_type = meas_type;
385 /* Absolute meter reading. */
386 devc->is_relative = FALSE;
387 else if (!strcmp(tokens[0], "19"))
388 /* Relative meter reading. */
389 devc->is_relative = TRUE;
393 static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens)
395 struct dev_context *devc;
396 struct sr_datafeed_packet packet;
397 struct sr_datafeed_analog analog;
398 struct sr_analog_encoding encoding;
399 struct sr_analog_meaning meaning;
400 struct sr_analog_spec spec;
405 if (!strcmp(tokens[0], "9.9E+37")) {
406 /* An invalid measurement shows up on the display as "OL", but
407 * comes through like this. Since comparing 38-digit floats
408 * is rather problematic, we'll cut through this here. */
411 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
412 sr_err("Invalid float '%s'.", tokens[0]);
415 digits = count_digits(tokens[0]);
419 if (devc->mq == 0 || devc->unit == 0)
420 /* Don't have valid metadata yet. */
423 if (devc->mq == SR_MQ_RESISTANCE && isnan(fvalue))
425 else if (devc->mq == SR_MQ_CONTINUITY) {
432 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
433 analog.meaning->channels = sdi->channels;
434 analog.num_samples = 1;
435 analog.data = &fvalue;
436 analog.meaning->mq = devc->mq;
437 analog.meaning->unit = devc->unit;
438 analog.meaning->mqflags = 0;
439 packet.type = SR_DF_ANALOG;
440 packet.payload = &analog;
441 sr_session_send(sdi, &packet);
443 sr_sw_limits_update_samples_read(&devc->limits, 1);
446 static void handle_line(const struct sr_dev_inst *sdi)
448 struct dev_context *devc;
449 struct sr_serial_dev_inst *serial;
450 int num_tokens, n, i;
451 char cmd[16], **tokens;
456 sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
458 if (devc->buflen == 1) {
459 if (devc->buf[0] != '0') {
460 /* Not just a CMD_ACK from the query command. */
461 sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
462 devc->expect_response = FALSE;
468 tokens = g_strsplit(devc->buf, ",", 0);
470 switch (devc->profile->model) {
475 devc->expect_response = FALSE;
476 handle_qm_18x(sdi, tokens);
480 devc->expect_response = FALSE;
481 handle_qm_28x(sdi, tokens);
484 devc->expect_response = FALSE;
485 num_tokens = g_strv_length(tokens);
486 if (num_tokens < 7) {
487 /* Response to QM <n> measurement request. */
488 handle_qm_19x_data(sdi, tokens);
492 * Response to QM: This is a comma-separated list of
493 * fields with metadata about the measurement. This
494 * format can return multiple sets of metadata,
495 * split into sets of 7 tokens each.
498 for (i = 0; i < num_tokens; i += 7)
499 handle_qm_19x_meta(sdi, tokens + i);
500 if (devc->meas_type) {
502 * Slip the request in now, before the main
503 * timer loop asks for metadata again.
505 n = sprintf(cmd, "QM %d\r", devc->meas_type);
506 ret = serial_write_blocking(serial,
507 cmd, n, SERIAL_WRITE_TIMEOUT_MS);
509 sr_err("Cannot send QM (measurement).");
518 SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
520 struct sr_dev_inst *sdi;
521 struct dev_context *devc;
522 struct sr_serial_dev_inst *serial;
524 int64_t now, elapsed;
528 if (!(sdi = cb_data))
531 if (!(devc = sdi->priv))
535 if (revents == G_IO_IN) {
536 /* Serial data arrived. */
537 while (FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
538 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
542 *(devc->buf + devc->buflen) = '\0';
543 if (*(devc->buf + devc->buflen - 1) == '\r') {
544 *(devc->buf + --devc->buflen) = '\0';
551 if (sr_sw_limits_check(&devc->limits)) {
552 sr_dev_acquisition_stop(sdi);
556 now = g_get_monotonic_time() / 1000;
557 elapsed = now - devc->cmd_sent_at;
558 /* Send query command at poll_period interval, or after 1 second
559 * has elapsed. This will make it easier to recover from any
560 * out-of-sync or temporary disconnect issues. */
561 if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
562 || elapsed > devc->profile->timeout) {
563 if (serial_write_blocking(serial, "QM\r", 3, SERIAL_WRITE_TIMEOUT_MS) < 0)
564 sr_err("Unable to send QM.");
565 devc->cmd_sent_at = now;
566 devc->expect_response = TRUE;