Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add battery_management_system_protocol #410
base: master
Are you sure you want to change the base?
Add battery_management_system_protocol #410
Changes from 9 commits
ad9fabe
66778d8
b28c288
87ee4a0
042fc91
a623ed0
d96be77
26f169a
064f90a
1cf4f1e
c0a9dd0
6c3fab2
0fd3805
b052dc9
a76559c
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering what happens if
sum(body) > 0x10000
and I was surprised that the format authors would not think about it, but I believe that you did not quite follow the specification (page 2):It is a little bit weird English, but it makes sense to me and it also matches your interpretation (but solves the problem in case
sum(body) > 0x10000
).For this value of
checksum_input
(actually taken from https://github.com/Jakeler/bms-parser/blob/dfb9e05e3f4ee0e70cd71ab2114bff1c66661899/py/test.py#L10):the sum is
The
expected_checksum
is thenaccording to you, and
according to the spec.
In fact,
0x10000 - sum(checksum_input)
would work, but it would also have to be clamped to the lower 16 bits like(0x10000 - sum(checksum_input)) & 0xffff
. However, if you only take the lower 16 bits from0x10000 - a
, it's the same as if you did0x7af20000 - a
,0xff0000 - a
or just0x0000 - a
, because all these values have the value of0
as far as the 16 bits are concerned.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I admit that I have simplified the formula a bit, but this is really just a theoretical problem. Even if we are assuming the complete data consists of
0xff...
you would need 256 bytes to reach the 16 bits max value. The data length field is just one byte, so the body can not be larger than 256 bytes.Yes, there is also a status byte (which is
0x00
on every valid packet) and the length itself, but real data is never always0xff
of course, so this easily would work in practice even if the length is pushed to the limit.In reality there are even earlier limits, for example the balancing flags are for max. 32 cells, that would produce only 64 + 2 = 66 bytes in the cell voltages response and the other structures are constant size.
But I can add a note to the doc...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps, but I don't perceive this as a solid reason to intentionally write a checksum formula there that does not work in general case, especially when the official one in the original specification does. That's not how reliable software is written. What is the point? To simplify the calculation? Is it worth to study the entire format and evaluate all its edge cases to make sure that in absolutely no case cannot the sum of the payload bytes exceed 65536, only to be able to "simplify" the expression from
-sum(checksum_input) & 0xffff
to0x10000 - sum(checksum_input)
? What if the format will be extended at some point in the future (some structure will be added) and thesum(checksum_input)
will be able to go over that 65536?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess
read_req
is the same aswrite_req
, just without a payload. I wonder if it can bevalid
ated.