CRC16 in SML Nachrichten

Ich beschäftige mich mit SML-Nachrichten von Stromzählern und aktuell mit den darin enthaltenen crc16 Prüfsummen.

Mein einfaches Testprogramm basiert auf dem crc16 Algorithmus, den ich bei
olliiiver/sml_parser gefunden habe. Hier habe ich Beispiel-Testdaten, die @my_xy_projekt schonmal aufgedröselt hat, verwendet.

// crc16 Test  SMLMessage in Testdaten
#include <Streaming.h>
static unsigned short crc = 0xFFFF;
#include "smlCrcTable.h"
#include "crc16.h"
#include "testmsg2.h"  // definiert byte testmsg[] und byte crcmsg[3]

void setup() {
Serial.begin(115200);
delay(600); // erste Meldung, wenn SerialMonitor nach Hochladen wieder aktiv wird

Serial << "\nESPCRC16Test bildet aus gespeicherten SML Daten die crc16 Prüfsumme"  << endl;

for (auto& b: testmsg) crc16(b);
Serial <<_HEX(crc) << "  check: " << _HEX(crcmsg[1]) << " " << _HEX(crcmsg[2]) << endl;
uint16_t comp = (crcmsg[2] << 8) + crcmsg[1];  // ??? Reihenfolge
uint16_t chk = crc^comp;   /// ??? crc und comp sind nicht gleich, sondern komplementär 
Serial << _HEX(comp) << "\n---- \n" << _HEX(chk) << ((chk == 0xFFFF)?"  OK":" Fehler !!") << endl;
}
void loop() { }
“crc16.h“
void crc16(const unsigned char &b) {
#ifdef ARDUINO
  crc =
      pgm_read_word_near(&smlCrcTable[(b ^ crc) & 0xff]) ^ (crc >> 8 & 0xff);
#else
  crc = smlCrcTable[(b ^ crc) & 0xff] ^ (crc >> 8 & 0xff);
#endif
}
smlCrcTable.h
#include <stdint.h>
#ifdef ARDUINO
#include <Arduino.h>
 static const uint16_t smlCrcTable[256] PROGMEM =
#else
 static const uint16_t smlCrcTable[256] =
#endif
    {0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF, 0x8C48,
     0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7, 0x1081, 0x0108,
     0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E, 0x9CC9, 0x8D40, 0xBFDB,
     0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876, 0x2102, 0x308B, 0x0210, 0x1399,
     0x6726, 0x76AF, 0x4434, 0x55BD, 0xAD4A, 0xBCC3, 0x8E58, 0x9FD1, 0xEB6E,
     0xFAE7, 0xC87C, 0xD9F5, 0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E,
     0x54B5, 0x453C, 0xBDCB, 0xAC42, 0x9ED9, 0x8F50, 0xFBEF, 0xEA66, 0xD8FD,
     0xC974, 0x4204, 0x538D, 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB,
     0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1, 0xAB7A, 0xBAF3, 0x5285,
     0x430C, 0x7197, 0x601E, 0x14A1, 0x0528, 0x37B3, 0x263A, 0xDECD, 0xCF44,
     0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72, 0x6306, 0x728F, 0x4014,
     0x519D, 0x2522, 0x34AB, 0x0630, 0x17B9, 0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5,
     0xA96A, 0xB8E3, 0x8A78, 0x9BF1, 0x7387, 0x620E, 0x5095, 0x411C, 0x35A3,
     0x242A, 0x16B1, 0x0738, 0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862,
     0x9AF9, 0x8B70, 0x8408, 0x9581, 0xA71A, 0xB693, 0xC22C, 0xD3A5, 0xE13E,
     0xF0B7, 0x0840, 0x19C9, 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF,
     0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324, 0xF1BF, 0xE036, 0x18C1,
     0x0948, 0x3BD3, 0x2A5A, 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E, 0xA50A, 0xB483,
     0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5, 0x2942, 0x38CB, 0x0A50,
     0x1BD9, 0x6F66, 0x7EEF, 0x4C74, 0x5DFD, 0xB58B, 0xA402, 0x9699, 0x8710,
     0xF3AF, 0xE226, 0xD0BD, 0xC134, 0x39C3, 0x284A, 0x1AD1, 0x0B58, 0x7FE7,
     0x6E6E, 0x5CF5, 0x4D7C, 0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1,
     0xA33A, 0xB2B3, 0x4A44, 0x5BCD, 0x6956, 0x78DF, 0x0C60, 0x1DE9, 0x2F72,
     0x3EFB, 0xD68D, 0xC704, 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232,
     0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68, 0x3FF3, 0x2E7A, 0xE70E,
     0xF687, 0xC41C, 0xD595, 0xA12A, 0xB0A3, 0x8238, 0x93B1, 0x6B46, 0x7ACF,
     0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9, 0xF78F, 0xE606, 0xD49D,
     0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330, 0x7BC7, 0x6A4E, 0x58D5, 0x495C,
     0x3DE3, 0x2C6A, 0x1EF1, 0x0F78};
