implment CRCcheck

This commit is contained in:
nago 2025-04-26 23:55:28 +02:00
parent 46ac08ac9c
commit 604de64deb
3 changed files with 73 additions and 17 deletions

BIN
docs/Enedis-NOI-CPT_54E.pdf Normal file

Binary file not shown.

84
tic.cpp
View File

@ -23,6 +23,9 @@ int nbActions;
static struct GroupDetail processGroup(String group) { static struct GroupDetail processGroup(String group) {
struct GroupDetail gd; struct GroupDetail gd;
gd.globale = group;
unsigned char computedChecksum = calcCheckSum(group);
int indexgrp = group.indexOf(HT); int indexgrp = group.indexOf(HT);
gd.name = group.substring(0, indexgrp); gd.name = group.substring(0, indexgrp);
@ -32,15 +35,20 @@ static struct GroupDetail processGroup(String group) {
group = group.substring(indexgrp + 1); group = group.substring(indexgrp + 1);
indexgrp = group.indexOf(HT); indexgrp = group.indexOf(HT);
String key = group.substring(0, indexgrp); String key = "";
group = group.substring(indexgrp + 1);
indexgrp = group.indexOf(HT);
if (indexgrp != -1) // some parameters may have hour recording. if (indexgrp != -1) // some parameters may have hour recording.
{ {
gd.horodate = gd.value; gd.horodate = gd.value;
gd.value = key; gd.value = group.substring(0, indexgrp);
group = group.substring(indexgrp + 1);
} }
//gd.checksum = group;
if (group[0]==computedChecksum) {
gd.checkok = true;
} else {
gd.checkok = false;
}
return gd; return gd;
} }
@ -114,19 +122,18 @@ static void processTrame(String &data) {
while ((SelectedEtiquette[t] != gd.name) && (t < NB_ETIQUETTE)) { while ((SelectedEtiquette[t] != gd.name) && (t < NB_ETIQUETTE)) {
++t; ++t;
} }
// If a match is found, update the corresponding TicValues entry // If a match is found, update the corresponding TicValues entry if the group confirms the checksum
if (t < NB_ETIQUETTE) { if (t < NB_ETIQUETTE) {
//If there is a value update //If there is a value update
if (TicValues[t].value.compareTo(gd.value) != 0) { if (TicValues[t].value.compareTo(gd.value) != 0 && gd.checkok){
//There is some noise on instantaneous value, filter //There is some noise on instantaneous value, filter
if (SelectedEtiquette[t] == "SINSTS" || SelectedEtiquette[t] == "SINSTS1" || SelectedEtiquette[t] == "SINSTS2" || SelectedEtiquette[t] == "SINSTS3") { if (SelectedEtiquette[t] == "SINSTS" || SelectedEtiquette[t] == "SINSTS1" || SelectedEtiquette[t] == "SINSTS2" || SelectedEtiquette[t] == "SINSTS3") {
int oldval = TicValues[t].value.toInt(); int oldval = TicValues[t].value.toInt();
int newcal = (gd.value.toInt() + oldval) / 2; int newcal = (gd.value.toInt() + oldval) / 2;
if (newcal < oldval*0.92 || newcal > oldval*1.02){ if (newcal < oldval * 0.92 || newcal > oldval * 1.02) {
gd.updated = true; gd.updated = true;
TicValues[t] = gd; TicValues[t] = gd;
} } else {
else {
TicValues[t].value = String(newcal); TicValues[t].value = String(newcal);
} }
} else { } else {
@ -295,9 +302,11 @@ String ticValuesAsJson() {
// Use snprintf to construct the JSON string efficiently // Use snprintf to construct the JSON string efficiently
snprintf(jres, sizeof(jres), snprintf(jres, sizeof(jres),
"\"%s\": \"%s\"", "\"%s\": [\"%s\", \"%s\", \"%s\"]",
SelectedEtiquette[i].c_str(), SelectedEtiquette[i].c_str(),
TicValues[i].value.c_str()); TicValues[i].globale.c_str(),
TicValues[i].value.c_str(),
TicValues[i].checkok?"true":"false");
response += jres; response += jres;
} }
@ -426,3 +435,46 @@ void readTicPort() {
} }
} }
} }
// Function to calculate the checksum
unsigned char calcCheckSum(const String &data) {
unsigned int sum = 0;
// Calculate the sum of ASCII values
for (size_t i = 0; i < data.length() - 1; ++i) {
sum += data[i];
}
// Truncate the sum to 6 bits
sum &= 0x3F;
// Add 0x20 to get the final checksum
unsigned char checksum = sum + 0x20;
return checksum;
}
unsigned char calcCheckSum2(GroupDetail *data) {
char c;
uint sum = 2 * HT;
if (data->name.length() && data->value.length()) {
for (char &c : data->name) {
if (c >= 0x20 && c <= 0x7E) {
sum += c;
} else {
return 0;
}
}
for (char &c : data->value) {
if (c >= 0x20 && c <= 0x7E) {
sum += c;
} else {
return 0;
}
}
return (sum & 0x3f) + 0x20;
}
return 0;
}

4
tic.h
View File

@ -13,6 +13,7 @@
#define HTTP_PORT 80 //port #define HTTP_PORT 80 //port
// Définition des constantes pour les délimiteurs de trame TIC // Définition des constantes pour les délimiteurs de trame TIC
//Uniquement le mode standard est supporté
//CF document ENEDIS //CF document ENEDIS
#define STX 0x02 // Début de la trame : 0x02 (<STX>) #define STX 0x02 // Début de la trame : 0x02 (<STX>)
#define ETX 0x03 // Fin de la trame : 0x03 (<ETX>) End Of Text #define ETX 0x03 // Fin de la trame : 0x03 (<ETX>) End Of Text
@ -29,10 +30,12 @@
// Structure pour stocker les détails d'un groupe TIC // Structure pour stocker les détails d'un groupe TIC
struct GroupDetail { struct GroupDetail {
String globale;
String name; // Nom de l'étiquette String name; // Nom de l'étiquette
String value; // Valeur associée à l'étiquette String value; // Valeur associée à l'étiquette
String horodate; // Horodatage de la valeur String horodate; // Horodatage de la valeur
bool updated; bool updated;
bool checkok; //status of checksum
}; };
// Structure pour les bits de statut du registre // Structure pour les bits de statut du registre
@ -163,4 +166,5 @@ String ticValuesAsJson();
String ticBasicValuesAsJson(); String ticBasicValuesAsJson();
void mqttPublish(PubSubClient *mqttclient); void mqttPublish(PubSubClient *mqttclient);
void mqttForcePublish(PubSubClient *mqttclient); void mqttForcePublish(PubSubClient *mqttclient);
unsigned char calcCheckSum(const String &data);//(GroupDetail *data);
#endif #endif