2

I have a system that involves an Arduino Due connected to a Raspi 4 through UART in which the Due sends a heartbeat signal over UART for the pi to receive and send back as confirmation. One of the events that I need to handle is re-establishing the UART connection between the two devices if the program receiving UART on the pi (or the pi itself) fails for any reason and reboots. At the moment, I have the initial heartbeat working (as long as the pi program starts first) but when I stop the pi program to simulate a fatal error and restart it, the Due doesn't seem to receive heartbeat confirmation until it's reset as well.

Is there a clean way to reset just the one Serial connection from the Arduino script? I thought of using Serial.end() and Serial.begin() but that's really ugly and I'd prefer to avoid it. None of the other Serial library methods seem to provide the ability to reset the connection.

I'm using Serial2 at 115200 baud.

Edit: Including relevant heartbeat code:

#include <DueTimer.h>

bool heartbeat = true;

void rx_heartbeat() {
  int received_data;

  if (Serial2.available() > 0) {
    received_data = Serial2.read();
    heartbeat = true;
    Serial.println("beat");
  }
}

void setup() {
  Serial2.begin(115200);
  Timer1.attachInterrupt(heartbeat_ISR).setFrequency(1).start();
}

void loop() {
  rx_heartbeat();
}

void heartbeat_ISR() {
  if (heartbeat) {
    heartbeat = false;
  } else {
    // no heartbeat returned - stop normal operation
  }
  Serial.println("heart");
  Serial2.write(255);
}
5
  • 1
    You want to reset the connection, that is to turn it off and back on again. Why do you think end and then begin isn't a good solution for that? What's ugly about it?
    – Delta_G
    Aug 26 '20 at 1:31
  • why do you need to use 115200 baud for a heartbeat?
    – jsotola
    Aug 26 '20 at 1:38
  • @Delta_G I could be approaching it very wrong, but the way I was thinking of implementing them would mean that both Serial.end() and Serial.begin() would be executed with every loop iteration. It just seems messy. Aug 26 '20 at 1:40
  • @jsotola it's definitely not necessary to have that high of a baud for a heartbeat. It's just my default rate I set for serial connections in general Aug 26 '20 at 1:41
  • Why would you do it every loop? Does the communication need to be reset every loop? Just reset it when needed, like when you stop getting your heartbeat.
    – Delta_G
    Aug 26 '20 at 2:32
1

Silly me forgot to actually upload the code that fixed my issue. The code shown in the question is correct and works as intended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.