Donnerstag, 1. Dezember 2011

Integer overflows

Yesterday my newly installed energy monitoring system broke. While the dryer was running, the power usage suddenly jumped from a realistic 3500 Watts to 45000 Watt or so. What happened was, that an overflow of one or more variables must have occured somewhere in the firmware. I use unsigned int16's (uint16_t) for holding the 10 Bit sample result, which then get squared and summed up.

As i capture 200 samples for my True-RMS current measurement, the maximum value for each sum should be 200*1023*1023=209305800 which is twenty times below 2^32 - 1 (4294967295) which is the limit for an unsinged 32bit integer value.

Anyhow, the energy meter went beserk: The E_total counter quickly jumped into cosmic regions:

I still dont now what exactly caused the problem, i guess the snprintf routine i use to output the meter readings when polled by my monitoring server is buggy. Anyhow: the meter is back online and running again.

Keine Kommentare:

Kommentar veröffentlichen