testmsg2.h
const byte testmsg[] {
0x76,                                          // Open MSG
0x05, 0x01, 0x02, 0x03, 0x04,
0x62, 0x00,
0x62, 0x00,
0x72,
0x65, 0x00, 0x00, 0x01, 0x01,
0x76,
0x01,
0x0B, 0x0A, 0x01, 0x4C, 0x4F, 0x47, 0x00, 0x05, 0x7C, 0xB6, 0xA6,
0x05, 0x01, 0x02, 0x03, 0x04,
0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x72,
0x62, 0x01,
0x65, 0x01, 0x0E, 0xBE, 0xF1,
0x01, };

const byte crcmsg\[\] { 0x63, 0x25, 0x2C, };  // 0x2C25 ^ 0xD3DA
// 0x00,                                            // Ende MSG

Dieser Test zeigt mit viel Mühe :upside_down_face: , ob der crc -PrüfWert in der Nachricht stimmt.

  • Mich wundert allerdings, dass bei dem crc16-Wert die high- und low Bytes gegenüber anderen Werten mit dem Typ 0x63 (uint16) vertauscht sind. Und dass das Ergebnis des Algorithmus nicht der crc16 - Wert sondern dessen xor-Komplement sind.

Die Prüfsumme (Element ‚crc16’) ist als CRC16 nach DIN EN 62056-46 zu berechnen.

Da noch tiefer in die Grundlagenforschung einzusteigen wollte ich mir bei meiner begrenzten verbleibenden Lebenserwartung ersparen und frage daher hier:

Hat jemand hier ( @my_xy_projekt :wink:) sich schonmal die Mühe der crc16-Prüfung gemacht und ein einfacheres System gefunden?

Ja.
Wir haben das komplett durch gehabt und ich hatte das in meinem Ansatz schon soweit, dass sowohl die Nachricht als auch der Msg-Block auf gültige CRC abgeklopft wird.

Wenn ich nachher Zeit habe, schau ich mal, ob ich das Paket zerlegen kann.

Edit: Ich hab den alten Post gefunden.
In der DIN ist folgender Code abgedruckt(!)

Mit was für einem Controller liest Du den Zähler aus?

"Einfach" weiß ich nicht, aber funktionierend, sogar nach einem Zählerwechsel:

#include <rom/crc.h>
...
crcWert = (~crc16_le((uint16_t)~(0xffff), smlArray, smlIdx - 1)) ^ 0xffff;
if ( crcWert == smlArray[smlIdx] * 256 + smlArray[smlIdx - 1] )
{
  debPrintf("CRC-16/X25 Überprüfung bestanden.\n");
  schritt = Schritt::AUSW_INIT;
} else {
  debPrintf("Fehler: CRC-16/X25 = 0x%04X ungleich CRC aus Daten = 0x%04X\n", crcWert, smlArray[smlIdx] * 256 + smlArray[smlIdx - 1]);
  schritt = Schritt::START;
}

Wegen der Lebenszeit habe ich mich nicht im Detail damit auseinandergesetzt :blush:

??
Ein PT333 mit Transistor, LED und zwei Widerständen (damit man sieht, dass der "Sensor" richtig positioniert ist), hängt mit 3 Adern an einem ESP8266. Der liest z.Zt. erstmal nur die Daten und protokolliert sie auf der Seriellen Schnittstelle:
" 1B 1B 1B 1B 01 01 01 01 76 ... " usw.
Daraus dann c-lesbare Daten-Dateien für Testprogramme per Editor. ( Ersetze Leerzeichen durch ", 0x" )
Aber das ist erstmal nicht das Thema ...

Danke für deinen Link @my_xy_projekt , da hab ich erstmal zu tun.

Aber was hinter
#include <rom/crc.h> steckt, verrätst du bitte ?

Ich hab das ja alles schon fertig. Wenn ich das git clone, könnte ich da was für Dich extrahieren.

Sicher doch, das ist ein Teil der ESP32-Installation:
esp32\include\esp_rom\esp32\include\esp32\rom\crc.h

crc.h
/*
 * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#ifndef ROM_CRC_H
#define ROM_CRC_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/** \defgroup uart_apis, uart configuration and communication related apis
  * @brief uart apis
  */

/** @addtogroup uart_apis
  * @{
  */


