Receving string from AT command ?

Go To Last Post
15 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Guys,

 

How can read received character from RX buffer ?

 

I try this :

LCD_CLEAR;
		LCD_FIRST_ROW;
		usart_pstr("AT\r\n");
		lcd_string("Send AT DONE!");
		
		char_receive = usart_receive_str();
       if (char_receive != "")
	     {
		   	LCD_CLEAR;
			lcd_string(char_receive);
			lcd_string("Rx Something");
		 }
		
		_delay_ms(1000);
		
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;
}		

I'm waiting for "OK"

Any clues ?

thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I got :

Error    8    incompatible types when assigning to type 'char[50]' from type 'int' 

 

char char_receive[50];
char_receive = usart_receive_str();

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[50];
}

I didn't use integer...???

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have an opinion, but the folks with actual knowledge here may haver further comments.

 

I think if you change 'return line[50];' to 'return line;', you will have better luck. When you declare a char array(or any array), you have to define a size -- 50 in this case. But the actual char array is just the variable name (line in this case). Line[50] defines the single character that exists at position 50 in the array.

 

The compiler is calling you on that because that is a single char variable which it is apparently calling an int. (Read up on data types ). Worse than that, line[50] actually defines the 51st char in the array -- because the numbering starts at 0. For example, an array char x[3] would have elements x[0] x[1] and x[2] available. (Extra credit -- x[2] should be a null to indicate that it's the end of a string.  )

 

Anyway, try that fix to see if it helps.

 

Good Luck,

 

hj

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, I have ommited [50] and it does the job, but how can I check if it gets OK ?

 

char_receive = usart_receive_str();
       if (char_receive != "")
	     {
		   	LCD_CLEAR;
			//lcd_string(char_receive);
			lcd_string("Rx Something");
			if (strcmp(char_receive, "OK\n") == 0) {
				lcd_string("OK!");
			   }

I got until Rx something....

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

or

if (strcmp(char_receive, "OK\r\n") == 0) {
				lcd_string("OK!");
			   }

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looks like it's BAUD RATE issue....I can't get exactly 115.2 Kbaud for 16MHz clock....hmmm...any tricks ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried with 57600 from my computer and send OK\r\n but it's not responding ...why ??

 

    

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();

 


if (strcmp(char_receive, "OK\r\n") == 0) {
				lcd_string("OK!");
			
			   }

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why would you call a string printing routine to out put a single character? You do understand the difference between "char" and "char *"? 

 

Once again you appear to be heading off into another of your 100+ post threads because you are attempting something complex without having stopped to learn the basics. Oprah says the universe will keep teaching you the same lesson until you finally learn from it. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
char* char_receive[50];

I still get the same result ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Let's just keep guessing and see if we can stumble across the solution?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have no idea as well ?

The idea is,  AVR receive OK from UART and display OK on LCD....

 

I have never done receiving string on UART ....I got the function from clawson example in this forum...I forget the link...

Let me find it..

 

I got it :

https://www.avrfreaks.net/forum/reading-string-usart-and-storing-char-array

Last Edited: Sun. Jul 3, 2016 - 11:32 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Look. Why don't you get a book or find a good website and actually learn C? I don't see how you can hope to develop C programs when you clearly don't understand even the most basic aspects of the language. 

 

Bottom line is that the interface to lcd_string() is "char *" which means you pass it the address of a block of characters in memory so you can

 

lcd_string("hello") ;

 

Or

 

char text[] = "hello" ;

lcd_string(text) ;

 

Because in both cases you are passing the address of the characters "hello" in memory. What you can't do is

 

char c;

c = 'A' ;

lcd_string(c) ;

 

Because 'c' in this is NOT the address of a block of characters in memory. 

 

If you don't understand this (one of the most basic concepts in C) you cannot possibly hope to write C programs. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, thanks,

I worked ok now....

 

char *char_receive;

char *usart_receive_str()

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Some 'C' learning & reference materials for you: http://blog.antronics.co.uk/2011...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@awneil

 

Thanks for the info...