2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2016 Gerhard Sittig <gerhard.sittig@gmx.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 * Parser for the ASYC-II 16-bytes ASCII protocol (PRINT).
24 * This should work for various multimeters which use this kind of protocol,
25 * even though there is some variation in which modes each DMM supports.
27 * This implementation was developed for and tested with a Metrix MX56C,
28 * which is identical to the BK Precision 5390.
29 * See the metex14.c implementation for the 14-byte protocol used by many
40 #include <libsigrok/libsigrok.h>
41 #include "libsigrok-internal.h"
43 #define LOG_PREFIX "asycii"
46 * Parse sign and value from text buffer, byte 0-6.
48 * The first character always is the sign (' ' or '-'). Subsequent
49 * positions contain digits, dots, or spaces. Overflow / open inputs
50 * are signalled with several magic literals that cannot get interpreted
51 * as a number, either with 'X' characters in them, or with several
54 * @param[in] buf The text buffer received from the DMM.
55 * @param[out] result A floating point number value.
56 * @param[out] exponent Augments the number value.
58 static int parse_value(const char *buf, struct asycii_info *info,
59 float *result, int *exponent)
63 int i, cnt, is_ol, dot_pos;
66 * Strip all spaces from bytes 0-6. By copying all
67 * non-space characters into a buffer.
70 for (i = 0; i < 7; i++) {
72 valstr[cnt++] = buf[i];
76 sr_spew("%s(), number buffer [%s]", __func__, valp);
79 * Check for "over limit" conditions. Depending on the meter's
80 * selected mode, the textual representation might differ. Test
81 * all known variations.
84 is_ol += (g_ascii_strcasecmp(valp, ".OL") == 0) ? 1 : 0;
85 is_ol += (g_ascii_strcasecmp(valp, "O.L") == 0) ? 1 : 0;
86 is_ol += (g_ascii_strcasecmp(valp, "-.OL") == 0) ? 1 : 0;
87 is_ol += (g_ascii_strcasecmp(valp, "-O.L") == 0) ? 1 : 0;
88 is_ol += (g_ascii_strncasecmp(valp, "X", 1) == 0) ? 1 : 0;
89 is_ol += (g_ascii_strncasecmp(valp, "-X", 2) == 0) ? 1 : 0;
91 sr_spew("%s(), over limit", __func__);
97 * Convert the textual number representation to a float, and
100 if (sr_atof_ascii(valp, result) != SR_OK) {
101 info->is_invalid = TRUE;
102 sr_spew("%s(), cannot convert number", __func__);
105 dot_pos = strcspn(valstr, ".");
107 *exponent = -(cnt - dot_pos - 1);
110 sr_spew("%s(), display value is %f", __func__, *result);
115 * Parse unit and flags from text buffer, bytes 7-14.
117 * The unit and flags optionally follow the number value for the
118 * measurement. Either can be present or absent. The scale factor
119 * is always at index 7. The unit starts at index 8, and is of
120 * variable length. Flags immediately follow the unit. The remainder
121 * of the text buffer is SPACE padded, and terminated with CR.
123 * Notice the implementation detail of case @b sensitive comparison.
124 * Since the measurement unit and flags are directly adjacent and are
125 * not separated from each other, case insensitive comparison would
126 * yield wrong results. It's essential that e.g. "Vac" gets split into
127 * the "V" unit and the "ac" flag, not into "VA" and the unknown "c"
130 * Notice, too, that order of comparison matters in the absence of
131 * separators or fixed positions and with ambiguous text (note that we do
132 * partial comparison). It's essential to e.g. correctly tell "VA" from "V".
134 * @param[in] buf The text buffer received from the DMM.
135 * @param[out] info Broken down measurement details.
137 static void parse_flags(const char *buf, struct asycii_info *info)
143 /* Bytes 0-6: Number value, see parse_value(). */
145 /* Strip spaces from bytes 7-14. */
147 for (i = 7; i < 15; i++) {
149 unit[cnt++] = buf[i];
153 sr_spew("%s(): unit/flag buffer [%s]", __func__, u);
155 /* Scan for the scale factor. */
156 sr_spew("%s(): scanning factor, buffer [%s]", __func__, u);
159 info->is_pico = TRUE;
160 } else if (*u == 'n') {
162 info->is_nano = TRUE;
163 } else if (*u == 'u') {
165 info->is_micro = TRUE;
166 } else if (*u == 'm') {
168 info->is_milli = TRUE;
169 } else if (*u == ' ') {
171 } else if (*u == 'k') {
173 info->is_kilo = TRUE;
174 } else if (*u == 'M') {
176 info->is_mega = TRUE;
178 /* Absence of a scale modifier is perfectly fine. */
181 /* Scan for the measurement unit. */
182 sr_spew("%s(): scanning unit, buffer [%s]", __func__, u);
183 if (g_str_has_prefix(u, "A")) {
185 info->is_ampere = TRUE;
186 } else if (g_str_has_prefix(u, "VA")) {
188 info->is_volt_ampere = TRUE;
189 } else if (g_str_has_prefix(u, "V")) {
191 info->is_volt = TRUE;
192 } else if (g_str_has_prefix(u, "ohm")) {
194 info->is_resistance = TRUE;
196 } else if (g_str_has_prefix(u, "F")) {
198 info->is_capacitance = TRUE;
199 info->is_farad = TRUE;
200 } else if (g_str_has_prefix(u, "dB")) {
202 info->is_gain = TRUE;
203 info->is_decibel = TRUE;
204 } else if (g_str_has_prefix(u, "Hz")) {
206 info->is_frequency = TRUE;
207 info->is_hertz = TRUE;
208 } else if (g_str_has_prefix(u, "%")) {
210 info->is_duty_cycle = TRUE;
213 info->is_duty_pos = TRUE;
214 } else if (*u == '-') {
216 info->is_duty_neg = TRUE;
218 info->is_invalid = TRUE;
220 } else if (g_str_has_prefix(u, "Cnt")) {
222 info->is_pulse_count = TRUE;
223 info->is_unitless = TRUE;
226 info->is_count_pos = TRUE;
227 } else if (*u == '-') {
229 info->is_count_neg = TRUE;
231 info->is_invalid = TRUE;
233 } else if (g_str_has_prefix(u, "s")) {
235 info->is_pulse_width = TRUE;
236 info->is_seconds = TRUE;
239 info->is_period_pos = TRUE;
240 } else if (*u == '-') {
242 info->is_period_neg = TRUE;
244 info->is_invalid = TRUE;
247 /* Not strictly illegal, but unknown/unsupported. */
248 sr_spew("%s(): measurement: unsupported", __func__);
249 info->is_invalid = TRUE;
252 /* Scan for additional flags. */
253 sr_spew("%s(): scanning flags, buffer [%s]", __func__, u);
254 if (g_str_has_prefix(u, "ac+dc")) {
255 u += strlen("ac+dc");
256 info->is_ac_and_dc = TRUE;
257 } else if (g_str_has_prefix(u, "ac")) {
260 } else if (g_str_has_prefix(u, "dc")) {
263 } else if (g_str_has_prefix(u, "d")) {
265 info->is_diode = TRUE;
266 } else if (g_str_has_prefix(u, "Pk")) {
270 info->is_peak_max = TRUE;
271 } else if (*u == '-') {
273 info->is_peak_min = TRUE;
275 info->is_invalid = TRUE;
277 } else if (*u == '\0') {
278 /* Absence of any flags is acceptable. */
280 /* Presence of unknown flags is not. */
281 sr_dbg("%s(): flag: unknown", __func__);
282 info->is_invalid = TRUE;
285 /* Was all of the received data consumed? */
287 info->is_invalid = TRUE;
291 * - The protocol does not distinguish between "resistance"
293 * - Relative measurement and hold cannot get recognized.
298 * Fill in a datafeed from previously parsed measurement details.
300 * @param[out] analog The datafeed which gets filled in.
301 * @param[in] floatval The number value of the measurement.
302 * @param[in] exponent Augments the number value.
303 * @param[in] info Scale and unit and other attributes.
305 static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
306 int *exponent, const struct asycii_info *info)
323 *floatval *= powf(10, factor);
326 /* Measurement modes */
328 analog->meaning->mq = SR_MQ_VOLTAGE;
329 analog->meaning->unit = SR_UNIT_VOLT;
331 if (info->is_volt_ampere) {
332 analog->meaning->mq = SR_MQ_POWER;
333 analog->meaning->unit = SR_UNIT_VOLT_AMPERE;
335 if (info->is_ampere) {
336 analog->meaning->mq = SR_MQ_CURRENT;
337 analog->meaning->unit = SR_UNIT_AMPERE;
339 if (info->is_frequency) {
340 analog->meaning->mq = SR_MQ_FREQUENCY;
341 analog->meaning->unit = SR_UNIT_HERTZ;
343 if (info->is_duty_cycle) {
344 analog->meaning->mq = SR_MQ_DUTY_CYCLE;
345 analog->meaning->unit = SR_UNIT_PERCENTAGE;
347 if (info->is_pulse_width) {
348 analog->meaning->mq = SR_MQ_PULSE_WIDTH;
349 analog->meaning->unit = SR_UNIT_SECOND;
351 if (info->is_pulse_count) {
352 analog->meaning->mq = SR_MQ_COUNT;
353 analog->meaning->unit = SR_UNIT_UNITLESS;
355 if (info->is_resistance) {
356 analog->meaning->mq = SR_MQ_RESISTANCE;
357 analog->meaning->unit = SR_UNIT_OHM;
359 if (info->is_capacitance) {
360 analog->meaning->mq = SR_MQ_CAPACITANCE;
361 analog->meaning->unit = SR_UNIT_FARAD;
363 if (info->is_diode) {
364 analog->meaning->mq = SR_MQ_VOLTAGE;
365 analog->meaning->unit = SR_UNIT_VOLT;
368 analog->meaning->mq = SR_MQ_GAIN;
369 analog->meaning->unit = SR_UNIT_DECIBEL_VOLT;
372 /* Measurement related flags */
374 analog->meaning->mqflags |= SR_MQFLAG_AC;
375 if (info->is_ac_and_dc)
376 analog->meaning->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_DC;
378 analog->meaning->mqflags |= SR_MQFLAG_DC;
380 analog->meaning->mqflags |= SR_MQFLAG_DIODE;
381 if (info->is_peak_max)
382 analog->meaning->mqflags |= SR_MQFLAG_MAX;
383 if (info->is_peak_min)
384 analog->meaning->mqflags |= SR_MQFLAG_MIN;
388 * Check measurement details for consistency and validity.
390 * @param[in] info The previously parsed details.
392 * @return TRUE on success, FALSE otherwise.
394 static gboolean flags_valid(const struct asycii_info *info)
398 /* Have previous checks raised the "invalid" flag? */
399 if (info->is_invalid) {
400 sr_dbg("Previous parse raised \"invalid\" flag for packet.");
404 /* Does the packet have more than one multiplier? */
406 count += (info->is_pico) ? 1 : 0;
407 count += (info->is_nano) ? 1 : 0;
408 count += (info->is_micro) ? 1 : 0;
409 count += (info->is_milli) ? 1 : 0;
410 count += (info->is_kilo) ? 1 : 0;
411 count += (info->is_mega) ? 1 : 0;
413 sr_dbg("More than one multiplier detected in packet.");
417 /* Does the packet "measure" more than one type of value? */
419 count += (info->is_volt || info->is_diode) ? 1 : 0;
420 count += (info->is_volt_ampere) ? 1 : 0;
421 count += (info->is_ampere) ? 1 : 0;
422 count += (info->is_gain) ? 1 : 0;
423 count += (info->is_resistance) ? 1 : 0;
424 count += (info->is_capacitance) ? 1 : 0;
425 count += (info->is_frequency) ? 1 : 0;
426 count += (info->is_duty_cycle) ? 1 : 0;
427 count += (info->is_pulse_width) ? 1 : 0;
428 count += (info->is_pulse_count) ? 1 : 0;
430 sr_dbg("More than one measurement type detected in packet.");
434 /* Are conflicting AC and DC flags set? */
436 count += (info->is_ac) ? 1 : 0;
437 count += (info->is_ac_and_dc) ? 1 : 0;
438 count += (info->is_dc) ? 1 : 0;
440 sr_dbg("Conflicting AC and DC flags detected in packet.");
447 #ifdef HAVE_LIBSERIALPORT
449 * Arrange for the reception of another measurement from the DMM.
451 * This routine is unused in the currently implemented PRINT mode,
452 * where the meter sends measurements to the PC in pre-set intervals,
453 * without the PC's intervention.
455 * @param[in] serial The serial connection.
459 SR_PRIV int sr_asycii_packet_request(struct sr_serial_dev_inst *serial)
462 * The current implementation assumes that the user pressed
463 * the PRINT button. It has no support to query/trigger packet
464 * reception from the meter.
467 sr_spew("NOT requesting DMM packet.");
473 * Check whether a received frame is valid.
475 * @param[in] buf The text buffer with received data.
477 * @return TRUE upon success, FALSE otherwise.
479 SR_PRIV gboolean sr_asycii_packet_valid(const uint8_t *buf)
481 struct asycii_info info;
483 /* First check whether we are in sync with the packet stream. */
487 /* Have the received packet content parsed. */
488 memset(&info, 0x00, sizeof(info));
489 parse_flags((const char *)buf, &info);
490 if (!flags_valid(&info))
497 * Parse a protocol packet.
499 * @param[in] buf Buffer containing the protocol packet. Must not be NULL.
500 * @param[out] floatval Pointer to a float variable. That variable will
501 * be modified in-place depending on the protocol packet.
503 * @param[out] analog Pointer to a struct sr_datafeed_analog. The struct
504 * will be filled with data according to the protocol packet.
506 * @param[out] info Pointer to a struct asycii_info. The struct will be
507 * filled with data according to the protocol packet. Must
510 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
511 * 'analog' variable contents are undefined and should not
514 SR_PRIV int sr_asycii_parse(const uint8_t *buf, float *floatval,
515 struct sr_datafeed_analog *analog, void *info)
518 struct asycii_info *info_local;
520 info_local = (struct asycii_info *)info;
522 /* Don't print byte 15. That one contains the carriage return. */
523 sr_dbg("DMM packet: \"%.15s\"", buf);
525 memset(info_local, 0x00, sizeof(*info_local));
528 ret = parse_value((const char *)buf, info_local, floatval, &exponent);
530 sr_dbg("Error parsing value: %d.", ret);
534 parse_flags((const char *)buf, info_local);
535 handle_flags(analog, floatval, &exponent, info_local);
537 analog->encoding->digits = -exponent;
538 analog->spec->spec_digits = -exponent;