• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Arduino call function on serial.timeout

dr pepper

Well-Known Member
Most Helpful Member
I have a 'duino that receives speed info from another 'duino via serial.
I need to detect a loss of comms, so I just send a value which has 1 added to it each time, so the receiving 'duino can tell when a 'block' of data goes missing.
I'd like to do this 'better', using serial timeout if it can be done, the 'duino that receives the values via serial will have to request the values, thats no problem.
So can I call a function on a serial timeout, and can I reset the timeout period?
 

DrG

Active Member
something like this (slopped together quickly for illustration)

C:
  unsigned long wait = 15000;

void setup() {
  Serial.begin(9600);
}

void loop() {
  uint8_t BytesIn[60];
  size_t count;
 
  Serial.setTimeout(wait);
  Serial.print("Enter 10 bytes to serial or wait ");
  Serial.print(wait / 1000);
  Serial.println(" seconds");
  count = Serial.readBytes(BytesIn, 10); // BLOCKS here
  if (!count) {
    Serial.println("Timed out with no characters sent");
    TONC();
  }
  else if (count < 10) {
    Serial.print("Timed out with ");
    Serial.print(count);
    Serial.println(" characters sent");
    TOCS();
  }
  else {
    Serial.println("10 characters sent");
    CSOK();
  }
}

void TONC() {
  wait = 30000;
}

void TOCS() {
  wait = 20000;
}

void CSOK() {
  wait = 15000;
}
 

dr pepper

Well-Known Member
Most Helpful Member
Thanks for that.
I didnt think of a fixed length of bytes, the length can easily be fixed, so that would work.
 

EE World Online Articles

Loading

 
Top