====== Checksum des trames de téléinformation enedis ======
===== Documentation Enedis =====
{{ :domotique:enedis_checksum.webp |}}
===== Python =====
def checksum(frame):
somme = 0
for c in frame[:-1]:
somme += ord(c)
somme_trunc = bin(somme)[-6:]
calc_check = chr(32 + int(somme_trunc, 2))
if calc_check == frame[-1:]:
return True
else:
return False
===== JavaScript =====
function checksum(frame) {
let somme = 0;
for (let i = 0; i < frame.length - 1; i++) {
somme += frame.charCodeAt(i);
}
const sommeTrunc = (somme & 0b111111).toString(2);
const calcCheck = String.fromCharCode(32 + parseInt(sommeTrunc, 2));
if (calcCheck === frame.slice(-1)) {
return true;
} else {
return false;
}
}