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