/*           Notes about CRC APIs usage
 * The ESP32 ROM include some CRC tables and CRC APIs to speed up CRC calculation.
 * The CRC APIs include CRC8, CRC16, CRC32 algorithms for both little endian and big endian modes.
 * Here are the polynomials for the algorithms:
 * CRC-8        x8+x2+x1+1                                              0x07
 * CRC16-CCITT  x16+x12+x5+1                                            0x1021
 * CRC32        x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x1+1     0x04c11db7
 *
 * These group of CRC APIs are designed to calculate the data in buffers either continuous or not.
 * To make it easy, we had added a `~` at the beginning and the end of the functions.
 * To calculate non-continuous buffers, we can write the code like this:
 *     init = ~init;
 *     crc = crc32_le(init, buf0, length0);
 *     crc = crc32_le(crc, buf1, length1);
 *     crc = ~crc;
 *
 * However, it is not easy to select which API to use and give the correct parameters.
 * A specific CRC algorithm will include this parameters: width, polynomials, init, refin, refout, xorout
 *     refin and refout show the endian of the algorithm:
 *         if both of them are true, please use the little endian API.
 *         if both of them are false, please use the big endian API.
 *     xorout is the value which you need to be xored to the raw result.
 * However, these group of APIs need one '~' before and after the APIs.
 *
 * Here are some examples for CRC16:
 * CRC-16/CCITT, poly = 0x1021, init = 0x0000, refin = true, refout = true, xorout = 0x0000
 *     crc = ~crc16_le((uint16_t)~0x0000, buf, length);
 *
 * CRC-16/CCITT-FALSE, poly = 0x1021, init = 0xffff, refin = false, refout = false, xorout = 0x0000
 *     crc = ~crc16_be((uint16_t)~0xffff, buf, length);
 *
 * CRC-16/X25, poly = 0x1021, init = 0xffff, refin = true, refout = true, xorout = 0xffff
 *     crc = (~crc16_le((uint16_t)~(0xffff), buf, length))^0xffff;
 *
 * CRC-16/XMODEM, poly= 0x1021, init = 0x0000, refin = false, refout = false, xorout = 0x0000
 *     crc = ~crc16_be((uint16_t)~0x0000, buf, length);
 *
 *
 */

/**
  * @brief CRC32 value that is in little endian.
  *
  * @param  uint32_t crc : init crc value, use 0 at the first use.
  *
  * @param  uint8_t const *buf : buffer to start calculate crc.
  *
  * @param  uint32_t len : buffer length in byte.
  *
  * @return None
  */
uint32_t crc32_le(uint32_t crc, uint8_t const *buf, uint32_t len);

/**
  * @brief CRC32 value that is in big endian.
  *
  * @param  uint32_t crc : init crc value, use 0 at the first use.
  *
  * @param  uint8_t const *buf : buffer to start calculate crc.
  *
  * @param  uint32_t len : buffer length in byte.
  *
  * @return None
  */
uint32_t crc32_be(uint32_t crc, uint8_t const *buf, uint32_t len);

/**
  * @brief CRC16 value that is in little endian.
  *
  * @param  uint16_t crc : init crc value, use 0 at the first use.
  *
  * @param  uint8_t const *buf : buffer to start calculate crc.
  *
  * @param  uint32_t len : buffer length in byte.
  *
  * @return None
  */
uint16_t crc16_le(uint16_t crc, uint8_t const *buf, uint32_t len);

/**
  * @brief CRC16 value that is in big endian.
  *
  * @param  uint16_t crc : init crc value, use 0 at the first use.
  *
  * @param  uint8_t const *buf : buffer to start calculate crc.
  *
  * @param  uint32_t len : buffer length in byte.
  *
  * @return None
  */
uint16_t crc16_be(uint16_t crc, uint8_t const *buf, uint32_t len);

/**
  * @brief CRC8 value that is in little endian.
  *
  * @param  uint8_t crc : init crc value, use 0 at the first use.
  *
  * @param  uint8_t const *buf : buffer to start calculate crc.
  *
  * @param  uint32_t len : buffer length in byte.
  *
  * @return None
  */
uint8_t crc8_le(uint8_t crc, uint8_t const *buf, uint32_t len);

/**
  * @brief CRC8 value that is in big endian.
  *
  * @param  uint32_t crc : init crc value, use 0 at the first use.
  *
  * @param  uint8_t const *buf : buffer to start calculate crc.
  *
  * @param  uint32_t len : buffer length in byte.
  *
  * @return None
  */
uint8_t crc8_be(uint8_t crc, uint8_t const *buf, uint32_t len);

/**
  * @}
  */

#ifdef __cplusplus
}
#endif


#endif

Das liest sich so, als ob anders als bei anderen SML Werten vom Typ uint16 ( 0x63 ) auch hier das HI Byte erst nach dem LO Byte kommt.
Wenn ich einen online CRC16 Calculator verwende, finde ich zu meinen Testdaten passende Ergebnisse mit dem Polynom 0x1021 aber nur in umgekehrter Byte-Reihenfolge

Ah ja, Danke. Das hatte ich mit meinem ESP8266 nicht auf dem Schirm.

Ich war an meinem Stromverbrauch interessiert und nicht an einem "schönen" Programm, daher habe ich auf eine pragmatische Lösung für meinen Zähler gesetzt. Die Ausführungen von my_xy_projekt haben zwar sehr zu meinem Verständnis beigetragen, dennoch war eine normgerechte Verwendung der SML-Sprache nie mein Ziel. Daher habe ich auch CRC ohne tiefere Gedanken einfach verwendet. Für mich gibt es spannendere Dinge :innocent: