]>
Commit | Line | Data |
---|---|---|
626027df UH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 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 | |
2ea1fdf1 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
626027df UH |
18 | */ |
19 | ||
20 | /* | |
21 | * UNI-T UT71x protocol parser. | |
22 | * | |
23 | * Communication parameters: Unidirectional, 2400/7o1 | |
24 | */ | |
25 | ||
6ec6c43b | 26 | #include <config.h> |
626027df UH |
27 | #include <string.h> |
28 | #include <ctype.h> | |
29 | #include <math.h> | |
30 | #include <glib.h> | |
c1aae900 | 31 | #include <libsigrok/libsigrok.h> |
626027df UH |
32 | #include "libsigrok-internal.h" |
33 | ||
34 | #define LOG_PREFIX "ut71x" | |
35 | ||
36 | /* | |
4f414d4c | 37 | * Exponents for the respective measurement mode. |
626027df UH |
38 | * |
39 | * The Conrad/Voltcraft protocol descriptions have a typo (they suggest | |
40 | * index 0 for the 10A range (which is incorrect, it's range 1). | |
41 | */ | |
4f414d4c AJ |
42 | static const int exponents[16][8] = { |
43 | { -5, 0, 0, 0, 0, 0, 0, 0 }, /* AC mV */ | |
44 | { 0, -4, -3, -2, -1, 0, 0, 0 }, /* DC V */ | |
45 | { 0, -4, -3, -2, -1, 0, 0, 0 }, /* AC V */ | |
46 | { -5, 0, 0, 0, 0, 0, 0, 0 }, /* DC mV */ | |
47 | { 0, -1, 0, 1, 2, 3, 4, 0 }, /* Resistance */ | |
48 | { 0, -12, -11, -10, -9, -8, -7, -6 }, /* Capacitance */ | |
49 | { -1, 0, 0, 0, 0, 0, 0, 0 }, /* Temp (C) */ | |
50 | { -8, -7, 0, 0, 0, 0, 0, 0 }, /* uA */ | |
51 | { -6, -5, 0, 0, 0, 0, 0, 0 }, /* mA */ | |
52 | { 0, -3, 0, 0, 0, 0, 0, 0 }, /* 10A */ | |
53 | { -1, 0, 0, 0, 0, 0, 0, 0 }, /* Continuity */ | |
54 | { -4, 0, 0, 0, 0, 0, 0, 0 }, /* Diode */ | |
55 | { -3, -2, -1, 0, 1, 2, 3, 4 }, /* Frequency */ | |
56 | { -1, 0, 0, 0, 0, 0, 0, 0 }, /* Temp (F) */ | |
57 | { 0, 0, 0, 0, 0, 0, 0, 0 }, /* Power */ | |
58 | { -2, 0, 0, 0, 0, 0, 0, 0 }, /* Loop current */ | |
626027df UH |
59 | }; |
60 | ||
61 | static int parse_value(const uint8_t *buf, struct ut71x_info *info, float *result) | |
62 | { | |
63 | int i, intval, num_digits = 5; | |
64 | ||
65 | /* Bytes 0-4: Value (5 decimal digits) */ | |
66 | if (!strncmp((const char *)buf, "::0<:", 5)) { | |
67 | sr_spew("Over limit."); | |
68 | *result = INFINITY; | |
69 | return SR_OK; | |
70 | } else if (!strncmp((const char *)buf, ":<0::", 5)) { | |
71 | sr_spew("Under limit."); | |
72 | *result = INFINITY; | |
73 | return SR_OK; | |
74 | } else if (buf[4] == ':') { | |
75 | sr_dbg("4000 count mode, only 4 digits used."); | |
76 | num_digits = 4; | |
77 | } else if (!isdigit(buf[0]) || !isdigit(buf[1]) || | |
78 | !isdigit(buf[2]) || !isdigit(buf[3]) || !isdigit(buf[4])) { | |
79 | sr_dbg("Invalid digits: %02x %02x %02x %02x %02x (%c %c " | |
80 | "%c %c %c).", buf[0], buf[1], buf[2], buf[3], buf[4], | |
81 | buf[0], buf[1], buf[2], buf[3], buf[4]); | |
82 | return SR_ERR; | |
83 | } | |
84 | for (i = 0, intval = 0; i < num_digits; i++) | |
85 | intval = 10 * intval + (buf[i] - '0'); | |
86 | ||
87 | /* Apply sign. */ | |
88 | intval *= info->is_sign ? -1 : 1; | |
89 | ||
90 | /* Note: The decimal point position will be parsed later. */ | |
91 | *result = (float)intval; | |
92 | sr_spew("The display value is %f.", *result); | |
93 | ||
94 | return SR_OK; | |
95 | } | |
96 | ||
4f414d4c | 97 | static int parse_range(const uint8_t *buf, float *floatval, int *exponent) |
626027df UH |
98 | { |
99 | int idx, mode; | |
626027df UH |
100 | |
101 | idx = buf[5] - '0'; | |
102 | if (idx < 0 || idx > 7) { | |
103 | sr_dbg("Invalid range byte 0x%02x (idx 0x%02x).", buf[5], idx); | |
104 | return SR_ERR; | |
105 | } | |
106 | ||
107 | mode = buf[6] - '0'; | |
108 | if (mode < 0 || mode > 15) { | |
109 | sr_dbg("Invalid mode byte 0x%02x (idx 0x%02x).", buf[6], mode); | |
110 | return SR_ERR; | |
111 | } | |
112 | ||
113 | sr_spew("mode/idx = %d/%d", mode, idx); | |
114 | ||
4f414d4c | 115 | *exponent = exponents[mode][idx]; |
626027df | 116 | |
4f414d4c AJ |
117 | /* Apply respective exponent (mode-dependent) on the value. */ |
118 | *floatval *= powf(10, *exponent); | |
119 | sr_dbg("Applying exponent %d, new value is %f.", *exponent, *floatval); | |
626027df UH |
120 | |
121 | return SR_OK; | |
122 | } | |
123 | ||
124 | static void parse_flags(const uint8_t *buf, struct ut71x_info *info) | |
125 | { | |
126 | /* Function byte */ | |
127 | switch (buf[6] - '0') { | |
128 | case 0: /* AC mV */ | |
129 | info->is_voltage = info->is_ac = TRUE; | |
130 | break; | |
131 | case 1: /* DC V */ | |
132 | info->is_voltage = info->is_dc = TRUE; | |
133 | break; | |
134 | case 2: /* AC V */ | |
135 | info->is_voltage = info->is_ac = TRUE; | |
136 | break; | |
137 | case 3: /* DC mV */ | |
138 | info->is_voltage = info->is_dc = TRUE; | |
139 | break; | |
140 | case 4: /* Resistance */ | |
141 | info->is_resistance = TRUE; | |
142 | break; | |
143 | case 5: /* Capacitance */ | |
144 | info->is_capacitance = TRUE; | |
145 | break; | |
146 | case 6: /* Temperature (Celsius) */ | |
147 | info->is_temperature = info->is_celsius = TRUE; | |
148 | break; | |
149 | case 7: /* uA */ | |
150 | info->is_current = info->is_dc = TRUE; | |
151 | break; | |
152 | case 8: /* mA */ | |
153 | info->is_current = info->is_dc = TRUE; | |
154 | break; | |
155 | case 9: /* 10A */ | |
156 | info->is_current = info->is_dc = TRUE; | |
157 | break; | |
158 | case 10: /* Continuity */ | |
159 | info->is_continuity = TRUE; | |
160 | break; | |
161 | case 11: /* Diode */ | |
162 | info->is_diode = TRUE; | |
163 | break; | |
164 | case 12: /* Frequency */ | |
165 | info->is_frequency = TRUE; | |
166 | break; | |
167 | case 13: /* Temperature (F) */ | |
168 | info->is_temperature = info->is_fahrenheit = TRUE; | |
169 | break; | |
170 | case 14: /* Power */ | |
171 | /* Note: Only available on UT71E (range 0-2500W). */ | |
172 | info->is_power = TRUE; | |
173 | break; | |
174 | case 15: /* DC loop current, percentage display (range 4-20mA) */ | |
175 | info->is_loop_current = TRUE; | |
176 | break; | |
177 | default: | |
178 | sr_dbg("Invalid function byte: 0x%02x.", buf[6]); | |
179 | break; | |
180 | } | |
181 | ||
182 | /* | |
183 | * State 1 byte: bit 0 = AC, bit 1 = DC | |
184 | * Either AC or DC or both or none can be set at the same time. | |
185 | */ | |
186 | info->is_ac = (buf[7] & (1 << 0)) != 0; | |
187 | info->is_dc = (buf[7] & (1 << 1)) != 0; | |
188 | ||
189 | /* | |
190 | * State 2 byte: bit 0 = auto, bit 1 = manual, bit 2 = sign | |
191 | * | |
192 | * The Conrad/Voltcraft protocol descriptions have a typo | |
193 | * (they suggest bit 3 as sign bit, which is incorrect). | |
194 | * | |
195 | * For modes where there's only one possible range (e.g. AC mV) | |
196 | * neither the "auto" nor the "manual" bits will be set. | |
197 | */ | |
198 | info->is_auto = (buf[8] & (1 << 0)) != 0; | |
199 | info->is_manual = (buf[8] & (1 << 1)) != 0; | |
200 | info->is_sign = (buf[8] & (1 << 2)) != 0; | |
201 | ||
202 | /* Note: "Frequency mode + sign bit" means "duty cycle mode". */ | |
203 | if (info->is_frequency && info->is_sign) { | |
204 | info->is_duty_cycle = TRUE; | |
205 | info->is_frequency = info->is_sign = FALSE; | |
206 | } | |
207 | } | |
208 | ||
b02bb45f | 209 | static void handle_flags(struct sr_datafeed_analog *analog, |
626027df UH |
210 | float *floatval, const struct ut71x_info *info) |
211 | { | |
212 | /* Measurement modes */ | |
213 | if (info->is_voltage) { | |
b02bb45f UH |
214 | analog->meaning->mq = SR_MQ_VOLTAGE; |
215 | analog->meaning->unit = SR_UNIT_VOLT; | |
626027df UH |
216 | } |
217 | if (info->is_current) { | |
b02bb45f UH |
218 | analog->meaning->mq = SR_MQ_CURRENT; |
219 | analog->meaning->unit = SR_UNIT_AMPERE; | |
626027df UH |
220 | } |
221 | if (info->is_resistance) { | |
b02bb45f UH |
222 | analog->meaning->mq = SR_MQ_RESISTANCE; |
223 | analog->meaning->unit = SR_UNIT_OHM; | |
626027df UH |
224 | } |
225 | if (info->is_frequency) { | |
b02bb45f UH |
226 | analog->meaning->mq = SR_MQ_FREQUENCY; |
227 | analog->meaning->unit = SR_UNIT_HERTZ; | |
626027df UH |
228 | } |
229 | if (info->is_capacitance) { | |
b02bb45f UH |
230 | analog->meaning->mq = SR_MQ_CAPACITANCE; |
231 | analog->meaning->unit = SR_UNIT_FARAD; | |
626027df UH |
232 | } |
233 | if (info->is_temperature && info->is_celsius) { | |
b02bb45f UH |
234 | analog->meaning->mq = SR_MQ_TEMPERATURE; |
235 | analog->meaning->unit = SR_UNIT_CELSIUS; | |
626027df UH |
236 | } |
237 | if (info->is_temperature && info->is_fahrenheit) { | |
b02bb45f UH |
238 | analog->meaning->mq = SR_MQ_TEMPERATURE; |
239 | analog->meaning->unit = SR_UNIT_FAHRENHEIT; | |
626027df UH |
240 | } |
241 | if (info->is_continuity) { | |
b02bb45f UH |
242 | analog->meaning->mq = SR_MQ_CONTINUITY; |
243 | analog->meaning->unit = SR_UNIT_BOOLEAN; | |
626027df UH |
244 | *floatval = (*floatval < 0.0 || *floatval > 60.0) ? 0.0 : 1.0; |
245 | } | |
246 | if (info->is_diode) { | |
b02bb45f UH |
247 | analog->meaning->mq = SR_MQ_VOLTAGE; |
248 | analog->meaning->unit = SR_UNIT_VOLT; | |
626027df UH |
249 | } |
250 | if (info->is_duty_cycle) { | |
b02bb45f UH |
251 | analog->meaning->mq = SR_MQ_DUTY_CYCLE; |
252 | analog->meaning->unit = SR_UNIT_PERCENTAGE; | |
626027df UH |
253 | } |
254 | if (info->is_power) { | |
b02bb45f UH |
255 | analog->meaning->mq = SR_MQ_POWER; |
256 | analog->meaning->unit = SR_UNIT_WATT; | |
626027df UH |
257 | } |
258 | if (info->is_loop_current) { | |
259 | /* 4mA = 0%, 20mA = 100% */ | |
b02bb45f UH |
260 | analog->meaning->mq = SR_MQ_CURRENT; |
261 | analog->meaning->unit = SR_UNIT_PERCENTAGE; | |
626027df UH |
262 | } |
263 | ||
264 | /* Measurement related flags */ | |
265 | if (info->is_ac) | |
b02bb45f | 266 | analog->meaning->mqflags |= SR_MQFLAG_AC; |
626027df | 267 | if (info->is_dc) |
b02bb45f | 268 | analog->meaning->mqflags |= SR_MQFLAG_DC; |
626027df UH |
269 | if (info->is_ac) |
270 | /* All AC modes do True-RMS measurements. */ | |
b02bb45f | 271 | analog->meaning->mqflags |= SR_MQFLAG_RMS; |
626027df | 272 | if (info->is_auto) |
b02bb45f | 273 | analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE; |
626027df | 274 | if (info->is_diode) |
b02bb45f | 275 | analog->meaning->mqflags |= SR_MQFLAG_DIODE; |
626027df UH |
276 | } |
277 | ||
278 | static gboolean flags_valid(const struct ut71x_info *info) | |
279 | { | |
280 | int count; | |
281 | ||
282 | /* Does the packet "measure" more than one type of value? */ | |
283 | count = (info->is_voltage) ? 1 : 0; | |
284 | count += (info->is_current) ? 1 : 0; | |
285 | count += (info->is_resistance) ? 1 : 0; | |
286 | count += (info->is_capacitance) ? 1 : 0; | |
287 | count += (info->is_frequency) ? 1 : 0; | |
288 | count += (info->is_temperature) ? 1 : 0; | |
289 | count += (info->is_continuity) ? 1 : 0; | |
290 | count += (info->is_diode) ? 1 : 0; | |
291 | count += (info->is_power) ? 1 : 0; | |
292 | count += (info->is_loop_current) ? 1 : 0; | |
293 | if (count > 1) { | |
294 | sr_dbg("More than one measurement type detected in packet."); | |
295 | return FALSE; | |
296 | } | |
297 | ||
298 | /* Auto and manual can't be active at the same time. */ | |
299 | if (info->is_auto && info->is_manual) { | |
300 | sr_dbg("Auto and manual modes are both active."); | |
301 | return FALSE; | |
302 | } | |
303 | ||
304 | return TRUE; | |
305 | } | |
306 | ||
307 | SR_PRIV gboolean sr_ut71x_packet_valid(const uint8_t *buf) | |
308 | { | |
309 | struct ut71x_info info; | |
310 | ||
311 | memset(&info, 0, sizeof(struct ut71x_info)); | |
312 | ||
313 | if (buf[9] != '\r' || buf[10] != '\n') | |
314 | return FALSE; | |
315 | ||
316 | parse_flags(buf, &info); | |
317 | ||
318 | return flags_valid(&info); | |
319 | } | |
320 | ||
321 | SR_PRIV int sr_ut71x_parse(const uint8_t *buf, float *floatval, | |
b02bb45f | 322 | struct sr_datafeed_analog *analog, void *info) |
626027df | 323 | { |
4f414d4c | 324 | int ret, exponent = 0; |
626027df UH |
325 | struct ut71x_info *info_local; |
326 | ||
327 | info_local = (struct ut71x_info *)info; | |
328 | memset(info_local, 0, sizeof(struct ut71x_info)); | |
329 | ||
330 | if (!sr_ut71x_packet_valid(buf)) | |
331 | return SR_ERR; | |
332 | ||
333 | parse_flags(buf, info_local); | |
334 | ||
335 | if ((ret = parse_value(buf, info, floatval)) != SR_OK) { | |
336 | sr_dbg("Error parsing value: %d.", ret); | |
337 | return ret; | |
338 | } | |
339 | ||
4f414d4c | 340 | if ((ret = parse_range(buf, floatval, &exponent)) != SR_OK) |
626027df UH |
341 | return ret; |
342 | ||
343 | handle_flags(analog, floatval, info); | |
344 | ||
d9251a2c | 345 | analog->encoding->digits = -exponent; |
4f414d4c AJ |
346 | analog->spec->spec_digits = -exponent; |
347 | ||
626027df UH |
348 | return SR_OK; |
349 | } |