2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
6 * Copyright (C) 2018 Stefan Mandl
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 * MASTECH MS8250D protocol parser.
26 * 40 02 32 75 53 33 35 5303 10 00 00 00 00 00 00 10 00
28 * - Communication parameters: Unidirectional, 2400/8n1
29 * - CP2102 USB to UART bridge controller
37 #include <libsigrok/libsigrok.h>
38 #include "libsigrok-internal.h"
40 #define LOG_PREFIX "ms8250d"
43 * Main display (7-segment LCD value): xxDGA xxEF xxxx xxCB
44 * https://en.wikipedia.org/wiki/Seven-segment_display
46 static int parse_digit(uint16_t b)
49 case 0x0: /* 7-segment not active */
51 case 0x430: /* Overflow */
74 sr_dbg("Invalid digit word: 0x%04x.", b);
79 /* Parse second display. */
80 static int parse_digit2(uint16_t b)
106 sr_dbg("Invalid second display digit word: 0x%04x.", b);
111 static void parse_flags(const uint8_t *buf, struct ms8250d_info *info)
113 info->is_volt = (buf[9] & (1 << 4)) ? 1 : 0;
114 info->is_ohm = (buf[9] & (1 << 6)) ? 1 : 0;
115 info->is_ampere = (buf[10] & (1 << 0)) ? 1 : 0;
116 info->is_hz = (buf[10] & (1 << 2)) ? 1 : 0;
117 info->is_farad = (buf[10] & (1 << 1)) ? 1 : 0;
121 info->is_micro = (buf[8] & (1 << 4)) ? 1 : 0;
123 info->is_micro = (buf[9] & (1 << 1)) ? 1 : 0; /* uF */
125 info->is_nano = (buf[8] & (1 << 5)) ? 1 : 0;
126 info->is_milli = (buf[9] & (1 << 0)) ? 1 : 0;
127 info->is_kilo = (buf[9] & (1 << 2)) ? 1 : 0;
128 info->is_mega = (buf[8] & (1 << 6)) ? 1 : 0;
130 info->is_autotimer = (buf[1] & (1 << 0)) ? 1 : 0; /* Auto off timer */
131 info->is_rs232 = (buf[1] & (1 << 1)) ? 1 : 0; /* RS232 via USB */
132 info->is_ac = (buf[1] & (1 << 4)) ? 1 : 0;
133 info->is_dc = (buf[2] & (1 << 1)) ? 1 : 0;
134 info->is_auto = (buf[16] & (1 << 4)) ? 1 : 0;
135 info->is_bat = (buf[1] & (1 << 5)) ? 1 : 0; /* Low battery */
136 info->is_min = (buf[16] & (1 << 2)) ? 1 : 0;
137 info->is_max = (buf[16] & (1 << 1)) ? 1 : 0;
138 info->is_rel = (buf[15] & (1 << 7)) ? 1 : 0;
139 info->is_hold = (buf[16] & (1 << 3)) ? 1 : 0;
140 info->is_diode = (buf[11] & (1 << 0)) ? 1 : 0;
141 info->is_beep = (buf[11] & (1 << 1)) ? 1 : 0;
142 info->is_ncv = (buf[0] & (1 << 0)) ? 1 : 0;
145 static gboolean flags_valid(const struct ms8250d_info *info)
149 /* Does the packet have more than one multiplier? */
151 count += (info->is_nano) ? 1 : 0;
152 count += (info->is_micro) ? 1 : 0;
153 count += (info->is_milli) ? 1 : 0;
154 count += (info->is_kilo) ? 1 : 0;
155 count += (info->is_mega) ? 1 : 0;
157 sr_dbg("More than one multiplier detected in packet.");
161 /* Does the packet "measure" more than one type of value? */
163 count += (info->is_hz) ? 1 : 0;
164 count += (info->is_ohm) ? 1 : 0;
165 count += (info->is_farad) ? 1 : 0;
166 count += (info->is_ampere) ? 1 : 0;
167 count += (info->is_volt) ? 1 : 0;
169 sr_dbg("More than one measurement type detected in packet.");
173 /* Both AC and DC set? */
174 if (info->is_ac && info->is_dc) {
175 sr_dbg("Both AC and DC flags detected in packet.");
179 /* RS232 flag set? */
180 if (!info->is_rs232) {
181 sr_dbg("No RS232 flag detected in packet.");
188 static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
189 int *exponent, const struct ms8250d_info *info)
202 *floatval *= powf(10, *exponent);
204 /* Measurement modes */
206 analog->meaning->mq = SR_MQ_VOLTAGE;
207 analog->meaning->unit = SR_UNIT_VOLT;
209 if (info->is_ampere) {
210 analog->meaning->mq = SR_MQ_CURRENT;
211 analog->meaning->unit = SR_UNIT_AMPERE;
214 analog->meaning->mq = SR_MQ_RESISTANCE;
215 analog->meaning->unit = SR_UNIT_OHM;
218 analog->meaning->mq = SR_MQ_FREQUENCY;
219 analog->meaning->unit = SR_UNIT_HERTZ;
221 if (info->is_farad) {
222 analog->meaning->mq = SR_MQ_CAPACITANCE;
223 analog->meaning->unit = SR_UNIT_FARAD;
226 analog->meaning->mq = SR_MQ_CONTINUITY;
227 analog->meaning->unit = SR_UNIT_BOOLEAN;
228 *floatval = (*floatval == INFINITY) ? 0.0 : 1.0;
230 if (info->is_diode) {
231 analog->meaning->mq = SR_MQ_VOLTAGE;
232 analog->meaning->unit = SR_UNIT_VOLT;
234 if (info->is_percent) {
235 analog->meaning->mq = SR_MQ_DUTY_CYCLE;
236 analog->meaning->unit = SR_UNIT_PERCENTAGE;
239 /* Measurement related flags */
241 analog->meaning->mqflags |= SR_MQFLAG_AC;
243 analog->meaning->mqflags |= SR_MQFLAG_DC;
245 analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE;
247 analog->meaning->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
249 analog->meaning->mqflags |= SR_MQFLAG_HOLD;
251 analog->meaning->mqflags |= SR_MQFLAG_RELATIVE;
255 sr_spew("RS232 enabled.");
257 sr_spew("Battery is low.");
259 sr_spew("Beep is active");
262 SR_PRIV gboolean sr_ms8250d_packet_valid(const uint8_t *buf)
264 struct ms8250d_info info;
266 sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x "
267 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
268 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
269 buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13],
270 buf[14], buf[15], buf[16], buf[17]);
272 parse_flags(buf, &info);
274 if ((buf[17] == 0x00) && flags_valid(&info))
281 * Parse a protocol packet.
283 * @param buf Buffer containing the 18-byte protocol packet. Must not be NULL.
284 * @param floatval Pointer to a float variable. That variable will contain the
285 * result value upon parsing success. Must not be NULL.
286 * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
287 * filled with data according to the protocol packet.
289 * @param info Pointer to a struct ms8250d_info. The struct will be filled
290 * with data according to the protocol packet. Must not be NULL.
292 * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
293 * 'analog' variable contents are undefined and should not be used.
295 SR_PRIV int sr_ms8250d_parse(const uint8_t *buf, float *floatval,
296 struct sr_datafeed_analog *analog, void *info)
298 int exponent = 0, sec_exponent = 0, sign;
301 /* buf[0] bar display. */
302 /* buf[1] bar display. */
304 /* Parse seven segment digit. */
305 int16_t digit4 = parse_digit(((buf[7] & 0x73) << 4) | (buf[8] & 0x3));
307 int16_t digit3 = parse_digit(((buf[6] & 0x07) << 8) | (buf[5] & 0x30) \
308 | ((buf[6] & 0x30) >> 4));
310 int16_t digit2 = parse_digit(((buf[4] & 0x73) << 4) | (buf[5] & 0x03));
312 int16_t digit1 = parse_digit(((buf[3] & 0x07) << 8) | (buf[2] & 0x30) \
313 | ((buf[3] & 0x30) >> 4));
315 sr_dbg("Digits: %d %d %d %d.", digit1, digit2, digit3, digit4);
317 /* Decimal point position. */
318 if ((buf[3] & (1 << 6)) != 0) {
320 sr_spew("Decimal point after first digit.");
321 } else if ((buf[5] & (1 << 6)) != 0) {
323 sr_spew("Decimal point after second digit.");
324 } else if ((buf[7] & (1 << 2)) != 0) {
326 sr_spew("Decimal point after third digit.");
329 sr_spew("No decimal point in the number.");
332 struct ms8250d_info *info_local;
336 parse_flags(buf, info_local);
339 sign = (buf[0] & (1 << 2)) ? -1 : 1;
341 /* Parse second display. */
342 int16_t sec_digit4 = parse_digit2(buf[12] & 0x7F);
343 int16_t sec_digit3 = parse_digit2(buf[13] & 0x7F);
344 int16_t sec_digit2 = parse_digit2(buf[14] & 0x7F);
345 int16_t sec_digit1 = parse_digit2(buf[15] & 0x7F);
347 sr_dbg("Digits (2nd display): %d %d %d %d.",
348 sec_digit1, sec_digit2, sec_digit3, sec_digit4);
350 /* Second display decimal point position. */
351 if ((buf[14] & (1 << 7)) != 0) {
353 sr_spew("Sec decimal point after first digit.");
354 } else if ((buf[13] & (1 << 7)) != 0) {
356 sr_spew("Sec decimal point after second digit.");
357 } else if ((buf[12] & (1 << 7)) != 0) {
359 sr_spew("Sec decimal point after third digit.");
362 sr_spew("Sec no decimal point in the number.");
365 *floatval = (double)((digit1 * 1000) + (digit2 * 100) + (digit3 * 10) + digit4);
367 sec_floatval = (double)(sec_digit1 * 1000) + (sec_digit2 * 100) + (sec_digit3 * 10) + sec_digit4;
368 sec_floatval *= powf(10, sec_exponent);
373 handle_flags(analog, floatval, &exponent, info_local);
375 /* Check for "OL". */
376 if (digit3 == 0x0F) {
377 sr_spew("Over limit.");
378 *floatval = INFINITY;
382 sr_spew("The display value is %f.", (double)*floatval);
383 sr_spew("The 2nd display value is %f.", sec_floatval);
385 analog->encoding->digits = -exponent;
386 analog->spec->spec_digits = -exponent;