Hope someone can help here. Below is a small part of my sketch am coding that reads serial data of ASCII characters bigger then the space character untill a > character is received then subroutine exits. But if byte i gets to 16 before a > character is received the subroutine also exits. Works well.
So my problem now is that what if this subroutine is called but no device is attached/goes faulty? My tests show that my sketch hangs inside this subroutine.
What i would like todo and think is the right way to go is to add a bailout function after lets say 10 seconds. Could someone help me implement this into the subroutine below? Am just not sure how todo it and if the millis() would be a good answer. Would be much grateful.
byte STN_read(char *str)
byte i = 0;
unsigned long time_out = millis();
while((temp = SoftwareSerial.read()) != '>' && time_out <= 3000)
if(temp >= ' ')
str[i++] = temp;
if(i >= 16)
return 0; //Read is probably bad.
str[i] = '\0';
return 1; //Read is probably good.