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