From 95d809566b73daffae468851963b0014b4c4182d Mon Sep 17 00:00:00 2001 From: Kateryna Makarova <kateryna.makarova@tuhh.de> Date: Wed, 25 Sep 2024 12:14:59 +0200 Subject: [PATCH] Update Sender_Encoder.ino --- Sender_Encoder.ino | 57 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/Sender_Encoder.ino b/Sender_Encoder.ino index 5face69..3694629 100644 --- a/Sender_Encoder.ino +++ b/Sender_Encoder.ino @@ -5,11 +5,13 @@ The primary board (Board 1) sends data to a secondary board (Board 2) via serial const int ledPin = PE1; // LED pin to indicate data sent (Blink Yellow) HardwareSerial mySerial(D0, D1); // RX = D0, TX = D1 +const int maxRetransmissions = 3; // Maximum number of retransmissions +const unsigned long responseTimeout = 2000; // Timeout in milliseconds void setup() { - pinMode(ledPin, OUTPUT); // Set LED pin as output - Serial.begin(9600); // Communication with PC - mySerial.begin(9600); // Communication with Board 2 via USART + pinMode(ledPin, OUTPUT); // Set LED pin as output + Serial.begin(9600); // Communication with PC + mySerial.begin(9600); // Communication with Board 2 via USART } void loop() { @@ -23,24 +25,51 @@ void loop() { digitalWrite(ledPin, HIGH); delay(1000); // LED on for 1000ms digitalWrite(ledPin, LOW); - delay(2000); // Wait 2 second before sending the next message + + // Wait for the acknowledgment or error message + if (!waitForResponse(encodedMessage)) { + Serial.println("Error: No response from Board 2, reached retransmission limit."); + } } - - if (mySerial.available() > 0) { - String receivedData = mySerial.readStringUntil('\n'); // Read response from Board 2 - Serial.println("Received from Board 2: " + receivedData); - - if (receivedData.startsWith("Error")) { - // Handle retransmission if necessary - String lastMessage = receivedData.substring(receivedData.indexOf(':') + 1); - lastMessage.trim(); // Remove leading and trailing whitespace +} + +bool waitForResponse(String lastMessage) { + int retransmissionCount = 0; + unsigned long startTime = millis(); + + while (retransmissionCount < maxRetransmissions) { + if (mySerial.available() > 0) { + String receivedData = mySerial.readStringUntil('\n'); // Read response from Board 2 + Serial.println("Received from Board 2: " + receivedData); + + if (receivedData.startsWith("Error")) { + // Handle retransmission if necessary + retransmissionCount++; + Serial.println("Error detected. Retransmitting..."); + sendMessageToBoard2(lastMessage); + delay(1000); // Delay before retransmitting + } else { + // Valid response received, no need to retransmit + return true; + } + } + + // Timeout check + if (millis() - startTime > responseTimeout) { + Serial.println("Response timeout. Retrying..."); + retransmissionCount++; sendMessageToBoard2(lastMessage); + delay(1000); // Delay before retransmitting + startTime = millis(); // Reset timeout counter } } + + // Retransmission limit reached + return false; } void sendMessageToBoard2(String message) { - mySerial.println(message); // Send message to Board 2 + mySerial.println(message); // Send message to Board 2 } String encodeData(String data) { -- GitLab