]>
Commit | Line | Data |
---|---|---|
0759e2f5 GS |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org> | |
5 | * Copyright (C) 2019 Gerhard Sittig <gerhard.sittig@gmx.net> | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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/>. | |
19 | */ | |
20 | ||
21 | /** | |
22 | * @file | |
23 | * | |
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 | |
29 | */ | |
30 | ||
31 | #include <config.h> | |
32 | #include <math.h> | |
33 | #include <libsigrok/libsigrok.h> | |
34 | #include "libsigrok-internal.h" | |
35 | #include <string.h> | |
36 | ||
37 | #define LOG_PREFIX "brymen-bm86x" | |
38 | ||
39 | #ifdef HAVE_SERIAL_COMM | |
40 | SR_PRIV int sr_brymen_bm86x_packet_request(struct sr_serial_dev_inst *serial) | |
41 | { | |
42 | static const uint8_t request[] = { 0x00, 0x00, 0x86, 0x66, }; | |
43 | ||
0759e2f5 GS |
44 | serial_write_nonblocking(serial, request, sizeof(request)); |
45 | ||
46 | return SR_OK; | |
47 | } | |
48 | #endif | |
49 | ||
50 | SR_PRIV gboolean sr_brymen_bm86x_packet_valid(const uint8_t *buf) | |
51 | { | |
0759e2f5 GS |
52 | /* |
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. | |
58 | */ | |
59 | if (buf[19] != 0x86) | |
60 | return FALSE; | |
61 | ||
62 | return TRUE; | |
63 | } | |
64 | ||
65 | /* | |
66 | * Data bytes in the DMM packet encode LCD segments in an unusual order | |
67 | * (bgcdafe) and in an unusual position (bits 7:1 within the byte). The | |
68 | * decimal point (bit 0) for one digit resides in the _next_ digit's byte. | |
69 | * | |
70 | * These routines convert LCD segments to characters, and a section of the | |
71 | * DMM packet (which corresponds to the primary or secondary display) to | |
72 | * the text representation of the measurement's value, before regular text | |
73 | * to number conversion is applied. The first byte of the passed in block | |
74 | * contains indicators, the value's digits start at the second byte. | |
75 | */ | |
76 | ||
77 | static char brymen_bm86x_parse_digit(uint8_t b) | |
78 | { | |
79 | switch (b >> 1) { | |
80 | /* Sign. */ | |
81 | case 0x20: return '-'; | |
82 | /* Decimal digits. */ | |
83 | case 0x5f: return '0'; | |
84 | case 0x50: return '1'; | |
85 | case 0x6d: return '2'; | |
86 | case 0x7c: return '3'; | |
87 | case 0x72: return '4'; | |
88 | case 0x3e: return '5'; | |
89 | case 0x3f: return '6'; | |
90 | case 0x54: return '7'; | |
91 | case 0x7f: return '8'; | |
92 | case 0x7e: return '9'; | |
93 | /* Temperature units. */ | |
94 | case 0x0f: return 'C'; | |
95 | case 0x27: return 'F'; | |
96 | /* OL condition, and diode mode. */ | |
97 | case 0x0b: return 'L'; | |
98 | case 0x79: return 'd'; | |
99 | case 0x10: return 'i'; | |
100 | case 0x39: return 'o'; | |
101 | /* Blank digit. */ | |
102 | case 0x00: return '\0'; | |
103 | /* Invalid or unknown segment combination. */ | |
104 | default: | |
105 | sr_warn("Unknown encoding for digit: 0x%02x.", b); | |
106 | return '\0'; | |
107 | } | |
108 | } | |
109 | ||
110 | static int brymen_bm86x_parse_digits(const uint8_t *pkt, size_t pktlen, | |
111 | char *txtbuf, float *value, char *temp_unit, int *digits, int signflag) | |
112 | { | |
113 | uint8_t byte; | |
114 | char *txtptr, txtchar; | |
115 | size_t pos; | |
116 | int ret; | |
117 | ||
118 | txtptr = txtbuf; | |
119 | if (digits) | |
120 | *digits = INT_MIN; | |
121 | ||
122 | if (pkt[0] & signflag) | |
123 | *txtptr++ = '-'; | |
124 | for (pos = 0; pos < pktlen; pos++) { | |
125 | byte = pkt[1 + pos]; | |
126 | if (pos && pos < 5 && (byte & 0x01)) { | |
127 | *txtptr++ = '.'; | |
128 | if (digits) | |
129 | *digits = 0; | |
130 | } | |
131 | txtchar = brymen_bm86x_parse_digit(byte); | |
132 | if (pos == 5 && (txtchar == 'C' || txtchar == 'F')) { | |
133 | if (temp_unit) | |
134 | *temp_unit = txtchar; | |
135 | } else if (txtchar) { | |
136 | *txtptr++ = txtchar; | |
137 | if (digits) | |
138 | (*digits)++; | |
139 | } | |
140 | } | |
141 | *txtptr = '\0'; | |
142 | ||
143 | if (digits && *digits < 0) | |
144 | *digits = 0; | |
145 | ||
146 | ret = value ? sr_atof_ascii(txtbuf, value) : SR_OK; | |
147 | if (ret != SR_OK) { | |
148 | sr_dbg("invalid float string: '%s'", txtbuf); | |
149 | return ret; | |
150 | } | |
151 | ||
152 | return SR_OK; | |
153 | } | |
154 | ||
155 | /* | |
156 | * Extract the measurement value and its properties for one of the | |
157 | * meter's displays from the DMM packet. | |
158 | */ | |
159 | static void brymen_bm86x_parse(const uint8_t *buf, float *floatval, | |
160 | struct sr_datafeed_analog *analog, size_t ch_idx) | |
161 | { | |
162 | char txtbuf[16], temp_unit; | |
163 | int ret, digits, is_diode, over_limit, scale; | |
164 | uint8_t ind1, ind15; | |
165 | ||
e378e3a2 | 166 | temp_unit = '\0'; |
0759e2f5 GS |
167 | if (ch_idx == 0) { |
168 | /* | |
169 | * Main display. Note that _some_ of the second display's | |
170 | * indicators are involved in the inspection of the _first_ | |
171 | * display's measurement value. So we have to get the | |
172 | * second display's text buffer here, too. | |
173 | */ | |
174 | (void)brymen_bm86x_parse_digits(&buf[9], 4, txtbuf, | |
175 | NULL, NULL, NULL, 0); | |
176 | is_diode = strcmp(txtbuf, "diod") == 0; | |
177 | ret = brymen_bm86x_parse_digits(&buf[2], 6, txtbuf, | |
178 | floatval, &temp_unit, &digits, 0x80); | |
179 | over_limit = strstr(txtbuf, "0L") || strstr(txtbuf, "0.L"); | |
180 | if (ret != SR_OK && !over_limit) | |
181 | return; | |
182 | ||
183 | /* SI unit. */ | |
184 | if (buf[8] & 0x01) { | |
185 | analog->meaning->mq = SR_MQ_VOLTAGE; | |
186 | analog->meaning->unit = SR_UNIT_VOLT; | |
187 | if (is_diode) { | |
188 | analog->meaning->mqflags |= SR_MQFLAG_DIODE; | |
189 | analog->meaning->mqflags |= SR_MQFLAG_DC; | |
190 | } | |
191 | } else if (buf[14] & 0x80) { | |
192 | analog->meaning->mq = SR_MQ_CURRENT; | |
193 | analog->meaning->unit = SR_UNIT_AMPERE; | |
194 | } else if (buf[14] & 0x20) { | |
195 | analog->meaning->mq = SR_MQ_CAPACITANCE; | |
196 | analog->meaning->unit = SR_UNIT_FARAD; | |
197 | } else if (buf[14] & 0x10) { | |
198 | analog->meaning->mq = SR_MQ_CONDUCTANCE; | |
199 | analog->meaning->unit = SR_UNIT_SIEMENS; | |
200 | } else if (buf[15] & 0x01) { | |
201 | analog->meaning->mq = SR_MQ_FREQUENCY; | |
202 | analog->meaning->unit = SR_UNIT_HERTZ; | |
203 | } else if (buf[10] & 0x01) { | |
204 | analog->meaning->mq = SR_MQ_CONTINUITY; | |
205 | analog->meaning->unit = SR_UNIT_OHM; | |
206 | } else if (buf[15] & 0x10) { | |
207 | analog->meaning->mq = SR_MQ_RESISTANCE; | |
208 | analog->meaning->unit = SR_UNIT_OHM; | |
209 | } else if (buf[15] & 0x02) { | |
210 | analog->meaning->mq = SR_MQ_POWER; | |
211 | analog->meaning->unit = SR_UNIT_DECIBEL_MW; | |
212 | } else if (buf[15] & 0x80) { | |
213 | analog->meaning->mq = SR_MQ_DUTY_CYCLE; | |
214 | analog->meaning->unit = SR_UNIT_PERCENTAGE; | |
e378e3a2 | 215 | } else if ((buf[2] & 0x0a) && temp_unit) { |
0759e2f5 GS |
216 | analog->meaning->mq = SR_MQ_TEMPERATURE; |
217 | if (temp_unit == 'F') | |
218 | analog->meaning->unit = SR_UNIT_FAHRENHEIT; | |
219 | else | |
220 | analog->meaning->unit = SR_UNIT_CELSIUS; | |
221 | } | |
222 | ||
223 | /* | |
224 | * Remove the MIN/MAX/AVG indicators when all of them | |
225 | * are shown at the same time. | |
226 | */ | |
227 | ind1 = buf[1]; | |
228 | if ((ind1 & 0xe0) == 0xe0) | |
229 | ind1 &= ~0xe0; | |
230 | ||
231 | /* AC/DC/Auto flags. */ | |
232 | if (buf[1] & 0x10) | |
233 | analog->meaning->mqflags |= SR_MQFLAG_DC; | |
234 | if (buf[2] & 0x01) | |
235 | analog->meaning->mqflags |= SR_MQFLAG_AC; | |
236 | if (buf[1] & 0x01) | |
237 | analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE; | |
238 | if (buf[1] & 0x08) | |
239 | analog->meaning->mqflags |= SR_MQFLAG_HOLD; | |
240 | if (ind1 & 0x20) | |
241 | analog->meaning->mqflags |= SR_MQFLAG_MAX; | |
242 | if (ind1 & 0x40) | |
243 | analog->meaning->mqflags |= SR_MQFLAG_MIN; | |
244 | if (ind1 & 0x80) | |
245 | analog->meaning->mqflags |= SR_MQFLAG_AVG; | |
246 | if (buf[3] & 0x01) | |
247 | analog->meaning->mqflags |= SR_MQFLAG_RELATIVE; | |
248 | ||
249 | /* | |
250 | * Remove the "dBm" indication's "m" indicator before the | |
251 | * SI unit's prefixes get inspected. To avoid an interaction | |
252 | * with the "milli" prefix. | |
253 | */ | |
254 | ind15 = buf[15]; | |
255 | if (ind15 & 0x02) | |
256 | ind15 &= ~0x04; | |
257 | ||
258 | /* SI prefix. */ | |
259 | scale = 0; | |
260 | if (buf[14] & 0x40) /* n */ | |
261 | scale = -9; | |
262 | if (buf[15] & 0x08) /* u */ | |
263 | scale = -6; | |
264 | if (ind15 & 0x04) /* m */ | |
265 | scale = -3; | |
266 | if (buf[15] & 0x40) /* k */ | |
267 | scale = +3; | |
268 | if (buf[15] & 0x20) /* M */ | |
269 | scale = +6; | |
270 | if (scale) { | |
271 | *floatval *= pow(10, scale); | |
272 | digits += -scale; | |
273 | } | |
274 | ||
275 | if (over_limit) | |
276 | *floatval = INFINITY; | |
277 | ||
278 | analog->encoding->digits = digits; | |
279 | analog->spec->spec_digits = digits; | |
280 | } else if (ch_idx == 1) { | |
e378e3a2 GS |
281 | /* |
282 | * Secondary display. Also inspect _some_ primary display | |
283 | * data, to determine the secondary display's validity. | |
284 | */ | |
285 | (void)brymen_bm86x_parse_digits(&buf[2], 6, txtbuf, | |
286 | NULL, &temp_unit, NULL, 0x80); | |
0759e2f5 | 287 | ret = brymen_bm86x_parse_digits(&buf[9], 4, txtbuf, |
e378e3a2 | 288 | floatval, NULL, &digits, 0x10); |
0759e2f5 GS |
289 | |
290 | /* SI unit. */ | |
291 | if (buf[14] & 0x08) { | |
292 | analog->meaning->mq = SR_MQ_VOLTAGE; | |
293 | analog->meaning->unit = SR_UNIT_VOLT; | |
294 | } else if (buf[9] & 0x04) { | |
295 | analog->meaning->mq = SR_MQ_CURRENT; | |
296 | analog->meaning->unit = SR_UNIT_AMPERE; | |
297 | } else if (buf[9] & 0x08) { | |
298 | analog->meaning->mq = SR_MQ_CURRENT; | |
299 | analog->meaning->unit = SR_UNIT_PERCENTAGE; | |
300 | } else if (buf[14] & 0x04) { | |
301 | analog->meaning->mq = SR_MQ_FREQUENCY; | |
302 | analog->meaning->unit = SR_UNIT_HERTZ; | |
e378e3a2 | 303 | } else if ((buf[9] & 0x40) && temp_unit) { |
0759e2f5 GS |
304 | analog->meaning->mq = SR_MQ_TEMPERATURE; |
305 | if (temp_unit == 'F') | |
306 | analog->meaning->unit = SR_UNIT_FAHRENHEIT; | |
307 | else | |
308 | analog->meaning->unit = SR_UNIT_CELSIUS; | |
309 | } | |
310 | ||
311 | /* AC flag. */ | |
312 | if (buf[9] & 0x20) | |
313 | analog->meaning->mqflags |= SR_MQFLAG_AC; | |
314 | ||
315 | /* SI prefix. */ | |
316 | scale = 0; | |
317 | if (buf[ 9] & 0x01) /* u */ | |
318 | scale = -6; | |
319 | if (buf[ 9] & 0x02) /* m */ | |
320 | scale = -3; | |
321 | if (buf[14] & 0x02) /* k */ | |
322 | scale = +3; | |
323 | if (buf[14] & 0x01) /* M */ | |
324 | scale = +6; | |
325 | if (scale) { | |
326 | *floatval *= pow(10, scale); | |
327 | digits += -scale; | |
328 | } | |
329 | ||
330 | analog->encoding->digits = digits; | |
331 | analog->spec->spec_digits = digits; | |
332 | } | |
333 | ||
334 | if (buf[9] & 0x80) | |
335 | sr_warn("Battery is low."); | |
336 | } | |
337 | ||
338 | SR_PRIV int sr_brymen_bm86x_parse(const uint8_t *buf, float *val, | |
339 | struct sr_datafeed_analog *analog, void *info) | |
340 | { | |
341 | struct brymen_bm86x_info *info_local; | |
342 | size_t ch_idx; | |
343 | ||
344 | /* | |
345 | * Scan a portion of the received DMM packet which corresponds | |
346 | * to the caller's specified display. Then prepare to scan a | |
347 | * different portion of the packet for another display. This | |
348 | * routine gets called multiple times for one received packet. | |
349 | */ | |
350 | info_local = info; | |
351 | ch_idx = info_local->ch_idx; | |
352 | brymen_bm86x_parse(buf, val, analog, ch_idx); | |
353 | info_local->ch_idx = ch_idx + 1; | |
354 | ||
355 | return SR_OK; | |
356 | } |