Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

USART Receive question ?

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Activity points
9,442
Guys,

I tried to accept character from serial,
but why the response is always NOT OK....I can see it's sending OK, with echo...

but ??
Code :
Code:
unsigned char usart_receive( void )
{
	/* Wait for data to be received */
	while ( !(UCSR0A & (1<<RXC)) )
	;
	/* Get and return received data from buffer */
	return UDR0;
}

usart_receive_str()
{
	char line[50];
	int  i = 0;
	while(1) {
		line[i] = usart_receive();
		if (line[i] == '\n') break;
		i++;
	}
	line[i] = 0;
	return line;
}

char_receive = usart_receive_str();
		usart_pstr(char_receive);
      
			if (strcmp(char_receive, "OK\r\n") == 0) 
			{
				LCD_CLEAR;
				lcd_string("OK!");
			
			   }
			   else
			   {
				   LCD_CLEAR;
				   lcd_string("NOT OK!");

			   }

any ideas ?
Thanks
 

Taking a quick look at the code above, it looks like you're always returning the same value of the variable "line [0]" at the function usart_receive_str(). You could review that to check if you should send "line", obviously just before reset it.
 

Looking at the code once again, it is not also clear - at least for me - how are you appending just incoming characters to the array "char_receive", and the line " if (strcmp(char_receive, "OK\r\n") == 0)" is perhaps comparing the last received byte .The "usart_pstr(char_receive)" line is likely only sending bytes one at a time.
 

Are you receiving SMS reading SMS after issuing AT+CMGR command ? or are you handling any AT commands of GSM ? If yes, the response ends with \r\nOK\r\n. There maybe other \r\n in the data received which appears before the \r\nOK\r\n. So assuming there is no other \r\n and only the data contains \r\nOK\r\n then the while loop in usart_receive_str() breaks at the first encounter of \n and so OK will not be present in the resulting string. So, it is always failing.

Make UART buffer 200 bytes. It will hold all the types of GSM data. It can be a ring buffer also. Then use strstr() or strcmp() to check if response is OK or ERROR.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top