]> sigrok.org Git - libsigrok.git/blame - src/dmm/metex14.c
output/csv: use intermediate time_t var, silence compiler warning
[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
2ea1fdf1 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
ac913e5c
UH
18 */
19
e83437ae
MH
20/**
21 * @file
22 *
ac913e5c
UH
23 * Metex 14-bytes ASCII protocol parser.
24 *
25 * This should work for various multimeters which use this kind of protocol,
26 * even though there is some variation in which modes each DMM supports.
27 *
28 * It does _not_ work for all Metex DMMs, some use a quite different protocol.
29 */
30
6ec6c43b 31#include <config.h>
ac913e5c 32#include <string.h>
ba464a12 33#include <strings.h>
ac913e5c
UH
34#include <ctype.h>
35#include <math.h>
36#include <glib.h>
c1aae900 37#include <libsigrok/libsigrok.h>
ac913e5c
UH
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,
e677490e 44 float *result, int *exponent)
ac913e5c 45{
e677490e 46 int i, is_ol, cnt, dot_pos;
1a807c13
UH
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;
34577da6
UH
58 is_ol += (!g_ascii_strcasecmp((const char *)&valstr, ".OL")) ? 1 : 0;
59 is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "O.L")) ? 1 : 0;
60 is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "OL.")) ? 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, "-O.L")) ? 1 : 0;
64 is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-OL.")) ? 1 : 0;
65 is_ol += (!g_ascii_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 87 /* Bytes 2-8: Sign, value (up to 5 digits) and decimal point */
4f0463a0 88 sr_atof_ascii((const char *)&valstr, result);
ac913e5c 89
e677490e
AJ
90 dot_pos = strcspn(valstr, ".");
91 if (dot_pos < cnt)
92 *exponent = -(cnt - dot_pos - 1);
93 else
94 *exponent = 0;
95
1a807c13 96 sr_spew("The display value is %f.", *result);
ac913e5c
UH
97
98 return SR_OK;
99}
100
101static void parse_flags(const char *buf, struct metex14_info *info)
102{
71f1302b
UH
103 int i, cnt;
104 char unit[4 + 1];
105 const char *u;
106
e83437ae
MH
107 /* Bytes 0-1: Measurement mode AC, DC */
108 info->is_ac = !strncmp(buf, "AC", 2);
109 info->is_dc = !strncmp(buf, "DC", 2);
ac913e5c 110
1a807c13 111 /* Bytes 2-8: See parse_value(). */
ac913e5c 112
1a807c13 113 /* Strip all spaces from bytes 9-12. */
71f1302b
UH
114 memset(&unit, 0, 4 + 1);
115 for (i = 0, cnt = 0; i < 4; i++) {
116 if (buf[9 + i] != ' ')
117 unit[cnt++] = buf[9 + i];
118 }
1a807c13
UH
119
120 /* Bytes 9-12: Unit */
71f1302b 121 u = (const char *)&unit;
34577da6 122 if (!g_ascii_strcasecmp(u, "A"))
ac913e5c 123 info->is_ampere = TRUE;
34577da6 124 else if (!g_ascii_strcasecmp(u, "mA"))
ac913e5c 125 info->is_milli = info->is_ampere = TRUE;
34577da6 126 else if (!g_ascii_strcasecmp(u, "uA"))
2477fb95 127 info->is_micro = info->is_ampere = TRUE;
34577da6 128 else if (!g_ascii_strcasecmp(u, "V"))
ac913e5c 129 info->is_volt = TRUE;
34577da6 130 else if (!g_ascii_strcasecmp(u, "mV"))
ac913e5c 131 info->is_milli = info->is_volt = TRUE;
34577da6 132 else if (!g_ascii_strcasecmp(u, "Ohm"))
ac913e5c 133 info->is_ohm = TRUE;
34577da6 134 else if (!g_ascii_strcasecmp(u, "KOhm"))
ac913e5c 135 info->is_kilo = info->is_ohm = TRUE;
34577da6 136 else if (!g_ascii_strcasecmp(u, "MOhm"))
ac913e5c 137 info->is_mega = info->is_ohm = TRUE;
34577da6 138 else if (!g_ascii_strcasecmp(u, "pF"))
c02dc3e2 139 info->is_pico = info->is_farad = TRUE;
34577da6 140 else if (!g_ascii_strcasecmp(u, "nF"))
ac913e5c 141 info->is_nano = info->is_farad = TRUE;
34577da6 142 else if (!g_ascii_strcasecmp(u, "uF"))
ac913e5c 143 info->is_micro = info->is_farad = TRUE;
34577da6 144 else if (!g_ascii_strcasecmp(u, "KHz"))
ac913e5c 145 info->is_kilo = info->is_hertz = TRUE;
34577da6 146 else if (!g_ascii_strcasecmp(u, "C"))
ac913e5c 147 info->is_celsius = TRUE;
7fb4ff02
FS
148 else if (!g_ascii_strcasecmp(u, "F"))
149 info->is_fahrenheit = TRUE;
34577da6 150 else if (!g_ascii_strcasecmp(u, "DB"))
9871215c 151 info->is_decibel = TRUE;
7fb4ff02
FS
152 else if (!g_ascii_strcasecmp(u, "dBm"))
153 info->is_decibel_mw = TRUE;
154 else if (!g_ascii_strcasecmp(u, "W"))
155 info->is_watt = TRUE;
34577da6 156 else if (!g_ascii_strcasecmp(u, ""))
9871215c 157 info->is_unitless = TRUE;
ac913e5c 158
e83437ae 159 /* Bytes 0-1: Measurement mode, except AC/DC */
d9251a2c 160 info->is_resistance = !strncmp(buf, "OH", 2) ||
e83437ae 161 (!strncmp(buf, " ", 2) && info->is_ohm);
d9251a2c 162 info->is_capacity = !strncmp(buf, "CA", 2) ||
e83437ae 163 (!strncmp(buf, " ", 2) && info->is_farad);
7fb4ff02
FS
164 info->is_temperature = !strncmp(buf, "TE", 2) ||
165 info->is_celsius || info->is_fahrenheit;
d9251a2c 166 info->is_diode = !strncmp(buf, "DI", 2) ||
e83437ae 167 (!strncmp(buf, " ", 2) && info->is_volt && info->is_milli);
d9251a2c 168 info->is_frequency = !strncmp(buf, "FR", 2) ||
e83437ae 169 (!strncmp(buf, " ", 2) && info->is_hertz);
7fb4ff02
FS
170 info->is_gain = !strncmp(buf, "DB", 2) && info->is_decibel;
171 info->is_power = (!strncmp(buf, "dB", 2) && info->is_decibel_mw) ||
172 ((!strncmp(buf, "WT", 2) && info->is_watt));
fd8dc1db 173 info->is_power_factor = !strncmp(buf, "CO", 2) && info->is_unitless;
d9251a2c 174 info->is_hfe = !strncmp(buf, "HF", 2) ||
cd97e39d
FS
175 (!strncmp(buf, " ", 2) && !info->is_ampere &&!info->is_volt &&
176 !info->is_resistance && !info->is_capacity && !info->is_frequency &&
177 !info->is_temperature && !info->is_power && !info->is_power_factor &&
178 !info->is_gain && !info->is_logic && !info->is_diode);
7fb4ff02
FS
179 info->is_min = !strncmp(buf, "MN", 2);
180 info->is_max = !strncmp(buf, "MX", 2);
181 info->is_avg = !strncmp(buf, "AG", 2);
182
e83437ae
MH
183 /*
184 * Note:
185 * - Protocol doesn't distinguish "resistance" from "beep" mode.
186 * - "DB" shows the logarithmic ratio of input voltage to a
187 * pre-stored (user-changeable) value in the DMM.
188 */
189
ac913e5c
UH
190 /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */
191}
192
b02bb45f 193static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
e677490e 194 int *exponent, const struct metex14_info *info)
ac913e5c 195{
7fb4ff02
FS
196 int factor;
197
ac913e5c 198 /* Factors */
7fb4ff02 199 factor = 0;
c02dc3e2 200 if (info->is_pico)
e677490e 201 factor -= 12;
ac913e5c 202 if (info->is_nano)
e677490e 203 factor -= 9;
ac913e5c 204 if (info->is_micro)
e677490e 205 factor -= 6;
ac913e5c 206 if (info->is_milli)
e677490e 207 factor -= 3;
ac913e5c 208 if (info->is_kilo)
e677490e 209 factor += 3;
ac913e5c 210 if (info->is_mega)
e677490e
AJ
211 factor += 6;
212 *floatval *= powf(10, factor);
762356c3 213 *exponent += factor;
ac913e5c
UH
214
215 /* Measurement modes */
216 if (info->is_volt) {
b02bb45f
UH
217 analog->meaning->mq = SR_MQ_VOLTAGE;
218 analog->meaning->unit = SR_UNIT_VOLT;
ac913e5c
UH
219 }
220 if (info->is_ampere) {
b02bb45f
UH
221 analog->meaning->mq = SR_MQ_CURRENT;
222 analog->meaning->unit = SR_UNIT_AMPERE;
ac913e5c
UH
223 }
224 if (info->is_ohm) {
b02bb45f
UH
225 analog->meaning->mq = SR_MQ_RESISTANCE;
226 analog->meaning->unit = SR_UNIT_OHM;
ac913e5c
UH
227 }
228 if (info->is_hertz) {
b02bb45f
UH
229 analog->meaning->mq = SR_MQ_FREQUENCY;
230 analog->meaning->unit = SR_UNIT_HERTZ;
ac913e5c
UH
231 }
232 if (info->is_farad) {
b02bb45f
UH
233 analog->meaning->mq = SR_MQ_CAPACITANCE;
234 analog->meaning->unit = SR_UNIT_FARAD;
ac913e5c 235 }
7fb4ff02 236 if (info->is_temperature) {
b02bb45f 237 analog->meaning->mq = SR_MQ_TEMPERATURE;
7fb4ff02
FS
238 if (info->is_celsius)
239 analog->meaning->unit = SR_UNIT_CELSIUS;
240 else if (info->is_fahrenheit)
241 analog->meaning->unit = SR_UNIT_FAHRENHEIT;
242 else
243 analog->meaning->unit = SR_UNIT_UNITLESS;
ac913e5c
UH
244 }
245 if (info->is_diode) {
b02bb45f
UH
246 analog->meaning->mq = SR_MQ_VOLTAGE;
247 analog->meaning->unit = SR_UNIT_VOLT;
ac913e5c 248 }
7fb4ff02
FS
249 if (info->is_power) {
250 analog->meaning->mq = SR_MQ_POWER;
251 if (info->is_decibel_mw)
252 analog->meaning->unit = SR_UNIT_DECIBEL_MW;
253 else if (info->is_watt)
254 analog->meaning->unit = SR_UNIT_WATT;
255 else
256 analog->meaning->unit = SR_UNIT_UNITLESS;
257 }
fd8dc1db
FS
258 if (info->is_power_factor) {
259 analog->meaning->mq = SR_MQ_POWER_FACTOR;
260 analog->meaning->unit = SR_UNIT_UNITLESS;
261 }
9871215c 262 if (info->is_gain) {
b02bb45f
UH
263 analog->meaning->mq = SR_MQ_GAIN;
264 analog->meaning->unit = SR_UNIT_DECIBEL_VOLT;
9871215c
UH
265 }
266 if (info->is_hfe) {
b02bb45f
UH
267 analog->meaning->mq = SR_MQ_GAIN;
268 analog->meaning->unit = SR_UNIT_UNITLESS;
9871215c 269 }
e83437ae 270 if (info->is_logic) {
b02bb45f
UH
271 analog->meaning->mq = SR_MQ_GAIN;
272 analog->meaning->unit = SR_UNIT_UNITLESS;
e83437ae 273 }
ac913e5c
UH
274
275 /* Measurement related flags */
276 if (info->is_ac)
b02bb45f 277 analog->meaning->mqflags |= SR_MQFLAG_AC;
ac913e5c 278 if (info->is_dc)
b02bb45f 279 analog->meaning->mqflags |= SR_MQFLAG_DC;
a6ed50f4 280 if (info->is_diode)
64aa214a 281 analog->meaning->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
7fb4ff02
FS
282 if (info->is_min)
283 analog->meaning->mqflags |= SR_MQFLAG_MIN;
284 if (info->is_max)
285 analog->meaning->mqflags |= SR_MQFLAG_MAX;
286 if (info->is_avg)
287 analog->meaning->mqflags |= SR_MQFLAG_AVG;
ac913e5c
UH
288}
289
290static gboolean flags_valid(const struct metex14_info *info)
291{
292 int count;
293
294 /* Does the packet have more than one multiplier? */
295 count = 0;
c02dc3e2 296 count += (info->is_pico) ? 1 : 0;
ac913e5c
UH
297 count += (info->is_nano) ? 1 : 0;
298 count += (info->is_micro) ? 1 : 0;
299 count += (info->is_milli) ? 1 : 0;
300 count += (info->is_kilo) ? 1 : 0;
301 count += (info->is_mega) ? 1 : 0;
302 if (count > 1) {
ec5186f9 303 sr_dbg("More than one multiplier detected in packet.");
ac913e5c
UH
304 return FALSE;
305 }
306
307 /* Does the packet "measure" more than one type of value? */
308 count = 0;
309 count += (info->is_ac) ? 1 : 0;
310 count += (info->is_dc) ? 1 : 0;
311 count += (info->is_resistance) ? 1 : 0;
312 count += (info->is_capacity) ? 1 : 0;
313 count += (info->is_temperature) ? 1 : 0;
314 count += (info->is_diode) ? 1 : 0;
315 count += (info->is_frequency) ? 1 : 0;
316 if (count > 1) {
ec5186f9 317 sr_dbg("More than one measurement type detected in packet.");
ac913e5c
UH
318 return FALSE;
319 }
320
321 /* Both AC and DC set? */
322 if (info->is_ac && info->is_dc) {
ec5186f9 323 sr_dbg("Both AC and DC flags detected in packet.");
ac913e5c
UH
324 return FALSE;
325 }
326
327 return TRUE;
328}
329
1df81f4b 330#ifdef HAVE_SERIAL_COMM
e90cf076
UH
331SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial)
332{
333 const uint8_t wbuf = 'D';
d7838e48
GS
334 size_t wrlen;
335 int ret;
e90cf076
UH
336
337 sr_spew("Requesting DMM packet.");
338
d7838e48
GS
339 wrlen = sizeof(wbuf);
340 ret = serial_write_blocking(serial, &wbuf, wrlen, 0);
341 if (ret < 0)
342 return ret;
343 if ((size_t)ret != wrlen)
344 return SR_ERR_IO;
345
346 return SR_OK;
e90cf076 347}
c4f2dfd0 348#endif
e90cf076 349
ac913e5c
UH
350SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf)
351{
352 struct metex14_info info;
353
354 memset(&info, 0x00, sizeof(struct metex14_info));
355 parse_flags((const char *)buf, &info);
356
357 if (!flags_valid(&info))
358 return FALSE;
359
360 if (buf[13] != '\r')
361 return FALSE;
362
363 return TRUE;
364}
365
556a926d
GS
366SR_PRIV gboolean sr_metex14_4packets_valid(const uint8_t *buf)
367{
368 struct metex14_info info;
369 size_t ch_idx;
370 const uint8_t *ch_buf;
371
372 ch_buf = buf;
373 for (ch_idx = 0; ch_idx < 4; ch_idx++) {
374 if (ch_buf[13] != '\r')
375 return FALSE;
376 memset(&info, 0x00, sizeof(info));
377 parse_flags((const char *)ch_buf, &info);
378 if (!flags_valid(&info))
379 return FALSE;
380 ch_buf += METEX14_PACKET_SIZE;
381 }
382 return TRUE;
383}
384
ac913e5c
UH
385/**
386 * Parse a protocol packet.
387 *
1fbab466 388 * @param buf Buffer containing the protocol packet. Must not be NULL.
ac913e5c 389 * @param floatval Pointer to a float variable. That variable will be modified
1fbab466 390 * in-place depending on the protocol packet. Must not be NULL.
b02bb45f 391 * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
ac913e5c 392 * filled with data according to the protocol packet.
1fbab466
UH
393 * Must not be NULL.
394 * @param info Pointer to a struct metex14_info. The struct will be filled
395 * with data according to the protocol packet. Must not be NULL.
ac913e5c
UH
396 *
397 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
398 * 'analog' variable contents are undefined and should not be used.
399 */
400SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
b02bb45f 401 struct sr_datafeed_analog *analog, void *info)
ac913e5c 402{
e677490e 403 int ret, exponent = 0;
1fbab466
UH
404 struct metex14_info *info_local;
405
94b1d506 406 info_local = info;
ac913e5c 407
e82d7dbc
AG
408 /* Don't print byte 13. That one contains the carriage return. */
409 sr_dbg("DMM packet: \"%.13s\"", buf);
410
e83437ae
MH
411 memset(info_local, 0x00, sizeof(struct metex14_info));
412
e677490e 413 if ((ret = parse_value(buf, info_local, floatval, &exponent)) != SR_OK) {
ec5186f9 414 sr_dbg("Error parsing value: %d.", ret);
ac913e5c
UH
415 return ret;
416 }
417
1fbab466 418 parse_flags((const char *)buf, info_local);
e677490e
AJ
419 handle_flags(analog, floatval, &exponent, info_local);
420
d9251a2c 421 analog->encoding->digits = -exponent;
e677490e 422 analog->spec->spec_digits = -exponent;
ac913e5c
UH
423
424 return SR_OK;
425}
556a926d
GS
426
427/**
428 * Parse one out of four values of a four-display Metex14 variant.
429 *
430 * The caller's 'info' parameter can be used to track the channel index,
431 * as long as the information is kept across calls to the 14-byte packet
432 * parse routine (which clears the 'info' container).
433 *
434 * Since analog values have further details in the 'analog' parameter,
435 * passing multiple values per parse routine call is problematic. So we
436 * prefer the approach of passing one value per call, which is most
437 * reliable and shall fit every similar device with multiple displays.
438 *
439 * The meters which use this parse routine send one 14-byte packet per
440 * display. Each packet has the regular Metex14 layout.
441 */
442SR_PRIV int sr_metex14_4packets_parse(const uint8_t *buf, float *floatval,
443 struct sr_datafeed_analog *analog, void *info)
444{
445 struct metex14_info *info_local;
446 size_t ch_idx;
447 const uint8_t *ch_buf;
448 int rc;
449
450 info_local = info;
451 ch_idx = info_local->ch_idx;
452 ch_buf = buf + ch_idx * METEX14_PACKET_SIZE;
453 rc = sr_metex14_parse(ch_buf, floatval, analog, info);
454 info_local->ch_idx = ch_idx + 1;
455 return rc;
456}