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