2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Metex 14-bytes ASCII protocol parser.
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.
27 * It does _not_ work for all Metex DMMs, some use a quite different protocol.
34 #include "libsigrok.h"
35 #include "libsigrok-internal.h"
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)
46 static int parse_value(const uint8_t *buf, float *result)
48 int i, sign, intval = 0, factor, decimal_point = 0, is_ol;
52 /* Byte 3: Sign (' ' or '-') */
55 } else if (buf[3] == '-') {
58 sr_err("Invalid sign byte: 0x%02x.", buf[3]);
62 /* Bytes 5-7: Over limit (various forms) */
64 is_ol += (!strncmp((char *)&buf[5], ".OL", 3)) ? 1 : 0;
65 is_ol += (!strncmp((char *)&buf[5], "O.L", 3)) ? 1 : 0;
66 is_ol += (!strncmp((char *)&buf[5], "OL.", 3)) ? 1 : 0;
67 is_ol += (!strncmp((char *)&buf[5], " OL", 3)) ? 1 : 0;
69 sr_spew("Over limit.");
74 /* Bytes 4-8: Value (up to 4 digits) and decimal point */
76 for (i = 0; i < 5; i++) {
78 /* Convert spaces to '0', so that we can parse them. */
83 } else if (isdigit(digit)) {
84 intval += (digit - '0') * factor;
87 sr_err("Invalid digit byte: 0x%02x.", digit);
92 floatval = (float)intval;
94 /* Decimal point position */
95 if (decimal_point == 0 || decimal_point == 4) {
96 /* TODO: Doesn't happen? */
97 } else if (decimal_point == 1) {
99 } else if (decimal_point == 2) {
101 } else if (decimal_point == 3) {
104 sr_err("Invalid decimal point position: %d.", decimal_point);
111 sr_spew("The display value is %f.", floatval);
118 static void parse_flags(const char *buf, struct metex14_info *info)
120 /* Bytes 0-1: Measurement mode */
121 /* Note: Protocol doesn't distinguish "resistance" from "beep" mode. */
122 info->is_ac = !strncmp(buf, "AC", 2);
123 info->is_dc = !strncmp(buf, "DC", 2);
124 info->is_resistance = !strncmp(buf, "OH", 2);
125 info->is_capacity = !strncmp(buf, "CA", 2);
126 info->is_temperature = !strncmp(buf, "TE", 2);
127 info->is_diode = !strncmp(buf, "DI", 2);
128 info->is_frequency = !strncmp(buf, "FR", 2);
129 info->is_gain = !strncmp(buf, "DB", 2);
130 info->is_hfe = !strncmp(buf, "HF", 2);
133 * Note: "DB" shows the logarithmic ratio of input voltage to a
134 * pre-stored (user-changeable) value in the DMM.
137 if (info->is_dc || info->is_ac)
138 info->is_volt = TRUE;
140 /* Byte 2: Always space (0x20). */
142 /* Bytes 3-8: See parse_value(). */
144 /* Bytes 9-12: Unit */
145 if (!strncmp(buf + 9, " A", 4))
146 info->is_ampere = TRUE;
147 else if (!strncmp(buf + 9, " mA", 4))
148 info->is_milli = info->is_ampere = TRUE;
149 else if (!strncmp(buf + 9, " uA", 4))
150 info->is_micro = info->is_ampere = TRUE;
151 else if (!strncmp(buf + 9, " V", 4))
152 info->is_volt = TRUE;
153 else if (!strncmp(buf + 9, " mV", 4))
154 info->is_milli = info->is_volt = TRUE;
155 else if (!strncmp(buf + 9, " Ohm", 4))
157 else if (!strncmp(buf + 9, "KOhm", 4))
158 info->is_kilo = info->is_ohm = TRUE;
159 else if (!strncmp(buf + 9, "MOhm", 4))
160 info->is_mega = info->is_ohm = TRUE;
161 else if (!strncmp(buf + 9, " nF", 4))
162 info->is_nano = info->is_farad = TRUE;
163 else if (!strncmp(buf + 9, " uF", 4))
164 info->is_micro = info->is_farad = TRUE;
165 else if (!strncmp(buf + 9, " KHz", 4))
166 info->is_kilo = info->is_hertz = TRUE;
167 else if (!strncmp(buf + 9, " C", 4))
168 info->is_celsius = TRUE;
169 else if (!strncmp(buf + 9, " DB", 4))
170 info->is_decibel = TRUE;
171 else if (!strncmp(buf + 9, " ", 4))
172 info->is_unitless = TRUE;
174 /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */
177 static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
178 const struct metex14_info *info)
182 *floatval /= 1000000000;
184 *floatval /= 1000000;
190 *floatval *= 1000000;
192 /* Measurement modes */
194 analog->mq = SR_MQ_VOLTAGE;
195 analog->unit = SR_UNIT_VOLT;
197 if (info->is_ampere) {
198 analog->mq = SR_MQ_CURRENT;
199 analog->unit = SR_UNIT_AMPERE;
202 analog->mq = SR_MQ_RESISTANCE;
203 analog->unit = SR_UNIT_OHM;
205 if (info->is_hertz) {
206 analog->mq = SR_MQ_FREQUENCY;
207 analog->unit = SR_UNIT_HERTZ;
209 if (info->is_farad) {
210 analog->mq = SR_MQ_CAPACITANCE;
211 analog->unit = SR_UNIT_FARAD;
213 if (info->is_celsius) {
214 analog->mq = SR_MQ_TEMPERATURE;
215 analog->unit = SR_UNIT_CELSIUS;
217 if (info->is_diode) {
218 analog->mq = SR_MQ_VOLTAGE;
219 analog->unit = SR_UNIT_VOLT;
222 analog->mq = SR_MQ_GAIN;
223 analog->unit = SR_UNIT_DECIBEL_VOLT;
226 analog->mq = SR_MQ_GAIN;
227 analog->unit = SR_UNIT_UNITLESS;
230 /* Measurement related flags */
232 analog->mqflags |= SR_MQFLAG_AC;
234 analog->mqflags |= SR_MQFLAG_DC;
237 static gboolean flags_valid(const struct metex14_info *info)
241 /* Does the packet have more than one multiplier? */
243 count += (info->is_nano) ? 1 : 0;
244 count += (info->is_micro) ? 1 : 0;
245 count += (info->is_milli) ? 1 : 0;
246 count += (info->is_kilo) ? 1 : 0;
247 count += (info->is_mega) ? 1 : 0;
249 sr_err("More than one multiplier detected in packet.");
253 /* Does the packet "measure" more than one type of value? */
255 count += (info->is_ac) ? 1 : 0;
256 count += (info->is_dc) ? 1 : 0;
257 count += (info->is_resistance) ? 1 : 0;
258 count += (info->is_capacity) ? 1 : 0;
259 count += (info->is_temperature) ? 1 : 0;
260 count += (info->is_diode) ? 1 : 0;
261 count += (info->is_frequency) ? 1 : 0;
263 sr_err("More than one measurement type detected in packet.");
267 /* Both AC and DC set? */
268 if (info->is_ac && info->is_dc) {
269 sr_err("Both AC and DC flags detected in packet.");
276 SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial)
278 const uint8_t wbuf = 'D';
280 sr_spew("Requesting DMM packet.");
282 return (serial_write(serial, &wbuf, 1) == 1) ? SR_OK : SR_ERR;
285 SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf)
287 struct metex14_info info;
289 memset(&info, 0x00, sizeof(struct metex14_info));
290 parse_flags((const char *)buf, &info);
292 if (!flags_valid(&info))
302 * Parse a protocol packet.
304 * @param buf Buffer containing the protocol packet. Must not be NULL.
305 * @param floatval Pointer to a float variable. That variable will be modified
306 * in-place depending on the protocol packet. Must not be NULL.
307 * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
308 * filled with data according to the protocol packet.
310 * @param info Pointer to a struct metex14_info. The struct will be filled
311 * with data according to the protocol packet. Must not be NULL.
313 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
314 * 'analog' variable contents are undefined and should not be used.
316 SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
317 struct sr_datafeed_analog *analog, void *info)
320 struct metex14_info *info_local;
322 info_local = (struct metex14_info *)info;
324 /* Don't print byte 13. That one contains the carriage return. */
325 sr_dbg("DMM packet: \"%.13s\"", buf);
327 if ((ret = parse_value(buf, floatval)) != SR_OK) {
328 sr_err("Error parsing value: %d.", ret);
332 memset(info_local, 0x00, sizeof(struct metex14_info));
333 parse_flags((const char *)buf, info_local);
334 handle_flags(analog, floatval, info_local);