JKBMS: Sometimes wrong readings
See original GitHub issueDescribe the bug I sometimes get wrong readings from the jkbms code. I already added code to test the checksum and the various framing bits (command type, etc), but e.g. the SoC (offset 0x85) sometimes goes to 133%, while battery voltage stays in the correct range (offset 0x83).
The captured status_data in this case is
Invalid soc: [bytearray(b"\x01y0\x01\r\'\x02\r,\x03\r+\x04\r\'\x05\r-\x06\r(\x07\r,\x08\r+\t\r2\n\r\'\x0b\r-\x0c\r(\r\r+\x0e\r-\x0f\r\'\x10\r,\x80\x00\x1d\x81\x00\x18\x82\x00\x18\x83\x15\x10\x84\x85\xc3\x85M\x86\x02\x87\x00\x03\x89\x00\x00\x03\x00\x8a\x00\x10\x8b\x00\x00\x8c\x00\x07\x8e\x16\x80\x8f\x10@\x90\x0e\x10\x91\rH\x92\x00\x05\x93\n(\x94\x0b\xb8\x95\x00\x05\x96\x01,\x97\x00d\x98\x01,\x99\x002\x9a\x00\x1e\x9b\x05\xdc\x9c\x00\x03\x9d\x01\x9e\x00Z\x9f\x00F\xa0\x00d\xa1\x00d\xa2\x00\x14\xa3\x00F\xa4\x00F\xa5\xff\xec\xa6\xff\xf6\xa7\xff\xec\xa8\xff\xf6\xa9\x10\xaa\x00\x00\x01\x18\xab\x01\xac\x01\xad\x04\x0f\xae\x01\xaf\x01\xb0\x00\n\xb1\x14\xb2123456\x00\x00\x00\x00\xb3\x00\xb4Input Us\xb52101\xb6\x00\x00\xd6\x00\xb7H6.X__S6.1.0S__\xb8\x00\xb9\x00\x00\x01\x00\xbaBT307202012000")]
A correct case right next to the wrong frame is like this:
[bytearray(b"\x01y0\x01\r\'\x02\r,\x03\r+\x04\r\'\x05\r-\x06\r(\x07\r,\x08\r+\t\r8\n\r,\x0b\r+\x0c\r(\r\r-\x0e\r\'\x0f\r,\x10\r+\x80\x00\x1d\x81\x00\x18\x82\x00\x18\x83\x15\x11\x84\x86\x01\x85M\x86\x02\x87\x00\x03\x89\x00\x00\x03\x00\x8a\x00\x10\x8b\x00\x00\x8c\x00\x07\x8e\x16\x80\x8f\x10@\x90\x0e\x10\x91\rH\x92\x00\x05\x93\n(\x94\x0b\xb8\x95\x00\x05\x96\x01,\x97\x00d\x98\x01,\x99\x002\x9a\x00\x1e\x9b\x05\xdc\x9c\x00\x03\x9d\x01\x9e\x00Z\x9f\x00F\xa0\x00d\xa1\x00d\xa2\x00\x14\xa3\x00F\xa4\x00F\xa5\xff\xec\xa6\xff\xf6\xa7\xff\xec\xa8\xff\xf6\xa9\x10\xaa\x00\x00\x01\x18\xab\x01\xac\x01\xad\x04\x0f\xae\x01\xaf\x01\xb0\x00\n\xb1\x14\xb2123456\x00\x00\x00\x00\xb3\x00\xb4Input Us\xb52101\xb6\x00\x00\xd6\x00\xb7H6.X__S6.1.0S__\xb8\x00\xb9\x00\x00\x01\x00\xbaBT307202012000")]
In this particular case (it’s unfortunately not the only ones), the following bytes are flipped (some might be due to different values):
Offset 0x1d Wrong 0x32 OK 0x38 Offset 0x20 Wrong 0x27 OK 0x2c Offset 0x23 Wrong 0x2d OK 0x2b Offset 0x29 Wrong 0x2b OK 0x2d Offset 0x2c Wrong 0x2d OK 0x27 Offset 0x2f Wrong 0x27 OK 0x2c Offset 0x32 Wrong 0x2c OK 0x2b Offset 0x3e Wrong 0x10 OK 0x11 Offset 0x40 Wrong 0x85 OK 0x86 Offset 0x41 Wrong 0xc3 OK 0x01
What I believe is that the find() function in get_data is brittle as it doesn’t differentiate between value and idcodes. I think it would be better to parse the bytes continuously, ie. first read the ID, then read the number of bytes associated to it, then continue, etc. I’m happy to write up a patch if you agree with the approach.
ps: Modified code to test for checksum and print framing bits (in read_serial_data_jkbms):
` start, length = unpack_from(‘>HH’, data) terminal = unpack_from(‘>L’, data[4:])[0] cmd, src, tt = unpack_from(‘>bbb’, data[8:]) frame = unpack_from(‘>H’, data[-9:])[0] end, crc_hi, crc_lo = unpack_from(‘>BHH’, data[-5:])
s = sum(data[0:-4])
logger.info('S%d C%d C%d' % (s, crc_lo, crc_hi))
logger.info('T%d C%d S%d F%d TT%d' % (terminal, cmd, src, frame, tt))
if s != crc_lo:
logger.error('CRC checksum mismatch: Expected 0x%04x, Got 0x%04x' % (crc_lo, s))
`
update formatting
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (5 by maintainers)

Top Related StackOverflow Question
I already got something rudimentary working and confirming my theory. I can clean this up over the weekend and send a patch.
On Fri, Sep 10, 2021 at 11:04 AM Louis Van Der Walt < @.***> wrote:
– Jan Dittmer @.***>, https://l4x.org
Correct, that is the idea.