From 8d2b71cd04151ae67c34669d17c05b43868786ed Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Mon, 20 Jun 2022 20:47:21 +0200 Subject: [PATCH] Implemented resend of whole requests --- lib/Hoymiles/src/HoymilesRadio.cpp | 39 +++++++++++++++++++++--------- lib/Hoymiles/src/HoymilesRadio.h | 4 +++ lib/Hoymiles/src/types.h | 12 ++++++++- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/Hoymiles/src/HoymilesRadio.cpp b/lib/Hoymiles/src/HoymilesRadio.cpp index dbd6515..942834d 100644 --- a/lib/Hoymiles/src/HoymilesRadio.cpp +++ b/lib/Hoymiles/src/HoymilesRadio.cpp @@ -86,9 +86,13 @@ void HoymilesRadio::loop() if (nullptr != inv) { uint8_t verifyResult = inv->verifyAllFragments(); if (verifyResult == 255) { - Serial.println(F("Should Retransmit whole thing")); - // todo: irgendwas tun wenn garnichts ankam.... - _busyFlag = false; + if (currentTransaction.sendCount < MAX_RESEND_COUNT) { + Serial.println(F("Nothing received, resend whole request")); + sendEsbPacket(currentTransaction.target, currentTransaction.mainCmd, currentTransaction.subCmd, currentTransaction.payload, currentTransaction.len, currentTransaction.timeout, true); + } else { + Serial.println(F("Nothing received, resend count exeeded")); + _busyFlag = false; + } } else if (verifyResult == 254) { Serial.println(F("Retransmit timeout")); @@ -208,17 +212,28 @@ void HoymilesRadio::sendEsbPacket(serial_u target, uint8_t mainCmd, uint8_t subC static uint8_t txBuffer[MAX_RF_PAYLOAD_SIZE]; if (!resend) { - memset(txBuffer, 0, MAX_RF_PAYLOAD_SIZE); - - txBuffer[0] = mainCmd; - convertSerialToPacketId(&txBuffer[1], target); // 4 byte long - convertSerialToPacketId(&txBuffer[5], DtuSerial()); // 4 byte long - txBuffer[9] = subCmd; - - memcpy(&txBuffer[10], payload, len); - txBuffer[10 + len] = crc8(txBuffer, 10 + len); + currentTransaction.sendCount = 0; + } else { + currentTransaction.sendCount++; } + memset(txBuffer, 0, MAX_RF_PAYLOAD_SIZE); + + txBuffer[0] = mainCmd; + convertSerialToPacketId(&txBuffer[1], target); // 4 byte long + convertSerialToPacketId(&txBuffer[5], DtuSerial()); // 4 byte long + txBuffer[9] = subCmd; + + memcpy(&txBuffer[10], payload, len); + txBuffer[10 + len] = crc8(txBuffer, 10 + len); + + currentTransaction.mainCmd = mainCmd; + currentTransaction.target = target; + currentTransaction.subCmd = subCmd; + memcpy(currentTransaction.payload, payload, len); + currentTransaction.len = len; + currentTransaction.timeout = timeout; + _radio->stopListening(); _radio->setChannel(getTxNxtChannel()); openWritingPipe(target); diff --git a/lib/Hoymiles/src/HoymilesRadio.h b/lib/Hoymiles/src/HoymilesRadio.h index 8e883a4..2158873 100644 --- a/lib/Hoymiles/src/HoymilesRadio.h +++ b/lib/Hoymiles/src/HoymilesRadio.h @@ -11,6 +11,8 @@ // number of fragments hold in buffer #define FRAGMENT_BUFFER_SIZE 30 +#define MAX_RESEND_COUNT 3 + class HoymilesRadio { public: void init(); @@ -55,4 +57,6 @@ private: serial_u _activeSerial; bool _busyFlag = false; + + inverter_transaction_t currentTransaction; }; \ No newline at end of file diff --git a/lib/Hoymiles/src/types.h b/lib/Hoymiles/src/types.h index 9e317a3..86ed49b 100644 --- a/lib/Hoymiles/src/types.h +++ b/lib/Hoymiles/src/types.h @@ -13,4 +13,14 @@ union serial_u { typedef struct { uint8_t fragment[MAX_RF_PAYLOAD_SIZE]; uint8_t len; -} fragment_t; \ No newline at end of file +} fragment_t; + +typedef struct { + serial_u target; + uint8_t mainCmd; + uint8_t subCmd; + uint8_t payload[MAX_RF_PAYLOAD_SIZE]; + uint8_t len; + uint32_t timeout; + uint8_t sendCount; +} inverter_transaction_t; \ No newline at end of file