]>
Commit | Line | Data |
---|---|---|
ac913e5c UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 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 | * 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 | ||
37 | /* Message logging helpers with driver-specific prefix string. */ | |
38 | #define DRIVER_LOG_DOMAIN "metex14: " | |
39 | #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args) | |
40 | #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args) | |
41 | #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args) | |
42 | #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args) | |
43 | #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args) | |
44 | #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args) | |
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; | |
64 | is_ol += !strncmp((char *)&buf[5], ".OL", 3); | |
65 | is_ol += !strncmp((char *)&buf[5], "O.L", 3); | |
66 | is_ol += !strncmp((char *)&buf[5], "OL.", 3); | |
67 | is_ol += !strncmp((char *)&buf[5], " OL", 3); | |
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]; | |
78 | if (digit == '.') { | |
79 | decimal_point = i; | |
80 | } else if (isdigit(digit)) { | |
81 | intval += (digit - '0') * factor; | |
82 | factor /= 10; | |
83 | } else { | |
84 | sr_err("Invalid digit byte: 0x%02x.", digit); | |
85 | return SR_ERR; | |
86 | } | |
87 | } | |
88 | ||
89 | floatval = (float)intval; | |
90 | ||
91 | /* Decimal point position */ | |
92 | if (decimal_point == 0 || decimal_point == 4) { | |
93 | /* TODO: Doesn't happen? */ | |
94 | } else if (decimal_point == 1) { | |
95 | floatval /= 1000; | |
96 | } else if (decimal_point == 2) { | |
97 | floatval /= 100; | |
98 | } else if (decimal_point == 3) { | |
99 | floatval /= 10; | |
100 | } else { | |
101 | sr_err("Invalid decimal point position: %d.", decimal_point); | |
102 | return SR_ERR; | |
103 | } | |
104 | ||
105 | /* Apply sign. */ | |
106 | floatval *= sign; | |
107 | ||
108 | sr_spew("The display value is %f.", floatval); | |
109 | ||
110 | *result = floatval; | |
111 | ||
112 | return SR_OK; | |
113 | } | |
114 | ||
115 | static void parse_flags(const char *buf, struct metex14_info *info) | |
116 | { | |
117 | /* Bytes 0-1: Measurement mode */ | |
118 | /* Note: Protocol doesn't distinguish "resistance" from "beep" mode. */ | |
119 | info->is_ac = !strncmp(buf, "AC", 2); | |
120 | info->is_dc = !strncmp(buf, "DC", 2); | |
121 | info->is_resistance = !strncmp(buf, "OH", 2); | |
122 | info->is_capacity = !strncmp(buf, "CA", 2); | |
123 | info->is_temperature = !strncmp(buf, "TE", 2); | |
124 | info->is_diode = !strncmp(buf, "DI", 2); | |
125 | info->is_frequency = !strncmp(buf, "FR", 2); | |
126 | ||
127 | if (info->is_dc || info->is_ac) | |
128 | info->is_volt = TRUE; | |
129 | ||
130 | /* Byte 2: Always space (0x20). */ | |
131 | ||
132 | /* Bytes 3-8: See parse_value(). */ | |
133 | ||
134 | /* Bytes 9-12: Unit */ | |
135 | if (!strcmp(buf + 9, " A")) | |
136 | info->is_ampere = TRUE; | |
137 | else if (!strcmp(buf + 9, " mA")) | |
138 | info->is_milli = info->is_ampere = TRUE; | |
139 | else if (!strcmp(buf + 9, " V")) | |
140 | info->is_volt = TRUE; | |
141 | else if (!strcmp(buf + 9, " mV")) | |
142 | info->is_milli = info->is_volt = TRUE; | |
143 | else if (!strcmp(buf + 9, " Ohm")) | |
144 | info->is_ohm = TRUE; | |
145 | else if (!strcmp(buf + 9, "KOhm")) | |
146 | info->is_kilo = info->is_ohm = TRUE; | |
147 | else if (!strcmp(buf + 9, "MOhm")) | |
148 | info->is_mega = info->is_ohm = TRUE; | |
149 | else if (!strcmp(buf + 9, " nF")) | |
150 | info->is_nano = info->is_farad = TRUE; | |
151 | else if (!strcmp(buf + 9, " uF")) | |
152 | info->is_micro = info->is_farad = TRUE; | |
153 | else if (!strcmp(buf + 9, " KHz")) | |
154 | info->is_kilo = info->is_hertz = TRUE; | |
155 | else if (!strcmp(buf + 9, " C")) | |
156 | info->is_celsius = TRUE; | |
157 | ||
158 | /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */ | |
159 | } | |
160 | ||
161 | static void handle_flags(struct sr_datafeed_analog *analog, float *floatval, | |
162 | const struct metex14_info *info) | |
163 | { | |
164 | /* Factors */ | |
165 | if (info->is_nano) | |
166 | *floatval /= 1000000000; | |
167 | if (info->is_micro) | |
168 | *floatval /= 1000000; | |
169 | if (info->is_milli) | |
170 | *floatval /= 1000; | |
171 | if (info->is_kilo) | |
172 | *floatval *= 1000; | |
173 | if (info->is_mega) | |
174 | *floatval *= 1000000; | |
175 | ||
176 | /* Measurement modes */ | |
177 | if (info->is_volt) { | |
178 | analog->mq = SR_MQ_VOLTAGE; | |
179 | analog->unit = SR_UNIT_VOLT; | |
180 | } | |
181 | if (info->is_ampere) { | |
182 | analog->mq = SR_MQ_CURRENT; | |
183 | analog->unit = SR_UNIT_AMPERE; | |
184 | } | |
185 | if (info->is_ohm) { | |
186 | analog->mq = SR_MQ_RESISTANCE; | |
187 | analog->unit = SR_UNIT_OHM; | |
188 | } | |
189 | if (info->is_hertz) { | |
190 | analog->mq = SR_MQ_FREQUENCY; | |
191 | analog->unit = SR_UNIT_HERTZ; | |
192 | } | |
193 | if (info->is_farad) { | |
194 | analog->mq = SR_MQ_CAPACITANCE; | |
195 | analog->unit = SR_UNIT_FARAD; | |
196 | } | |
197 | if (info->is_celsius) { | |
198 | analog->mq = SR_MQ_TEMPERATURE; | |
199 | analog->unit = SR_UNIT_CELSIUS; | |
200 | } | |
201 | if (info->is_diode) { | |
202 | analog->mq = SR_MQ_VOLTAGE; | |
203 | analog->unit = SR_UNIT_VOLT; | |
204 | } | |
205 | ||
206 | /* Measurement related flags */ | |
207 | if (info->is_ac) | |
208 | analog->mqflags |= SR_MQFLAG_AC; | |
209 | if (info->is_dc) | |
210 | analog->mqflags |= SR_MQFLAG_DC; | |
211 | } | |
212 | ||
213 | static gboolean flags_valid(const struct metex14_info *info) | |
214 | { | |
215 | int count; | |
216 | ||
217 | /* Does the packet have more than one multiplier? */ | |
218 | count = 0; | |
219 | count += (info->is_nano) ? 1 : 0; | |
220 | count += (info->is_micro) ? 1 : 0; | |
221 | count += (info->is_milli) ? 1 : 0; | |
222 | count += (info->is_kilo) ? 1 : 0; | |
223 | count += (info->is_mega) ? 1 : 0; | |
224 | if (count > 1) { | |
225 | sr_err("More than one multiplier detected in packet."); | |
226 | return FALSE; | |
227 | } | |
228 | ||
229 | /* Does the packet "measure" more than one type of value? */ | |
230 | count = 0; | |
231 | count += (info->is_ac) ? 1 : 0; | |
232 | count += (info->is_dc) ? 1 : 0; | |
233 | count += (info->is_resistance) ? 1 : 0; | |
234 | count += (info->is_capacity) ? 1 : 0; | |
235 | count += (info->is_temperature) ? 1 : 0; | |
236 | count += (info->is_diode) ? 1 : 0; | |
237 | count += (info->is_frequency) ? 1 : 0; | |
238 | if (count > 1) { | |
239 | sr_err("More than one measurement type detected in packet."); | |
240 | return FALSE; | |
241 | } | |
242 | ||
243 | /* Both AC and DC set? */ | |
244 | if (info->is_ac && info->is_dc) { | |
245 | sr_err("Both AC and DC flags detected in packet."); | |
246 | return FALSE; | |
247 | } | |
248 | ||
249 | return TRUE; | |
250 | } | |
251 | ||
252 | SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf) | |
253 | { | |
254 | struct metex14_info info; | |
255 | ||
256 | memset(&info, 0x00, sizeof(struct metex14_info)); | |
257 | parse_flags((const char *)buf, &info); | |
258 | ||
259 | if (!flags_valid(&info)) | |
260 | return FALSE; | |
261 | ||
262 | if (buf[13] != '\r') | |
263 | return FALSE; | |
264 | ||
265 | return TRUE; | |
266 | } | |
267 | ||
268 | /** | |
269 | * Parse a protocol packet. | |
270 | * | |
271 | * @param buf Buffer containing the protocol packet. | |
272 | * @param floatval Pointer to a float variable. That variable will be modified | |
273 | * in-place depending on the protocol packet. | |
274 | * @param analog Pointer to a struct sr_datafeed_analog. The struct will be | |
275 | * filled with data according to the protocol packet. | |
276 | * | |
277 | * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the | |
278 | * 'analog' variable contents are undefined and should not be used. | |
279 | */ | |
280 | SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval, | |
281 | struct sr_datafeed_analog *analog, | |
282 | struct metex14_info *info) | |
283 | { | |
284 | int ret; | |
285 | ||
286 | if ((ret = parse_value(buf, floatval)) != SR_OK) { | |
287 | sr_err("Error parsing value: %d.", ret); | |
288 | return ret; | |
289 | } | |
290 | ||
291 | memset(info, 0x00, sizeof(struct metex14_info)); | |
292 | parse_flags((const char *)buf, info); | |
293 | handle_flags(analog, floatval, info); | |
294 | ||
295 | return SR_OK; | |
296 | } |