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