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