]> sigrok.org Git - libsigrok.git/blame - hardware/common/dmm/metex14.c
build: Portability fixes.
[libsigrok.git] / hardware / common / 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>
34#include <ctype.h>
35#include <math.h>
36#include <glib.h>
37#include "libsigrok.h"
38#include "libsigrok-internal.h"
39
3544f848 40#define LOG_PREFIX "metex14"
ac913e5c 41
e83437ae
MH
42/** Parse value from buf, byte 2-8. */
43static int parse_value(const uint8_t *buf, struct metex14_info *info,
44 float *result)
ac913e5c 45{
1a807c13
UH
46 int i, is_ol, cnt;
47 char valstr[7 + 1];
48
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];
ac913e5c
UH
54 }
55
56 /* Bytes 5-7: Over limit (various forms) */
57 is_ol = 0;
ee6cb5a4
UH
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;
3a8cad91
UH
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;
ac913e5c
UH
66 if (is_ol != 0) {
67 sr_spew("Over limit.");
68 *result = INFINITY;
69 return SR_OK;
70 }
71
e83437ae
MH
72 /* Logic functions */
73 if (!strcmp((const char *)&valstr, "READY") ||
74 !strcmp((const char *)&valstr, "FLOAT")) {
75 *result = INFINITY;
76 info->is_logic = TRUE;
77 } else if (!strcmp((const char *)&valstr, "Hi")) {
78 *result = 1.0;
79 info->is_logic = TRUE;
80 } else if (!strcmp((const char *)&valstr, "Lo")) {
81 *result = 0.0;
82 info->is_logic = TRUE;
83 }
84 if (info->is_logic)
85 return SR_OK;
86
1a807c13
UH
87 /* Bytes 2-8: Sign, value (up to 5 digits) and decimal point */
88 sscanf((const char *)&valstr, "%f", result);
ac913e5c 89
1a807c13 90 sr_spew("The display value is %f.", *result);
ac913e5c
UH
91
92 return SR_OK;
93}
94
95static void parse_flags(const char *buf, struct metex14_info *info)
96{
71f1302b
UH
97 int i, cnt;
98 char unit[4 + 1];
99 const char *u;
100
e83437ae
MH
101 /* Bytes 0-1: Measurement mode AC, DC */
102 info->is_ac = !strncmp(buf, "AC", 2);
103 info->is_dc = !strncmp(buf, "DC", 2);
ac913e5c 104
1a807c13 105 /* Bytes 2-8: See parse_value(). */
ac913e5c 106
1a807c13 107 /* Strip all spaces from bytes 9-12. */
71f1302b
UH
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];
112 }
1a807c13
UH
113
114 /* Bytes 9-12: Unit */
71f1302b 115 u = (const char *)&unit;
ee6cb5a4 116 if (!strcasecmp(u, "A"))
ac913e5c 117 info->is_ampere = TRUE;
ee6cb5a4 118 else if (!strcasecmp(u, "mA"))
ac913e5c 119 info->is_milli = info->is_ampere = TRUE;
ee6cb5a4 120 else if (!strcasecmp(u, "uA"))
2477fb95 121 info->is_micro = info->is_ampere = TRUE;
ee6cb5a4 122 else if (!strcasecmp(u, "V"))
ac913e5c 123 info->is_volt = TRUE;
ee6cb5a4 124 else if (!strcasecmp(u, "mV"))
ac913e5c 125 info->is_milli = info->is_volt = TRUE;
ee6cb5a4 126 else if (!strcasecmp(u, "Ohm"))
ac913e5c 127 info->is_ohm = TRUE;
ee6cb5a4 128 else if (!strcasecmp(u, "KOhm"))
ac913e5c 129 info->is_kilo = info->is_ohm = TRUE;
ee6cb5a4 130 else if (!strcasecmp(u, "MOhm"))
ac913e5c 131 info->is_mega = info->is_ohm = TRUE;
c02dc3e2
UH
132 else if (!strcasecmp(u, "pF"))
133 info->is_pico = info->is_farad = TRUE;
ee6cb5a4 134 else if (!strcasecmp(u, "nF"))
ac913e5c 135 info->is_nano = info->is_farad = TRUE;
ee6cb5a4 136 else if (!strcasecmp(u, "uF"))
ac913e5c 137 info->is_micro = info->is_farad = TRUE;
ee6cb5a4 138 else if (!strcasecmp(u, "KHz"))
ac913e5c 139 info->is_kilo = info->is_hertz = TRUE;
ee6cb5a4 140 else if (!strcasecmp(u, "C"))
ac913e5c 141 info->is_celsius = TRUE;
ee6cb5a4 142 else if (!strcasecmp(u, "DB"))
9871215c 143 info->is_decibel = TRUE;
ee6cb5a4 144 else if (!strcasecmp(u, ""))
9871215c 145 info->is_unitless = TRUE;
ac913e5c 146
e83437ae
MH
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);
161 /*
162 * Note:
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.
166 */
167
ac913e5c
UH
168 /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */
169}
170
171static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
172 const struct metex14_info *info)
173{
174 /* Factors */
c02dc3e2
UH
175 if (info->is_pico)
176 *floatval /= 1000000000000ULL;
ac913e5c
UH
177 if (info->is_nano)
178 *floatval /= 1000000000;
179 if (info->is_micro)
180 *floatval /= 1000000;
181 if (info->is_milli)
182 *floatval /= 1000;
183 if (info->is_kilo)
184 *floatval *= 1000;
185 if (info->is_mega)
186 *floatval *= 1000000;
187
188 /* Measurement modes */
189 if (info->is_volt) {
190 analog->mq = SR_MQ_VOLTAGE;
191 analog->unit = SR_UNIT_VOLT;
192 }
193 if (info->is_ampere) {
194 analog->mq = SR_MQ_CURRENT;
195 analog->unit = SR_UNIT_AMPERE;
196 }
197 if (info->is_ohm) {
198 analog->mq = SR_MQ_RESISTANCE;
199 analog->unit = SR_UNIT_OHM;
200 }
201 if (info->is_hertz) {
202 analog->mq = SR_MQ_FREQUENCY;
203 analog->unit = SR_UNIT_HERTZ;
204 }
205 if (info->is_farad) {
206 analog->mq = SR_MQ_CAPACITANCE;
207 analog->unit = SR_UNIT_FARAD;
208 }
209 if (info->is_celsius) {
210 analog->mq = SR_MQ_TEMPERATURE;
211 analog->unit = SR_UNIT_CELSIUS;
212 }
213 if (info->is_diode) {
214 analog->mq = SR_MQ_VOLTAGE;
215 analog->unit = SR_UNIT_VOLT;
216 }
9871215c
UH
217 if (info->is_gain) {
218 analog->mq = SR_MQ_GAIN;
219 analog->unit = SR_UNIT_DECIBEL_VOLT;
220 }
221 if (info->is_hfe) {
222 analog->mq = SR_MQ_GAIN;
223 analog->unit = SR_UNIT_UNITLESS;
224 }
e83437ae
MH
225 if (info->is_logic) {
226 analog->mq = SR_MQ_GAIN;
227 analog->unit = SR_UNIT_UNITLESS;
228 }
ac913e5c
UH
229
230 /* Measurement related flags */
231 if (info->is_ac)
232 analog->mqflags |= SR_MQFLAG_AC;
233 if (info->is_dc)
234 analog->mqflags |= SR_MQFLAG_DC;
a6ed50f4
UH
235 if (info->is_diode)
236 analog->mqflags |= SR_MQFLAG_DIODE;
ac913e5c
UH
237}
238
239static gboolean flags_valid(const struct metex14_info *info)
240{
241 int count;
242
243 /* Does the packet have more than one multiplier? */
244 count = 0;
c02dc3e2 245 count += (info->is_pico) ? 1 : 0;
ac913e5c
UH
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;
251 if (count > 1) {
ec5186f9 252 sr_dbg("More than one multiplier detected in packet.");
ac913e5c
UH
253 return FALSE;
254 }
255
256 /* Does the packet "measure" more than one type of value? */
257 count = 0;
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;
265 if (count > 1) {
ec5186f9 266 sr_dbg("More than one measurement type detected in packet.");
ac913e5c
UH
267 return FALSE;
268 }
269
270 /* Both AC and DC set? */
271 if (info->is_ac && info->is_dc) {
ec5186f9 272 sr_dbg("Both AC and DC flags detected in packet.");
ac913e5c
UH
273 return FALSE;
274 }
275
276 return TRUE;
277}
278
c4f2dfd0 279#ifdef HAVE_LIBSERIALPORT
e90cf076
UH
280SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial)
281{
282 const uint8_t wbuf = 'D';
283
284 sr_spew("Requesting DMM packet.");
285
286 return (serial_write(serial, &wbuf, 1) == 1) ? SR_OK : SR_ERR;
287}
c4f2dfd0 288#endif
e90cf076 289
ac913e5c
UH
290SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf)
291{
292 struct metex14_info info;
293
294 memset(&info, 0x00, sizeof(struct metex14_info));
295 parse_flags((const char *)buf, &info);
296
297 if (!flags_valid(&info))
298 return FALSE;
299
300 if (buf[13] != '\r')
301 return FALSE;
302
303 return TRUE;
304}
305
306/**
307 * Parse a protocol packet.
308 *
1fbab466 309 * @param buf Buffer containing the protocol packet. Must not be NULL.
ac913e5c 310 * @param floatval Pointer to a float variable. That variable will be modified
1fbab466 311 * in-place depending on the protocol packet. Must not be NULL.
ac913e5c
UH
312 * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
313 * filled with data according to the protocol packet.
1fbab466
UH
314 * Must not be NULL.
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.
ac913e5c
UH
317 *
318 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
319 * 'analog' variable contents are undefined and should not be used.
320 */
321SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
1fbab466 322 struct sr_datafeed_analog *analog, void *info)
ac913e5c
UH
323{
324 int ret;
1fbab466
UH
325 struct metex14_info *info_local;
326
327 info_local = (struct metex14_info *)info;
ac913e5c 328
e82d7dbc
AG
329 /* Don't print byte 13. That one contains the carriage return. */
330 sr_dbg("DMM packet: \"%.13s\"", buf);
331
e83437ae
MH
332 memset(info_local, 0x00, sizeof(struct metex14_info));
333
334 if ((ret = parse_value(buf, info_local, floatval)) != SR_OK) {
ec5186f9 335 sr_dbg("Error parsing value: %d.", ret);
ac913e5c
UH
336 return ret;
337 }
338
1fbab466
UH
339 parse_flags((const char *)buf, info_local);
340 handle_flags(analog, floatval, info_local);
ac913e5c
UH
341
342 return SR_OK;
343}