]> sigrok.org Git - libsigrok.git/blame - src/dmm/metex14.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / dmm / metex14.c
CommitLineData
ac913e5c 1/*
50985c20 2 * This file is part of the libsigrok project.
ac913e5c 3 *
71f1302b 4 * Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
ac913e5c
UH
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
e83437ae
MH
21/**
22 * @file
23 *
ac913e5c
UH
24 * Metex 14-bytes ASCII protocol parser.
25 *
e83437ae 26 * @internal
ac913e5c
UH
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.
29 *
30 * It does _not_ work for all Metex DMMs, some use a quite different protocol.
31 */
32
33#include <string.h>
ba464a12 34#include <strings.h>
ac913e5c
UH
35#include <ctype.h>
36#include <math.h>
37#include <glib.h>
c1aae900 38#include <libsigrok/libsigrok.h>
ac913e5c
UH
39#include "libsigrok-internal.h"
40
3544f848 41#define LOG_PREFIX "metex14"
ac913e5c 42
e83437ae
MH
43/** Parse value from buf, byte 2-8. */
44static int parse_value(const uint8_t *buf, struct metex14_info *info,
45 float *result)
ac913e5c 46{
1a807c13
UH
47 int i, is_ol, cnt;
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];
ac913e5c
UH
55 }
56
57 /* Bytes 5-7: Over limit (various forms) */
58 is_ol = 0;
ee6cb5a4
UH
59 is_ol += (!strcasecmp((const char *)&valstr, ".OL")) ? 1 : 0;
60 is_ol += (!strcasecmp((const char *)&valstr, "O.L")) ? 1 : 0;
61 is_ol += (!strcasecmp((const char *)&valstr, "OL.")) ? 1 : 0;
62 is_ol += (!strcasecmp((const char *)&valstr, "OL")) ? 1 : 0;
3a8cad91
UH
63 is_ol += (!strcasecmp((const char *)&valstr, "-.OL")) ? 1 : 0;
64 is_ol += (!strcasecmp((const char *)&valstr, "-O.L")) ? 1 : 0;
65 is_ol += (!strcasecmp((const char *)&valstr, "-OL.")) ? 1 : 0;
66 is_ol += (!strcasecmp((const char *)&valstr, "-OL")) ? 1 : 0;
ac913e5c
UH
67 if (is_ol != 0) {
68 sr_spew("Over limit.");
69 *result = INFINITY;
70 return SR_OK;
71 }
72
e83437ae
MH
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
1a807c13
UH
88 /* Bytes 2-8: Sign, value (up to 5 digits) and decimal point */
89 sscanf((const char *)&valstr, "%f", result);
ac913e5c 90
1a807c13 91 sr_spew("The display value is %f.", *result);
ac913e5c
UH
92
93 return SR_OK;
94}
95
96static void parse_flags(const char *buf, struct metex14_info *info)
97{
71f1302b
UH
98 int i, cnt;
99 char unit[4 + 1];
100 const char *u;
101
e83437ae
MH
102 /* Bytes 0-1: Measurement mode AC, DC */
103 info->is_ac = !strncmp(buf, "AC", 2);
104 info->is_dc = !strncmp(buf, "DC", 2);
ac913e5c 105
1a807c13 106 /* Bytes 2-8: See parse_value(). */
ac913e5c 107
1a807c13 108 /* Strip all spaces from bytes 9-12. */
71f1302b
UH
109 memset(&unit, 0, 4 + 1);
110 for (i = 0, cnt = 0; i < 4; i++) {
111 if (buf[9 + i] != ' ')
112 unit[cnt++] = buf[9 + i];
113 }
1a807c13
UH
114
115 /* Bytes 9-12: Unit */
71f1302b 116 u = (const char *)&unit;
ee6cb5a4 117 if (!strcasecmp(u, "A"))
ac913e5c 118 info->is_ampere = TRUE;
ee6cb5a4 119 else if (!strcasecmp(u, "mA"))
ac913e5c 120 info->is_milli = info->is_ampere = TRUE;
ee6cb5a4 121 else if (!strcasecmp(u, "uA"))
2477fb95 122 info->is_micro = info->is_ampere = TRUE;
ee6cb5a4 123 else if (!strcasecmp(u, "V"))
ac913e5c 124 info->is_volt = TRUE;
ee6cb5a4 125 else if (!strcasecmp(u, "mV"))
ac913e5c 126 info->is_milli = info->is_volt = TRUE;
ee6cb5a4 127 else if (!strcasecmp(u, "Ohm"))
ac913e5c 128 info->is_ohm = TRUE;
ee6cb5a4 129 else if (!strcasecmp(u, "KOhm"))
ac913e5c 130 info->is_kilo = info->is_ohm = TRUE;
ee6cb5a4 131 else if (!strcasecmp(u, "MOhm"))
ac913e5c 132 info->is_mega = info->is_ohm = TRUE;
c02dc3e2
UH
133 else if (!strcasecmp(u, "pF"))
134 info->is_pico = info->is_farad = TRUE;
ee6cb5a4 135 else if (!strcasecmp(u, "nF"))
ac913e5c 136 info->is_nano = info->is_farad = TRUE;
ee6cb5a4 137 else if (!strcasecmp(u, "uF"))
ac913e5c 138 info->is_micro = info->is_farad = TRUE;
ee6cb5a4 139 else if (!strcasecmp(u, "KHz"))
ac913e5c 140 info->is_kilo = info->is_hertz = TRUE;
ee6cb5a4 141 else if (!strcasecmp(u, "C"))
ac913e5c 142 info->is_celsius = TRUE;
ee6cb5a4 143 else if (!strcasecmp(u, "DB"))
9871215c 144 info->is_decibel = TRUE;
ee6cb5a4 145 else if (!strcasecmp(u, ""))
9871215c 146 info->is_unitless = TRUE;
ac913e5c 147
e83437ae
MH
148 /* Bytes 0-1: Measurement mode, except AC/DC */
149 info->is_resistance = !strncmp(buf, "OH", 2) ||
150 (!strncmp(buf, " ", 2) && info->is_ohm);
151 info->is_capacity = !strncmp(buf, "CA", 2) ||
152 (!strncmp(buf, " ", 2) && info->is_farad);
153 info->is_temperature = !strncmp(buf, "TE", 2);
154 info->is_diode = !strncmp(buf, "DI", 2) ||
155 (!strncmp(buf, " ", 2) && info->is_volt && info->is_milli);
156 info->is_frequency = !strncmp(buf, "FR", 2) ||
157 (!strncmp(buf, " ", 2) && info->is_hertz);
158 info->is_gain = !strncmp(buf, "DB", 2);
159 info->is_hfe = !strncmp(buf, "HF", 2) ||
160 (!strncmp(buf, " ", 2) && !info->is_volt && !info->is_ohm &&
161 !info->is_logic && !info->is_farad && !info->is_hertz);
162 /*
163 * Note:
164 * - Protocol doesn't distinguish "resistance" from "beep" mode.
165 * - "DB" shows the logarithmic ratio of input voltage to a
166 * pre-stored (user-changeable) value in the DMM.
167 */
168
ac913e5c
UH
169 /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */
170}
171
172static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
173 const struct metex14_info *info)
174{
175 /* Factors */
c02dc3e2
UH
176 if (info->is_pico)
177 *floatval /= 1000000000000ULL;
ac913e5c
UH
178 if (info->is_nano)
179 *floatval /= 1000000000;
180 if (info->is_micro)
181 *floatval /= 1000000;
182 if (info->is_milli)
183 *floatval /= 1000;
184 if (info->is_kilo)
185 *floatval *= 1000;
186 if (info->is_mega)
187 *floatval *= 1000000;
188
189 /* Measurement modes */
190 if (info->is_volt) {
191 analog->mq = SR_MQ_VOLTAGE;
192 analog->unit = SR_UNIT_VOLT;
193 }
194 if (info->is_ampere) {
195 analog->mq = SR_MQ_CURRENT;
196 analog->unit = SR_UNIT_AMPERE;
197 }
198 if (info->is_ohm) {
199 analog->mq = SR_MQ_RESISTANCE;
200 analog->unit = SR_UNIT_OHM;
201 }
202 if (info->is_hertz) {
203 analog->mq = SR_MQ_FREQUENCY;
204 analog->unit = SR_UNIT_HERTZ;
205 }
206 if (info->is_farad) {
207 analog->mq = SR_MQ_CAPACITANCE;
208 analog->unit = SR_UNIT_FARAD;
209 }
210 if (info->is_celsius) {
211 analog->mq = SR_MQ_TEMPERATURE;
212 analog->unit = SR_UNIT_CELSIUS;
213 }
214 if (info->is_diode) {
215 analog->mq = SR_MQ_VOLTAGE;
216 analog->unit = SR_UNIT_VOLT;
217 }
9871215c
UH
218 if (info->is_gain) {
219 analog->mq = SR_MQ_GAIN;
220 analog->unit = SR_UNIT_DECIBEL_VOLT;
221 }
222 if (info->is_hfe) {
223 analog->mq = SR_MQ_GAIN;
224 analog->unit = SR_UNIT_UNITLESS;
225 }
e83437ae
MH
226 if (info->is_logic) {
227 analog->mq = SR_MQ_GAIN;
228 analog->unit = SR_UNIT_UNITLESS;
229 }
ac913e5c
UH
230
231 /* Measurement related flags */
232 if (info->is_ac)
233 analog->mqflags |= SR_MQFLAG_AC;
234 if (info->is_dc)
235 analog->mqflags |= SR_MQFLAG_DC;
a6ed50f4
UH
236 if (info->is_diode)
237 analog->mqflags |= SR_MQFLAG_DIODE;
ac913e5c
UH
238}
239
240static gboolean flags_valid(const struct metex14_info *info)
241{
242 int count;
243
244 /* Does the packet have more than one multiplier? */
245 count = 0;
c02dc3e2 246 count += (info->is_pico) ? 1 : 0;
ac913e5c
UH
247 count += (info->is_nano) ? 1 : 0;
248 count += (info->is_micro) ? 1 : 0;
249 count += (info->is_milli) ? 1 : 0;
250 count += (info->is_kilo) ? 1 : 0;
251 count += (info->is_mega) ? 1 : 0;
252 if (count > 1) {
ec5186f9 253 sr_dbg("More than one multiplier detected in packet.");
ac913e5c
UH
254 return FALSE;
255 }
256
257 /* Does the packet "measure" more than one type of value? */
258 count = 0;
259 count += (info->is_ac) ? 1 : 0;
260 count += (info->is_dc) ? 1 : 0;
261 count += (info->is_resistance) ? 1 : 0;
262 count += (info->is_capacity) ? 1 : 0;
263 count += (info->is_temperature) ? 1 : 0;
264 count += (info->is_diode) ? 1 : 0;
265 count += (info->is_frequency) ? 1 : 0;
266 if (count > 1) {
ec5186f9 267 sr_dbg("More than one measurement type detected in packet.");
ac913e5c
UH
268 return FALSE;
269 }
270
271 /* Both AC and DC set? */
272 if (info->is_ac && info->is_dc) {
ec5186f9 273 sr_dbg("Both AC and DC flags detected in packet.");
ac913e5c
UH
274 return FALSE;
275 }
276
277 return TRUE;
278}
279
c4f2dfd0 280#ifdef HAVE_LIBSERIALPORT
e90cf076
UH
281SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial)
282{
283 const uint8_t wbuf = 'D';
284
285 sr_spew("Requesting DMM packet.");
286
4277ac34 287 return (serial_write_nonblocking(serial, &wbuf, 1) == 1) ? SR_OK : SR_ERR;
e90cf076 288}
c4f2dfd0 289#endif
e90cf076 290
ac913e5c
UH
291SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf)
292{
293 struct metex14_info info;
294
295 memset(&info, 0x00, sizeof(struct metex14_info));
296 parse_flags((const char *)buf, &info);
297
298 if (!flags_valid(&info))
299 return FALSE;
300
301 if (buf[13] != '\r')
302 return FALSE;
303
304 return TRUE;
305}
306
307/**
308 * Parse a protocol packet.
309 *
1fbab466 310 * @param buf Buffer containing the protocol packet. Must not be NULL.
ac913e5c 311 * @param floatval Pointer to a float variable. That variable will be modified
1fbab466 312 * in-place depending on the protocol packet. Must not be NULL.
ac913e5c
UH
313 * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
314 * filled with data according to the protocol packet.
1fbab466
UH
315 * Must not be NULL.
316 * @param info Pointer to a struct metex14_info. The struct will be filled
317 * with data according to the protocol packet. Must not be NULL.
ac913e5c
UH
318 *
319 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
320 * 'analog' variable contents are undefined and should not be used.
321 */
322SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
1fbab466 323 struct sr_datafeed_analog *analog, void *info)
ac913e5c
UH
324{
325 int ret;
1fbab466
UH
326 struct metex14_info *info_local;
327
328 info_local = (struct metex14_info *)info;
ac913e5c 329
e82d7dbc
AG
330 /* Don't print byte 13. That one contains the carriage return. */
331 sr_dbg("DMM packet: \"%.13s\"", buf);
332
e83437ae
MH
333 memset(info_local, 0x00, sizeof(struct metex14_info));
334
335 if ((ret = parse_value(buf, info_local, floatval)) != SR_OK) {
ec5186f9 336 sr_dbg("Error parsing value: %d.", ret);
ac913e5c
UH
337 return ret;
338 }
339
1fbab466
UH
340 parse_flags((const char *)buf, info_local);
341 handle_flags(analog, floatval, info_local);
ac913e5c
UH
342
343 return SR_OK;
344}