UART programming for receiving string in embedded c language for AVR

hi to all,

i want to receive the string from the user in using UART communication...i have coded the below code but in this case the length of the string should be known to me .

But what if i want to take input from user and i dont know the length of the string ..in that case how should i do.not getting any thoughts ...so any suggestions or guidance how that can be done.

#include <uarts.h>
int main(void)
{ char buff[30];
UART_Init0();
//UART_Init3(); /* Initiate USART with 115200 baud rate */
//UART_Init1();
printString0("hellow");
printString0("\n");
/
Start global interrupt */

while(1)
{
printString0("type a character \n");
char b=UART_RxChar0();
UART_TxChar0(b);
printString0("\n");
for(int i=0;i<10;i++)
{
buff*=UART_RxChar0();*
UART_TxChar0(buff*);
_
}_
_
printString0(buff);_
_
printString0("\n");*_

* }*
}
[/quote]

function defination of the uart functions which i have used in above code
> void UART_Init0()
> {
> UBRR0L = 103;
> UCSR0B = (1<<TXEN0)|(1<<RXEN0);
> UCSR0C = (1<<UCSZ00)|(1<<UCSZ01)|(1<<UPE0);
> }
>
> void UART_TxChar0(uint16_t data)
> {
> while((UCSR0A & (1<<UDRE0))==0);
> UDR0 = data;
*> *
> }
>
> char UART_RxChar0()
> { //unsigned char status, resh, resl;
> //status = UCSR0A;
> //bit0(UCSR0A);
> //printString0("\n");
> while( !(UCSR0A & (1<<RXC0)));
> //if ( status & (1<<DOR0)|(1<<UPE0)|(1<<FE0)==1)
> //return -1;
> //bit0(UCSR0A);
> //printString0("\n");
> return UDR0;
> }
>
> char rxstring0()
*> { *
*> *
> char myValue;
> while((UCSR0A & (1<<RXC0))==0);
> myValue = UART_RxChar0();
> return myValue;
*> *
> }
>
> void printString0(char *myString) /////to print any string
> {
>
> while (*myString)
> {
> UART_TxChar0(myString++);
_
> }_
_
> }*_

if you don't know the length, how do you know that the user has finished typing her/is string ?

I'm not sure what's the value (besides learning) of going low level. Just use the Serial class...

why not wait for a termination character such as a line feed? even the serial monitor has some options for terminating a string when sent

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

If you want to do low-level programming of the USART then there is example code in the relevant Atmega datasheet.

...R

thanks for suggestion my problem is solved.

Well solution ia

void rxstring3(char *buff)
{ int i=0;
char myValue;
do
{

myValue = UART_RxChar0();
if(myValue!='>')
{

buff*=myValue;*
UART_TxChar3(myValue);
i++;
} else
{
buff*='\0';*
break;
}
}while( !(UCSR0A & (1<<RXC0)));
}
[/quote]
this is the function which i create and call this function like this
> rxstring3(buff);
here buff is the character array or u can also choose variable string as parameter... and for terminaing the receiving of string ,end the string with ">"

Learn to use code tags first...
[​code][​/code]
When we can read your code and it isn't in italics and stuck inside quote blocks you will get better quality responses.