]> sigrok.org Git - libsigrok.git/blame - src/hardware/fluke-dmm/fluke.c
fluke-dmm: Convert to SR_DF_ANALOG.
[libsigrok.git] / src / hardware / fluke-dmm / fluke.c
CommitLineData
d38d2ef0 1/*
50985c20 2 * This file is part of the libsigrok project.
d38d2ef0
BV
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
6ec6c43b 20#include <config.h>
d38d2ef0
BV
21#include <stdlib.h>
22#include <math.h>
23#include <string.h>
4cea0ff7 24#include <glib.h>
c1aae900 25#include <libsigrok/libsigrok.h>
4cea0ff7
BV
26#include "libsigrok-internal.h"
27#include "fluke-dmm.h"
d38d2ef0 28
3f5cf2a0 29static struct sr_datafeed_analog *handle_qm_18x(const struct sr_dev_inst *sdi,
fe31f8b9
BV
30 char **tokens)
31{
3f5cf2a0
UH
32 struct sr_datafeed_analog *analog;
33 struct sr_analog_encoding encoding;
34 struct sr_analog_meaning meaning;
35 struct sr_analog_spec spec;
fe31f8b9
BV
36 float fvalue;
37 char *e, *u;
38 gboolean is_oor;
39
4cea0ff7 40 if (strcmp(tokens[0], "QM") || !tokens[1])
fe31f8b9
BV
41 return NULL;
42
43 if ((e = strstr(tokens[1], "Out of range"))) {
44 is_oor = TRUE;
45 fvalue = -1;
0c5f2abc 46 while (*e && *e != '.')
357e341d 47 e++;
fe31f8b9
BV
48 } else {
49 is_oor = FALSE;
357e341d
BV
50 /* Delimit the float, since sr_atof_ascii() wants only
51 * a valid float here. */
52 e = tokens[1];
0c5f2abc 53 while (*e && *e != ' ')
357e341d
BV
54 e++;
55 *e++ = '\0';
56 if (sr_atof_ascii(tokens[1], &fvalue) != SR_OK || fvalue == 0.0) {
fe31f8b9 57 /* Happens all the time, when switching modes. */
31d84da3 58 sr_dbg("Invalid float.");
fe31f8b9
BV
59 return NULL;
60 }
61 }
0c5f2abc 62 while (*e && *e == ' ')
fe31f8b9
BV
63 e++;
64
3f5cf2a0
UH
65 analog = g_malloc0(sizeof(struct sr_datafeed_analog));
66 sr_analog_init(analog, &encoding, &meaning, &spec, 0);
a95f142e 67 analog->data = g_malloc(sizeof(float));
3f5cf2a0 68 analog->meaning->channels = sdi->channels;
fe31f8b9 69 analog->num_samples = 1;
fe31f8b9 70 if (is_oor)
3f5cf2a0 71 *((float *)analog->data) = NAN;
fe31f8b9 72 else
3f5cf2a0
UH
73 *((float *)analog->data) = fvalue;
74 analog->meaning->mq = 0;
fe31f8b9
BV
75
76 if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) {
3f5cf2a0
UH
77 analog->meaning->mq = SR_MQ_VOLTAGE;
78 analog->meaning->unit = SR_UNIT_VOLT;
fe31f8b9 79 if (!is_oor && e[0] == 'm')
3f5cf2a0 80 *((float *)analog->data) /= 1000;
fe31f8b9
BV
81 /* This catches "V AC", "V DC" and "V AC+DC". */
82 if (strstr(u, "AC"))
3f5cf2a0 83 analog->meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
fe31f8b9 84 if (strstr(u, "DC"))
3f5cf2a0 85 analog->meaning->mqflags |= SR_MQFLAG_DC;
fe31f8b9 86 } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
3f5cf2a0 87 analog->meaning->mq = SR_MQ_VOLTAGE;
fe31f8b9 88 if (u[2] == 'm')
3f5cf2a0 89 analog->meaning->unit = SR_UNIT_DECIBEL_MW;
fe31f8b9 90 else
3f5cf2a0
UH
91 analog->meaning->unit = SR_UNIT_DECIBEL_VOLT;
92 analog->meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
fe31f8b9 93 } else if ((u = strstr(e, "Ohms"))) {
3f5cf2a0
UH
94 analog->meaning->mq = SR_MQ_RESISTANCE;
95 analog->meaning->unit = SR_UNIT_OHM;
fe31f8b9 96 if (is_oor)
3f5cf2a0 97 *((float *)analog->data) = INFINITY;
fe31f8b9 98 else if (e[0] == 'k')
3f5cf2a0 99 *((float *)analog->data) *= 1000;
fe31f8b9 100 else if (e[0] == 'M')
3f5cf2a0 101 *((float *)analog->data) *= 1000000;
fe31f8b9 102 } else if (!strcmp(e, "nS")) {
3f5cf2a0
UH
103 analog->meaning->mq = SR_MQ_CONDUCTANCE;
104 analog->meaning->unit = SR_UNIT_SIEMENS;
105 *((float *)analog->data) /= 1e+9;
fe31f8b9 106 } else if ((u = strstr(e, "Farads"))) {
3f5cf2a0
UH
107 analog->meaning->mq = SR_MQ_CAPACITANCE;
108 analog->meaning->unit = SR_UNIT_FARAD;
fe31f8b9
BV
109 if (!is_oor) {
110 if (e[0] == 'm')
3f5cf2a0 111 *((float *)analog->data) /= 1e+3;
fe31f8b9 112 else if (e[0] == 'u')
3f5cf2a0 113 *((float *)analog->data) /= 1e+6;
fe31f8b9 114 else if (e[0] == 'n')
3f5cf2a0 115 *((float *)analog->data) /= 1e+9;
fe31f8b9
BV
116 }
117 } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
3f5cf2a0 118 analog->meaning->mq = SR_MQ_TEMPERATURE;
fe31f8b9 119 if (u[4] == 'C')
3f5cf2a0 120 analog->meaning->unit = SR_UNIT_CELSIUS;
fe31f8b9 121 else
3f5cf2a0 122 analog->meaning->unit = SR_UNIT_FAHRENHEIT;
fe31f8b9 123 } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) {
3f5cf2a0
UH
124 analog->meaning->mq = SR_MQ_CURRENT;
125 analog->meaning->unit = SR_UNIT_AMPERE;
fe31f8b9
BV
126 /* This catches "A AC", "A DC" and "A AC+DC". */
127 if (strstr(u, "AC"))
3f5cf2a0 128 analog->meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
fe31f8b9 129 if (strstr(u, "DC"))
3f5cf2a0 130 analog->meaning->mqflags |= SR_MQFLAG_DC;
fe31f8b9
BV
131 if (!is_oor) {
132 if (e[0] == 'm')
3f5cf2a0 133 *((float *)analog->data) /= 1e+3;
fe31f8b9 134 else if (e[0] == 'u')
3f5cf2a0 135 *((float *)analog->data) /= 1e+6;
fe31f8b9
BV
136 }
137 } else if ((u = strstr(e, "Hz"))) {
3f5cf2a0
UH
138 analog->meaning->mq = SR_MQ_FREQUENCY;
139 analog->meaning->unit = SR_UNIT_HERTZ;
fe31f8b9 140 if (e[0] == 'k')
3f5cf2a0 141 *((float *)analog->data) *= 1e+3;
fe31f8b9 142 } else if (!strcmp(e, "%")) {
3f5cf2a0
UH
143 analog->meaning->mq = SR_MQ_DUTY_CYCLE;
144 analog->meaning->unit = SR_UNIT_PERCENTAGE;
fe31f8b9 145 } else if ((u = strstr(e, "ms"))) {
3f5cf2a0
UH
146 analog->meaning->mq = SR_MQ_PULSE_WIDTH;
147 analog->meaning->unit = SR_UNIT_SECOND;
148 *((float *)analog->data) /= 1e+3;
fe31f8b9
BV
149 }
150
3f5cf2a0 151 if (analog->meaning->mq == 0) {
fe31f8b9
BV
152 /* Not a valid measurement. */
153 g_free(analog->data);
154 g_free(analog);
155 analog = NULL;
156 }
157
158 return analog;
159}
160
3f5cf2a0 161static struct sr_datafeed_analog *handle_qm_28x(const struct sr_dev_inst *sdi,
d38d2ef0
BV
162 char **tokens)
163{
3f5cf2a0
UH
164 struct sr_datafeed_analog *analog;
165 struct sr_analog_encoding encoding;
166 struct sr_analog_meaning meaning;
167 struct sr_analog_spec spec;
d38d2ef0 168 float fvalue;
d38d2ef0 169
4cea0ff7
BV
170 if (!tokens[1])
171 return NULL;
172
357e341d
BV
173 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
174 sr_err("Invalid float '%s'.", tokens[0]);
d38d2ef0
BV
175 return NULL;
176 }
177
3f5cf2a0
UH
178 analog = g_malloc0(sizeof(struct sr_datafeed_analog));
179 sr_analog_init(analog, &encoding, &meaning, &spec, 0);
a95f142e 180 analog->data = g_malloc(sizeof(float));
3f5cf2a0 181 analog->meaning->channels = sdi->channels;
d38d2ef0 182 analog->num_samples = 1;
3f5cf2a0
UH
183 *((float *)analog->data) = fvalue;
184 analog->meaning->mq = 0;
d38d2ef0
BV
185
186 if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) {
3f5cf2a0
UH
187 analog->meaning->mq = SR_MQ_VOLTAGE;
188 analog->meaning->unit = SR_UNIT_VOLT;
d38d2ef0
BV
189 if (!strcmp(tokens[2], "NORMAL")) {
190 if (tokens[1][1] == 'A') {
3f5cf2a0
UH
191 analog->meaning->mqflags |= SR_MQFLAG_AC;
192 analog->meaning->mqflags |= SR_MQFLAG_RMS;
d38d2ef0 193 } else
3f5cf2a0 194 analog->meaning->mqflags |= SR_MQFLAG_DC;
d38d2ef0 195 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
3f5cf2a0 196 *((float *)analog->data) = NAN;
d38d2ef0 197 } else
3f5cf2a0 198 analog->meaning->mq = 0;
a28dac0a 199 } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
3f5cf2a0 200 analog->meaning->mq = SR_MQ_VOLTAGE;
2c04dede 201 if (tokens[1][2] == 'm')
3f5cf2a0 202 analog->meaning->unit = SR_UNIT_DECIBEL_MW;
2c04dede 203 else
3f5cf2a0
UH
204 analog->meaning->unit = SR_UNIT_DECIBEL_VOLT;
205 analog->meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
a28dac0a 206 } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
d38d2ef0 207 if (!strcmp(tokens[2], "NORMAL")) {
3f5cf2a0 208 analog->meaning->mq = SR_MQ_TEMPERATURE;
d38d2ef0 209 if (tokens[1][0] == 'C')
3f5cf2a0 210 analog->meaning->unit = SR_UNIT_CELSIUS;
d38d2ef0 211 else
3f5cf2a0 212 analog->meaning->unit = SR_UNIT_FAHRENHEIT;
d38d2ef0 213 }
a28dac0a 214 } else if (!strcmp(tokens[1], "OHM")) {
d38d2ef0 215 if (!strcmp(tokens[3], "NONE")) {
3f5cf2a0
UH
216 analog->meaning->mq = SR_MQ_RESISTANCE;
217 analog->meaning->unit = SR_UNIT_OHM;
d38d2ef0 218 if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
3f5cf2a0 219 *((float *)analog->data) = INFINITY;
d38d2ef0 220 } else if (strcmp(tokens[2], "NORMAL"))
3f5cf2a0 221 analog->meaning->mq = 0;
d38d2ef0 222 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
3f5cf2a0
UH
223 analog->meaning->mq = SR_MQ_CONTINUITY;
224 analog->meaning->unit = SR_UNIT_BOOLEAN;
225 *((float *)analog->data) = 0.0;
d38d2ef0 226 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
3f5cf2a0
UH
227 analog->meaning->mq = SR_MQ_CONTINUITY;
228 analog->meaning->unit = SR_UNIT_BOOLEAN;
229 *((float *)analog->data) = 1.0;
d38d2ef0 230 }
a28dac0a 231 } else if (!strcmp(tokens[1], "F")
d38d2ef0
BV
232 && !strcmp(tokens[2], "NORMAL")
233 && !strcmp(tokens[3], "NONE")) {
3f5cf2a0
UH
234 analog->meaning->mq = SR_MQ_CAPACITANCE;
235 analog->meaning->unit = SR_UNIT_FARAD;
d38d2ef0 236 } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
3f5cf2a0
UH
237 analog->meaning->mq = SR_MQ_CURRENT;
238 analog->meaning->unit = SR_UNIT_AMPERE;
d38d2ef0
BV
239 if (!strcmp(tokens[2], "NORMAL")) {
240 if (tokens[1][1] == 'A') {
3f5cf2a0
UH
241 analog->meaning->mqflags |= SR_MQFLAG_AC;
242 analog->meaning->mqflags |= SR_MQFLAG_RMS;
d38d2ef0 243 } else
3f5cf2a0 244 analog->meaning->mqflags |= SR_MQFLAG_DC;
d38d2ef0 245 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
3f5cf2a0 246 *((float *)analog->data) = NAN;
d38d2ef0 247 } else
3f5cf2a0 248 analog->meaning->mq = 0;
d38d2ef0 249 } if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
3f5cf2a0
UH
250 analog->meaning->mq = SR_MQ_FREQUENCY;
251 analog->meaning->unit = SR_UNIT_HERTZ;
a28dac0a 252 } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
3f5cf2a0
UH
253 analog->meaning->mq = SR_MQ_DUTY_CYCLE;
254 analog->meaning->unit = SR_UNIT_PERCENTAGE;
a28dac0a 255 } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
3f5cf2a0
UH
256 analog->meaning->mq = SR_MQ_PULSE_WIDTH;
257 analog->meaning->unit = SR_UNIT_SECOND;
a28dac0a 258 } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
3f5cf2a0
UH
259 analog->meaning->mq = SR_MQ_CONDUCTANCE;
260 analog->meaning->unit = SR_UNIT_SIEMENS;
d38d2ef0
BV
261 }
262
3f5cf2a0 263 if (analog->meaning->mq == 0) {
d38d2ef0
BV
264 /* Not a valid measurement. */
265 g_free(analog->data);
266 g_free(analog);
267 analog = NULL;
268 }
269
270 return analog;
271}
272
11fb7110
BV
273static void handle_qm_19x_meta(const struct sr_dev_inst *sdi, char **tokens)
274{
275 struct dev_context *devc;
276 int meas_type, meas_unit, meas_char, i;
277
278 /* Make sure we have 7 valid tokens. */
279 for (i = 0; tokens[i] && i < 7; i++);
280 if (i != 7)
281 return;
282
283 if (strcmp(tokens[1], "1"))
284 /* Invalid measurement. */
285 return;
286
287 if (strcmp(tokens[2], "3"))
288 /* Only interested in input from the meter mode source. */
289 return;
290
291 devc = sdi->priv;
292
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. */
297 return;
298
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)
302 return;
303
304 meas_unit = strtol(tokens[3], NULL, 10);
305 if (meas_unit == 0)
306 /* Device is turned off. Really. */
307 return;
308 meas_char = strtol(tokens[4], NULL, 10);
309
3f5cf2a0 310 devc->mq = devc->unit = devc->mqflags = 0;
11fb7110
BV
311 switch (meas_unit) {
312 case 1:
313 devc->mq = SR_MQ_VOLTAGE;
314 devc->unit = SR_UNIT_VOLT;
315 if (meas_char == 1)
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;
323 break;
324 case 2:
325 devc->mq = SR_MQ_CURRENT;
326 devc->unit = SR_UNIT_AMPERE;
327 if (meas_char == 1)
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;
333 break;
334 case 3:
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;
341 }
342 break;
343 case 12:
344 devc->mq = SR_MQ_TEMPERATURE;
345 devc->unit = SR_UNIT_CELSIUS;
346 break;
347 case 13:
348 devc->mq = SR_MQ_TEMPERATURE;
349 devc->unit = SR_UNIT_FAHRENHEIT;
350 break;
351 default:
352 sr_dbg("unknown unit: %d", meas_unit);
353 }
3f5cf2a0 354 if (devc->mq == 0 && devc->unit == 0)
11fb7110
BV
355 return;
356
357 /* If we got here, we know how to interpret the measurement. */
358 devc->meas_type = meas_type;
359 if (meas_type == 11)
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;
365
366}
367
368static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens)
369{
370 struct dev_context *devc;
371 struct sr_datafeed_packet packet;
3f5cf2a0
UH
372 struct sr_datafeed_analog analog;
373 struct sr_analog_encoding encoding;
374 struct sr_analog_meaning meaning;
375 struct sr_analog_spec spec;
11fb7110 376 float fvalue;
11fb7110
BV
377
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. */
382 fvalue = NAN;
383 } else {
357e341d 384 if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) {
11fb7110
BV
385 sr_err("Invalid float '%s'.", tokens[0]);
386 return;
387 }
388 }
389
390 devc = sdi->priv;
3f5cf2a0 391 if (devc->mq == 0 || devc->unit == 0)
11fb7110
BV
392 /* Don't have valid metadata yet. */
393 return;
394
11fb7110
BV
395 if (devc->mq == SR_MQ_RESISTANCE && isnan(fvalue))
396 fvalue = INFINITY;
397 else if (devc->mq == SR_MQ_CONTINUITY) {
398 if (isnan(fvalue))
399 fvalue = 0.0;
400 else
401 fvalue = 1.0;
402 }
403
3f5cf2a0
UH
404 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
405 analog.meaning->channels = sdi->channels;
11fb7110
BV
406 analog.num_samples = 1;
407 analog.data = &fvalue;
3f5cf2a0
UH
408 analog.meaning->mq = devc->mq;
409 analog.meaning->unit = devc->unit;
410 analog.meaning->mqflags = 0;
411 packet.type = SR_DF_ANALOG;
11fb7110 412 packet.payload = &analog;
695dc859 413 sr_session_send(sdi, &packet);
11fb7110 414
6aacf011 415 sr_sw_limits_update_samples_read(&devc->limits, 1);
11fb7110
BV
416}
417
d38d2ef0
BV
418static void handle_line(const struct sr_dev_inst *sdi)
419{
420 struct dev_context *devc;
919681f0 421 struct sr_serial_dev_inst *serial;
d38d2ef0 422 struct sr_datafeed_packet packet;
3f5cf2a0 423 struct sr_datafeed_analog *analog;
11fb7110
BV
424 int num_tokens, n, i;
425 char cmd[16], **tokens;
d38d2ef0
BV
426
427 devc = sdi->priv;
919681f0 428 serial = sdi->conn;
31d84da3 429 sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
d38d2ef0
BV
430
431 if (devc->buflen == 1) {
432 if (devc->buf[0] != '0') {
433 /* Not just a CMD_ACK from the query command. */
31d84da3 434 sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
d38d2ef0
BV
435 devc->expect_response = FALSE;
436 }
437 devc->buflen = 0;
438 return;
439 }
440
441 analog = NULL;
442 tokens = g_strsplit(devc->buf, ",", 0);
4cea0ff7 443 if (tokens[0]) {
0bc3ab92 444 if (devc->profile->model == FLUKE_187 || devc->profile->model == FLUKE_189) {
fe31f8b9 445 devc->expect_response = FALSE;
4cea0ff7 446 analog = handle_qm_18x(sdi, tokens);
ba4dfbde 447 } else if (devc->profile->model == FLUKE_287 || devc->profile->model == FLUKE_289) {
d38d2ef0 448 devc->expect_response = FALSE;
4cea0ff7 449 analog = handle_qm_28x(sdi, tokens);
11fb7110
BV
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. */
458 devc->meas_type = 0;
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);
20401400 465 if (serial_write_blocking(serial, cmd, n, SERIAL_WRITE_TIMEOUT_MS) < 0)
bb27c765 466 sr_err("Unable to send QM (measurement).");
11fb7110
BV
467 }
468 } else {
469 /* Response to QM <n> measurement request. */
470 handle_qm_19x_data(sdi, tokens);
471 }
d38d2ef0 472 }
d38d2ef0
BV
473 }
474 g_strfreev(tokens);
475 devc->buflen = 0;
476
477 if (analog) {
478 /* Got a measurement. */
3f5cf2a0 479 packet.type = SR_DF_ANALOG;
d38d2ef0 480 packet.payload = analog;
695dc859 481 sr_session_send(sdi, &packet);
6aacf011 482 sr_sw_limits_update_samples_read(&devc->limits, 1);
d38d2ef0
BV
483 g_free(analog->data);
484 g_free(analog);
485 }
486
487}
488
489SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
490{
642e9d62 491 struct sr_dev_inst *sdi;
d38d2ef0 492 struct dev_context *devc;
919681f0 493 struct sr_serial_dev_inst *serial;
d38d2ef0
BV
494 int len;
495 int64_t now, elapsed;
496
58d03f03
BV
497 (void)fd;
498
d38d2ef0
BV
499 if (!(sdi = cb_data))
500 return TRUE;
501
502 if (!(devc = sdi->priv))
503 return TRUE;
504
919681f0 505 serial = sdi->conn;
d38d2ef0
BV
506 if (revents == G_IO_IN) {
507 /* Serial data arrived. */
0c5f2abc 508 while (FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
707fa85a 509 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
d38d2ef0
BV
510 if (len < 1)
511 break;
512 devc->buflen++;
513 *(devc->buf + devc->buflen) = '\0';
514 if (*(devc->buf + devc->buflen - 1) == '\r') {
515 *(devc->buf + --devc->buflen) = '\0';
516 handle_line(sdi);
517 break;
518 }
519 }
520 }
521
6aacf011 522 if (sr_sw_limits_check(&devc->limits)) {
695dc859 523 sdi->driver->dev_acquisition_stop(sdi);
d38d2ef0
BV
524 return TRUE;
525 }
526
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
d4b11de0
BV
530 * has elapsed. This will make it easier to recover from any
531 * out-of-sync or temporary disconnect issues. */
d38d2ef0 532 if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
d4b11de0 533 || elapsed > devc->profile->timeout) {
20401400 534 if (serial_write_blocking(serial, "QM\r", 3, SERIAL_WRITE_TIMEOUT_MS) < 0)
bb27c765 535 sr_err("Unable to send QM.");
d38d2ef0
BV
536 devc->cmd_sent_at = now;
537 devc->expect_response = TRUE;
538 }
539
540 return TRUE;
541}