]>
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 | ||
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 | 26 | #include "libsigrok-internal.h" |
2cb232a9 | 27 | #include "protocol.h" |
d38d2ef0 | 28 | |
d2e3ce76 MP |
29 | static int count_digits(const char *str) { |
30 | int digits; | |
31 | ||
32 | while (*str && *str != ' ' && *str != ',' && *str != '.') | |
33 | str++; | |
34 | ||
35 | digits = 0; | |
36 | if (*str == '.') { | |
37 | str++; | |
38 | while (*str && *str != ' ' && *str != ',') { | |
39 | str++; | |
40 | digits++; | |
41 | } | |
42 | } | |
43 | ||
44 | return digits; | |
45 | } | |
46 | ||
19ab8e36 | 47 | static void handle_qm_18x(const struct sr_dev_inst *sdi, char **tokens) |
fe31f8b9 | 48 | { |
19ab8e36 AS |
49 | struct dev_context *devc; |
50 | struct sr_datafeed_packet packet; | |
51 | struct sr_datafeed_analog analog; | |
3f5cf2a0 UH |
52 | struct sr_analog_encoding encoding; |
53 | struct sr_analog_meaning meaning; | |
54 | struct sr_analog_spec spec; | |
fe31f8b9 BV |
55 | float fvalue; |
56 | char *e, *u; | |
57 | gboolean is_oor; | |
d2e3ce76 | 58 | int digits; |
104c7d56 MP |
59 | int exponent; |
60 | enum sr_mq mq; | |
61 | enum sr_unit unit; | |
62 | enum sr_mqflag mqflags; | |
fe31f8b9 | 63 | |
19ab8e36 AS |
64 | devc = sdi->priv; |
65 | ||
4cea0ff7 | 66 | if (strcmp(tokens[0], "QM") || !tokens[1]) |
19ab8e36 | 67 | return; |
fe31f8b9 BV |
68 | |
69 | if ((e = strstr(tokens[1], "Out of range"))) { | |
70 | is_oor = TRUE; | |
71 | fvalue = -1; | |
d2e3ce76 | 72 | digits = 0; |
0c5f2abc | 73 | while (*e && *e != '.') |
357e341d | 74 | e++; |
fe31f8b9 BV |
75 | } else { |
76 | is_oor = FALSE; | |
357e341d BV |
77 | /* Delimit the float, since sr_atof_ascii() wants only |
78 | * a valid float here. */ | |
79 | e = tokens[1]; | |
0c5f2abc | 80 | while (*e && *e != ' ') |
357e341d BV |
81 | e++; |
82 | *e++ = '\0'; | |
ad42cfeb | 83 | if (sr_atof_ascii(tokens[1], &fvalue) != SR_OK) { |
fe31f8b9 | 84 | /* Happens all the time, when switching modes. */ |
e7f803f3 | 85 | sr_dbg("Invalid float: '%s'", tokens[1]); |
19ab8e36 | 86 | return; |
fe31f8b9 | 87 | } |
d2e3ce76 | 88 | digits = count_digits(tokens[1]); |
fe31f8b9 | 89 | } |
0c5f2abc | 90 | while (*e && *e == ' ') |
fe31f8b9 BV |
91 | e++; |
92 | ||
fe31f8b9 | 93 | if (is_oor) |
19ab8e36 | 94 | fvalue = NAN; |
fe31f8b9 | 95 | |
104c7d56 MP |
96 | mq = 0; |
97 | unit = 0; | |
98 | exponent = 0; | |
99 | mqflags = 0; | |
fe31f8b9 | 100 | if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) { |
104c7d56 MP |
101 | mq = SR_MQ_VOLTAGE; |
102 | unit = SR_UNIT_VOLT; | |
fe31f8b9 | 103 | if (!is_oor && e[0] == 'm') |
104c7d56 | 104 | exponent = -3; |
fe31f8b9 BV |
105 | /* This catches "V AC", "V DC" and "V AC+DC". */ |
106 | if (strstr(u, "AC")) | |
104c7d56 | 107 | mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS; |
fe31f8b9 | 108 | if (strstr(u, "DC")) |
104c7d56 | 109 | mqflags |= SR_MQFLAG_DC; |
fe31f8b9 | 110 | } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) { |
104c7d56 | 111 | mq = SR_MQ_VOLTAGE; |
fe31f8b9 | 112 | if (u[2] == 'm') |
104c7d56 | 113 | unit = SR_UNIT_DECIBEL_MW; |
fe31f8b9 | 114 | else |
104c7d56 MP |
115 | unit = SR_UNIT_DECIBEL_VOLT; |
116 | mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS; | |
fe31f8b9 | 117 | } else if ((u = strstr(e, "Ohms"))) { |
104c7d56 MP |
118 | mq = SR_MQ_RESISTANCE; |
119 | unit = SR_UNIT_OHM; | |
fe31f8b9 | 120 | if (is_oor) |
19ab8e36 | 121 | fvalue = INFINITY; |
fe31f8b9 | 122 | else if (e[0] == 'k') |
104c7d56 | 123 | exponent = 3; |
fe31f8b9 | 124 | else if (e[0] == 'M') |
104c7d56 | 125 | exponent = 6; |
fe31f8b9 | 126 | } else if (!strcmp(e, "nS")) { |
104c7d56 MP |
127 | mq = SR_MQ_CONDUCTANCE; |
128 | unit = SR_UNIT_SIEMENS; | |
129 | exponent = -9; | |
fe31f8b9 | 130 | } else if ((u = strstr(e, "Farads"))) { |
104c7d56 MP |
131 | mq = SR_MQ_CAPACITANCE; |
132 | unit = SR_UNIT_FARAD; | |
fe31f8b9 BV |
133 | if (!is_oor) { |
134 | if (e[0] == 'm') | |
104c7d56 | 135 | exponent = -3; |
fe31f8b9 | 136 | else if (e[0] == 'u') |
104c7d56 | 137 | exponent = -6; |
fe31f8b9 | 138 | else if (e[0] == 'n') |
104c7d56 | 139 | exponent = -9; |
fe31f8b9 BV |
140 | } |
141 | } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) { | |
104c7d56 | 142 | mq = SR_MQ_TEMPERATURE; |
fe31f8b9 | 143 | if (u[4] == 'C') |
104c7d56 | 144 | unit = SR_UNIT_CELSIUS; |
fe31f8b9 | 145 | else |
104c7d56 | 146 | unit = SR_UNIT_FAHRENHEIT; |
fe31f8b9 | 147 | } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) { |
104c7d56 MP |
148 | mq = SR_MQ_CURRENT; |
149 | unit = SR_UNIT_AMPERE; | |
fe31f8b9 BV |
150 | /* This catches "A AC", "A DC" and "A AC+DC". */ |
151 | if (strstr(u, "AC")) | |
104c7d56 | 152 | mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS; |
fe31f8b9 | 153 | if (strstr(u, "DC")) |
104c7d56 | 154 | mqflags |= SR_MQFLAG_DC; |
fe31f8b9 BV |
155 | if (!is_oor) { |
156 | if (e[0] == 'm') | |
104c7d56 | 157 | exponent = -3; |
fe31f8b9 | 158 | else if (e[0] == 'u') |
104c7d56 | 159 | exponent = -6; |
fe31f8b9 BV |
160 | } |
161 | } else if ((u = strstr(e, "Hz"))) { | |
104c7d56 MP |
162 | mq = SR_MQ_FREQUENCY; |
163 | unit = SR_UNIT_HERTZ; | |
fe31f8b9 | 164 | if (e[0] == 'k') |
104c7d56 | 165 | exponent = 3; |
fe31f8b9 | 166 | } else if (!strcmp(e, "%")) { |
104c7d56 MP |
167 | mq = SR_MQ_DUTY_CYCLE; |
168 | unit = SR_UNIT_PERCENTAGE; | |
fe31f8b9 | 169 | } else if ((u = strstr(e, "ms"))) { |
104c7d56 MP |
170 | mq = SR_MQ_PULSE_WIDTH; |
171 | unit = SR_UNIT_SECOND; | |
172 | exponent = -3; | |
fe31f8b9 BV |
173 | } |
174 | ||
104c7d56 | 175 | if (mq != 0) { |
19ab8e36 | 176 | /* Got a measurement. */ |
104c7d56 MP |
177 | digits -= exponent; |
178 | fvalue *= pow(10.0f, exponent); | |
179 | ||
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; | |
187 | ||
19ab8e36 AS |
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); | |
fe31f8b9 | 192 | } |
fe31f8b9 BV |
193 | } |
194 | ||
19ab8e36 | 195 | static void handle_qm_28x(const struct sr_dev_inst *sdi, char **tokens) |
d38d2ef0 | 196 | { |
19ab8e36 AS |
197 | struct dev_context *devc; |
198 | struct sr_datafeed_packet packet; | |
199 | struct sr_datafeed_analog analog; | |
3f5cf2a0 UH |
200 | struct sr_analog_encoding encoding; |
201 | struct sr_analog_meaning meaning; | |
202 | struct sr_analog_spec spec; | |
d38d2ef0 | 203 | float fvalue; |
d2e3ce76 | 204 | int digits; |
d38d2ef0 | 205 | |
19ab8e36 AS |
206 | devc = sdi->priv; |
207 | ||
4cea0ff7 | 208 | if (!tokens[1]) |
19ab8e36 | 209 | return; |
4cea0ff7 | 210 | |
ad42cfeb | 211 | if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK) { |
357e341d | 212 | sr_err("Invalid float '%s'.", tokens[0]); |
19ab8e36 | 213 | return; |
d38d2ef0 | 214 | } |
d2e3ce76 | 215 | digits = count_digits(tokens[0]); |
d38d2ef0 | 216 | |
d2e3ce76 | 217 | sr_analog_init(&analog, &encoding, &meaning, &spec, digits); |
19ab8e36 AS |
218 | analog.data = &fvalue; |
219 | analog.meaning->channels = sdi->channels; | |
220 | analog.num_samples = 1; | |
221 | analog.meaning->mq = 0; | |
d38d2ef0 BV |
222 | |
223 | if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) { | |
19ab8e36 AS |
224 | analog.meaning->mq = SR_MQ_VOLTAGE; |
225 | analog.meaning->unit = SR_UNIT_VOLT; | |
d38d2ef0 BV |
226 | if (!strcmp(tokens[2], "NORMAL")) { |
227 | if (tokens[1][1] == 'A') { | |
19ab8e36 AS |
228 | analog.meaning->mqflags |= SR_MQFLAG_AC; |
229 | analog.meaning->mqflags |= SR_MQFLAG_RMS; | |
d38d2ef0 | 230 | } else |
19ab8e36 | 231 | analog.meaning->mqflags |= SR_MQFLAG_DC; |
d38d2ef0 | 232 | } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) { |
19ab8e36 | 233 | fvalue = NAN; |
d38d2ef0 | 234 | } else |
19ab8e36 | 235 | analog.meaning->mq = 0; |
a28dac0a | 236 | } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) { |
19ab8e36 | 237 | analog.meaning->mq = SR_MQ_VOLTAGE; |
2c04dede | 238 | if (tokens[1][2] == 'm') |
19ab8e36 | 239 | analog.meaning->unit = SR_UNIT_DECIBEL_MW; |
2c04dede | 240 | else |
19ab8e36 AS |
241 | analog.meaning->unit = SR_UNIT_DECIBEL_VOLT; |
242 | analog.meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS; | |
a28dac0a | 243 | } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) { |
d38d2ef0 | 244 | if (!strcmp(tokens[2], "NORMAL")) { |
19ab8e36 | 245 | analog.meaning->mq = SR_MQ_TEMPERATURE; |
d38d2ef0 | 246 | if (tokens[1][0] == 'C') |
19ab8e36 | 247 | analog.meaning->unit = SR_UNIT_CELSIUS; |
d38d2ef0 | 248 | else |
19ab8e36 | 249 | analog.meaning->unit = SR_UNIT_FAHRENHEIT; |
d38d2ef0 | 250 | } |
a28dac0a | 251 | } else if (!strcmp(tokens[1], "OHM")) { |
d38d2ef0 | 252 | if (!strcmp(tokens[3], "NONE")) { |
19ab8e36 AS |
253 | analog.meaning->mq = SR_MQ_RESISTANCE; |
254 | analog.meaning->unit = SR_UNIT_OHM; | |
d38d2ef0 | 255 | if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) { |
19ab8e36 | 256 | fvalue = INFINITY; |
d38d2ef0 | 257 | } else if (strcmp(tokens[2], "NORMAL")) |
19ab8e36 | 258 | analog.meaning->mq = 0; |
d38d2ef0 | 259 | } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) { |
19ab8e36 AS |
260 | analog.meaning->mq = SR_MQ_CONTINUITY; |
261 | analog.meaning->unit = SR_UNIT_BOOLEAN; | |
262 | fvalue = 0.0; | |
d38d2ef0 | 263 | } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) { |
19ab8e36 AS |
264 | analog.meaning->mq = SR_MQ_CONTINUITY; |
265 | analog.meaning->unit = SR_UNIT_BOOLEAN; | |
266 | fvalue = 1.0; | |
d38d2ef0 | 267 | } |
a28dac0a | 268 | } else if (!strcmp(tokens[1], "F") |
d38d2ef0 BV |
269 | && !strcmp(tokens[2], "NORMAL") |
270 | && !strcmp(tokens[3], "NONE")) { | |
19ab8e36 AS |
271 | analog.meaning->mq = SR_MQ_CAPACITANCE; |
272 | analog.meaning->unit = SR_UNIT_FARAD; | |
d38d2ef0 | 273 | } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) { |
19ab8e36 AS |
274 | analog.meaning->mq = SR_MQ_CURRENT; |
275 | analog.meaning->unit = SR_UNIT_AMPERE; | |
d38d2ef0 BV |
276 | if (!strcmp(tokens[2], "NORMAL")) { |
277 | if (tokens[1][1] == 'A') { | |
19ab8e36 AS |
278 | analog.meaning->mqflags |= SR_MQFLAG_AC; |
279 | analog.meaning->mqflags |= SR_MQFLAG_RMS; | |
d38d2ef0 | 280 | } else |
19ab8e36 | 281 | analog.meaning->mqflags |= SR_MQFLAG_DC; |
d38d2ef0 | 282 | } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) { |
19ab8e36 | 283 | fvalue = NAN; |
d38d2ef0 | 284 | } else |
19ab8e36 AS |
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; | |
a28dac0a | 289 | } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) { |
19ab8e36 AS |
290 | analog.meaning->mq = SR_MQ_DUTY_CYCLE; |
291 | analog.meaning->unit = SR_UNIT_PERCENTAGE; | |
a28dac0a | 292 | } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) { |
19ab8e36 AS |
293 | analog.meaning->mq = SR_MQ_PULSE_WIDTH; |
294 | analog.meaning->unit = SR_UNIT_SECOND; | |
a28dac0a | 295 | } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) { |
19ab8e36 AS |
296 | analog.meaning->mq = SR_MQ_CONDUCTANCE; |
297 | analog.meaning->unit = SR_UNIT_SIEMENS; | |
d38d2ef0 BV |
298 | } |
299 | ||
19ab8e36 AS |
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); | |
d38d2ef0 | 306 | } |
d38d2ef0 BV |
307 | } |
308 | ||
11fb7110 BV |
309 | static void handle_qm_19x_meta(const struct sr_dev_inst *sdi, char **tokens) |
310 | { | |
311 | struct dev_context *devc; | |
312 | int meas_type, meas_unit, meas_char, i; | |
313 | ||
314 | /* Make sure we have 7 valid tokens. */ | |
315 | for (i = 0; tokens[i] && i < 7; i++); | |
316 | if (i != 7) | |
317 | return; | |
318 | ||
319 | if (strcmp(tokens[1], "1")) | |
320 | /* Invalid measurement. */ | |
321 | return; | |
322 | ||
323 | if (strcmp(tokens[2], "3")) | |
324 | /* Only interested in input from the meter mode source. */ | |
325 | return; | |
326 | ||
327 | devc = sdi->priv; | |
328 | ||
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. */ | |
333 | return; | |
334 | ||
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) | |
338 | return; | |
339 | ||
340 | meas_unit = strtol(tokens[3], NULL, 10); | |
341 | if (meas_unit == 0) | |
342 | /* Device is turned off. Really. */ | |
343 | return; | |
344 | meas_char = strtol(tokens[4], NULL, 10); | |
345 | ||
55ec0b67 UH |
346 | devc->mq = 0; |
347 | devc->unit = 0; | |
348 | devc->mqflags = 0; | |
349 | ||
11fb7110 BV |
350 | switch (meas_unit) { |
351 | case 1: | |
352 | devc->mq = SR_MQ_VOLTAGE; | |
353 | devc->unit = SR_UNIT_VOLT; | |
354 | if (meas_char == 1) | |
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) | |
64aa214a | 361 | devc->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC; |
11fb7110 BV |
362 | break; |
363 | case 2: | |
364 | devc->mq = SR_MQ_CURRENT; | |
365 | devc->unit = SR_UNIT_AMPERE; | |
366 | if (meas_char == 1) | |
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; | |
372 | break; | |
373 | case 3: | |
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; | |
380 | } | |
381 | break; | |
382 | case 12: | |
383 | devc->mq = SR_MQ_TEMPERATURE; | |
384 | devc->unit = SR_UNIT_CELSIUS; | |
385 | break; | |
386 | case 13: | |
387 | devc->mq = SR_MQ_TEMPERATURE; | |
388 | devc->unit = SR_UNIT_FAHRENHEIT; | |
389 | break; | |
390 | default: | |
391 | sr_dbg("unknown unit: %d", meas_unit); | |
392 | } | |
3f5cf2a0 | 393 | if (devc->mq == 0 && devc->unit == 0) |
11fb7110 BV |
394 | return; |
395 | ||
396 | /* If we got here, we know how to interpret the measurement. */ | |
397 | devc->meas_type = meas_type; | |
398 | if (meas_type == 11) | |
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; | |
404 | ||
405 | } | |
406 | ||
407 | static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens) | |
408 | { | |
409 | struct dev_context *devc; | |
410 | struct sr_datafeed_packet packet; | |
3f5cf2a0 UH |
411 | struct sr_datafeed_analog analog; |
412 | struct sr_analog_encoding encoding; | |
413 | struct sr_analog_meaning meaning; | |
414 | struct sr_analog_spec spec; | |
11fb7110 | 415 | float fvalue; |
d2e3ce76 | 416 | int digits; |
11fb7110 | 417 | |
d2e3ce76 | 418 | digits = 2; |
11fb7110 BV |
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. */ | |
423 | fvalue = NAN; | |
424 | } else { | |
357e341d | 425 | if (sr_atof_ascii(tokens[0], &fvalue) != SR_OK || fvalue == 0.0) { |
11fb7110 BV |
426 | sr_err("Invalid float '%s'.", tokens[0]); |
427 | return; | |
428 | } | |
d2e3ce76 | 429 | digits = count_digits(tokens[0]); |
11fb7110 BV |
430 | } |
431 | ||
432 | devc = sdi->priv; | |
3f5cf2a0 | 433 | if (devc->mq == 0 || devc->unit == 0) |
11fb7110 BV |
434 | /* Don't have valid metadata yet. */ |
435 | return; | |
436 | ||
11fb7110 BV |
437 | if (devc->mq == SR_MQ_RESISTANCE && isnan(fvalue)) |
438 | fvalue = INFINITY; | |
439 | else if (devc->mq == SR_MQ_CONTINUITY) { | |
440 | if (isnan(fvalue)) | |
441 | fvalue = 0.0; | |
442 | else | |
443 | fvalue = 1.0; | |
444 | } | |
445 | ||
d2e3ce76 | 446 | sr_analog_init(&analog, &encoding, &meaning, &spec, digits); |
3f5cf2a0 | 447 | analog.meaning->channels = sdi->channels; |
11fb7110 BV |
448 | analog.num_samples = 1; |
449 | analog.data = &fvalue; | |
3f5cf2a0 UH |
450 | analog.meaning->mq = devc->mq; |
451 | analog.meaning->unit = devc->unit; | |
452 | analog.meaning->mqflags = 0; | |
453 | packet.type = SR_DF_ANALOG; | |
11fb7110 | 454 | packet.payload = &analog; |
695dc859 | 455 | sr_session_send(sdi, &packet); |
11fb7110 | 456 | |
6aacf011 | 457 | sr_sw_limits_update_samples_read(&devc->limits, 1); |
11fb7110 BV |
458 | } |
459 | ||
d38d2ef0 BV |
460 | static void handle_line(const struct sr_dev_inst *sdi) |
461 | { | |
462 | struct dev_context *devc; | |
919681f0 | 463 | struct sr_serial_dev_inst *serial; |
11fb7110 BV |
464 | int num_tokens, n, i; |
465 | char cmd[16], **tokens; | |
ebf6aa30 | 466 | int ret; |
d38d2ef0 BV |
467 | |
468 | devc = sdi->priv; | |
919681f0 | 469 | serial = sdi->conn; |
31d84da3 | 470 | sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen); |
d38d2ef0 BV |
471 | |
472 | if (devc->buflen == 1) { | |
473 | if (devc->buf[0] != '0') { | |
474 | /* Not just a CMD_ACK from the query command. */ | |
31d84da3 | 475 | sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]); |
d38d2ef0 BV |
476 | devc->expect_response = FALSE; |
477 | } | |
478 | devc->buflen = 0; | |
479 | return; | |
480 | } | |
481 | ||
d38d2ef0 | 482 | tokens = g_strsplit(devc->buf, ",", 0); |
4cea0ff7 | 483 | if (tokens[0]) { |
ebf6aa30 GS |
484 | switch (devc->profile->model) { |
485 | case FLUKE_87: | |
486 | case FLUKE_89: | |
487 | case FLUKE_187: | |
488 | case FLUKE_189: | |
fe31f8b9 | 489 | devc->expect_response = FALSE; |
19ab8e36 | 490 | handle_qm_18x(sdi, tokens); |
ebf6aa30 | 491 | break; |
ebf6aa30 | 492 | case FLUKE_190: |
11fb7110 | 493 | devc->expect_response = FALSE; |
ebf6aa30 GS |
494 | num_tokens = g_strv_length(tokens); |
495 | if (num_tokens < 7) { | |
11fb7110 BV |
496 | /* Response to QM <n> measurement request. */ |
497 | handle_qm_19x_data(sdi, tokens); | |
ebf6aa30 | 498 | break; |
11fb7110 | 499 | } |
ebf6aa30 GS |
500 | /* |
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. | |
505 | */ | |
506 | devc->meas_type = 0; | |
507 | for (i = 0; i < num_tokens; i += 7) | |
508 | handle_qm_19x_meta(sdi, tokens + i); | |
509 | if (devc->meas_type) { | |
510 | /* | |
511 | * Slip the request in now, before the main | |
512 | * timer loop asks for metadata again. | |
513 | */ | |
514 | n = sprintf(cmd, "QM %d\r", devc->meas_type); | |
515 | ret = serial_write_blocking(serial, | |
516 | cmd, n, SERIAL_WRITE_TIMEOUT_MS); | |
517 | if (ret < 0) | |
518 | sr_err("Cannot send QM (measurement)."); | |
519 | } | |
520 | break; | |
0d68d6ba GS |
521 | case FLUKE_287: |
522 | case FLUKE_289: | |
523 | devc->expect_response = FALSE; | |
524 | handle_qm_28x(sdi, tokens); | |
525 | break; | |
d38d2ef0 | 526 | } |
d38d2ef0 BV |
527 | } |
528 | g_strfreev(tokens); | |
529 | devc->buflen = 0; | |
d38d2ef0 BV |
530 | } |
531 | ||
532 | SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data) | |
533 | { | |
642e9d62 | 534 | struct sr_dev_inst *sdi; |
d38d2ef0 | 535 | struct dev_context *devc; |
919681f0 | 536 | struct sr_serial_dev_inst *serial; |
d38d2ef0 BV |
537 | int len; |
538 | int64_t now, elapsed; | |
539 | ||
58d03f03 BV |
540 | (void)fd; |
541 | ||
d38d2ef0 BV |
542 | if (!(sdi = cb_data)) |
543 | return TRUE; | |
544 | ||
545 | if (!(devc = sdi->priv)) | |
546 | return TRUE; | |
547 | ||
919681f0 | 548 | serial = sdi->conn; |
d38d2ef0 BV |
549 | if (revents == G_IO_IN) { |
550 | /* Serial data arrived. */ | |
0c5f2abc | 551 | while (FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) { |
707fa85a | 552 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1); |
d38d2ef0 BV |
553 | if (len < 1) |
554 | break; | |
555 | devc->buflen++; | |
556 | *(devc->buf + devc->buflen) = '\0'; | |
557 | if (*(devc->buf + devc->buflen - 1) == '\r') { | |
558 | *(devc->buf + --devc->buflen) = '\0'; | |
559 | handle_line(sdi); | |
560 | break; | |
561 | } | |
562 | } | |
563 | } | |
564 | ||
6aacf011 | 565 | if (sr_sw_limits_check(&devc->limits)) { |
d2f7c417 | 566 | sr_dev_acquisition_stop(sdi); |
d38d2ef0 BV |
567 | return TRUE; |
568 | } | |
569 | ||
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 | |
d4b11de0 BV |
573 | * has elapsed. This will make it easier to recover from any |
574 | * out-of-sync or temporary disconnect issues. */ | |
d38d2ef0 | 575 | if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period) |
d4b11de0 | 576 | || elapsed > devc->profile->timeout) { |
20401400 | 577 | if (serial_write_blocking(serial, "QM\r", 3, SERIAL_WRITE_TIMEOUT_MS) < 0) |
bb27c765 | 578 | sr_err("Unable to send QM."); |
d38d2ef0 BV |
579 | devc->cmd_sent_at = now; |
580 | devc->expect_response = TRUE; | |
581 | } | |
582 | ||
583 | return TRUE; | |
584 | } |