2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
5 * Copyright (C) 2019 Gerhard Sittig <gerhard.sittig@gmx.net>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * Brymen BM86x serial protocol parser. The USB protocol (for the cable)
25 * and the packet description (for the meter) were retrieved from:
26 * http://brymen.com/product-html/Download2.html
27 * http://brymen.com/product-html/PD02BM860s_protocolDL.html
28 * http://brymen.com/product-html/images/DownloadList/ProtocolList/BM860-BM860s_List/BM860-BM860s-500000-count-dual-display-DMMs-protocol.pdf
33 #include <libsigrok/libsigrok.h>
34 #include "libsigrok-internal.h"
37 #define LOG_PREFIX "brymen-bm86x"
39 #ifdef HAVE_SERIAL_COMM
40 SR_PRIV int sr_brymen_bm86x_packet_request(struct sr_serial_dev_inst *serial)
42 static const uint8_t request[] = { 0x00, 0x00, 0x86, 0x66, };
44 serial_write_nonblocking(serial, request, sizeof(request));
50 SR_PRIV gboolean sr_brymen_bm86x_packet_valid(const uint8_t *buf)
53 * "Model ID3" (3rd HID report, byte 3) is the only documented
54 * fixed value, and must be 0x86. All other positions either depend
55 * on the meter's function, or the measurement's value, or are not
56 * documented by the vendor (are marked as "don't care", no fixed
57 * values are listed). There is nothing else we can check reliably.
63 * 2021-05 update: The devices that we have seen in the field do
64 * provide four bytes which we can synchronize to. Which happens
65 * to match the bm52x and bm82x devices' protocol. This condition
66 * is not documented by the vendor, but improves reliability of
67 * the re-synchronization for slight offsets, which were seen
68 * in the field, and which would have kept failing in an earlier
82 * Data bytes in the DMM packet encode LCD segments in an unusual order
83 * (bgcdafe) and in an unusual position (bits 7:1 within the byte). The
84 * decimal point (bit 0) for one digit resides in the _next_ digit's byte.
86 * These routines convert LCD segments to characters, and a section of the
87 * DMM packet (which corresponds to the primary or secondary display) to
88 * the text representation of the measurement's value, before regular text
89 * to number conversion is applied. The first byte of the passed in block
90 * contains indicators, the value's digits start at the second byte.
93 static char brymen_bm86x_parse_digit(uint8_t b)
97 case 0x20: return '-';
99 case 0x5f: return '0';
100 case 0x50: return '1';
101 case 0x6d: return '2';
102 case 0x7c: return '3';
103 case 0x72: return '4';
104 case 0x3e: return '5';
105 case 0x3f: return '6';
106 case 0x54: return '7';
107 case 0x7f: return '8';
108 case 0x7e: return '9';
109 /* Temperature units. */
110 case 0x0f: return 'C';
111 case 0x27: return 'F';
112 /* OL condition, and diode mode. */
113 case 0x0b: return 'L';
114 case 0x79: return 'd';
115 case 0x10: return 'i';
116 case 0x39: return 'o';
118 case 0x00: return '\0';
119 /* Invalid or unknown segment combination. */
121 sr_warn("Unknown encoding for digit: 0x%02x.", b);
126 static int brymen_bm86x_parse_digits(const uint8_t *pkt, size_t pktlen,
127 char *txtbuf, float *value, char *temp_unit, int *digits, int signflag)
130 char *txtptr, txtchar;
138 if (pkt[0] & signflag)
140 for (pos = 0; pos < pktlen; pos++) {
142 if (pos && pos < 5 && (byte & 0x01)) {
147 txtchar = brymen_bm86x_parse_digit(byte);
148 if (pos == 5 && (txtchar == 'C' || txtchar == 'F')) {
150 *temp_unit = txtchar;
151 } else if (txtchar) {
159 if (digits && *digits < 0)
162 ret = value ? sr_atof_ascii(txtbuf, value) : SR_OK;
164 sr_dbg("invalid float string: '%s'", txtbuf);
172 * Extract the measurement value and its properties for one of the
173 * meter's displays from the DMM packet.
175 static void brymen_bm86x_parse(const uint8_t *buf, float *floatval,
176 struct sr_datafeed_analog *analog, size_t ch_idx)
178 char txtbuf[16], temp_unit;
179 int ret, digits, is_diode, over_limit, scale;
185 * Main display. Note that _some_ of the second display's
186 * indicators are involved in the inspection of the _first_
187 * display's measurement value. So we have to get the
188 * second display's text buffer here, too.
190 (void)brymen_bm86x_parse_digits(&buf[9], 4, txtbuf,
191 NULL, NULL, NULL, 0);
192 is_diode = strcmp(txtbuf, "diod") == 0;
193 ret = brymen_bm86x_parse_digits(&buf[2], 6, txtbuf,
194 floatval, &temp_unit, &digits, 0x80);
195 over_limit = strstr(txtbuf, "0L") || strstr(txtbuf, "0.L");
196 if (ret != SR_OK && !over_limit)
201 analog->meaning->mq = SR_MQ_VOLTAGE;
202 analog->meaning->unit = SR_UNIT_VOLT;
204 analog->meaning->mqflags |= SR_MQFLAG_DIODE;
205 analog->meaning->mqflags |= SR_MQFLAG_DC;
207 } else if (buf[14] & 0x80) {
208 analog->meaning->mq = SR_MQ_CURRENT;
209 analog->meaning->unit = SR_UNIT_AMPERE;
210 } else if (buf[14] & 0x20) {
211 analog->meaning->mq = SR_MQ_CAPACITANCE;
212 analog->meaning->unit = SR_UNIT_FARAD;
213 } else if (buf[14] & 0x10) {
214 analog->meaning->mq = SR_MQ_CONDUCTANCE;
215 analog->meaning->unit = SR_UNIT_SIEMENS;
216 } else if (buf[15] & 0x01) {
217 analog->meaning->mq = SR_MQ_FREQUENCY;
218 analog->meaning->unit = SR_UNIT_HERTZ;
219 } else if (buf[10] & 0x01) {
220 analog->meaning->mq = SR_MQ_CONTINUITY;
221 analog->meaning->unit = SR_UNIT_OHM;
222 } else if (buf[15] & 0x10) {
223 analog->meaning->mq = SR_MQ_RESISTANCE;
224 analog->meaning->unit = SR_UNIT_OHM;
225 } else if (buf[15] & 0x02) {
226 analog->meaning->mq = SR_MQ_POWER;
227 analog->meaning->unit = SR_UNIT_DECIBEL_MW;
228 } else if (buf[15] & 0x80) {
229 analog->meaning->mq = SR_MQ_DUTY_CYCLE;
230 analog->meaning->unit = SR_UNIT_PERCENTAGE;
231 } else if ((buf[2] & 0x0a) && temp_unit) {
232 analog->meaning->mq = SR_MQ_TEMPERATURE;
233 if (temp_unit == 'F')
234 analog->meaning->unit = SR_UNIT_FAHRENHEIT;
236 analog->meaning->unit = SR_UNIT_CELSIUS;
240 * Remove the MIN/MAX/AVG indicators when all of them
241 * are shown at the same time.
244 if ((ind1 & 0xe0) == 0xe0)
247 /* AC/DC/Auto flags. */
249 analog->meaning->mqflags |= SR_MQFLAG_DC;
251 analog->meaning->mqflags |= SR_MQFLAG_AC;
253 analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE;
255 analog->meaning->mqflags |= SR_MQFLAG_HOLD;
257 analog->meaning->mqflags |= SR_MQFLAG_MAX;
259 analog->meaning->mqflags |= SR_MQFLAG_MIN;
261 analog->meaning->mqflags |= SR_MQFLAG_AVG;
263 analog->meaning->mqflags |= SR_MQFLAG_RELATIVE;
266 * Remove the "dBm" indication's "m" indicator before the
267 * SI unit's prefixes get inspected. To avoid an interaction
268 * with the "milli" prefix.
276 if (buf[14] & 0x40) /* n */
278 if (buf[15] & 0x08) /* u */
280 if (ind15 & 0x04) /* m */
282 if (buf[15] & 0x40) /* k */
284 if (buf[15] & 0x20) /* M */
287 *floatval *= pow(10, scale);
292 *floatval = INFINITY;
294 analog->encoding->digits = digits;
295 analog->spec->spec_digits = digits;
296 } else if (ch_idx == 1) {
298 * Secondary display. Also inspect _some_ primary display
299 * data, to determine the secondary display's validity.
301 (void)brymen_bm86x_parse_digits(&buf[2], 6, txtbuf,
302 NULL, &temp_unit, NULL, 0x80);
303 ret = brymen_bm86x_parse_digits(&buf[9], 4, txtbuf,
304 floatval, NULL, &digits, 0x10);
309 if (buf[14] & 0x08) {
310 analog->meaning->mq = SR_MQ_VOLTAGE;
311 analog->meaning->unit = SR_UNIT_VOLT;
312 } else if (buf[9] & 0x04) {
313 analog->meaning->mq = SR_MQ_CURRENT;
314 analog->meaning->unit = SR_UNIT_AMPERE;
315 } else if (buf[9] & 0x08) {
316 analog->meaning->mq = SR_MQ_CURRENT;
317 analog->meaning->unit = SR_UNIT_PERCENTAGE;
318 } else if (buf[14] & 0x04) {
319 analog->meaning->mq = SR_MQ_FREQUENCY;
320 analog->meaning->unit = SR_UNIT_HERTZ;
321 } else if ((buf[9] & 0x40) && temp_unit) {
322 analog->meaning->mq = SR_MQ_TEMPERATURE;
323 if (temp_unit == 'F')
324 analog->meaning->unit = SR_UNIT_FAHRENHEIT;
326 analog->meaning->unit = SR_UNIT_CELSIUS;
331 analog->meaning->mqflags |= SR_MQFLAG_AC;
335 if (buf[ 9] & 0x01) /* u */
337 if (buf[ 9] & 0x02) /* m */
339 if (buf[14] & 0x02) /* k */
341 if (buf[14] & 0x01) /* M */
344 *floatval *= pow(10, scale);
348 analog->encoding->digits = digits;
349 analog->spec->spec_digits = digits;
353 sr_warn("Battery is low.");
356 SR_PRIV int sr_brymen_bm86x_parse(const uint8_t *buf, float *val,
357 struct sr_datafeed_analog *analog, void *info)
359 struct brymen_bm86x_info *info_local;
363 * Scan a portion of the received DMM packet which corresponds
364 * to the caller's specified display. Then prepare to scan a
365 * different portion of the packet for another display. This
366 * routine gets called multiple times for one received packet.
369 ch_idx = info_local->ch_idx;
370 brymen_bm86x_parse(buf, val, analog, ch_idx);
371 info_local->ch_idx = ch_idx + 1;