This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to make UARTgets function timeout?

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.

  • It looks like you are using polling and without tasking. In which case the usual solution is to add a timeout to your gets call and simply monitor the passing time in your polling loop inside your gets function.

    If you're using tasking and interrupts then more interesting options present themselves

    Robert
  • So you're suggesting modifying the code inside of UART1gets?


    This function is one predefined by TI with UARTstdio - I was hoping there was some option that would let me use it as-is. But, I have modified some of these functions before.


    I'll dig into the code and see what I find.

  • Well, you already said you were modifying them in your original post.

    In any case the stdio (not studio no matter what your spell checker says) are usually ill suited to embedded work. I would expect more problems especially if you need to do anything other than parse the strings from a single serial device.

    You can keep accumulating modifications as you are. It will work but at some point it's likely to collapse under it's own weight. If your problem is simple enough and contained enough that may be sufficient. If not then you will eventually need to redesign your approach.

    You're already arbitrarily throwing out long lines, perhaps legitimately. The step you are taking now is to throw out incomplete lines. The next obvious issue will be detecting invalid lines.

    Robert