]>
Commit | Line | Data |
---|---|---|
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 | ||
d38d2ef0 BV |
20 | #include <stdlib.h> |
21 | #include <math.h> | |
22 | #include <string.h> | |
23 | #include <errno.h> | |
4cea0ff7 BV |
24 | #include <glib.h> |
25 | #include "libsigrok.h" | |
26 | #include "libsigrok-internal.h" | |
27 | #include "fluke-dmm.h" | |
d38d2ef0 BV |
28 | |
29 | ||
4cea0ff7 | 30 | static struct sr_datafeed_analog *handle_qm_18x(const struct sr_dev_inst *sdi, |
fe31f8b9 BV |
31 | char **tokens) |
32 | { | |
33 | struct sr_datafeed_analog *analog; | |
34 | float fvalue; | |
35 | char *e, *u; | |
36 | gboolean is_oor; | |
37 | ||
38 | (void)sdi; | |
64d33dc2 | 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; | |
46 | } else { | |
47 | is_oor = FALSE; | |
48 | fvalue = strtof(tokens[1], &e); | |
49 | if (fvalue == 0.0 && e == tokens[1]) { | |
50 | /* Happens all the time, when switching modes. */ | |
31d84da3 | 51 | sr_dbg("Invalid float."); |
fe31f8b9 BV |
52 | return NULL; |
53 | } | |
54 | } | |
55 | while(*e && *e == ' ') | |
56 | e++; | |
57 | ||
69e19dd7 BV |
58 | if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) |
59 | return NULL; | |
60 | if (!(analog->data = g_try_malloc(sizeof(float)))) | |
61 | return NULL; | |
62 | analog->probes = sdi->probes; | |
fe31f8b9 | 63 | analog->num_samples = 1; |
fe31f8b9 BV |
64 | if (is_oor) |
65 | *analog->data = NAN; | |
66 | else | |
67 | *analog->data = fvalue; | |
68 | analog->mq = -1; | |
69 | ||
70 | if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) { | |
71 | analog->mq = SR_MQ_VOLTAGE; | |
72 | analog->unit = SR_UNIT_VOLT; | |
73 | if (!is_oor && e[0] == 'm') | |
74 | *analog->data /= 1000; | |
75 | /* This catches "V AC", "V DC" and "V AC+DC". */ | |
76 | if (strstr(u, "AC")) | |
77 | analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS; | |
78 | if (strstr(u, "DC")) | |
79 | analog->mqflags |= SR_MQFLAG_DC; | |
80 | } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) { | |
81 | analog->mq = SR_MQ_VOLTAGE; | |
82 | if (u[2] == 'm') | |
83 | analog->unit = SR_UNIT_DECIBEL_MW; | |
84 | else | |
85 | analog->unit = SR_UNIT_DECIBEL_VOLT; | |
86 | analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS; | |
87 | } else if ((u = strstr(e, "Ohms"))) { | |
88 | analog->mq = SR_MQ_RESISTANCE; | |
89 | analog->unit = SR_UNIT_OHM; | |
90 | if (is_oor) | |
91 | *analog->data = INFINITY; | |
92 | else if (e[0] == 'k') | |
93 | *analog->data *= 1000; | |
94 | else if (e[0] == 'M') | |
95 | *analog->data *= 1000000; | |
96 | } else if (!strcmp(e, "nS")) { | |
97 | analog->mq = SR_MQ_CONDUCTANCE; | |
98 | analog->unit = SR_UNIT_SIEMENS; | |
99 | *analog->data /= 1e+9; | |
100 | } else if ((u = strstr(e, "Farads"))) { | |
101 | analog->mq = SR_MQ_CAPACITANCE; | |
102 | analog->unit = SR_UNIT_FARAD; | |
103 | if (!is_oor) { | |
104 | if (e[0] == 'm') | |
105 | *analog->data /= 1e+3; | |
106 | else if (e[0] == 'u') | |
107 | *analog->data /= 1e+6; | |
108 | else if (e[0] == 'n') | |
109 | *analog->data /= 1e+9; | |
110 | } | |
111 | } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) { | |
112 | analog->mq = SR_MQ_TEMPERATURE; | |
113 | if (u[4] == 'C') | |
114 | analog->unit = SR_UNIT_CELSIUS; | |
115 | else | |
116 | analog->unit = SR_UNIT_FAHRENHEIT; | |
117 | } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) { | |
118 | analog->mq = SR_MQ_CURRENT; | |
119 | analog->unit = SR_UNIT_AMPERE; | |
120 | /* This catches "A AC", "A DC" and "A AC+DC". */ | |
121 | if (strstr(u, "AC")) | |
122 | analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS; | |
123 | if (strstr(u, "DC")) | |
124 | analog->mqflags |= SR_MQFLAG_DC; | |
125 | if (!is_oor) { | |
126 | if (e[0] == 'm') | |
127 | *analog->data /= 1e+3; | |
128 | else if (e[0] == 'u') | |
129 | *analog->data /= 1e+6; | |
130 | } | |
131 | } else if ((u = strstr(e, "Hz"))) { | |
132 | analog->mq = SR_MQ_FREQUENCY; | |
133 | analog->unit = SR_UNIT_HERTZ; | |
134 | if (e[0] == 'k') | |
135 | *analog->data *= 1e+3; | |
136 | } else if (!strcmp(e, "%")) { | |
137 | analog->mq = SR_MQ_DUTY_CYCLE; | |
138 | analog->unit = SR_UNIT_PERCENTAGE; | |
139 | } else if ((u = strstr(e, "ms"))) { | |
140 | analog->mq = SR_MQ_PULSE_WIDTH; | |
141 | analog->unit = SR_UNIT_SECOND; | |
142 | *analog->data /= 1e+3; | |
143 | } | |
144 | ||
145 | if (analog->mq == -1) { | |
146 | /* Not a valid measurement. */ | |
147 | g_free(analog->data); | |
148 | g_free(analog); | |
149 | analog = NULL; | |
150 | } | |
151 | ||
152 | return analog; | |
153 | } | |
154 | ||
4cea0ff7 | 155 | static struct sr_datafeed_analog *handle_qm_28x(const struct sr_dev_inst *sdi, |
d38d2ef0 BV |
156 | char **tokens) |
157 | { | |
158 | struct sr_datafeed_analog *analog; | |
159 | float fvalue; | |
160 | char *eptr; | |
161 | ||
162 | (void)sdi; | |
64d33dc2 | 163 | |
4cea0ff7 BV |
164 | if (!tokens[1]) |
165 | return NULL; | |
166 | ||
d38d2ef0 BV |
167 | fvalue = strtof(tokens[0], &eptr); |
168 | if (fvalue == 0.0 && eptr == tokens[0]) { | |
31d84da3 | 169 | sr_err("Invalid float."); |
d38d2ef0 BV |
170 | return NULL; |
171 | } | |
172 | ||
69e19dd7 BV |
173 | if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) |
174 | return NULL; | |
175 | if (!(analog->data = g_try_malloc(sizeof(float)))) | |
176 | return NULL; | |
177 | analog->probes = sdi->probes; | |
d38d2ef0 | 178 | analog->num_samples = 1; |
d38d2ef0 BV |
179 | *analog->data = fvalue; |
180 | analog->mq = -1; | |
181 | ||
182 | if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) { | |
183 | analog->mq = SR_MQ_VOLTAGE; | |
184 | analog->unit = SR_UNIT_VOLT; | |
185 | if (!strcmp(tokens[2], "NORMAL")) { | |
186 | if (tokens[1][1] == 'A') { | |
187 | analog->mqflags |= SR_MQFLAG_AC; | |
188 | analog->mqflags |= SR_MQFLAG_RMS; | |
189 | } else | |
190 | analog->mqflags |= SR_MQFLAG_DC; | |
191 | } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) { | |
192 | *analog->data = NAN; | |
193 | } else | |
194 | analog->mq = -1; | |
a28dac0a | 195 | } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) { |
2c04dede BV |
196 | analog->mq = SR_MQ_VOLTAGE; |
197 | if (tokens[1][2] == 'm') | |
198 | analog->unit = SR_UNIT_DECIBEL_MW; | |
199 | else | |
200 | analog->unit = SR_UNIT_DECIBEL_VOLT; | |
201 | analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS; | |
a28dac0a | 202 | } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) { |
d38d2ef0 BV |
203 | if (!strcmp(tokens[2], "NORMAL")) { |
204 | analog->mq = SR_MQ_TEMPERATURE; | |
205 | if (tokens[1][0] == 'C') | |
fe31f8b9 | 206 | analog->unit = SR_UNIT_CELSIUS; |
d38d2ef0 BV |
207 | else |
208 | analog->unit = SR_UNIT_FAHRENHEIT; | |
209 | } | |
a28dac0a | 210 | } else if (!strcmp(tokens[1], "OHM")) { |
d38d2ef0 BV |
211 | if (!strcmp(tokens[3], "NONE")) { |
212 | analog->mq = SR_MQ_RESISTANCE; | |
213 | analog->unit = SR_UNIT_OHM; | |
214 | if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) { | |
215 | *analog->data = INFINITY; | |
216 | } else if (strcmp(tokens[2], "NORMAL")) | |
217 | analog->mq = -1; | |
218 | } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) { | |
219 | analog->mq = SR_MQ_CONTINUITY; | |
220 | analog->unit = SR_UNIT_BOOLEAN; | |
221 | *analog->data = 0.0; | |
222 | } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) { | |
223 | analog->mq = SR_MQ_CONTINUITY; | |
224 | analog->unit = SR_UNIT_BOOLEAN; | |
225 | *analog->data = 1.0; | |
226 | } | |
a28dac0a | 227 | } else if (!strcmp(tokens[1], "F") |
d38d2ef0 BV |
228 | && !strcmp(tokens[2], "NORMAL") |
229 | && !strcmp(tokens[3], "NONE")) { | |
230 | analog->mq = SR_MQ_CAPACITANCE; | |
231 | analog->unit = SR_UNIT_FARAD; | |
232 | } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) { | |
233 | analog->mq = SR_MQ_CURRENT; | |
234 | analog->unit = SR_UNIT_AMPERE; | |
d38d2ef0 BV |
235 | if (!strcmp(tokens[2], "NORMAL")) { |
236 | if (tokens[1][1] == 'A') { | |
237 | analog->mqflags |= SR_MQFLAG_AC; | |
238 | analog->mqflags |= SR_MQFLAG_RMS; | |
239 | } else | |
240 | analog->mqflags |= SR_MQFLAG_DC; | |
241 | } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) { | |
242 | *analog->data = NAN; | |
243 | } else | |
244 | analog->mq = -1; | |
245 | } if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) { | |
246 | analog->mq = SR_MQ_FREQUENCY; | |
247 | analog->unit = SR_UNIT_HERTZ; | |
a28dac0a | 248 | } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) { |
d38d2ef0 BV |
249 | analog->mq = SR_MQ_DUTY_CYCLE; |
250 | analog->unit = SR_UNIT_PERCENTAGE; | |
a28dac0a | 251 | } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) { |
d38d2ef0 BV |
252 | analog->mq = SR_MQ_PULSE_WIDTH; |
253 | analog->unit = SR_UNIT_SECOND; | |
a28dac0a | 254 | } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) { |
d38d2ef0 BV |
255 | analog->mq = SR_MQ_CONDUCTANCE; |
256 | analog->unit = SR_UNIT_SIEMENS; | |
257 | } | |
258 | ||
d38d2ef0 BV |
259 | if (analog->mq == -1) { |
260 | /* Not a valid measurement. */ | |
261 | g_free(analog->data); | |
262 | g_free(analog); | |
263 | analog = NULL; | |
264 | } | |
265 | ||
266 | return analog; | |
267 | } | |
268 | ||
11fb7110 BV |
269 | static void handle_qm_19x_meta(const struct sr_dev_inst *sdi, char **tokens) |
270 | { | |
271 | struct dev_context *devc; | |
272 | int meas_type, meas_unit, meas_char, i; | |
273 | ||
274 | /* Make sure we have 7 valid tokens. */ | |
275 | for (i = 0; tokens[i] && i < 7; i++); | |
276 | if (i != 7) | |
277 | return; | |
278 | ||
279 | if (strcmp(tokens[1], "1")) | |
280 | /* Invalid measurement. */ | |
281 | return; | |
282 | ||
283 | if (strcmp(tokens[2], "3")) | |
284 | /* Only interested in input from the meter mode source. */ | |
285 | return; | |
286 | ||
287 | devc = sdi->priv; | |
288 | ||
289 | /* Measurement type 11 == absolute, 19 = relative */ | |
290 | meas_type = strtol(tokens[0], NULL, 10); | |
291 | if (meas_type != 11 && meas_type != 19) | |
292 | /* Device is in some mode we don't support. */ | |
293 | return; | |
294 | ||
295 | /* We might get metadata for absolute and relative mode (if the device | |
296 | * is in relative mode). In that case, relative takes precedence. */ | |
297 | if (meas_type == 11 && devc->meas_type == 19) | |
298 | return; | |
299 | ||
300 | meas_unit = strtol(tokens[3], NULL, 10); | |
301 | if (meas_unit == 0) | |
302 | /* Device is turned off. Really. */ | |
303 | return; | |
304 | meas_char = strtol(tokens[4], NULL, 10); | |
305 | ||
306 | devc->mq = devc->unit = -1; | |
307 | devc->mqflags = 0; | |
308 | switch (meas_unit) { | |
309 | case 1: | |
310 | devc->mq = SR_MQ_VOLTAGE; | |
311 | devc->unit = SR_UNIT_VOLT; | |
312 | if (meas_char == 1) | |
313 | devc->mqflags |= SR_MQFLAG_DC; | |
314 | else if (meas_char == 2) | |
315 | devc->mqflags |= SR_MQFLAG_AC; | |
316 | else if (meas_char == 3) | |
317 | devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC; | |
318 | else if (meas_char == 15) | |
319 | devc->mqflags |= SR_MQFLAG_DIODE; | |
320 | break; | |
321 | case 2: | |
322 | devc->mq = SR_MQ_CURRENT; | |
323 | devc->unit = SR_UNIT_AMPERE; | |
324 | if (meas_char == 1) | |
325 | devc->mqflags |= SR_MQFLAG_DC; | |
326 | else if (meas_char == 2) | |
327 | devc->mqflags |= SR_MQFLAG_AC; | |
328 | else if (meas_char == 3) | |
329 | devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC; | |
330 | break; | |
331 | case 3: | |
332 | if (meas_char == 1) { | |
333 | devc->mq = SR_MQ_RESISTANCE; | |
334 | devc->unit = SR_UNIT_OHM; | |
335 | } else if (meas_char == 16) { | |
336 | devc->mq = SR_MQ_CONTINUITY; | |
337 | devc->unit = SR_UNIT_BOOLEAN; | |
338 | } | |
339 | break; | |
340 | case 12: | |
341 | devc->mq = SR_MQ_TEMPERATURE; | |
342 | devc->unit = SR_UNIT_CELSIUS; | |
343 | break; | |
344 | case 13: | |
345 | devc->mq = SR_MQ_TEMPERATURE; | |
346 | devc->unit = SR_UNIT_FAHRENHEIT; | |
347 | break; | |
348 | default: | |
349 | sr_dbg("unknown unit: %d", meas_unit); | |
350 | } | |
351 | if (devc->mq == -1 && devc->unit == -1) | |
352 | return; | |
353 | ||
354 | /* If we got here, we know how to interpret the measurement. */ | |
355 | devc->meas_type = meas_type; | |
356 | if (meas_type == 11) | |
357 | /* Absolute meter reading. */ | |
358 | devc->is_relative = FALSE; | |
359 | else if (!strcmp(tokens[0], "19")) | |
360 | /* Relative meter reading. */ | |
361 | devc->is_relative = TRUE; | |
362 | ||
363 | } | |
364 | ||
365 | static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens) | |
366 | { | |
367 | struct dev_context *devc; | |
368 | struct sr_datafeed_packet packet; | |
369 | struct sr_datafeed_analog analog; | |
370 | float fvalue; | |
371 | char *eptr; | |
372 | ||
373 | if (!strcmp(tokens[0], "9.9E+37")) { | |
374 | /* An invalid measurement shows up on the display as "OL", but | |
375 | * comes through like this. Since comparing 38-digit floats | |
376 | * is rather problematic, we'll cut through this here. */ | |
377 | fvalue = NAN; | |
378 | } else { | |
379 | fvalue = strtof(tokens[0], &eptr); | |
380 | if (fvalue == 0.0 && eptr == tokens[0]) { | |
381 | sr_err("Invalid float '%s'.", tokens[0]); | |
382 | return; | |
383 | } | |
384 | } | |
385 | ||
386 | devc = sdi->priv; | |
387 | if (devc->mq == -1 || devc->unit == -1) | |
388 | /* Don't have valid metadata yet. */ | |
389 | return; | |
390 | ||
391 | ||
392 | if (devc->mq == SR_MQ_RESISTANCE && isnan(fvalue)) | |
393 | fvalue = INFINITY; | |
394 | else if (devc->mq == SR_MQ_CONTINUITY) { | |
395 | if (isnan(fvalue)) | |
396 | fvalue = 0.0; | |
397 | else | |
398 | fvalue = 1.0; | |
399 | } | |
400 | ||
69e19dd7 | 401 | analog.probes = sdi->probes; |
11fb7110 BV |
402 | analog.num_samples = 1; |
403 | analog.data = &fvalue; | |
404 | analog.mq = devc->mq; | |
405 | analog.unit = devc->unit; | |
406 | analog.mqflags = 0; | |
407 | packet.type = SR_DF_ANALOG; | |
408 | packet.payload = &analog; | |
409 | sr_session_send(devc->cb_data, &packet); | |
410 | devc->num_samples++; | |
411 | ||
412 | } | |
413 | ||
d38d2ef0 BV |
414 | static void handle_line(const struct sr_dev_inst *sdi) |
415 | { | |
416 | struct dev_context *devc; | |
919681f0 | 417 | struct sr_serial_dev_inst *serial; |
d38d2ef0 BV |
418 | struct sr_datafeed_packet packet; |
419 | struct sr_datafeed_analog *analog; | |
11fb7110 BV |
420 | int num_tokens, n, i; |
421 | char cmd[16], **tokens; | |
d38d2ef0 BV |
422 | |
423 | devc = sdi->priv; | |
919681f0 | 424 | serial = sdi->conn; |
31d84da3 | 425 | sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen); |
d38d2ef0 BV |
426 | |
427 | if (devc->buflen == 1) { | |
428 | if (devc->buf[0] != '0') { | |
429 | /* Not just a CMD_ACK from the query command. */ | |
31d84da3 | 430 | sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]); |
d38d2ef0 BV |
431 | devc->expect_response = FALSE; |
432 | } | |
433 | devc->buflen = 0; | |
434 | return; | |
435 | } | |
436 | ||
437 | analog = NULL; | |
438 | tokens = g_strsplit(devc->buf, ",", 0); | |
4cea0ff7 | 439 | if (tokens[0]) { |
fe31f8b9 BV |
440 | if (devc->profile->model == FLUKE_187) { |
441 | devc->expect_response = FALSE; | |
4cea0ff7 | 442 | analog = handle_qm_18x(sdi, tokens); |
fe31f8b9 | 443 | } else if (devc->profile->model == FLUKE_287) { |
d38d2ef0 | 444 | devc->expect_response = FALSE; |
4cea0ff7 | 445 | analog = handle_qm_28x(sdi, tokens); |
11fb7110 BV |
446 | } else if (devc->profile->model == FLUKE_190) { |
447 | devc->expect_response = FALSE; | |
448 | for (num_tokens = 0; tokens[num_tokens]; num_tokens++); | |
449 | if (num_tokens >= 7) { | |
450 | /* Response to QM: this is a comma-separated list of | |
451 | * fields with metadata about the measurement. This | |
452 | * format can return multiple sets of metadata, | |
453 | * split into sets of 7 tokens each. */ | |
454 | devc->meas_type = 0; | |
455 | for (i = 0; i < num_tokens; i += 7) | |
456 | handle_qm_19x_meta(sdi, tokens + i); | |
457 | if (devc->meas_type) { | |
458 | /* Slip the request in now, before the main | |
459 | * timer loop asks for metadata again. */ | |
460 | n = sprintf(cmd, "QM %d\r", devc->meas_type); | |
919681f0 | 461 | if (serial_write(serial, cmd, n) == -1) |
11fb7110 BV |
462 | sr_err("Unable to send QM (measurement): %s.", |
463 | strerror(errno)); | |
464 | } | |
465 | } else { | |
466 | /* Response to QM <n> measurement request. */ | |
467 | handle_qm_19x_data(sdi, tokens); | |
468 | } | |
d38d2ef0 | 469 | } |
d38d2ef0 BV |
470 | } |
471 | g_strfreev(tokens); | |
472 | devc->buflen = 0; | |
473 | ||
474 | if (analog) { | |
475 | /* Got a measurement. */ | |
d38d2ef0 BV |
476 | packet.type = SR_DF_ANALOG; |
477 | packet.payload = analog; | |
478 | sr_session_send(devc->cb_data, &packet); | |
479 | devc->num_samples++; | |
480 | g_free(analog->data); | |
481 | g_free(analog); | |
482 | } | |
483 | ||
484 | } | |
485 | ||
486 | SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data) | |
487 | { | |
642e9d62 | 488 | struct sr_dev_inst *sdi; |
d38d2ef0 | 489 | struct dev_context *devc; |
919681f0 | 490 | struct sr_serial_dev_inst *serial; |
d38d2ef0 BV |
491 | int len; |
492 | int64_t now, elapsed; | |
493 | ||
58d03f03 BV |
494 | (void)fd; |
495 | ||
d38d2ef0 BV |
496 | if (!(sdi = cb_data)) |
497 | return TRUE; | |
498 | ||
499 | if (!(devc = sdi->priv)) | |
500 | return TRUE; | |
501 | ||
919681f0 | 502 | serial = sdi->conn; |
d38d2ef0 BV |
503 | if (revents == G_IO_IN) { |
504 | /* Serial data arrived. */ | |
505 | while(FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) { | |
919681f0 | 506 | len = serial_read(serial, devc->buf + devc->buflen, 1); |
d38d2ef0 BV |
507 | if (len < 1) |
508 | break; | |
509 | devc->buflen++; | |
510 | *(devc->buf + devc->buflen) = '\0'; | |
511 | if (*(devc->buf + devc->buflen - 1) == '\r') { | |
512 | *(devc->buf + --devc->buflen) = '\0'; | |
513 | handle_line(sdi); | |
514 | break; | |
515 | } | |
516 | } | |
517 | } | |
518 | ||
2f1e5c7c | 519 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { |
d38d2ef0 BV |
520 | sdi->driver->dev_acquisition_stop(sdi, cb_data); |
521 | return TRUE; | |
522 | } | |
523 | ||
524 | now = g_get_monotonic_time() / 1000; | |
525 | elapsed = now - devc->cmd_sent_at; | |
526 | /* Send query command at poll_period interval, or after 1 second | |
d4b11de0 BV |
527 | * has elapsed. This will make it easier to recover from any |
528 | * out-of-sync or temporary disconnect issues. */ | |
d38d2ef0 | 529 | if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period) |
d4b11de0 | 530 | || elapsed > devc->profile->timeout) { |
919681f0 | 531 | if (serial_write(serial, "QM\r", 3) == -1) |
31d84da3 | 532 | sr_err("Unable to send QM: %s.", strerror(errno)); |
d38d2ef0 BV |
533 | devc->cmd_sent_at = now; |
534 | devc->expect_response = TRUE; | |
535 | } | |
536 | ||
537 | return TRUE; | |
538 | } | |
539 | ||
540 |