]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de> | |
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 2 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 | ||
20 | /** | |
21 | * @file | |
22 | * | |
23 | * Metex 14-bytes ASCII protocol parser. | |
24 | * | |
25 | * This should work for various multimeters which use this kind of protocol, | |
26 | * even though there is some variation in which modes each DMM supports. | |
27 | * | |
28 | * It does _not_ work for all Metex DMMs, some use a quite different protocol. | |
29 | */ | |
30 | ||
31 | #include <config.h> | |
32 | #include <string.h> | |
33 | #include <strings.h> | |
34 | #include <ctype.h> | |
35 | #include <math.h> | |
36 | #include <glib.h> | |
37 | #include <libsigrok/libsigrok.h> | |
38 | #include "libsigrok-internal.h" | |
39 | ||
40 | #define LOG_PREFIX "metex14" | |
41 | ||
42 | /** Parse value from buf, byte 2-8. */ | |
43 | static int parse_value(const uint8_t *buf, struct metex14_info *info, | |
44 | float *result, int *exponent) | |
45 | { | |
46 | int i, is_ol, cnt, dot_pos; | |
47 | char valstr[7 + 1]; | |
48 | ||
49 | /* Strip all spaces from bytes 2-8. */ | |
50 | memset(&valstr, 0, 7 + 1); | |
51 | for (i = 0, cnt = 0; i < 7; i++) { | |
52 | if (buf[2 + i] != ' ') | |
53 | valstr[cnt++] = buf[2 + i]; | |
54 | } | |
55 | ||
56 | /* Bytes 5-7: Over limit (various forms) */ | |
57 | is_ol = 0; | |
58 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, ".OL")) ? 1 : 0; | |
59 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "O.L")) ? 1 : 0; | |
60 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "OL.")) ? 1 : 0; | |
61 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "OL")) ? 1 : 0; | |
62 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-.OL")) ? 1 : 0; | |
63 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-O.L")) ? 1 : 0; | |
64 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-OL.")) ? 1 : 0; | |
65 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-OL")) ? 1 : 0; | |
66 | if (is_ol != 0) { | |
67 | sr_spew("Over limit."); | |
68 | *result = INFINITY; | |
69 | return SR_OK; | |
70 | } | |
71 | ||
72 | /* Logic functions */ | |
73 | if (!strcmp((const char *)&valstr, "READY") || | |
74 | !strcmp((const char *)&valstr, "FLOAT")) { | |
75 | *result = INFINITY; | |
76 | info->is_logic = TRUE; | |
77 | } else if (!strcmp((const char *)&valstr, "Hi")) { | |
78 | *result = 1.0; | |
79 | info->is_logic = TRUE; | |
80 | } else if (!strcmp((const char *)&valstr, "Lo")) { | |
81 | *result = 0.0; | |
82 | info->is_logic = TRUE; | |
83 | } | |
84 | if (info->is_logic) | |
85 | return SR_OK; | |
86 | ||
87 | /* Bytes 2-8: Sign, value (up to 5 digits) and decimal point */ | |
88 | sr_atof_ascii((const char *)&valstr, result); | |
89 | ||
90 | dot_pos = strcspn(valstr, "."); | |
91 | if (dot_pos < cnt) | |
92 | *exponent = -(cnt - dot_pos - 1); | |
93 | else | |
94 | *exponent = 0; | |
95 | ||
96 | sr_spew("The display value is %f.", *result); | |
97 | ||
98 | return SR_OK; | |
99 | } | |
100 | ||
101 | static void parse_flags(const char *buf, struct metex14_info *info) | |
102 | { | |
103 | int i, cnt; | |
104 | char unit[4 + 1]; | |
105 | const char *u; | |
106 | ||
107 | /* Bytes 0-1: Measurement mode AC, DC */ | |
108 | info->is_ac = !strncmp(buf, "AC", 2); | |
109 | info->is_dc = !strncmp(buf, "DC", 2); | |
110 | ||
111 | /* Bytes 2-8: See parse_value(). */ | |
112 | ||
113 | /* Strip all spaces from bytes 9-12. */ | |
114 | memset(&unit, 0, 4 + 1); | |
115 | for (i = 0, cnt = 0; i < 4; i++) { | |
116 | if (buf[9 + i] != ' ') | |
117 | unit[cnt++] = buf[9 + i]; | |
118 | } | |
119 | ||
120 | /* Bytes 9-12: Unit */ | |
121 | u = (const char *)&unit; | |
122 | if (!g_ascii_strcasecmp(u, "A")) | |
123 | info->is_ampere = TRUE; | |
124 | else if (!g_ascii_strcasecmp(u, "mA")) | |
125 | info->is_milli = info->is_ampere = TRUE; | |
126 | else if (!g_ascii_strcasecmp(u, "uA")) | |
127 | info->is_micro = info->is_ampere = TRUE; | |
128 | else if (!g_ascii_strcasecmp(u, "V")) | |
129 | info->is_volt = TRUE; | |
130 | else if (!g_ascii_strcasecmp(u, "mV")) | |
131 | info->is_milli = info->is_volt = TRUE; | |
132 | else if (!g_ascii_strcasecmp(u, "Ohm")) | |
133 | info->is_ohm = TRUE; | |
134 | else if (!g_ascii_strcasecmp(u, "KOhm")) | |
135 | info->is_kilo = info->is_ohm = TRUE; | |
136 | else if (!g_ascii_strcasecmp(u, "MOhm")) | |
137 | info->is_mega = info->is_ohm = TRUE; | |
138 | else if (!g_ascii_strcasecmp(u, "pF")) | |
139 | info->is_pico = info->is_farad = TRUE; | |
140 | else if (!g_ascii_strcasecmp(u, "nF")) | |
141 | info->is_nano = info->is_farad = TRUE; | |
142 | else if (!g_ascii_strcasecmp(u, "uF")) | |
143 | info->is_micro = info->is_farad = TRUE; | |
144 | else if (!g_ascii_strcasecmp(u, "KHz")) | |
145 | info->is_kilo = info->is_hertz = TRUE; | |
146 | else if (!g_ascii_strcasecmp(u, "C")) | |
147 | info->is_celsius = TRUE; | |
148 | else if (!g_ascii_strcasecmp(u, "F")) | |
149 | info->is_fahrenheit = TRUE; | |
150 | else if (!g_ascii_strcasecmp(u, "DB")) | |
151 | info->is_decibel = TRUE; | |
152 | else if (!g_ascii_strcasecmp(u, "dBm")) | |
153 | info->is_decibel_mw = TRUE; | |
154 | else if (!g_ascii_strcasecmp(u, "W")) | |
155 | info->is_watt = TRUE; | |
156 | else if (!g_ascii_strcasecmp(u, "")) | |
157 | info->is_unitless = TRUE; | |
158 | ||
159 | /* Bytes 0-1: Measurement mode, except AC/DC */ | |
160 | info->is_resistance = !strncmp(buf, "OH", 2) || | |
161 | (!strncmp(buf, " ", 2) && info->is_ohm); | |
162 | info->is_capacity = !strncmp(buf, "CA", 2) || | |
163 | (!strncmp(buf, " ", 2) && info->is_farad); | |
164 | info->is_temperature = !strncmp(buf, "TE", 2) || | |
165 | info->is_celsius || info->is_fahrenheit; | |
166 | info->is_diode = !strncmp(buf, "DI", 2) || | |
167 | (!strncmp(buf, " ", 2) && info->is_volt && info->is_milli); | |
168 | info->is_frequency = !strncmp(buf, "FR", 2) || | |
169 | (!strncmp(buf, " ", 2) && info->is_hertz); | |
170 | info->is_gain = !strncmp(buf, "DB", 2) && info->is_decibel; | |
171 | info->is_power = (!strncmp(buf, "dB", 2) && info->is_decibel_mw) || | |
172 | ((!strncmp(buf, "WT", 2) && info->is_watt)); | |
173 | info->is_power_factor = !strncmp(buf, "CO", 2) && info->is_unitless; | |
174 | info->is_hfe = !strncmp(buf, "HF", 2) || | |
175 | (!strncmp(buf, " ", 2) && !info->is_ampere &&!info->is_volt && | |
176 | !info->is_resistance && !info->is_capacity && !info->is_frequency && | |
177 | !info->is_temperature && !info->is_power && !info->is_power_factor && | |
178 | !info->is_gain && !info->is_logic && !info->is_diode); | |
179 | info->is_min = !strncmp(buf, "MN", 2); | |
180 | info->is_max = !strncmp(buf, "MX", 2); | |
181 | info->is_avg = !strncmp(buf, "AG", 2); | |
182 | ||
183 | /* | |
184 | * Note: | |
185 | * - Protocol doesn't distinguish "resistance" from "beep" mode. | |
186 | * - "DB" shows the logarithmic ratio of input voltage to a | |
187 | * pre-stored (user-changeable) value in the DMM. | |
188 | */ | |
189 | ||
190 | /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */ | |
191 | } | |
192 | ||
193 | static void handle_flags(struct sr_datafeed_analog *analog, float *floatval, | |
194 | int *exponent, const struct metex14_info *info) | |
195 | { | |
196 | int factor; | |
197 | ||
198 | (void)exponent; | |
199 | ||
200 | /* Factors */ | |
201 | factor = 0; | |
202 | if (info->is_pico) | |
203 | factor -= 12; | |
204 | if (info->is_nano) | |
205 | factor -= 9; | |
206 | if (info->is_micro) | |
207 | factor -= 6; | |
208 | if (info->is_milli) | |
209 | factor -= 3; | |
210 | if (info->is_kilo) | |
211 | factor += 3; | |
212 | if (info->is_mega) | |
213 | factor += 6; | |
214 | *floatval *= powf(10, factor); | |
215 | ||
216 | /* Measurement modes */ | |
217 | if (info->is_volt) { | |
218 | analog->meaning->mq = SR_MQ_VOLTAGE; | |
219 | analog->meaning->unit = SR_UNIT_VOLT; | |
220 | } | |
221 | if (info->is_ampere) { | |
222 | analog->meaning->mq = SR_MQ_CURRENT; | |
223 | analog->meaning->unit = SR_UNIT_AMPERE; | |
224 | } | |
225 | if (info->is_ohm) { | |
226 | analog->meaning->mq = SR_MQ_RESISTANCE; | |
227 | analog->meaning->unit = SR_UNIT_OHM; | |
228 | } | |
229 | if (info->is_hertz) { | |
230 | analog->meaning->mq = SR_MQ_FREQUENCY; | |
231 | analog->meaning->unit = SR_UNIT_HERTZ; | |
232 | } | |
233 | if (info->is_farad) { | |
234 | analog->meaning->mq = SR_MQ_CAPACITANCE; | |
235 | analog->meaning->unit = SR_UNIT_FARAD; | |
236 | } | |
237 | if (info->is_temperature) { | |
238 | analog->meaning->mq = SR_MQ_TEMPERATURE; | |
239 | if (info->is_celsius) | |
240 | analog->meaning->unit = SR_UNIT_CELSIUS; | |
241 | else if (info->is_fahrenheit) | |
242 | analog->meaning->unit = SR_UNIT_FAHRENHEIT; | |
243 | else | |
244 | analog->meaning->unit = SR_UNIT_UNITLESS; | |
245 | } | |
246 | if (info->is_diode) { | |
247 | analog->meaning->mq = SR_MQ_VOLTAGE; | |
248 | analog->meaning->unit = SR_UNIT_VOLT; | |
249 | } | |
250 | if (info->is_power) { | |
251 | analog->meaning->mq = SR_MQ_POWER; | |
252 | if (info->is_decibel_mw) | |
253 | analog->meaning->unit = SR_UNIT_DECIBEL_MW; | |
254 | else if (info->is_watt) | |
255 | analog->meaning->unit = SR_UNIT_WATT; | |
256 | else | |
257 | analog->meaning->unit = SR_UNIT_UNITLESS; | |
258 | } | |
259 | if (info->is_power_factor) { | |
260 | analog->meaning->mq = SR_MQ_POWER_FACTOR; | |
261 | analog->meaning->unit = SR_UNIT_UNITLESS; | |
262 | } | |
263 | if (info->is_gain) { | |
264 | analog->meaning->mq = SR_MQ_GAIN; | |
265 | analog->meaning->unit = SR_UNIT_DECIBEL_VOLT; | |
266 | } | |
267 | if (info->is_hfe) { | |
268 | analog->meaning->mq = SR_MQ_GAIN; | |
269 | analog->meaning->unit = SR_UNIT_UNITLESS; | |
270 | } | |
271 | if (info->is_logic) { | |
272 | analog->meaning->mq = SR_MQ_GAIN; | |
273 | analog->meaning->unit = SR_UNIT_UNITLESS; | |
274 | } | |
275 | ||
276 | /* Measurement related flags */ | |
277 | if (info->is_ac) | |
278 | analog->meaning->mqflags |= SR_MQFLAG_AC; | |
279 | if (info->is_dc) | |
280 | analog->meaning->mqflags |= SR_MQFLAG_DC; | |
281 | if (info->is_diode) | |
282 | analog->meaning->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC; | |
283 | if (info->is_min) | |
284 | analog->meaning->mqflags |= SR_MQFLAG_MIN; | |
285 | if (info->is_max) | |
286 | analog->meaning->mqflags |= SR_MQFLAG_MAX; | |
287 | if (info->is_avg) | |
288 | analog->meaning->mqflags |= SR_MQFLAG_AVG; | |
289 | } | |
290 | ||
291 | static gboolean flags_valid(const struct metex14_info *info) | |
292 | { | |
293 | int count; | |
294 | ||
295 | /* Does the packet have more than one multiplier? */ | |
296 | count = 0; | |
297 | count += (info->is_pico) ? 1 : 0; | |
298 | count += (info->is_nano) ? 1 : 0; | |
299 | count += (info->is_micro) ? 1 : 0; | |
300 | count += (info->is_milli) ? 1 : 0; | |
301 | count += (info->is_kilo) ? 1 : 0; | |
302 | count += (info->is_mega) ? 1 : 0; | |
303 | if (count > 1) { | |
304 | sr_dbg("More than one multiplier detected in packet."); | |
305 | return FALSE; | |
306 | } | |
307 | ||
308 | /* Does the packet "measure" more than one type of value? */ | |
309 | count = 0; | |
310 | count += (info->is_ac) ? 1 : 0; | |
311 | count += (info->is_dc) ? 1 : 0; | |
312 | count += (info->is_resistance) ? 1 : 0; | |
313 | count += (info->is_capacity) ? 1 : 0; | |
314 | count += (info->is_temperature) ? 1 : 0; | |
315 | count += (info->is_diode) ? 1 : 0; | |
316 | count += (info->is_frequency) ? 1 : 0; | |
317 | if (count > 1) { | |
318 | sr_dbg("More than one measurement type detected in packet."); | |
319 | return FALSE; | |
320 | } | |
321 | ||
322 | /* Both AC and DC set? */ | |
323 | if (info->is_ac && info->is_dc) { | |
324 | sr_dbg("Both AC and DC flags detected in packet."); | |
325 | return FALSE; | |
326 | } | |
327 | ||
328 | return TRUE; | |
329 | } | |
330 | ||
331 | #ifdef HAVE_SERIAL_COMM | |
332 | SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial) | |
333 | { | |
334 | const uint8_t wbuf = 'D'; | |
335 | size_t wrlen; | |
336 | int ret; | |
337 | ||
338 | sr_spew("Requesting DMM packet."); | |
339 | ||
340 | wrlen = sizeof(wbuf); | |
341 | ret = serial_write_blocking(serial, &wbuf, wrlen, 0); | |
342 | if (ret < 0) | |
343 | return ret; | |
344 | if ((size_t)ret != wrlen) | |
345 | return SR_ERR_IO; | |
346 | ||
347 | return SR_OK; | |
348 | } | |
349 | #endif | |
350 | ||
351 | SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf) | |
352 | { | |
353 | struct metex14_info info; | |
354 | ||
355 | memset(&info, 0x00, sizeof(struct metex14_info)); | |
356 | parse_flags((const char *)buf, &info); | |
357 | ||
358 | if (!flags_valid(&info)) | |
359 | return FALSE; | |
360 | ||
361 | if (buf[13] != '\r') | |
362 | return FALSE; | |
363 | ||
364 | return TRUE; | |
365 | } | |
366 | ||
367 | SR_PRIV gboolean sr_metex14_4packets_valid(const uint8_t *buf) | |
368 | { | |
369 | struct metex14_info info; | |
370 | size_t ch_idx; | |
371 | const uint8_t *ch_buf; | |
372 | ||
373 | ch_buf = buf; | |
374 | for (ch_idx = 0; ch_idx < 4; ch_idx++) { | |
375 | if (ch_buf[13] != '\r') | |
376 | return FALSE; | |
377 | memset(&info, 0x00, sizeof(info)); | |
378 | parse_flags((const char *)ch_buf, &info); | |
379 | if (!flags_valid(&info)) | |
380 | return FALSE; | |
381 | ch_buf += METEX14_PACKET_SIZE; | |
382 | } | |
383 | return TRUE; | |
384 | } | |
385 | ||
386 | /** | |
387 | * Parse a protocol packet. | |
388 | * | |
389 | * @param buf Buffer containing the protocol packet. Must not be NULL. | |
390 | * @param floatval Pointer to a float variable. That variable will be modified | |
391 | * in-place depending on the protocol packet. Must not be NULL. | |
392 | * @param analog Pointer to a struct sr_datafeed_analog. The struct will be | |
393 | * filled with data according to the protocol packet. | |
394 | * Must not be NULL. | |
395 | * @param info Pointer to a struct metex14_info. The struct will be filled | |
396 | * with data according to the protocol packet. Must not be NULL. | |
397 | * | |
398 | * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the | |
399 | * 'analog' variable contents are undefined and should not be used. | |
400 | */ | |
401 | SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval, | |
402 | struct sr_datafeed_analog *analog, void *info) | |
403 | { | |
404 | int ret, exponent = 0; | |
405 | struct metex14_info *info_local; | |
406 | ||
407 | info_local = info; | |
408 | ||
409 | /* Don't print byte 13. That one contains the carriage return. */ | |
410 | sr_dbg("DMM packet: \"%.13s\"", buf); | |
411 | ||
412 | memset(info_local, 0x00, sizeof(struct metex14_info)); | |
413 | ||
414 | if ((ret = parse_value(buf, info_local, floatval, &exponent)) != SR_OK) { | |
415 | sr_dbg("Error parsing value: %d.", ret); | |
416 | return ret; | |
417 | } | |
418 | ||
419 | parse_flags((const char *)buf, info_local); | |
420 | handle_flags(analog, floatval, &exponent, info_local); | |
421 | ||
422 | analog->encoding->digits = -exponent; | |
423 | analog->spec->spec_digits = -exponent; | |
424 | ||
425 | return SR_OK; | |
426 | } | |
427 | ||
428 | /** | |
429 | * Parse one out of four values of a four-display Metex14 variant. | |
430 | * | |
431 | * The caller's 'info' parameter can be used to track the channel index, | |
432 | * as long as the information is kept across calls to the 14-byte packet | |
433 | * parse routine (which clears the 'info' container). | |
434 | * | |
435 | * Since analog values have further details in the 'analog' parameter, | |
436 | * passing multiple values per parse routine call is problematic. So we | |
437 | * prefer the approach of passing one value per call, which is most | |
438 | * reliable and shall fit every similar device with multiple displays. | |
439 | * | |
440 | * The meters which use this parse routine send one 14-byte packet per | |
441 | * display. Each packet has the regular Metex14 layout. | |
442 | */ | |
443 | SR_PRIV int sr_metex14_4packets_parse(const uint8_t *buf, float *floatval, | |
444 | struct sr_datafeed_analog *analog, void *info) | |
445 | { | |
446 | struct metex14_info *info_local; | |
447 | size_t ch_idx; | |
448 | const uint8_t *ch_buf; | |
449 | int rc; | |
450 | ||
451 | info_local = info; | |
452 | ch_idx = info_local->ch_idx; | |
453 | ch_buf = buf + ch_idx * METEX14_PACKET_SIZE; | |
454 | rc = sr_metex14_parse(ch_buf, floatval, analog, info); | |
455 | info_local->ch_idx = ch_idx + 1; | |
456 | return rc; | |
457 | } |