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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 * Metex 14-bytes ASCII protocol parser.
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.
30 * It does _not_ work for all Metex DMMs, some use a quite different protocol.
37 #include "libsigrok.h"
38 #include "libsigrok-internal.h"
40 #define LOG_PREFIX "metex14"
42 /** Parse value from buf, byte 2-8. */
43 static int parse_value(const uint8_t *buf, struct metex14_info *info,
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];
56 /* Bytes 5-7: Over limit (various forms) */
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;
67 sr_spew("Over limit.");
73 if (!strcmp((const char *)&valstr, "READY") ||
74 !strcmp((const char *)&valstr, "FLOAT")) {
76 info->is_logic = TRUE;
77 } else if (!strcmp((const char *)&valstr, "Hi")) {
79 info->is_logic = TRUE;
80 } else if (!strcmp((const char *)&valstr, "Lo")) {
82 info->is_logic = TRUE;
87 /* Bytes 2-8: Sign, value (up to 5 digits) and decimal point */
88 sscanf((const char *)&valstr, "%f", result);
90 sr_spew("The display value is %f.", *result);
95 static void parse_flags(const char *buf, struct metex14_info *info)
101 /* Bytes 0-1: Measurement mode AC, DC */
102 info->is_ac = !strncmp(buf, "AC", 2);
103 info->is_dc = !strncmp(buf, "DC", 2);
105 /* Bytes 2-8: See parse_value(). */
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];
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"))
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;
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);
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.
168 /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */
171 static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
172 const struct metex14_info *info)
176 *floatval /= 1000000000000ULL;
178 *floatval /= 1000000000;
180 *floatval /= 1000000;
186 *floatval *= 1000000;
188 /* Measurement modes */
190 analog->mq = SR_MQ_VOLTAGE;
191 analog->unit = SR_UNIT_VOLT;
193 if (info->is_ampere) {
194 analog->mq = SR_MQ_CURRENT;
195 analog->unit = SR_UNIT_AMPERE;
198 analog->mq = SR_MQ_RESISTANCE;
199 analog->unit = SR_UNIT_OHM;
201 if (info->is_hertz) {
202 analog->mq = SR_MQ_FREQUENCY;
203 analog->unit = SR_UNIT_HERTZ;
205 if (info->is_farad) {
206 analog->mq = SR_MQ_CAPACITANCE;
207 analog->unit = SR_UNIT_FARAD;
209 if (info->is_celsius) {
210 analog->mq = SR_MQ_TEMPERATURE;
211 analog->unit = SR_UNIT_CELSIUS;
213 if (info->is_diode) {
214 analog->mq = SR_MQ_VOLTAGE;
215 analog->unit = SR_UNIT_VOLT;
218 analog->mq = SR_MQ_GAIN;
219 analog->unit = SR_UNIT_DECIBEL_VOLT;
222 analog->mq = SR_MQ_GAIN;
223 analog->unit = SR_UNIT_UNITLESS;
225 if (info->is_logic) {
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;
236 analog->mqflags |= SR_MQFLAG_DIODE;
239 static gboolean flags_valid(const struct metex14_info *info)
243 /* Does the packet have more than one multiplier? */
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;
252 sr_dbg("More than one multiplier detected in packet.");
256 /* Does the packet "measure" more than one type of value? */
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;
266 sr_dbg("More than one measurement type detected in packet.");
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.");
279 #ifdef HAVE_LIBSERIALPORT
280 SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial)
282 const uint8_t wbuf = 'D';
284 sr_spew("Requesting DMM packet.");
286 return (serial_write_nonblocking(serial, &wbuf, 1) == 1) ? SR_OK : SR_ERR;
290 SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf)
292 struct metex14_info info;
294 memset(&info, 0x00, sizeof(struct metex14_info));
295 parse_flags((const char *)buf, &info);
297 if (!flags_valid(&info))
307 * Parse a protocol packet.
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.
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.
318 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
319 * 'analog' variable contents are undefined and should not be used.
321 SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
322 struct sr_datafeed_analog *analog, void *info)
325 struct metex14_info *info_local;
327 info_local = (struct metex14_info *)info;
329 /* Don't print byte 13. That one contains the carriage return. */
330 sr_dbg("DMM packet: \"%.13s\"", buf);
332 memset(info_local, 0x00, sizeof(struct metex14_info));
334 if ((ret = parse_value(buf, info_local, floatval)) != SR_OK) {
335 sr_dbg("Error parsing value: %d.", ret);
339 parse_flags((const char *)buf, info_local);
340 handle_flags(analog, floatval, info_local);