I have written a function to receive a line of text via UART. This function waits for a newline character to know that the incoming text is complete. The problem is, sometimes the newline takes a long time to come, or sometimes it doesn't. I'd like to be able to timeout after a while and return some value indicating the response didn't come. Any suggestions?
Here's the function I've got. Note that I'm using two UARTs so I've set one as UART1 and the other as UART0 and renamed the standard UART studio functions accordingly.
int
GSMgetResponse(char *expResponse)
{
bool readResponse = true; // Keeps the loop open while getting message
int readLine = 1; // Counts the lines of the message
char *GSMresponse = NULL; // Use to grab input
static char g_cInput[128]; // String input to a UART
while ( readResponse )
{
// Grab a line
UART1gets(g_cInput,sizeof(g_cInput));
// Stop after newline and store to global message array
GSMresponse = strtok(g_cInput,"\n");
strcpy(responseLine[readLine], GSMresponse);
// If this line has our expected response we've got the whole message
if ( strstr(responseLine[readLine],expResponse) != '\0' ){readResponse = false;}
else { readLine++; }
}
// Return the number of lines total in the message (for indexing)
return readLine;
}
There may be other ways to do this (UARTPeek comes to mind) - I'm open to suggestions.