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