I'm trying to calculate a checksum, and I have to admit my binary manipulation knowlege is failing me.
If anyone could help with the below (and improve the title of this question) I'd be most grateful.
(I've tried quite a lot of things, but I think I'm just not going about it correctly)
A previous question I asked is here, but the technique is different, I think - How to build byte array frame and calculate checksum
Type 0x56 “V”
Checksum = 0x02+0x30+0x30+0x81+0x56+0x8B+0x82 = 0x46
Checksum1 = Checksum | 0xF0 = 0x46 | 0xF0 = 0xF6
Checksum2 = Checksum | 0x0F = 0x46 | 0x0F = 0x4F
print("\n" + " ".join("\\x%02X" % v for v in bytes))
frame = bytearray([0x02,0x30,0x30,0x81,0x56,0x8B,0x82])
The result is obtained as an unsigned char.
This means that you must only use the 8 least significant bits to calculate.
>>> (0x02 + 0x30 + 0x30 + 0x81 + 0x56 + 0x8b + 0x82) 582 >>> (0x02 + 0x30 + 0x30 + 0x81 + 0x56 + 0x8b + 0x82) & 0xff 70
And then within those 8 bits you need to set the 4 most and least significant bits.
>>> 70 | 0xf0 246 >>> 70 | 0x0f 79
This then results in 0xf6 and 0x4f as shown in the checksum bytes.