From a8719c47cf2beaca582e1751739fb05d3e2bb834 Mon Sep 17 00:00:00 2001 From: schuma-gta02 <2666410+schuma-gta02@users.noreply.github.com> Date: Fri, 24 Nov 2023 16:56:25 +0100 Subject: [PATCH 1/5] Update vc96.c Fixes wrong detection and calculation. --- src/dmm/vc96.c | 63 +++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 34 deletions(-) diff --git a/src/dmm/vc96.c b/src/dmm/vc96.c index 8e51097ef..12395600d 100644 --- a/src/dmm/vc96.c +++ b/src/dmm/vc96.c @@ -2,7 +2,7 @@ * This file is part of the libsigrok project. * * Copyright (C) 2012-2013 Uwe Hermann - * Copyright (C) 2018 Matthias Schulz + * Copyright (C) 2018 Matthias Schulz * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,6 +20,7 @@ /* * Voltcraft 13-bytes ASCII protocol parser. + * derived from metex14.c * * Bytes 1-3 measuring mode, byte 4 '-' for negative, * bytes 5-9 value, bytes 10-11 unit, bytes 12-13 CRLF 0d 0a. @@ -37,13 +38,11 @@ #define LOG_PREFIX "vc96" /** Parse value from buf, byte 3-8. */ -static int parse_value(const uint8_t *buf, struct vc96_info *info, +static int parse_value(const uint8_t *buf, float *result, int *exponent) { int i, is_ol, cnt, dot_pos; - char valstr[8 + 1]; - - (void)info; + char valstr[6 + 1]; /* Strip all spaces from bytes 3-8. */ memset(&valstr, 0, 6 + 1); @@ -63,21 +62,19 @@ static int parse_value(const uint8_t *buf, struct vc96_info *info, is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-OL.")) ? 1 : 0; is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-OL")) ? 1 : 0; if (is_ol != 0) { - sr_spew("Over limit."); *result = INFINITY; return SR_OK; } - /* Bytes 3-10: Sign, value (up to 5 digits) and decimal point */ + /* Bytes 3-10: Sign, value (up to 5 digits including decimal point) */ sr_atof_ascii((const char *)&valstr, result); dot_pos = strcspn(valstr, "."); - if (dot_pos < cnt) + if (dot_pos < cnt){ *exponent = -(cnt - dot_pos - 1); - else + }else{ *exponent = 0; - - sr_spew("The display value is %f.", *result); + }; return SR_OK; } @@ -92,10 +89,13 @@ static void parse_flags(const char *buf, struct vc96_info *info) info->is_ac = !strncmp(buf, "AC", 2); info->is_dc = !strncmp(buf, "DC", 2); - /* Bytes 0-2: Measurement mode DIO, OHM */ - info->is_ohm = !strncmp(buf, "OHM", 3); - info->is_diode = !strncmp(buf, "DIO", 3); - info->is_hfe = !strncmp(buf, "hfe", 3); + /* Bytes 0-2: Measurement mode, except AC/DC */ + info->is_ohm = !strncmp(buf, "OHM", 3) || + (!strncmp(buf, " ", 3) && info->is_ohm); + info->is_diode = !strncmp(buf, "DIO", 3) || + (!strncmp(buf, " ", 3) && info->is_volt && info->is_milli); + info->is_hfe = !strncmp(buf, "hfe", 3) && !info->is_ampere && !info->is_volt && + !info->is_resistance && !info->is_diode; /* Bytes 3-8: See parse_value(). */ @@ -105,36 +105,28 @@ static void parse_flags(const char *buf, struct vc96_info *info) if (buf[9 + i] != ' ') unit[cnt++] = buf[9 + i]; } - sr_spew("Bytes 9..10 without spaces \"%.4s\".", unit); /* Bytes 9-10: Unit */ u = (const char *)&unit; - if (!g_ascii_strcasecmp(u, "A")) - info->is_ampere = TRUE; - else if (!g_ascii_strcasecmp(u, "mA")) + + if (!g_ascii_strcasecmp(u, "mA")) info->is_milli = info->is_ampere = TRUE; else if (!g_ascii_strcasecmp(u, "uA")) info->is_micro = info->is_ampere = TRUE; - else if (!g_ascii_strcasecmp(u, "V")) - info->is_volt = TRUE; + else if (!g_ascii_strcasecmp(u, "A")) + info->is_ampere = TRUE; else if (!g_ascii_strcasecmp(u, "mV")) info->is_milli = info->is_volt = TRUE; - else if (!g_ascii_strcasecmp(u, "K")) + else if (!g_ascii_strcasecmp(u, "V")) + info->is_volt = TRUE; + /* ignore case, VC96 sends wrong upper case "K" */ + else if (!g_ascii_strncasecmp(u, "K", 1)) info->is_kilo = TRUE; - else if (!g_ascii_strcasecmp(u, "M")) + else if (strchr(u, (int)'M')) info->is_mega = TRUE; else if (!g_ascii_strcasecmp(u, "")) info->is_unitless = TRUE; - /* Bytes 0-2: Measurement mode, except AC/DC */ - info->is_resistance = !strncmp(buf, "OHM", 3) || - (!strncmp(buf, " ", 3) && info->is_ohm); - info->is_diode = !strncmp(buf, "DIO", 3) || - (!strncmp(buf, " ", 3) && info->is_volt && info->is_milli); - info->is_hfe = !strncmp(buf, "hfe", 3) || - (!strncmp(buf, " ", 3) && !info->is_ampere && !info->is_volt && - !info->is_resistance && !info->is_diode); - /* * Note: * - Protocol doesn't distinguish "resistance" from "beep" mode. @@ -153,15 +145,18 @@ static void handle_flags(struct sr_datafeed_analog *analog, float *floatval, /* Factors */ factor = 0; - if (info->is_micro) + if (info->is_micro){ factor -= 6; + }; if (info->is_milli) factor -= 3; if (info->is_kilo) factor += 3; if (info->is_mega) factor += 6; + *floatval *= powf(10, factor); + *exponent += factor; /* Measurement modes */ if (info->is_volt) { @@ -273,7 +268,7 @@ SR_PRIV int sr_vc96_parse(const uint8_t *buf, float *floatval, memset(info_local, 0x00, sizeof(struct vc96_info)); - if ((ret = parse_value(buf, info_local, floatval, &exponent)) < 0) { + if ((ret = parse_value(buf, floatval, &exponent)) != SR_OK) { sr_dbg("Error parsing value: %d.", ret); return ret; } From 2f12079237aa845ec4fb285cd7877dcd8f99b5cf Mon Sep 17 00:00:00 2001 From: schuma-gta02 <2666410+schuma-gta02@users.noreply.github.com> Date: Sun, 26 Nov 2023 15:12:20 +0100 Subject: [PATCH 2/5] cleanup curly brackets, straightened the detection in parse_flags --- src/dmm/vc96.c | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/src/dmm/vc96.c b/src/dmm/vc96.c index 12395600d..019aa6624 100644 --- a/src/dmm/vc96.c +++ b/src/dmm/vc96.c @@ -70,15 +70,15 @@ static int parse_value(const uint8_t *buf, sr_atof_ascii((const char *)&valstr, result); dot_pos = strcspn(valstr, "."); - if (dot_pos < cnt){ + if (dot_pos < cnt) // *exponent = -(cnt - dot_pos - 1); - }else{ + else *exponent = 0; - }; - + return SR_OK; } + static void parse_flags(const char *buf, struct vc96_info *info) { int i, cnt; @@ -89,13 +89,14 @@ static void parse_flags(const char *buf, struct vc96_info *info) info->is_ac = !strncmp(buf, "AC", 2); info->is_dc = !strncmp(buf, "DC", 2); + /* + * Note: + * - Protocol doesn't distinguish "resistance" from "beep" mode. + */ /* Bytes 0-2: Measurement mode, except AC/DC */ - info->is_ohm = !strncmp(buf, "OHM", 3) || - (!strncmp(buf, " ", 3) && info->is_ohm); - info->is_diode = !strncmp(buf, "DIO", 3) || - (!strncmp(buf, " ", 3) && info->is_volt && info->is_milli); - info->is_hfe = !strncmp(buf, "hfe", 3) && !info->is_ampere && !info->is_volt && - !info->is_resistance && !info->is_diode; + info->is_ohm = !strncmp(buf, "OHM", 3) || !strncmp(buf, "BEP", 3); + info->is_diode = !strncmp(buf, "DIO", 3); + info->is_hfe = !strncmp(buf, "hfe", 3); /* Bytes 3-8: See parse_value(). */ @@ -109,6 +110,7 @@ static void parse_flags(const char *buf, struct vc96_info *info) /* Bytes 9-10: Unit */ u = (const char *)&unit; +/* the lineup of mA, uA, A is important, 1st detecting A ends the detection and m or u are lost */ if (!g_ascii_strcasecmp(u, "mA")) info->is_milli = info->is_ampere = TRUE; else if (!g_ascii_strcasecmp(u, "uA")) @@ -127,11 +129,6 @@ static void parse_flags(const char *buf, struct vc96_info *info) else if (!g_ascii_strcasecmp(u, "")) info->is_unitless = TRUE; - /* - * Note: - * - Protocol doesn't distinguish "resistance" from "beep" mode. - */ - /* Byte 12: Always '\r' (carriage return, 0x0d, 12) */ /* Byte 13: Always '\n' (carriage return, 0x0a, 13) */ } @@ -143,11 +140,10 @@ static void handle_flags(struct sr_datafeed_analog *analog, float *floatval, (void)exponent; - /* Factors */ + /* Factors representing the exponent of 123Exx */ factor = 0; - if (info->is_micro){ + if (info->is_micro) factor -= 6; - }; if (info->is_milli) factor -= 3; if (info->is_kilo) @@ -268,11 +264,6 @@ SR_PRIV int sr_vc96_parse(const uint8_t *buf, float *floatval, memset(info_local, 0x00, sizeof(struct vc96_info)); - if ((ret = parse_value(buf, floatval, &exponent)) != SR_OK) { - sr_dbg("Error parsing value: %d.", ret); - return ret; - } - parse_flags((const char *)buf, info_local); handle_flags(analog, floatval, &exponent, info_local); From db929c0b49035e45b3dca46666958668175ffdf6 Mon Sep 17 00:00:00 2001 From: daxb2plus <2666410+schuma-gta02@users.noreply.github.com> Date: Tue, 28 Nov 2023 18:32:28 +0100 Subject: [PATCH 3/5] removed wrong comment regarding flags for units, restored the call for parsing values --- src/dmm/vc96.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/dmm/vc96.c b/src/dmm/vc96.c index 019aa6624..c7272df70 100644 --- a/src/dmm/vc96.c +++ b/src/dmm/vc96.c @@ -70,7 +70,7 @@ static int parse_value(const uint8_t *buf, sr_atof_ascii((const char *)&valstr, result); dot_pos = strcspn(valstr, "."); - if (dot_pos < cnt) // + if (dot_pos < cnt) *exponent = -(cnt - dot_pos - 1); else *exponent = 0; @@ -110,7 +110,6 @@ static void parse_flags(const char *buf, struct vc96_info *info) /* Bytes 9-10: Unit */ u = (const char *)&unit; -/* the lineup of mA, uA, A is important, 1st detecting A ends the detection and m or u are lost */ if (!g_ascii_strcasecmp(u, "mA")) info->is_milli = info->is_ampere = TRUE; else if (!g_ascii_strcasecmp(u, "uA")) @@ -264,6 +263,11 @@ SR_PRIV int sr_vc96_parse(const uint8_t *buf, float *floatval, memset(info_local, 0x00, sizeof(struct vc96_info)); + if ((ret = parse_value(buf, floatval, &exponent)) != SR_OK) { + sr_dbg("Error parsing value: %d.", ret); + return ret; + } + parse_flags((const char *)buf, info_local); handle_flags(analog, floatval, &exponent, info_local); From 651138ab70085ac42ea79469748e5d5adf52fc40 Mon Sep 17 00:00:00 2001 From: daxb2plus <2666410+schuma-gta02@users.noreply.github.com> Date: Fri, 1 Dec 2023 09:35:58 +0100 Subject: [PATCH 4/5] removed useless debug output --- src/dmm/vc96.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/dmm/vc96.c b/src/dmm/vc96.c index c7272df70..cabb5fa53 100644 --- a/src/dmm/vc96.c +++ b/src/dmm/vc96.c @@ -263,11 +263,6 @@ SR_PRIV int sr_vc96_parse(const uint8_t *buf, float *floatval, memset(info_local, 0x00, sizeof(struct vc96_info)); - if ((ret = parse_value(buf, floatval, &exponent)) != SR_OK) { - sr_dbg("Error parsing value: %d.", ret); - return ret; - } - parse_flags((const char *)buf, info_local); handle_flags(analog, floatval, &exponent, info_local); From 1b376098943ff2736331ac183c4e5d1469e27629 Mon Sep 17 00:00:00 2001 From: schuma-gta02 <2666410+schuma-gta02@users.noreply.github.com> Date: Tue, 27 Aug 2024 15:28:01 +0200 Subject: [PATCH 5/5] Update vc96.c reinvented sr_spew messages --- src/dmm/vc96.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/dmm/vc96.c b/src/dmm/vc96.c index cabb5fa53..e4d53bbd7 100644 --- a/src/dmm/vc96.c +++ b/src/dmm/vc96.c @@ -63,6 +63,9 @@ static int parse_value(const uint8_t *buf, is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-OL")) ? 1 : 0; if (is_ol != 0) { *result = INFINITY; + + sr_spew("Over limit."); + return SR_OK; } @@ -74,6 +77,8 @@ static int parse_value(const uint8_t *buf, *exponent = -(cnt - dot_pos - 1); else *exponent = 0; + + sr_spew("The display value is %f.", *result); return SR_OK; } @@ -106,7 +111,8 @@ static void parse_flags(const char *buf, struct vc96_info *info) if (buf[9 + i] != ' ') unit[cnt++] = buf[9 + i]; } - + sr_spew("Bytes 9..10 without spaces \"%.4s\".", unit); + /* Bytes 9-10: Unit */ u = (const char *)&unit;