]>
Commit | Line | Data |
---|---|---|
ac913e5c | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
ac913e5c | 3 | * |
71f1302b | 4 | * Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de> |
ac913e5c UH |
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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
e83437ae MH |
21 | /** |
22 | * @file | |
23 | * | |
ac913e5c UH |
24 | * Metex 14-bytes ASCII protocol parser. |
25 | * | |
e83437ae | 26 | * @internal |
ac913e5c UH |
27 | * This should work for various multimeters which use this kind of protocol, |
28 | * even though there is some variation in which modes each DMM supports. | |
29 | * | |
30 | * It does _not_ work for all Metex DMMs, some use a quite different protocol. | |
31 | */ | |
32 | ||
6ec6c43b | 33 | #include <config.h> |
ac913e5c | 34 | #include <string.h> |
ba464a12 | 35 | #include <strings.h> |
ac913e5c UH |
36 | #include <ctype.h> |
37 | #include <math.h> | |
38 | #include <glib.h> | |
c1aae900 | 39 | #include <libsigrok/libsigrok.h> |
ac913e5c UH |
40 | #include "libsigrok-internal.h" |
41 | ||
3544f848 | 42 | #define LOG_PREFIX "metex14" |
ac913e5c | 43 | |
e83437ae MH |
44 | /** Parse value from buf, byte 2-8. */ |
45 | static int parse_value(const uint8_t *buf, struct metex14_info *info, | |
e677490e | 46 | float *result, int *exponent) |
ac913e5c | 47 | { |
e677490e | 48 | int i, is_ol, cnt, dot_pos; |
1a807c13 UH |
49 | char valstr[7 + 1]; |
50 | ||
51 | /* Strip all spaces from bytes 2-8. */ | |
52 | memset(&valstr, 0, 7 + 1); | |
53 | for (i = 0, cnt = 0; i < 7; i++) { | |
54 | if (buf[2 + i] != ' ') | |
55 | valstr[cnt++] = buf[2 + i]; | |
ac913e5c UH |
56 | } |
57 | ||
58 | /* Bytes 5-7: Over limit (various forms) */ | |
59 | is_ol = 0; | |
34577da6 UH |
60 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, ".OL")) ? 1 : 0; |
61 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "O.L")) ? 1 : 0; | |
62 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "OL.")) ? 1 : 0; | |
63 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "OL")) ? 1 : 0; | |
64 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-.OL")) ? 1 : 0; | |
65 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-O.L")) ? 1 : 0; | |
66 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-OL.")) ? 1 : 0; | |
67 | is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-OL")) ? 1 : 0; | |
ac913e5c UH |
68 | if (is_ol != 0) { |
69 | sr_spew("Over limit."); | |
70 | *result = INFINITY; | |
71 | return SR_OK; | |
72 | } | |
73 | ||
e83437ae MH |
74 | /* Logic functions */ |
75 | if (!strcmp((const char *)&valstr, "READY") || | |
76 | !strcmp((const char *)&valstr, "FLOAT")) { | |
77 | *result = INFINITY; | |
78 | info->is_logic = TRUE; | |
79 | } else if (!strcmp((const char *)&valstr, "Hi")) { | |
80 | *result = 1.0; | |
81 | info->is_logic = TRUE; | |
82 | } else if (!strcmp((const char *)&valstr, "Lo")) { | |
83 | *result = 0.0; | |
84 | info->is_logic = TRUE; | |
85 | } | |
86 | if (info->is_logic) | |
87 | return SR_OK; | |
88 | ||
1a807c13 UH |
89 | /* Bytes 2-8: Sign, value (up to 5 digits) and decimal point */ |
90 | sscanf((const char *)&valstr, "%f", result); | |
ac913e5c | 91 | |
e677490e AJ |
92 | dot_pos = strcspn(valstr, "."); |
93 | if (dot_pos < cnt) | |
94 | *exponent = -(cnt - dot_pos - 1); | |
95 | else | |
96 | *exponent = 0; | |
97 | ||
1a807c13 | 98 | sr_spew("The display value is %f.", *result); |
ac913e5c UH |
99 | |
100 | return SR_OK; | |
101 | } | |
102 | ||
103 | static void parse_flags(const char *buf, struct metex14_info *info) | |
104 | { | |
71f1302b UH |
105 | int i, cnt; |
106 | char unit[4 + 1]; | |
107 | const char *u; | |
108 | ||
e83437ae MH |
109 | /* Bytes 0-1: Measurement mode AC, DC */ |
110 | info->is_ac = !strncmp(buf, "AC", 2); | |
111 | info->is_dc = !strncmp(buf, "DC", 2); | |
ac913e5c | 112 | |
1a807c13 | 113 | /* Bytes 2-8: See parse_value(). */ |
ac913e5c | 114 | |
1a807c13 | 115 | /* Strip all spaces from bytes 9-12. */ |
71f1302b UH |
116 | memset(&unit, 0, 4 + 1); |
117 | for (i = 0, cnt = 0; i < 4; i++) { | |
118 | if (buf[9 + i] != ' ') | |
119 | unit[cnt++] = buf[9 + i]; | |
120 | } | |
1a807c13 UH |
121 | |
122 | /* Bytes 9-12: Unit */ | |
71f1302b | 123 | u = (const char *)&unit; |
34577da6 | 124 | if (!g_ascii_strcasecmp(u, "A")) |
ac913e5c | 125 | info->is_ampere = TRUE; |
34577da6 | 126 | else if (!g_ascii_strcasecmp(u, "mA")) |
ac913e5c | 127 | info->is_milli = info->is_ampere = TRUE; |
34577da6 | 128 | else if (!g_ascii_strcasecmp(u, "uA")) |
2477fb95 | 129 | info->is_micro = info->is_ampere = TRUE; |
34577da6 | 130 | else if (!g_ascii_strcasecmp(u, "V")) |
ac913e5c | 131 | info->is_volt = TRUE; |
34577da6 | 132 | else if (!g_ascii_strcasecmp(u, "mV")) |
ac913e5c | 133 | info->is_milli = info->is_volt = TRUE; |
34577da6 | 134 | else if (!g_ascii_strcasecmp(u, "Ohm")) |
ac913e5c | 135 | info->is_ohm = TRUE; |
34577da6 | 136 | else if (!g_ascii_strcasecmp(u, "KOhm")) |
ac913e5c | 137 | info->is_kilo = info->is_ohm = TRUE; |
34577da6 | 138 | else if (!g_ascii_strcasecmp(u, "MOhm")) |
ac913e5c | 139 | info->is_mega = info->is_ohm = TRUE; |
34577da6 | 140 | else if (!g_ascii_strcasecmp(u, "pF")) |
c02dc3e2 | 141 | info->is_pico = info->is_farad = TRUE; |
34577da6 | 142 | else if (!g_ascii_strcasecmp(u, "nF")) |
ac913e5c | 143 | info->is_nano = info->is_farad = TRUE; |
34577da6 | 144 | else if (!g_ascii_strcasecmp(u, "uF")) |
ac913e5c | 145 | info->is_micro = info->is_farad = TRUE; |
34577da6 | 146 | else if (!g_ascii_strcasecmp(u, "KHz")) |
ac913e5c | 147 | info->is_kilo = info->is_hertz = TRUE; |
34577da6 | 148 | else if (!g_ascii_strcasecmp(u, "C")) |
ac913e5c | 149 | info->is_celsius = TRUE; |
34577da6 | 150 | else if (!g_ascii_strcasecmp(u, "DB")) |
9871215c | 151 | info->is_decibel = TRUE; |
34577da6 | 152 | else if (!g_ascii_strcasecmp(u, "")) |
9871215c | 153 | info->is_unitless = TRUE; |
ac913e5c | 154 | |
e83437ae MH |
155 | /* Bytes 0-1: Measurement mode, except AC/DC */ |
156 | info->is_resistance = !strncmp(buf, "OH", 2) || | |
157 | (!strncmp(buf, " ", 2) && info->is_ohm); | |
158 | info->is_capacity = !strncmp(buf, "CA", 2) || | |
159 | (!strncmp(buf, " ", 2) && info->is_farad); | |
160 | info->is_temperature = !strncmp(buf, "TE", 2); | |
161 | info->is_diode = !strncmp(buf, "DI", 2) || | |
162 | (!strncmp(buf, " ", 2) && info->is_volt && info->is_milli); | |
163 | info->is_frequency = !strncmp(buf, "FR", 2) || | |
164 | (!strncmp(buf, " ", 2) && info->is_hertz); | |
165 | info->is_gain = !strncmp(buf, "DB", 2); | |
166 | info->is_hfe = !strncmp(buf, "HF", 2) || | |
167 | (!strncmp(buf, " ", 2) && !info->is_volt && !info->is_ohm && | |
168 | !info->is_logic && !info->is_farad && !info->is_hertz); | |
169 | /* | |
170 | * Note: | |
171 | * - Protocol doesn't distinguish "resistance" from "beep" mode. | |
172 | * - "DB" shows the logarithmic ratio of input voltage to a | |
173 | * pre-stored (user-changeable) value in the DMM. | |
174 | */ | |
175 | ||
ac913e5c UH |
176 | /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */ |
177 | } | |
178 | ||
b02bb45f | 179 | static void handle_flags(struct sr_datafeed_analog *analog, float *floatval, |
e677490e | 180 | int *exponent, const struct metex14_info *info) |
ac913e5c | 181 | { |
e677490e | 182 | int factor = 0; |
ac913e5c | 183 | /* Factors */ |
c02dc3e2 | 184 | if (info->is_pico) |
e677490e | 185 | factor -= 12; |
ac913e5c | 186 | if (info->is_nano) |
e677490e | 187 | factor -= 9; |
ac913e5c | 188 | if (info->is_micro) |
e677490e | 189 | factor -= 6; |
ac913e5c | 190 | if (info->is_milli) |
e677490e | 191 | factor -= 3; |
ac913e5c | 192 | if (info->is_kilo) |
e677490e | 193 | factor += 3; |
ac913e5c | 194 | if (info->is_mega) |
e677490e AJ |
195 | factor += 6; |
196 | *floatval *= powf(10, factor); | |
197 | *exponent += factor; | |
ac913e5c UH |
198 | |
199 | /* Measurement modes */ | |
200 | if (info->is_volt) { | |
b02bb45f UH |
201 | analog->meaning->mq = SR_MQ_VOLTAGE; |
202 | analog->meaning->unit = SR_UNIT_VOLT; | |
ac913e5c UH |
203 | } |
204 | if (info->is_ampere) { | |
b02bb45f UH |
205 | analog->meaning->mq = SR_MQ_CURRENT; |
206 | analog->meaning->unit = SR_UNIT_AMPERE; | |
ac913e5c UH |
207 | } |
208 | if (info->is_ohm) { | |
b02bb45f UH |
209 | analog->meaning->mq = SR_MQ_RESISTANCE; |
210 | analog->meaning->unit = SR_UNIT_OHM; | |
ac913e5c UH |
211 | } |
212 | if (info->is_hertz) { | |
b02bb45f UH |
213 | analog->meaning->mq = SR_MQ_FREQUENCY; |
214 | analog->meaning->unit = SR_UNIT_HERTZ; | |
ac913e5c UH |
215 | } |
216 | if (info->is_farad) { | |
b02bb45f UH |
217 | analog->meaning->mq = SR_MQ_CAPACITANCE; |
218 | analog->meaning->unit = SR_UNIT_FARAD; | |
ac913e5c UH |
219 | } |
220 | if (info->is_celsius) { | |
b02bb45f UH |
221 | analog->meaning->mq = SR_MQ_TEMPERATURE; |
222 | analog->meaning->unit = SR_UNIT_CELSIUS; | |
ac913e5c UH |
223 | } |
224 | if (info->is_diode) { | |
b02bb45f UH |
225 | analog->meaning->mq = SR_MQ_VOLTAGE; |
226 | analog->meaning->unit = SR_UNIT_VOLT; | |
ac913e5c | 227 | } |
9871215c | 228 | if (info->is_gain) { |
b02bb45f UH |
229 | analog->meaning->mq = SR_MQ_GAIN; |
230 | analog->meaning->unit = SR_UNIT_DECIBEL_VOLT; | |
9871215c UH |
231 | } |
232 | if (info->is_hfe) { | |
b02bb45f UH |
233 | analog->meaning->mq = SR_MQ_GAIN; |
234 | analog->meaning->unit = SR_UNIT_UNITLESS; | |
9871215c | 235 | } |
e83437ae | 236 | if (info->is_logic) { |
b02bb45f UH |
237 | analog->meaning->mq = SR_MQ_GAIN; |
238 | analog->meaning->unit = SR_UNIT_UNITLESS; | |
e83437ae | 239 | } |
ac913e5c UH |
240 | |
241 | /* Measurement related flags */ | |
242 | if (info->is_ac) | |
b02bb45f | 243 | analog->meaning->mqflags |= SR_MQFLAG_AC; |
ac913e5c | 244 | if (info->is_dc) |
b02bb45f | 245 | analog->meaning->mqflags |= SR_MQFLAG_DC; |
a6ed50f4 | 246 | if (info->is_diode) |
b02bb45f | 247 | analog->meaning->mqflags |= SR_MQFLAG_DIODE; |
ac913e5c UH |
248 | } |
249 | ||
250 | static gboolean flags_valid(const struct metex14_info *info) | |
251 | { | |
252 | int count; | |
253 | ||
254 | /* Does the packet have more than one multiplier? */ | |
255 | count = 0; | |
c02dc3e2 | 256 | count += (info->is_pico) ? 1 : 0; |
ac913e5c UH |
257 | count += (info->is_nano) ? 1 : 0; |
258 | count += (info->is_micro) ? 1 : 0; | |
259 | count += (info->is_milli) ? 1 : 0; | |
260 | count += (info->is_kilo) ? 1 : 0; | |
261 | count += (info->is_mega) ? 1 : 0; | |
262 | if (count > 1) { | |
ec5186f9 | 263 | sr_dbg("More than one multiplier detected in packet."); |
ac913e5c UH |
264 | return FALSE; |
265 | } | |
266 | ||
267 | /* Does the packet "measure" more than one type of value? */ | |
268 | count = 0; | |
269 | count += (info->is_ac) ? 1 : 0; | |
270 | count += (info->is_dc) ? 1 : 0; | |
271 | count += (info->is_resistance) ? 1 : 0; | |
272 | count += (info->is_capacity) ? 1 : 0; | |
273 | count += (info->is_temperature) ? 1 : 0; | |
274 | count += (info->is_diode) ? 1 : 0; | |
275 | count += (info->is_frequency) ? 1 : 0; | |
276 | if (count > 1) { | |
ec5186f9 | 277 | sr_dbg("More than one measurement type detected in packet."); |
ac913e5c UH |
278 | return FALSE; |
279 | } | |
280 | ||
281 | /* Both AC and DC set? */ | |
282 | if (info->is_ac && info->is_dc) { | |
ec5186f9 | 283 | sr_dbg("Both AC and DC flags detected in packet."); |
ac913e5c UH |
284 | return FALSE; |
285 | } | |
286 | ||
287 | return TRUE; | |
288 | } | |
289 | ||
c4f2dfd0 | 290 | #ifdef HAVE_LIBSERIALPORT |
e90cf076 UH |
291 | SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial) |
292 | { | |
293 | const uint8_t wbuf = 'D'; | |
294 | ||
295 | sr_spew("Requesting DMM packet."); | |
296 | ||
4277ac34 | 297 | return (serial_write_nonblocking(serial, &wbuf, 1) == 1) ? SR_OK : SR_ERR; |
e90cf076 | 298 | } |
c4f2dfd0 | 299 | #endif |
e90cf076 | 300 | |
ac913e5c UH |
301 | SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf) |
302 | { | |
303 | struct metex14_info info; | |
304 | ||
305 | memset(&info, 0x00, sizeof(struct metex14_info)); | |
306 | parse_flags((const char *)buf, &info); | |
307 | ||
308 | if (!flags_valid(&info)) | |
309 | return FALSE; | |
310 | ||
311 | if (buf[13] != '\r') | |
312 | return FALSE; | |
313 | ||
314 | return TRUE; | |
315 | } | |
316 | ||
317 | /** | |
318 | * Parse a protocol packet. | |
319 | * | |
1fbab466 | 320 | * @param buf Buffer containing the protocol packet. Must not be NULL. |
ac913e5c | 321 | * @param floatval Pointer to a float variable. That variable will be modified |
1fbab466 | 322 | * in-place depending on the protocol packet. Must not be NULL. |
b02bb45f | 323 | * @param analog Pointer to a struct sr_datafeed_analog. The struct will be |
ac913e5c | 324 | * filled with data according to the protocol packet. |
1fbab466 UH |
325 | * Must not be NULL. |
326 | * @param info Pointer to a struct metex14_info. The struct will be filled | |
327 | * with data according to the protocol packet. Must not be NULL. | |
ac913e5c UH |
328 | * |
329 | * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the | |
330 | * 'analog' variable contents are undefined and should not be used. | |
331 | */ | |
332 | SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval, | |
b02bb45f | 333 | struct sr_datafeed_analog *analog, void *info) |
ac913e5c | 334 | { |
e677490e | 335 | int ret, exponent = 0; |
1fbab466 UH |
336 | struct metex14_info *info_local; |
337 | ||
338 | info_local = (struct metex14_info *)info; | |
ac913e5c | 339 | |
e82d7dbc AG |
340 | /* Don't print byte 13. That one contains the carriage return. */ |
341 | sr_dbg("DMM packet: \"%.13s\"", buf); | |
342 | ||
e83437ae MH |
343 | memset(info_local, 0x00, sizeof(struct metex14_info)); |
344 | ||
e677490e | 345 | if ((ret = parse_value(buf, info_local, floatval, &exponent)) != SR_OK) { |
ec5186f9 | 346 | sr_dbg("Error parsing value: %d.", ret); |
ac913e5c UH |
347 | return ret; |
348 | } | |
349 | ||
1fbab466 | 350 | parse_flags((const char *)buf, info_local); |
e677490e AJ |
351 | handle_flags(analog, floatval, &exponent, info_local); |
352 | ||
353 | analog->encoding->digits = -exponent; | |
354 | analog->spec->spec_digits = -exponent; | |
ac913e5c UH |
355 | |
356 | return SR_OK; | |
357 | } |