2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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/>.
23 * Metex 14-bytes ASCII protocol parser.
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.
29 * It does _not_ work for all Metex DMMs, some use a quite different protocol.
38 #include <libsigrok/libsigrok.h>
39 #include "libsigrok-internal.h"
41 #define LOG_PREFIX "metex14"
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)
47 int i, is_ol, cnt, dot_pos;
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];
57 /* Bytes 5-7: Over limit (various forms) */
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;
68 sr_spew("Over limit.");
74 if (!strcmp((const char *)&valstr, "READY") ||
75 !strcmp((const char *)&valstr, "FLOAT")) {
77 info->is_logic = TRUE;
78 } else if (!strcmp((const char *)&valstr, "Hi")) {
80 info->is_logic = TRUE;
81 } else if (!strcmp((const char *)&valstr, "Lo")) {
83 info->is_logic = TRUE;
88 /* Bytes 2-8: Sign, value (up to 5 digits) and decimal point */
89 sscanf((const char *)&valstr, "%f", result);
91 dot_pos = strcspn(valstr, ".");
93 *exponent = -(cnt - dot_pos - 1);
97 sr_spew("The display value is %f.", *result);
102 static void parse_flags(const char *buf, struct metex14_info *info)
108 /* Bytes 0-1: Measurement mode AC, DC */
109 info->is_ac = !strncmp(buf, "AC", 2);
110 info->is_dc = !strncmp(buf, "DC", 2);
112 /* Bytes 2-8: See parse_value(). */
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];
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"))
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, "DB"))
150 info->is_decibel = TRUE;
151 else if (!g_ascii_strcasecmp(u, ""))
152 info->is_unitless = TRUE;
154 /* Bytes 0-1: Measurement mode, except AC/DC */
155 info->is_resistance = !strncmp(buf, "OH", 2) ||
156 (!strncmp(buf, " ", 2) && info->is_ohm);
157 info->is_capacity = !strncmp(buf, "CA", 2) ||
158 (!strncmp(buf, " ", 2) && info->is_farad);
159 info->is_temperature = !strncmp(buf, "TE", 2);
160 info->is_diode = !strncmp(buf, "DI", 2) ||
161 (!strncmp(buf, " ", 2) && info->is_volt && info->is_milli);
162 info->is_frequency = !strncmp(buf, "FR", 2) ||
163 (!strncmp(buf, " ", 2) && info->is_hertz);
164 info->is_gain = !strncmp(buf, "DB", 2);
165 info->is_hfe = !strncmp(buf, "HF", 2) ||
166 (!strncmp(buf, " ", 2) && !info->is_volt && !info->is_ohm &&
167 !info->is_logic && !info->is_farad && !info->is_hertz);
170 * - Protocol doesn't distinguish "resistance" from "beep" mode.
171 * - "DB" shows the logarithmic ratio of input voltage to a
172 * pre-stored (user-changeable) value in the DMM.
175 /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */
178 static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
179 int *exponent, const struct metex14_info *info)
195 *floatval *= powf(10, factor);
198 /* Measurement modes */
200 analog->meaning->mq = SR_MQ_VOLTAGE;
201 analog->meaning->unit = SR_UNIT_VOLT;
203 if (info->is_ampere) {
204 analog->meaning->mq = SR_MQ_CURRENT;
205 analog->meaning->unit = SR_UNIT_AMPERE;
208 analog->meaning->mq = SR_MQ_RESISTANCE;
209 analog->meaning->unit = SR_UNIT_OHM;
211 if (info->is_hertz) {
212 analog->meaning->mq = SR_MQ_FREQUENCY;
213 analog->meaning->unit = SR_UNIT_HERTZ;
215 if (info->is_farad) {
216 analog->meaning->mq = SR_MQ_CAPACITANCE;
217 analog->meaning->unit = SR_UNIT_FARAD;
219 if (info->is_celsius) {
220 analog->meaning->mq = SR_MQ_TEMPERATURE;
221 analog->meaning->unit = SR_UNIT_CELSIUS;
223 if (info->is_diode) {
224 analog->meaning->mq = SR_MQ_VOLTAGE;
225 analog->meaning->unit = SR_UNIT_VOLT;
228 analog->meaning->mq = SR_MQ_GAIN;
229 analog->meaning->unit = SR_UNIT_DECIBEL_VOLT;
232 analog->meaning->mq = SR_MQ_GAIN;
233 analog->meaning->unit = SR_UNIT_UNITLESS;
235 if (info->is_logic) {
236 analog->meaning->mq = SR_MQ_GAIN;
237 analog->meaning->unit = SR_UNIT_UNITLESS;
240 /* Measurement related flags */
242 analog->meaning->mqflags |= SR_MQFLAG_AC;
244 analog->meaning->mqflags |= SR_MQFLAG_DC;
246 analog->meaning->mqflags |= SR_MQFLAG_DIODE;
249 static gboolean flags_valid(const struct metex14_info *info)
253 /* Does the packet have more than one multiplier? */
255 count += (info->is_pico) ? 1 : 0;
256 count += (info->is_nano) ? 1 : 0;
257 count += (info->is_micro) ? 1 : 0;
258 count += (info->is_milli) ? 1 : 0;
259 count += (info->is_kilo) ? 1 : 0;
260 count += (info->is_mega) ? 1 : 0;
262 sr_dbg("More than one multiplier detected in packet.");
266 /* Does the packet "measure" more than one type of value? */
268 count += (info->is_ac) ? 1 : 0;
269 count += (info->is_dc) ? 1 : 0;
270 count += (info->is_resistance) ? 1 : 0;
271 count += (info->is_capacity) ? 1 : 0;
272 count += (info->is_temperature) ? 1 : 0;
273 count += (info->is_diode) ? 1 : 0;
274 count += (info->is_frequency) ? 1 : 0;
276 sr_dbg("More than one measurement type detected in packet.");
280 /* Both AC and DC set? */
281 if (info->is_ac && info->is_dc) {
282 sr_dbg("Both AC and DC flags detected in packet.");
289 #ifdef HAVE_LIBSERIALPORT
290 SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial)
292 const uint8_t wbuf = 'D';
294 sr_spew("Requesting DMM packet.");
296 return serial_write_blocking(serial, &wbuf, 1, 0);
300 SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf)
302 struct metex14_info info;
304 memset(&info, 0x00, sizeof(struct metex14_info));
305 parse_flags((const char *)buf, &info);
307 if (!flags_valid(&info))
317 * Parse a protocol packet.
319 * @param buf Buffer containing the protocol packet. Must not be NULL.
320 * @param floatval Pointer to a float variable. That variable will be modified
321 * in-place depending on the protocol packet. Must not be NULL.
322 * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
323 * filled with data according to the protocol packet.
325 * @param info Pointer to a struct metex14_info. The struct will be filled
326 * with data according to the protocol packet. Must not be NULL.
328 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
329 * 'analog' variable contents are undefined and should not be used.
331 SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
332 struct sr_datafeed_analog *analog, void *info)
334 int ret, exponent = 0;
335 struct metex14_info *info_local;
337 info_local = (struct metex14_info *)info;
339 /* Don't print byte 13. That one contains the carriage return. */
340 sr_dbg("DMM packet: \"%.13s\"", buf);
342 memset(info_local, 0x00, sizeof(struct metex14_info));
344 if ((ret = parse_value(buf, info_local, floatval, &exponent)) != SR_OK) {
345 sr_dbg("Error parsing value: %d.", ret);
349 parse_flags((const char *)buf, info_local);
350 handle_flags(analog, floatval, &exponent, info_local);
352 analog->encoding->digits = -exponent;
353 analog->spec->spec_digits = -exponent;