From 472bef399078cd7e80f35940a527828a1c841c46 Mon Sep 17 00:00:00 2001 From: Martin Ling Date: Sun, 1 Mar 2015 22:26:21 +0000 Subject: [PATCH] ut372: Implement initial protocol parser. For now this only works correctly if the device is in the default state showing current RPM. The flags are not checked. --- src/dmm/ut372.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/dmm/ut372.c b/src/dmm/ut372.c index 4e763a36..b5e2c703 100644 --- a/src/dmm/ut372.c +++ b/src/dmm/ut372.c @@ -22,18 +22,66 @@ * UNI-T UT372 protocol parser. */ +#include +#include #include "libsigrok.h" #include "libsigrok-internal.h" #define LOG_PREFIX "ut372" +char lookup[] = { + 0x7B, + 0x60, + 0x5E, + 0x7C, + 0x65, + 0x3D, + 0x3F, + 0x70, + 0x7F, + 0x7D +}; + +#define DECIMAL_POINT_MASK 0x80 + SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf) { - return FALSE; + return (buf[25] == '\r' && buf[26] == '\n'); } SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval, struct sr_datafeed_analog *analog, void *info) { + unsigned int i, j, segments, value, divisor; + char hex[3]; + + (void) info; + + hex[2] = '\0'; + value = 0; + divisor = 1; + + for (i = 0; i < 5; i++) { + for (j = 0; j < 2; j++) { + hex[j] = buf[2*i + 1 + j]; + if (hex[j] > 0x39) + hex[j] += 7; + } + segments = strtol(hex, NULL, 16); + for (j = 0; j < ARRAY_SIZE(lookup); j++) { + if (lookup[j] == (segments & ~DECIMAL_POINT_MASK)) { + value += j * pow(10, i); + break; + } + } + if (segments & DECIMAL_POINT_MASK) + divisor = pow(10, i); + } + + *floatval = (float) value / divisor; + + analog->mq = SR_MQ_FREQUENCY; + analog->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE; + return SR_OK; }