uint8_t DCSParams[] = {
0x93, 0x02, 0xCC, 0x07, 0x5A
};
uint8_t calcChecksum(const uint8_t* data) {
uint8_t checksum = data[0];
const uint8_t data_size = data[1] + 3;
for (std::size_t index = 1; index < data_size - 1; ++index)
checksum ^= data[index];
return checksum;
}
uint32_t decodeResponse(const uint8_t* data) {
uint32_t result = 0;
const uint8_t response_size = data[1] + 3;
const uint8_t current_checksum = data[response_size - 1];
const uint8_t expected_checksum = calcChecksum(data);
if (expected_checksum == current_checksum) {
for (int i = response_size - 1; i >= response_size - data[1]; --i) {
result = (result << 8) | data[i - 1];
}
return result;
}
printf("Invalid checksum, expected 0x%.2X got 0x%.2X\n", expected_checksum, current_checksum);
return result;
}
void printData(const char* name, const char* units, const uint8_t* data, const float factor = 1.0f) {
printf("%s %.2f%s\n", name, static_cast<float>(decodeResponse(data)) / factor, units);
}
int main() {
printData("Battery Voltage:", "V", DCSParams, 100.0f);
return 0;
}