Solar inverter: decode a status frame
Check a frame from the inverter and decode its power, voltage and temperature. A medium embedded C problem (Protocols, framing and checksums), run and graded…
medium · Protocols, framing and checksums · framing · checksum · big-endian · product
Product: Solar inverter (Energy)
Requirement. The monitoring box reads a status frame from the solar inverter every second and must never show a value from a damaged frame.
A frame is 9 bytes: 0xAA, the length 6, power in watts (2 bytes, high byte first), grid voltage in tenths of a volt (2 bytes, high byte first), temperature in °C (one signed byte), a status byte, and a checksum: the XOR of every byte from the length to the status byte.
Decode f (of n bytes) into *out and return true, or return false, leaving *out alone, if the frame is not exactly that.
Starter code
bool inverter_decode(const uint8_t *f, size_t n, inverter_t *out) {
out->power_w = (uint16_t)(f[3] << 8 | f[2]);
out->volts_x10 = (uint16_t)(f[5] << 8 | f[4]);
out->temp_c = (int8_t)f[6];
out->status = f[7];
return true;
}Example tests
A sunny afternoon
uint8_t f[9] = { 0xAA, 6, 0x0B, 0xB8, 0x09, 0x60, 45, 0x01, 0 };
for (int i = 1; i < 8; i++) f[8] ^= f[i];
inverter_t v = { 0 };
bool ok = inverter_decode(f, 9, &v);
printf("%d %u %u %d %u\n", ok, v.power_w, v.volts_x10, v.temp_c, v.status);Expected output:
1 3000 2400 45 1A byte damaged by noise
uint8_t f[9] = { 0xAA, 6, 0x0B, 0xB8, 0x09, 0x60, 45, 0x01, 0 };
for (int i = 1; i < 8; i++) f[8] ^= f[i];
f[3] ^= 0x10;
inverter_t v = { 0 };
printf("%d\n", inverter_decode(f, 9, &v));Expected output:
0Learn it first
- Protocols, framing and checksums · Checksums, CRCs, byte order, framing and parsing a byte stream.
More Protocols, framing and checksums problems
- XOR checksum · Compute the XOR of every byte in a buffer.
- Two's-complement checksum · Compute the checksum byte of an Intel HEX record.
- CRC-8 · Implement a bitwise CRC-8 with polynomial 0x07.
- CRC-16 for Modbus · Implement the reflected CRC-16 used by Modbus RTU.
- Read a big-endian word · Assemble a 32-bit value from four bytes of a packet.
- I2C address byte · Build the first byte of an I2C transfer.
- BCD conversion · Convert between binary-coded decimal and binary.
- Build a UART frame · Assemble the bits a UART sends for one byte with parity.
- Build a framed message · Wrap a payload with a start byte, a length and a checksum.
- SLIP byte stuffing · Escape reserved bytes so a frame delimiter is unambiguous.
- Variable-length integer · Encode a 32-bit value in as few bytes as it needs.
- Validate an NMEA sentence · Check the checksum of a GPS sentence.
- Byte-stream frame parser · Reassemble frames from a byte stream one byte at a time.
- COBS encoding · Remove every zero byte from a packet with one byte of overhead.
- Fix: the bootloader hangs on large chunks · A loop counter is too narrow for the length it counts to.
- Fix: frame lengths come out 256 times too big · A big-endian length field is decoded as little-endian.
- Reliable delivery over a lossy link · Stop-and-wait with a sequence bit, acknowledgements, a retransmit timer and duplicate detection.