Closed tomek-szczesny closed 1 year ago
Looks like a buffer overflow. The buffer_checksum
array is 8 bytes. Just enough for "xx,yy\r\n" and a terminating \0
that sprintf()
adds. These buffers are stack-allocated, with the channel1 array directly after it in memory. The sprintf is thus writing \0
to the start of that string array, so nothing gets printed.
Fixing the cast should solve the problem, I think:
sprintf(buffer_checksum, "%02x,%02x\r\n", (byte)((~checksum8)+1), checksum8_xor);
I'm both grateful for your explanation and proud of myself I found that bug myself. Thanks a lot!
Fixed in release 2.0 (https://wiki.odroid.com/accessory/power_supply_battery/smartpower3#apr03_v20).
I noticed that today with up-to-date firmware, at 1000ms intervals via ttyUSB:
There is no visible pattern how often it happens, and the last number is not always 12. I've seen 10, 12, 14, 16, 18 and 1a. The number "100" is suspicious and always is there if error happens.
https://github.com/hardkernel/smartpower3/blob/933acc1c3775d7c108ffab2ebf82df65e639f60e/smartpower3/smartpower3.ino#L113 I'm worried that "(byte)(~checksum8)+1" may actually return 0x100 if checksum8 == 0x00.
Still don't know why would it influence data sent in the past.