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