]> sigrok.org Git - libsigrok.git/blob - src/dmm/metex14.c
metex14: Add power factor measurement mode
[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, see <http://www.gnu.org/licenses/>.
18  */
19
20 /**
21  * @file
22  *
23  * Metex 14-bytes ASCII protocol parser.
24  *
25  * @internal
26  * This should work for various multimeters which use this kind of protocol,
27  * even though there is some variation in which modes each DMM supports.
28  *
29  * It does _not_ work for all Metex DMMs, some use a quite different protocol.
30  */
31
32 #include <config.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <ctype.h>
36 #include <math.h>
37 #include <glib.h>
38 #include <libsigrok/libsigrok.h>
39 #include "libsigrok-internal.h"
40
41 #define LOG_PREFIX "metex14"
42
43 /** Parse value from buf, byte 2-8. */
44 static int parse_value(const uint8_t *buf, struct metex14_info *info,
45                         float *result, int *exponent)
46 {
47         int i, is_ol, cnt, dot_pos;
48         char valstr[7 + 1];
49
50         /* Strip all spaces from bytes 2-8. */
51         memset(&valstr, 0, 7 + 1);
52         for (i = 0, cnt = 0; i < 7; i++) {
53                 if (buf[2 + i] != ' ')
54                         valstr[cnt++] = buf[2 + i];
55         }
56
57         /* Bytes 5-7: Over limit (various forms) */
58         is_ol = 0;
59         is_ol += (!g_ascii_strcasecmp((const char *)&valstr, ".OL")) ? 1 : 0;
60         is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "O.L")) ? 1 : 0;
61         is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "OL.")) ? 1 : 0;
62         is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "OL")) ? 1 : 0;
63         is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-.OL")) ? 1 : 0;
64         is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-O.L")) ? 1 : 0;
65         is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-OL.")) ? 1 : 0;
66         is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-OL")) ? 1 : 0;
67         if (is_ol != 0) {
68                 sr_spew("Over limit.");
69                 *result = INFINITY;
70                 return SR_OK;
71         }
72
73         /* Logic functions */
74         if (!strcmp((const char *)&valstr, "READY") ||
75                         !strcmp((const char *)&valstr, "FLOAT")) {
76                 *result = INFINITY;
77                 info->is_logic = TRUE;
78         } else if (!strcmp((const char *)&valstr, "Hi")) {
79                 *result = 1.0;
80                 info->is_logic = TRUE;
81         } else if (!strcmp((const char *)&valstr, "Lo")) {
82                 *result = 0.0;
83                 info->is_logic = TRUE;
84         }
85         if (info->is_logic)
86                 return SR_OK;
87
88         /* Bytes 2-8: Sign, value (up to 5 digits) and decimal point */
89         sr_atof_ascii((const char *)&valstr, result);
90
91         dot_pos = strcspn(valstr, ".");
92         if (dot_pos < cnt)
93                 *exponent = -(cnt - dot_pos - 1);
94         else
95                 *exponent = 0;
96
97         sr_spew("The display value is %f.", *result);
98
99         return SR_OK;
100 }
101
102 static void parse_flags(const char *buf, struct metex14_info *info)
103 {
104         int i, cnt;
105         char unit[4 + 1];
106         const char *u;
107
108         /* Bytes 0-1: Measurement mode AC, DC */
109         info->is_ac = !strncmp(buf, "AC", 2);
110         info->is_dc = !strncmp(buf, "DC", 2);
111
112         /* Bytes 2-8: See parse_value(). */
113
114         /* Strip all spaces from bytes 9-12. */
115         memset(&unit, 0, 4 + 1);
116         for (i = 0, cnt = 0; i < 4; i++) {
117                 if (buf[9 + i] != ' ')
118                         unit[cnt++] = buf[9 + i];
119         }
120
121         /* Bytes 9-12: Unit */
122         u = (const char *)&unit;
123         if (!g_ascii_strcasecmp(u, "A"))
124                 info->is_ampere = TRUE;
125         else if (!g_ascii_strcasecmp(u, "mA"))
126                 info->is_milli = info->is_ampere = TRUE;
127         else if (!g_ascii_strcasecmp(u, "uA"))
128                 info->is_micro = info->is_ampere = TRUE;
129         else if (!g_ascii_strcasecmp(u, "V"))
130                 info->is_volt = TRUE;
131         else if (!g_ascii_strcasecmp(u, "mV"))
132                 info->is_milli = info->is_volt = TRUE;
133         else if (!g_ascii_strcasecmp(u, "Ohm"))
134                 info->is_ohm = TRUE;
135         else if (!g_ascii_strcasecmp(u, "KOhm"))
136                 info->is_kilo = info->is_ohm = TRUE;
137         else if (!g_ascii_strcasecmp(u, "MOhm"))
138                 info->is_mega = info->is_ohm = TRUE;
139         else if (!g_ascii_strcasecmp(u, "pF"))
140                 info->is_pico = info->is_farad = TRUE;
141         else if (!g_ascii_strcasecmp(u, "nF"))
142                 info->is_nano = info->is_farad = TRUE;
143         else if (!g_ascii_strcasecmp(u, "uF"))
144                 info->is_micro = info->is_farad = TRUE;
145         else if (!g_ascii_strcasecmp(u, "KHz"))
146                 info->is_kilo = info->is_hertz = TRUE;
147         else if (!g_ascii_strcasecmp(u, "C"))
148                 info->is_celsius = TRUE;
149         else if (!g_ascii_strcasecmp(u, "F"))
150                 info->is_fahrenheit = TRUE;
151         else if (!g_ascii_strcasecmp(u, "DB"))
152                 info->is_decibel = TRUE;
153         else if (!g_ascii_strcasecmp(u, "dBm"))
154                 info->is_decibel_mw = TRUE;
155         else if (!g_ascii_strcasecmp(u, "W"))
156                 info->is_watt = TRUE;
157         else if (!g_ascii_strcasecmp(u, ""))
158                 info->is_unitless = TRUE;
159
160         /* Bytes 0-1: Measurement mode, except AC/DC */
161         info->is_resistance = !strncmp(buf, "OH", 2) ||
162                 (!strncmp(buf, "  ", 2) && info->is_ohm);
163         info->is_capacity = !strncmp(buf, "CA", 2) ||
164                 (!strncmp(buf, "  ", 2) && info->is_farad);
165         info->is_temperature = !strncmp(buf, "TE", 2) ||
166                 info->is_celsius || info->is_fahrenheit;
167         info->is_diode = !strncmp(buf, "DI", 2) ||
168                 (!strncmp(buf, "  ", 2) && info->is_volt && info->is_milli);
169         info->is_frequency = !strncmp(buf, "FR", 2) ||
170                 (!strncmp(buf, "  ", 2) && info->is_hertz);
171         info->is_gain = !strncmp(buf, "DB", 2) && info->is_decibel;
172         info->is_power = (!strncmp(buf, "dB", 2) && info->is_decibel_mw) ||
173                 ((!strncmp(buf, "WT", 2) && info->is_watt));
174         info->is_power_factor = !strncmp(buf, "CO", 2) && info->is_unitless;
175         info->is_hfe = !strncmp(buf, "HF", 2) ||
176                 (!strncmp(buf, "  ", 2) && !info->is_volt && !info->is_ohm &&
177                  !info->is_logic && !info->is_farad && !info->is_hertz);
178         info->is_min = !strncmp(buf, "MN", 2);
179         info->is_max = !strncmp(buf, "MX", 2);
180         info->is_avg = !strncmp(buf, "AG", 2);
181
182         /*
183          * Note:
184          * - Protocol doesn't distinguish "resistance" from "beep" mode.
185          * - "DB" shows the logarithmic ratio of input voltage to a
186          *   pre-stored (user-changeable) value in the DMM.
187          */
188
189         /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */
190 }
191
192 static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
193                          int *exponent, const struct metex14_info *info)
194 {
195         int factor;
196
197         (void)exponent;
198
199         /* Factors */
200         factor = 0;
201         if (info->is_pico)
202                 factor -= 12;
203         if (info->is_nano)
204                 factor -= 9;
205         if (info->is_micro)
206                 factor -= 6;
207         if (info->is_milli)
208                 factor -= 3;
209         if (info->is_kilo)
210                 factor += 3;
211         if (info->is_mega)
212                 factor += 6;
213         *floatval *= powf(10, factor);
214
215         /* Measurement modes */
216         if (info->is_volt) {
217                 analog->meaning->mq = SR_MQ_VOLTAGE;
218                 analog->meaning->unit = SR_UNIT_VOLT;
219         }
220         if (info->is_ampere) {
221                 analog->meaning->mq = SR_MQ_CURRENT;
222                 analog->meaning->unit = SR_UNIT_AMPERE;
223         }
224         if (info->is_ohm) {
225                 analog->meaning->mq = SR_MQ_RESISTANCE;
226                 analog->meaning->unit = SR_UNIT_OHM;
227         }
228         if (info->is_hertz) {
229                 analog->meaning->mq = SR_MQ_FREQUENCY;
230                 analog->meaning->unit = SR_UNIT_HERTZ;
231         }
232         if (info->is_farad) {
233                 analog->meaning->mq = SR_MQ_CAPACITANCE;
234                 analog->meaning->unit = SR_UNIT_FARAD;
235         }
236         if (info->is_temperature) {
237                 analog->meaning->mq = SR_MQ_TEMPERATURE;
238                 if (info->is_celsius)
239                         analog->meaning->unit = SR_UNIT_CELSIUS;
240                 else if (info->is_fahrenheit)
241                         analog->meaning->unit = SR_UNIT_FAHRENHEIT;
242                 else
243                         analog->meaning->unit = SR_UNIT_UNITLESS;
244         }
245         if (info->is_diode) {
246                 analog->meaning->mq = SR_MQ_VOLTAGE;
247                 analog->meaning->unit = SR_UNIT_VOLT;
248         }
249         if (info->is_power) {
250                 analog->meaning->mq = SR_MQ_POWER;
251                 if (info->is_decibel_mw)
252                         analog->meaning->unit = SR_UNIT_DECIBEL_MW;
253                 else if (info->is_watt)
254                         analog->meaning->unit = SR_UNIT_WATT;
255                 else
256                         analog->meaning->unit = SR_UNIT_UNITLESS;
257         }
258         if (info->is_power_factor) {
259                 analog->meaning->mq = SR_MQ_POWER_FACTOR;
260                 analog->meaning->unit = SR_UNIT_UNITLESS;
261         }
262         if (info->is_gain) {
263                 analog->meaning->mq = SR_MQ_GAIN;
264                 analog->meaning->unit = SR_UNIT_DECIBEL_VOLT;
265         }
266         if (info->is_hfe) {
267                 analog->meaning->mq = SR_MQ_GAIN;
268                 analog->meaning->unit = SR_UNIT_UNITLESS;
269         }
270         if (info->is_logic) {
271                 analog->meaning->mq = SR_MQ_GAIN;
272                 analog->meaning->unit = SR_UNIT_UNITLESS;
273         }
274
275         /* Measurement related flags */
276         if (info->is_ac)
277                 analog->meaning->mqflags |= SR_MQFLAG_AC;
278         if (info->is_dc)
279                 analog->meaning->mqflags |= SR_MQFLAG_DC;
280         if (info->is_diode)
281                 analog->meaning->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
282         if (info->is_min)
283                 analog->meaning->mqflags |= SR_MQFLAG_MIN;
284         if (info->is_max)
285                 analog->meaning->mqflags |= SR_MQFLAG_MAX;
286         if (info->is_avg)
287                 analog->meaning->mqflags |= SR_MQFLAG_AVG;
288 }
289
290 static gboolean flags_valid(const struct metex14_info *info)
291 {
292         int count;
293
294         /* Does the packet have more than one multiplier? */
295         count = 0;
296         count += (info->is_pico) ? 1 : 0;
297         count += (info->is_nano) ? 1 : 0;
298         count += (info->is_micro) ? 1 : 0;
299         count += (info->is_milli) ? 1 : 0;
300         count += (info->is_kilo) ? 1 : 0;
301         count += (info->is_mega) ? 1 : 0;
302         if (count > 1) {
303                 sr_dbg("More than one multiplier detected in packet.");
304                 return FALSE;
305         }
306
307         /* Does the packet "measure" more than one type of value? */
308         count = 0;
309         count += (info->is_ac) ? 1 : 0;
310         count += (info->is_dc) ? 1 : 0;
311         count += (info->is_resistance) ? 1 : 0;
312         count += (info->is_capacity) ? 1 : 0;
313         count += (info->is_temperature) ? 1 : 0;
314         count += (info->is_diode) ? 1 : 0;
315         count += (info->is_frequency) ? 1 : 0;
316         if (count > 1) {
317                 sr_dbg("More than one measurement type detected in packet.");
318                 return FALSE;
319         }
320
321         /* Both AC and DC set? */
322         if (info->is_ac && info->is_dc) {
323                 sr_dbg("Both AC and DC flags detected in packet.");
324                 return FALSE;
325         }
326
327         return TRUE;
328 }
329
330 #ifdef HAVE_LIBSERIALPORT
331 SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial)
332 {
333         const uint8_t wbuf = 'D';
334
335         sr_spew("Requesting DMM packet.");
336
337         return serial_write_blocking(serial, &wbuf, 1, 0);
338 }
339 #endif
340
341 SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf)
342 {
343         struct metex14_info info;
344
345         memset(&info, 0x00, sizeof(struct metex14_info));
346         parse_flags((const char *)buf, &info);
347
348         if (!flags_valid(&info))
349                 return FALSE;
350
351         if (buf[13] != '\r')
352                 return FALSE;
353
354         return TRUE;
355 }
356
357 SR_PRIV gboolean sr_metex14_4packets_valid(const uint8_t *buf)
358 {
359         struct metex14_info info;
360         size_t ch_idx;
361         const uint8_t *ch_buf;
362
363         ch_buf = buf;
364         for (ch_idx = 0; ch_idx < 4; ch_idx++) {
365                 if (ch_buf[13] != '\r')
366                         return FALSE;
367                 memset(&info, 0x00, sizeof(info));
368                 parse_flags((const char *)ch_buf, &info);
369                 if (!flags_valid(&info))
370                         return FALSE;
371                 ch_buf += METEX14_PACKET_SIZE;
372         }
373         return TRUE;
374 }
375
376 /**
377  * Parse a protocol packet.
378  *
379  * @param buf Buffer containing the protocol packet. Must not be NULL.
380  * @param floatval Pointer to a float variable. That variable will be modified
381  *                 in-place depending on the protocol packet. Must not be NULL.
382  * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
383  *               filled with data according to the protocol packet.
384  *               Must not be NULL.
385  * @param info Pointer to a struct metex14_info. The struct will be filled
386  *             with data according to the protocol packet. Must not be NULL.
387  *
388  * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
389  *         'analog' variable contents are undefined and should not be used.
390  */
391 SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
392                              struct sr_datafeed_analog *analog, void *info)
393 {
394         int ret, exponent = 0;
395         struct metex14_info *info_local;
396
397         info_local = info;
398
399         /* Don't print byte 13. That one contains the carriage return. */
400         sr_dbg("DMM packet: \"%.13s\"", buf);
401
402         memset(info_local, 0x00, sizeof(struct metex14_info));
403
404         if ((ret = parse_value(buf, info_local, floatval, &exponent)) != SR_OK) {
405                 sr_dbg("Error parsing value: %d.", ret);
406                 return ret;
407         }
408
409         parse_flags((const char *)buf, info_local);
410         handle_flags(analog, floatval, &exponent, info_local);
411
412         analog->encoding->digits = -exponent;
413         analog->spec->spec_digits = -exponent;
414
415         return SR_OK;
416 }
417
418 /**
419  * Parse one out of four values of a four-display Metex14 variant.
420  *
421  * The caller's 'info' parameter can be used to track the channel index,
422  * as long as the information is kept across calls to the 14-byte packet
423  * parse routine (which clears the 'info' container).
424  *
425  * Since analog values have further details in the 'analog' parameter,
426  * passing multiple values per parse routine call is problematic. So we
427  * prefer the approach of passing one value per call, which is most
428  * reliable and shall fit every similar device with multiple displays.
429  *
430  * The meters which use this parse routine send one 14-byte packet per
431  * display. Each packet has the regular Metex14 layout.
432  */
433 SR_PRIV int sr_metex14_4packets_parse(const uint8_t *buf, float *floatval,
434         struct sr_datafeed_analog *analog, void *info)
435 {
436         struct metex14_info *info_local;
437         size_t ch_idx;
438         const uint8_t *ch_buf;
439         int rc;
440
441         info_local = info;
442         ch_idx = info_local->ch_idx;
443         ch_buf = buf + ch_idx * METEX14_PACKET_SIZE;
444         rc = sr_metex14_parse(ch_buf, floatval, analog, info);
445         info_local->ch_idx = ch_idx + 1;
446         return rc;
447 }