X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fdmm%2Fut71x.c;h=9a7d2eab93b0776bd56e1a75fcaf9fea86887cb1;hb=45fcaf2ccad796b4bc4434d81b1d0e23f11a8eae;hp=b17e058355182e068d4e4200354eae32003b22dd;hpb=6ec6c43b4738dbc7091f4a49a4ec80ea6102cb52;p=libsigrok.git diff --git a/src/dmm/ut71x.c b/src/dmm/ut71x.c index b17e0583..9a7d2eab 100644 --- a/src/dmm/ut71x.c +++ b/src/dmm/ut71x.c @@ -14,8 +14,7 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * along with this program; if not, see . */ /* @@ -35,28 +34,28 @@ #define LOG_PREFIX "ut71x" /* - * Factors for the respective measurement mode (0 means "invalid"). + * Exponents for the respective measurement mode. * * The Conrad/Voltcraft protocol descriptions have a typo (they suggest * index 0 for the 10A range (which is incorrect, it's range 1). */ -static const float factors[16][8] = { - {1e-5, 0, 0, 0, 0, 0, 0, 0 }, /* AC mV */ - {0, 1e-4, 1e-3, 1e-2, 1e-1, 0, 0, 0 }, /* DC V */ - {0, 1e-4, 1e-3, 1e-2, 1e-1, 0, 0, 0 }, /* AC V */ - {1e-5, 0, 0, 0, 0, 0, 0, 0 }, /* DC mV */ - {0, 1e-1, 1, 1e1, 1e2, 1e3, 1e4, 0 }, /* Resistance */ - {0, 1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6}, /* Capacitance */ - {1e-1, 0, 0, 0, 0, 0, 0, 0 }, /* Temp (C) */ - {1e-8, 1e-7, 0, 0, 0, 0, 0, 0 }, /* uA */ - {1e-6, 1e-5, 0, 0, 0, 0, 0, 0 }, /* mA */ - {0, 1e-3, 0, 0, 0, 0, 0, 0 }, /* 10A */ - {1e-1, 0, 0, 0, 0, 0, 0, 0 }, /* Continuity */ - {1e-4, 0, 0, 0, 0, 0, 0, 0 }, /* Diode */ - {1e-3, 1e-2, 1e-1, 1, 1e1, 1e2, 1e3, 1e4 }, /* Frequency */ - {1e-1, 0, 0, 0, 0, 0, 0, 0 }, /* Temp (F) */ - {0, 0, 0, 1, 0, 0, 0, 0 }, /* Power */ - {1e-2, 0, 0, 0, 0, 0, 0, 0 }, /* Loop current */ +static const int exponents[16][8] = { + { -5, 0, 0, 0, 0, 0, 0, 0 }, /* AC mV */ + { 0, -4, -3, -2, -1, 0, 0, 0 }, /* DC V */ + { 0, -4, -3, -2, -1, 0, 0, 0 }, /* AC V */ + { -5, 0, 0, 0, 0, 0, 0, 0 }, /* DC mV */ + { 0, -2, -1, 0, 1, 2, 3, 0 }, /* Resistance */ + { 0, -12, -11, -10, -9, -8, -7, -6 }, /* Capacitance */ + { -1, 0, 0, 0, 0, 0, 0, 0 }, /* Temp (C) */ + { -8, -7, 0, 0, 0, 0, 0, 0 }, /* uA */ + { -6, -5, 0, 0, 0, 0, 0, 0 }, /* mA */ + { 0, -3, 0, 0, 0, 0, 0, 0 }, /* 10A */ + { -1, 0, 0, 0, 0, 0, 0, 0 }, /* Continuity */ + { -4, 0, 0, 0, 0, 0, 0, 0 }, /* Diode */ + { -3, -2, -1, 0, 1, 2, 3, 4 }, /* Frequency */ + { -1, 0, 0, 0, 0, 0, 0, 0 }, /* Temp (F) */ + { 0, 0, 0, 0, 0, 0, 0, 0 }, /* Power */ + { -2, 0, 0, 0, 0, 0, 0, 0 }, /* Loop current */ }; static int parse_value(const uint8_t *buf, struct ut71x_info *info, float *result) @@ -82,9 +81,21 @@ static int parse_value(const uint8_t *buf, struct ut71x_info *info, float *resul buf[0], buf[1], buf[2], buf[3], buf[4]); return SR_ERR; } + for (i = 0, intval = 0; i < num_digits; i++) intval = 10 * intval + (buf[i] - '0'); + /* + * For measurements that only have 4000 instead of 40000 counts + * (resistance, continuity) we have to use an additional factor of 10. + * + * This seems to vary between DMMs. E.g. the Voltcraft VC920 and VC940 + * have 4000 counts for resistance, whereas the Tenma 72-9380A, + * 72-7730 and 72-7732 have 40000 counts for resistance. + */ + if (num_digits == 4) + intval *= 10; + /* Apply sign. */ intval *= info->is_sign ? -1 : 1; @@ -95,10 +106,9 @@ static int parse_value(const uint8_t *buf, struct ut71x_info *info, float *resul return SR_OK; } -static int parse_range(const uint8_t *buf, float *floatval) +static int parse_range(const uint8_t *buf, float *floatval, int *exponent) { int idx, mode; - float factor = 0; idx = buf[5] - '0'; if (idx < 0 || idx > 7) { @@ -114,15 +124,11 @@ static int parse_range(const uint8_t *buf, float *floatval) sr_spew("mode/idx = %d/%d", mode, idx); - factor = factors[mode][idx]; - if (factor == 0) { - sr_dbg("Invalid factor for range byte: 0x%02x.", buf[5]); - return SR_ERR; - } + *exponent = exponents[mode][idx]; - /* Apply respective factor (mode-dependent) on the value. */ - *floatval *= factor; - sr_dbg("Applying factor %f, new value is %f.", factor, *floatval); + /* Apply respective exponent (mode-dependent) on the value. */ + *floatval *= powf(10, *exponent); + sr_dbg("Applying exponent %d, new value is %g.", *exponent, *floatval); return SR_OK; } @@ -217,68 +223,68 @@ static void handle_flags(struct sr_datafeed_analog *analog, { /* Measurement modes */ if (info->is_voltage) { - analog->mq = SR_MQ_VOLTAGE; - analog->unit = SR_UNIT_VOLT; + analog->meaning->mq = SR_MQ_VOLTAGE; + analog->meaning->unit = SR_UNIT_VOLT; } if (info->is_current) { - analog->mq = SR_MQ_CURRENT; - analog->unit = SR_UNIT_AMPERE; + analog->meaning->mq = SR_MQ_CURRENT; + analog->meaning->unit = SR_UNIT_AMPERE; } if (info->is_resistance) { - analog->mq = SR_MQ_RESISTANCE; - analog->unit = SR_UNIT_OHM; + analog->meaning->mq = SR_MQ_RESISTANCE; + analog->meaning->unit = SR_UNIT_OHM; } if (info->is_frequency) { - analog->mq = SR_MQ_FREQUENCY; - analog->unit = SR_UNIT_HERTZ; + analog->meaning->mq = SR_MQ_FREQUENCY; + analog->meaning->unit = SR_UNIT_HERTZ; } if (info->is_capacitance) { - analog->mq = SR_MQ_CAPACITANCE; - analog->unit = SR_UNIT_FARAD; + analog->meaning->mq = SR_MQ_CAPACITANCE; + analog->meaning->unit = SR_UNIT_FARAD; } if (info->is_temperature && info->is_celsius) { - analog->mq = SR_MQ_TEMPERATURE; - analog->unit = SR_UNIT_CELSIUS; + analog->meaning->mq = SR_MQ_TEMPERATURE; + analog->meaning->unit = SR_UNIT_CELSIUS; } if (info->is_temperature && info->is_fahrenheit) { - analog->mq = SR_MQ_TEMPERATURE; - analog->unit = SR_UNIT_FAHRENHEIT; + analog->meaning->mq = SR_MQ_TEMPERATURE; + analog->meaning->unit = SR_UNIT_FAHRENHEIT; } if (info->is_continuity) { - analog->mq = SR_MQ_CONTINUITY; - analog->unit = SR_UNIT_BOOLEAN; + analog->meaning->mq = SR_MQ_CONTINUITY; + analog->meaning->unit = SR_UNIT_BOOLEAN; *floatval = (*floatval < 0.0 || *floatval > 60.0) ? 0.0 : 1.0; } if (info->is_diode) { - analog->mq = SR_MQ_VOLTAGE; - analog->unit = SR_UNIT_VOLT; + analog->meaning->mq = SR_MQ_VOLTAGE; + analog->meaning->unit = SR_UNIT_VOLT; } if (info->is_duty_cycle) { - analog->mq = SR_MQ_DUTY_CYCLE; - analog->unit = SR_UNIT_PERCENTAGE; + analog->meaning->mq = SR_MQ_DUTY_CYCLE; + analog->meaning->unit = SR_UNIT_PERCENTAGE; } if (info->is_power) { - analog->mq = SR_MQ_POWER; - analog->unit = SR_UNIT_WATT; + analog->meaning->mq = SR_MQ_POWER; + analog->meaning->unit = SR_UNIT_WATT; } if (info->is_loop_current) { /* 4mA = 0%, 20mA = 100% */ - analog->mq = SR_MQ_CURRENT; - analog->unit = SR_UNIT_PERCENTAGE; + analog->meaning->mq = SR_MQ_CURRENT; + analog->meaning->unit = SR_UNIT_PERCENTAGE; } /* Measurement related flags */ if (info->is_ac) - analog->mqflags |= SR_MQFLAG_AC; + analog->meaning->mqflags |= SR_MQFLAG_AC; if (info->is_dc) - analog->mqflags |= SR_MQFLAG_DC; + analog->meaning->mqflags |= SR_MQFLAG_DC; if (info->is_ac) /* All AC modes do True-RMS measurements. */ - analog->mqflags |= SR_MQFLAG_RMS; + analog->meaning->mqflags |= SR_MQFLAG_RMS; if (info->is_auto) - analog->mqflags |= SR_MQFLAG_AUTORANGE; + analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE; if (info->is_diode) - analog->mqflags |= SR_MQFLAG_DIODE; + analog->meaning->mqflags |= SR_MQFLAG_DIODE; } static gboolean flags_valid(const struct ut71x_info *info) @@ -327,7 +333,7 @@ SR_PRIV gboolean sr_ut71x_packet_valid(const uint8_t *buf) SR_PRIV int sr_ut71x_parse(const uint8_t *buf, float *floatval, struct sr_datafeed_analog *analog, void *info) { - int ret; + int ret, exponent = 0; struct ut71x_info *info_local; info_local = (struct ut71x_info *)info; @@ -343,10 +349,13 @@ SR_PRIV int sr_ut71x_parse(const uint8_t *buf, float *floatval, return ret; } - if ((ret = parse_range(buf, floatval)) != SR_OK) + if ((ret = parse_range(buf, floatval, &exponent)) != SR_OK) return ret; handle_flags(analog, floatval, info); + analog->encoding->digits = -exponent; + analog->spec->spec_digits = -exponent; + return SR_OK; }