X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fcommon%2Fdmm%2Fmetex14.c;h=8151aa9ded319006f485bf669a089af8039714e7;hb=43cd4637285833706f8a404ca027bcf0ee75b9ae;hp=7f4b890510c133fb6bffc7985bf2c5c946483c44;hpb=c02dc3e26141a13554828be9a4ffa2def857d2b3;p=libsigrok.git diff --git a/hardware/common/dmm/metex14.c b/hardware/common/dmm/metex14.c index 7f4b8905..8151aa9d 100644 --- a/hardware/common/dmm/metex14.c +++ b/hardware/common/dmm/metex14.c @@ -18,9 +18,12 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -/* +/** + * @file + * * Metex 14-bytes ASCII protocol parser. * + * @internal * This should work for various multimeters which use this kind of protocol, * even though there is some variation in which modes each DMM supports. * @@ -34,16 +37,11 @@ #include "libsigrok.h" #include "libsigrok-internal.h" -/* Message logging helpers with subsystem-specific prefix string. */ -#define LOG_PREFIX "metex14: " -#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args) -#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args) -#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args) -#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args) -#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args) -#define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args) +#define LOG_PREFIX "metex14" -static int parse_value(const uint8_t *buf, float *result) +/** Parse value from buf, byte 2-8. */ +static int parse_value(const uint8_t *buf, struct metex14_info *info, + float *result) { int i, is_ol, cnt; char valstr[7 + 1]; @@ -61,12 +59,31 @@ static int parse_value(const uint8_t *buf, float *result) is_ol += (!strcasecmp((const char *)&valstr, "O.L")) ? 1 : 0; is_ol += (!strcasecmp((const char *)&valstr, "OL.")) ? 1 : 0; is_ol += (!strcasecmp((const char *)&valstr, "OL")) ? 1 : 0; + is_ol += (!strcasecmp((const char *)&valstr, "-.OL")) ? 1 : 0; + is_ol += (!strcasecmp((const char *)&valstr, "-O.L")) ? 1 : 0; + is_ol += (!strcasecmp((const char *)&valstr, "-OL.")) ? 1 : 0; + is_ol += (!strcasecmp((const char *)&valstr, "-OL")) ? 1 : 0; if (is_ol != 0) { sr_spew("Over limit."); *result = INFINITY; return SR_OK; } + /* Logic functions */ + if (!strcmp((const char *)&valstr, "READY") || + !strcmp((const char *)&valstr, "FLOAT")) { + *result = INFINITY; + info->is_logic = TRUE; + } else if (!strcmp((const char *)&valstr, "Hi")) { + *result = 1.0; + info->is_logic = TRUE; + } else if (!strcmp((const char *)&valstr, "Lo")) { + *result = 0.0; + info->is_logic = TRUE; + } + if (info->is_logic) + return SR_OK; + /* Bytes 2-8: Sign, value (up to 5 digits) and decimal point */ sscanf((const char *)&valstr, "%f", result); @@ -81,25 +98,9 @@ static void parse_flags(const char *buf, struct metex14_info *info) char unit[4 + 1]; const char *u; - /* Bytes 0-1: Measurement mode */ - /* Note: Protocol doesn't distinguish "resistance" from "beep" mode. */ - info->is_ac = !strncmp(buf, "AC", 2); - info->is_dc = !strncmp(buf, "DC", 2); - info->is_resistance = !strncmp(buf, "OH", 2); - info->is_capacity = !strncmp(buf, "CA", 2); - info->is_temperature = !strncmp(buf, "TE", 2); - info->is_diode = !strncmp(buf, "DI", 2); - info->is_frequency = !strncmp(buf, "FR", 2); - info->is_gain = !strncmp(buf, "DB", 2); - info->is_hfe = !strncmp(buf, "HF", 2); - - /* - * Note: "DB" shows the logarithmic ratio of input voltage to a - * pre-stored (user-changeable) value in the DMM. - */ - - if (info->is_dc || info->is_ac) - info->is_volt = TRUE; + /* Bytes 0-1: Measurement mode AC, DC */ + info->is_ac = !strncmp(buf, "AC", 2); + info->is_dc = !strncmp(buf, "DC", 2); /* Bytes 2-8: See parse_value(). */ @@ -143,6 +144,27 @@ static void parse_flags(const char *buf, struct metex14_info *info) else if (!strcasecmp(u, "")) info->is_unitless = TRUE; + /* Bytes 0-1: Measurement mode, except AC/DC */ + info->is_resistance = !strncmp(buf, "OH", 2) || + (!strncmp(buf, " ", 2) && info->is_ohm); + info->is_capacity = !strncmp(buf, "CA", 2) || + (!strncmp(buf, " ", 2) && info->is_farad); + info->is_temperature = !strncmp(buf, "TE", 2); + info->is_diode = !strncmp(buf, "DI", 2) || + (!strncmp(buf, " ", 2) && info->is_volt && info->is_milli); + info->is_frequency = !strncmp(buf, "FR", 2) || + (!strncmp(buf, " ", 2) && info->is_hertz); + info->is_gain = !strncmp(buf, "DB", 2); + info->is_hfe = !strncmp(buf, "HF", 2) || + (!strncmp(buf, " ", 2) && !info->is_volt && !info->is_ohm && + !info->is_logic && !info->is_farad && !info->is_hertz); + /* + * Note: + * - Protocol doesn't distinguish "resistance" from "beep" mode. + * - "DB" shows the logarithmic ratio of input voltage to a + * pre-stored (user-changeable) value in the DMM. + */ + /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */ } @@ -200,6 +222,10 @@ static void handle_flags(struct sr_datafeed_analog *analog, float *floatval, analog->mq = SR_MQ_GAIN; analog->unit = SR_UNIT_UNITLESS; } + if (info->is_logic) { + analog->mq = SR_MQ_GAIN; + analog->unit = SR_UNIT_UNITLESS; + } /* Measurement related flags */ if (info->is_ac) @@ -223,7 +249,7 @@ static gboolean flags_valid(const struct metex14_info *info) count += (info->is_kilo) ? 1 : 0; count += (info->is_mega) ? 1 : 0; if (count > 1) { - sr_err("More than one multiplier detected in packet."); + sr_dbg("More than one multiplier detected in packet."); return FALSE; } @@ -237,19 +263,20 @@ static gboolean flags_valid(const struct metex14_info *info) count += (info->is_diode) ? 1 : 0; count += (info->is_frequency) ? 1 : 0; if (count > 1) { - sr_err("More than one measurement type detected in packet."); + sr_dbg("More than one measurement type detected in packet."); return FALSE; } /* Both AC and DC set? */ if (info->is_ac && info->is_dc) { - sr_err("Both AC and DC flags detected in packet."); + sr_dbg("Both AC and DC flags detected in packet."); return FALSE; } return TRUE; } +#ifdef HAVE_LIBSERIALPORT SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial) { const uint8_t wbuf = 'D'; @@ -258,6 +285,7 @@ SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial) return (serial_write(serial, &wbuf, 1) == 1) ? SR_OK : SR_ERR; } +#endif SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf) { @@ -301,12 +329,13 @@ SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval, /* Don't print byte 13. That one contains the carriage return. */ sr_dbg("DMM packet: \"%.13s\"", buf); - if ((ret = parse_value(buf, floatval)) != SR_OK) { - sr_err("Error parsing value: %d.", ret); + memset(info_local, 0x00, sizeof(struct metex14_info)); + + if ((ret = parse_value(buf, info_local, floatval)) != SR_OK) { + sr_dbg("Error parsing value: %d.", ret); return ret; } - memset(info_local, 0x00, sizeof(struct metex14_info)); parse_flags((const char *)buf, info_local); handle_flags(analog, floatval, info_local);