From: Mathieu Pilato Date: Fri, 10 Mar 2023 08:27:35 +0000 (+0100) Subject: fluke-dmm: Extract digit count from response. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d2e3ce76f8f849f769b5f2903e8055223bfdce1b;p=libsigrok.git fluke-dmm: Extract digit count from response. The DMMs kindly formats its responses with the proper number of digits. Extract and use this value instead, to not use an arbitrary digit count. [ gsi: apply the same logic to Fluke 190 ] --- diff --git a/src/hardware/fluke-dmm/protocol.c b/src/hardware/fluke-dmm/protocol.c index 77200c3e..ecb581fd 100644 --- a/src/hardware/fluke-dmm/protocol.c +++ b/src/hardware/fluke-dmm/protocol.c @@ -26,6 +26,24 @@ #include "libsigrok-internal.h" #include "protocol.h" +static int count_digits(const char *str) { + int digits; + + while (*str && *str != ' ' && *str != ',' && *str != '.') + str++; + + digits = 0; + if (*str == '.') { + str++; + while (*str && *str != ' ' && *str != ',') { + str++; + digits++; + } + } + + return digits; +} + static void handle_qm_18x(const struct sr_dev_inst *sdi, char **tokens) { struct dev_context *devc; @@ -37,6 +55,7 @@ static void handle_qm_18x(const struct sr_dev_inst *sdi, char **tokens) float fvalue; char *e, *u; gboolean is_oor; + int digits; devc = sdi->priv; @@ -46,6 +65,7 @@ static void handle_qm_18x(const struct sr_dev_inst *sdi, char **tokens) if ((e = strstr(tokens[1], "Out of range"))) { is_oor = TRUE; fvalue = -1; + digits = 0; while (*e && *e != '.') e++; } else { @@ -61,12 +81,12 @@ static void handle_qm_18x(const struct sr_dev_inst *sdi, char **tokens) sr_dbg("Invalid float."); return; } + digits = count_digits(tokens[1]); } while (*e && *e == ' ') e++; - /* TODO: Use proper 'digits' value for this device (and its modes). */ - sr_analog_init(&analog, &encoding, &meaning, &spec, 2); + sr_analog_init(&analog, &encoding, &meaning, &spec, digits); analog.data = &fvalue; analog.meaning->channels = sdi->channels; analog.num_samples = 1; @@ -167,6 +187,7 @@ static void handle_qm_28x(const struct sr_dev_inst *sdi, char **tokens) struct sr_analog_meaning meaning; struct sr_analog_spec spec; float fvalue; + int digits; devc = sdi->priv; @@ -177,9 +198,9 @@ static void handle_qm_28x(const struct sr_dev_inst *sdi, char **tokens) sr_err("Invalid float '%s'.", tokens[0]); return; } + digits = count_digits(tokens[0]); - /* TODO: Use proper 'digits' value for this device (and its modes). */ - sr_analog_init(&analog, &encoding, &meaning, &spec, 2); + sr_analog_init(&analog, &encoding, &meaning, &spec, digits); analog.data = &fvalue; analog.meaning->channels = sdi->channels; analog.num_samples = 1; @@ -378,7 +399,9 @@ static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens) struct sr_analog_meaning meaning; struct sr_analog_spec spec; float fvalue; + int digits; + digits = 2; if (!strcmp(tokens[0], "9.9E+37")) { /* An invalid measurement shows up on the display as "OL", but * comes through like this. Since comparing 38-digit floats @@ -389,6 +412,7 @@ static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens) sr_err("Invalid float '%s'.", tokens[0]); return; } + digits = count_digits(tokens[0]); } devc = sdi->priv; @@ -405,8 +429,7 @@ static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens) fvalue = 1.0; } - /* TODO: Use proper 'digits' value for this device (and its modes). */ - sr_analog_init(&analog, &encoding, &meaning, &spec, 2); + sr_analog_init(&analog, &encoding, &meaning, &spec, digits); analog.meaning->channels = sdi->channels; analog.num_samples = 1; analog.data = &fvalue;