]> sigrok.org Git - libsigrok.git/blob - src/dmm/metex14.c
Reorganize project tree.
[libsigrok.git] / src / dmm / metex14.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012-2013 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  * @file
23  *
24  * Metex 14-bytes ASCII protocol parser.
25  *
26  * @internal
27  * This should work for various multimeters which use this kind of protocol,
28  * even though there is some variation in which modes each DMM supports.
29  *
30  * It does _not_ work for all Metex DMMs, some use a quite different protocol.
31  */
32
33 #include <string.h>
34 #include <ctype.h>
35 #include <math.h>
36 #include <glib.h>
37 #include "libsigrok.h"
38 #include "libsigrok-internal.h"
39
40 #define LOG_PREFIX "metex14"
41
42 /** Parse value from buf, byte 2-8. */
43 static int parse_value(const uint8_t *buf, struct metex14_info *info,
44                         float *result)
45 {
46         int i, is_ol, cnt;
47         char valstr[7 + 1];
48
49         /* Strip all spaces from bytes 2-8. */
50         memset(&valstr, 0, 7 + 1);
51         for (i = 0, cnt = 0; i < 7; i++) {
52                 if (buf[2 + i] != ' ')
53                         valstr[cnt++] = buf[2 + i];
54         }
55
56         /* Bytes 5-7: Over limit (various forms) */
57         is_ol = 0;
58         is_ol += (!strcasecmp((const char *)&valstr, ".OL")) ? 1 : 0;
59         is_ol += (!strcasecmp((const char *)&valstr, "O.L")) ? 1 : 0;
60         is_ol += (!strcasecmp((const char *)&valstr, "OL.")) ? 1 : 0;
61         is_ol += (!strcasecmp((const char *)&valstr, "OL")) ? 1 : 0;
62         is_ol += (!strcasecmp((const char *)&valstr, "-.OL")) ? 1 : 0;
63         is_ol += (!strcasecmp((const char *)&valstr, "-O.L")) ? 1 : 0;
64         is_ol += (!strcasecmp((const char *)&valstr, "-OL.")) ? 1 : 0;
65         is_ol += (!strcasecmp((const char *)&valstr, "-OL")) ? 1 : 0;
66         if (is_ol != 0) {
67                 sr_spew("Over limit.");
68                 *result = INFINITY;
69                 return SR_OK;
70         }
71
72         /* Logic functions */
73         if (!strcmp((const char *)&valstr, "READY") ||
74                         !strcmp((const char *)&valstr, "FLOAT")) {
75                 *result = INFINITY;
76                 info->is_logic = TRUE;
77         } else if (!strcmp((const char *)&valstr, "Hi")) {
78                 *result = 1.0;
79                 info->is_logic = TRUE;
80         } else if (!strcmp((const char *)&valstr, "Lo")) {
81                 *result = 0.0;
82                 info->is_logic = TRUE;
83         }
84         if (info->is_logic)
85                 return SR_OK;
86
87         /* Bytes 2-8: Sign, value (up to 5 digits) and decimal point */
88         sscanf((const char *)&valstr, "%f", result);
89
90         sr_spew("The display value is %f.", *result);
91
92         return SR_OK;
93 }
94
95 static void parse_flags(const char *buf, struct metex14_info *info)
96 {
97         int i, cnt;
98         char unit[4 + 1];
99         const char *u;
100
101         /* Bytes 0-1: Measurement mode AC, DC */
102         info->is_ac = !strncmp(buf, "AC", 2);
103         info->is_dc = !strncmp(buf, "DC", 2);
104
105         /* Bytes 2-8: See parse_value(). */
106
107         /* Strip all spaces from bytes 9-12. */
108         memset(&unit, 0, 4 + 1);
109         for (i = 0, cnt = 0; i < 4; i++) {
110                 if (buf[9 + i] != ' ')
111                         unit[cnt++] = buf[9 + i];
112         }
113
114         /* Bytes 9-12: Unit */
115         u = (const char *)&unit;
116         if (!strcasecmp(u, "A"))
117                 info->is_ampere = TRUE;
118         else if (!strcasecmp(u, "mA"))
119                 info->is_milli = info->is_ampere = TRUE;
120         else if (!strcasecmp(u, "uA"))
121                 info->is_micro = info->is_ampere = TRUE;
122         else if (!strcasecmp(u, "V"))
123                 info->is_volt = TRUE;
124         else if (!strcasecmp(u, "mV"))
125                 info->is_milli = info->is_volt = TRUE;
126         else if (!strcasecmp(u, "Ohm"))
127                 info->is_ohm = TRUE;
128         else if (!strcasecmp(u, "KOhm"))
129                 info->is_kilo = info->is_ohm = TRUE;
130         else if (!strcasecmp(u, "MOhm"))
131                 info->is_mega = info->is_ohm = TRUE;
132         else if (!strcasecmp(u, "pF"))
133                 info->is_pico = info->is_farad = TRUE;
134         else if (!strcasecmp(u, "nF"))
135                 info->is_nano = info->is_farad = TRUE;
136         else if (!strcasecmp(u, "uF"))
137                 info->is_micro = info->is_farad = TRUE;
138         else if (!strcasecmp(u, "KHz"))
139                 info->is_kilo = info->is_hertz = TRUE;
140         else if (!strcasecmp(u, "C"))
141                 info->is_celsius = TRUE;
142         else if (!strcasecmp(u, "DB"))
143                 info->is_decibel = TRUE;
144         else if (!strcasecmp(u, ""))
145                 info->is_unitless = TRUE;
146
147         /* Bytes 0-1: Measurement mode, except AC/DC */
148         info->is_resistance  = !strncmp(buf, "OH", 2) ||
149                 (!strncmp(buf, "  ", 2) && info->is_ohm);
150         info->is_capacity    = !strncmp(buf, "CA", 2) ||
151                 (!strncmp(buf, "  ", 2) && info->is_farad);
152         info->is_temperature = !strncmp(buf, "TE", 2);
153         info->is_diode       = !strncmp(buf, "DI", 2) ||
154                 (!strncmp(buf, "  ", 2) && info->is_volt && info->is_milli);
155         info->is_frequency   = !strncmp(buf, "FR", 2) ||
156                 (!strncmp(buf, "  ", 2) && info->is_hertz);
157         info->is_gain        = !strncmp(buf, "DB", 2);
158         info->is_hfe         = !strncmp(buf, "HF", 2) ||
159                 (!strncmp(buf, "  ", 2) && !info->is_volt && !info->is_ohm &&
160                  !info->is_logic && !info->is_farad && !info->is_hertz);
161         /*
162          * Note:
163          * - Protocol doesn't distinguish "resistance" from "beep" mode.
164          * - "DB" shows the logarithmic ratio of input voltage to a
165          *   pre-stored (user-changeable) value in the DMM.
166          */
167
168         /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */
169 }
170
171 static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
172                          const struct metex14_info *info)
173 {
174         /* Factors */
175         if (info->is_pico)
176                 *floatval /= 1000000000000ULL;
177         if (info->is_nano)
178                 *floatval /= 1000000000;
179         if (info->is_micro)
180                 *floatval /= 1000000;
181         if (info->is_milli)
182                 *floatval /= 1000;
183         if (info->is_kilo)
184                 *floatval *= 1000;
185         if (info->is_mega)
186                 *floatval *= 1000000;
187
188         /* Measurement modes */
189         if (info->is_volt) {
190                 analog->mq = SR_MQ_VOLTAGE;
191                 analog->unit = SR_UNIT_VOLT;
192         }
193         if (info->is_ampere) {
194                 analog->mq = SR_MQ_CURRENT;
195                 analog->unit = SR_UNIT_AMPERE;
196         }
197         if (info->is_ohm) {
198                 analog->mq = SR_MQ_RESISTANCE;
199                 analog->unit = SR_UNIT_OHM;
200         }
201         if (info->is_hertz) {
202                 analog->mq = SR_MQ_FREQUENCY;
203                 analog->unit = SR_UNIT_HERTZ;
204         }
205         if (info->is_farad) {
206                 analog->mq = SR_MQ_CAPACITANCE;
207                 analog->unit = SR_UNIT_FARAD;
208         }
209         if (info->is_celsius) {
210                 analog->mq = SR_MQ_TEMPERATURE;
211                 analog->unit = SR_UNIT_CELSIUS;
212         }
213         if (info->is_diode) {
214                 analog->mq = SR_MQ_VOLTAGE;
215                 analog->unit = SR_UNIT_VOLT;
216         }
217         if (info->is_gain) {
218                 analog->mq = SR_MQ_GAIN;
219                 analog->unit = SR_UNIT_DECIBEL_VOLT;
220         }
221         if (info->is_hfe) {
222                 analog->mq = SR_MQ_GAIN;
223                 analog->unit = SR_UNIT_UNITLESS;
224         }
225         if (info->is_logic) {
226                 analog->mq = SR_MQ_GAIN;
227                 analog->unit = SR_UNIT_UNITLESS;
228         }
229
230         /* Measurement related flags */
231         if (info->is_ac)
232                 analog->mqflags |= SR_MQFLAG_AC;
233         if (info->is_dc)
234                 analog->mqflags |= SR_MQFLAG_DC;
235         if (info->is_diode)
236                 analog->mqflags |= SR_MQFLAG_DIODE;
237 }
238
239 static gboolean flags_valid(const struct metex14_info *info)
240 {
241         int count;
242
243         /* Does the packet have more than one multiplier? */
244         count = 0;
245         count += (info->is_pico) ? 1 : 0;
246         count += (info->is_nano) ? 1 : 0;
247         count += (info->is_micro) ? 1 : 0;
248         count += (info->is_milli) ? 1 : 0;
249         count += (info->is_kilo) ? 1 : 0;
250         count += (info->is_mega) ? 1 : 0;
251         if (count > 1) {
252                 sr_dbg("More than one multiplier detected in packet.");
253                 return FALSE;
254         }
255
256         /* Does the packet "measure" more than one type of value? */
257         count = 0;
258         count += (info->is_ac) ? 1 : 0;
259         count += (info->is_dc) ? 1 : 0;
260         count += (info->is_resistance) ? 1 : 0;
261         count += (info->is_capacity) ? 1 : 0;
262         count += (info->is_temperature) ? 1 : 0;
263         count += (info->is_diode) ? 1 : 0;
264         count += (info->is_frequency) ? 1 : 0;
265         if (count > 1) {
266                 sr_dbg("More than one measurement type detected in packet.");
267                 return FALSE;
268         }
269
270         /* Both AC and DC set? */
271         if (info->is_ac && info->is_dc) {
272                 sr_dbg("Both AC and DC flags detected in packet.");
273                 return FALSE;
274         }
275
276         return TRUE;
277 }
278
279 #ifdef HAVE_LIBSERIALPORT
280 SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial)
281 {
282         const uint8_t wbuf = 'D';
283
284         sr_spew("Requesting DMM packet.");
285
286         return (serial_write(serial, &wbuf, 1) == 1) ? SR_OK : SR_ERR;
287 }
288 #endif
289
290 SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf)
291 {
292         struct metex14_info info;
293
294         memset(&info, 0x00, sizeof(struct metex14_info));
295         parse_flags((const char *)buf, &info);
296
297         if (!flags_valid(&info))
298                 return FALSE;
299
300         if (buf[13] != '\r')
301                 return FALSE;
302
303         return TRUE;
304 }
305
306 /**
307  * Parse a protocol packet.
308  *
309  * @param buf Buffer containing the protocol packet. Must not be NULL.
310  * @param floatval Pointer to a float variable. That variable will be modified
311  *                 in-place depending on the protocol packet. Must not be NULL.
312  * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
313  *               filled with data according to the protocol packet.
314  *               Must not be NULL.
315  * @param info Pointer to a struct metex14_info. The struct will be filled
316  *             with data according to the protocol packet. Must not be NULL.
317  *
318  * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
319  *         'analog' variable contents are undefined and should not be used.
320  */
321 SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
322                              struct sr_datafeed_analog *analog, void *info)
323 {
324         int ret;
325         struct metex14_info *info_local;
326
327         info_local = (struct metex14_info *)info;
328
329         /* Don't print byte 13. That one contains the carriage return. */
330         sr_dbg("DMM packet: \"%.13s\"", buf);
331
332         memset(info_local, 0x00, sizeof(struct metex14_info));
333
334         if ((ret = parse_value(buf, info_local, floatval)) != SR_OK) {
335                 sr_dbg("Error parsing value: %d.", ret);
336                 return ret;
337         }
338
339         parse_flags((const char *)buf, info_local);
340         handle_flags(analog, floatval, info_local);
341
342         return SR_OK;
343 }