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