From 764380ae5791d04375cc4db9f9cf9665176c4c4c Mon Sep 17 00:00:00 2001 From: travis-farmer Date: Sun, 29 Dec 2024 06:02:06 -0500 Subject: [PATCH] save current, works for now --- IO_EXIO485.cpp | 21 +++++++++++++++++---- IO_EXIO485.h | 15 +++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/IO_EXIO485.cpp b/IO_EXIO485.cpp index 0c0b4ac..21eff9f 100644 --- a/IO_EXIO485.cpp +++ b/IO_EXIO485.cpp @@ -30,13 +30,13 @@ static const byte PAYLOAD_STRING = 2; ************************************************************/ // Constructor for EXIO485 -EXIO485::EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin, int cycleTime) { +EXIO485::EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin) { _serial = &serial; _baud = baud; _txPin = txPin; _busNo = busNo; - _cycleTime = cycleTime * 1000UL; + _retryTime = 1000000UL; // 1 second bufferLength=0; inCommandPayload=PAYLOAD_FALSE; // Add device to HAL device chain @@ -89,10 +89,21 @@ void EXIO485::_loop(unsigned long currentMicros) { } if ( hasTasks()){ // do we have any tasks on the docket - _cycleStartTimeA = _currentMicros; + if (CurrentTaskID == -1) CurrentTaskID = getNextTaskId(); - Task* currentTask = getTaskById(CurrentTaskID); + if (currentTask == nullptr) return; // dead task + + if (!currentTask->completed && _currentMicros - _cycleStartTimeA >= _retryTime) { // after CRC pass, timer is reset + + if (currentTask->currentRetryTimer == 0UL) { // first trigger + currentTask->currentRetryTimer = _currentMicros; // set timer + } else if (_currentMicros - currentTask->currentRetryTimer >= _retryTime) { + currentTask->currentRetryTimer = _currentMicros; // reset timer + currentTask->rxMode = false; // resend data + DIAG(F("EX-IOExplorer485: Fail RX, Retry TX. Task: %d"), CurrentTaskID); + } + } if (!currentTask->rxMode) { currentTask->crcPassFail = 0; @@ -167,6 +178,7 @@ void EXIO485::_loop(unsigned long currentMicros) { int nodeTo = received_data[0]; if (nodeTo == 0) { // for master. flagProc = true; + } } @@ -258,6 +270,7 @@ void EXIO485::_loop(unsigned long currentMicros) { break; } } + _cycleStartTimeA = currentMicros; // reset timer so we do not resend data } } } diff --git a/IO_EXIO485.h b/IO_EXIO485.h index b635fb9..52b1f09 100644 --- a/IO_EXIO485.h +++ b/IO_EXIO485.h @@ -371,7 +371,7 @@ private: unsigned long _cycleStartTime = 0; unsigned long _cycleStartTimeA = 0; unsigned long _timeoutStart = 0; - unsigned long _cycleTime; // target time between successive read/write cycles, microseconds + unsigned long _retryTime; // target time between successive read/write cycles, microseconds unsigned long _timeoutPeriod; // timeout on read responses, in microseconds. unsigned long _currentMicros; // last value of micros() from _loop function. unsigned long _postDelay; // delay time after transmission before switching off transmitter (in us) @@ -418,12 +418,13 @@ struct Task { int crcPassFail; bool completed; bool processed; + unsigned long currentRetryTimer; }; - static const int MAX_TASKS = 1000; + static const int MAX_TASKS = 100; // we don't want to run out of task slots, but memory? long taskIDCntr = 1; long CurrentTaskID = -1; int taskResendCount = 0; - Task taskBuffer[MAX_TASKS]; // Buffer to hold up to 100 tasks + Task taskBuffer[MAX_TASKS]; int currentTaskIndex = 0; void addTask(const uint8_t* cmd, int byteCount, uint8_t retFlag) { @@ -448,6 +449,7 @@ struct Task { taskBuffer[emptySlot].gotCallback = false; taskBuffer[emptySlot].completed = false; taskBuffer[emptySlot].processed = false; + taskBuffer[emptySlot].currentRetryTimer = 0UL; taskIDCntr++; if (taskIDCntr >= 5000000) taskIDCntr = 1; taskBuffer[emptySlot].taskID = taskIDCntr; @@ -485,6 +487,7 @@ struct Task { if (taskBuffer[i].taskID == id) { taskBuffer[i].completed = true; // completed taskBuffer[i].taskID = -1; // unassigned + taskBuffer[i].currentRetryTimer = 0UL; // stop timer CurrentTaskID = getNextTaskId(); break; } @@ -520,8 +523,8 @@ struct Task { EXIOWRAN = 0xEA, // Flag we're sending an analogue write (PWM) EXIOERR = 0xEF, // Flag we've received an error }; - static void create(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin=-1, int cycleTime=500) { - new EXIO485(busNo, serial, baud, txPin, cycleTime); + static void create(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin=-1) { + new EXIO485(busNo, serial, baud, txPin); } HardwareSerial* _serial; int _CommMode = 0; @@ -599,7 +602,7 @@ struct Task { } protected: - EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin, int cycleTime); + EXIO485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, int8_t txPin); public: