uart programing for receiving string.

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

 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[i]=UART_RxChar0();
            UART_TxChar0(buff[i]);
        }
        printString0(buff);
        printString0("\n");

        
    }
}

 

 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++);
    }
}

This topic has a solution.

Kunal Gupta

github.com/gkunalupta

Last Edited: Fri. May 29, 2020 - 10:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is an active thread here: https://www.avrfreaks.net/forum/avr-uart-string-receive-and-compare you might like to read that for some ideas.

 

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

Why not use carriage return (CR, 0x0D)  as a marker?  One you get a CR from the sender, consider that the end of your reception & take your next step(s). 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

So like in the string that i am getting entered by user i also get them write the \r or \n .. . Like
Writing kunal\r or kunal\n
And now make the loop and check if(buff[i] =\r or \n)
Then come out if the loop and my user eneterd string will be stored in the buff. Variable

Kunal Gupta

github.com/gkunalupta

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kunalgupta wrote:
check if(buff[i] =\r or \n)

Yes, but instead of storing the \r or \n, store a \0,(null terminated string) then all the C string handling functions in strlib will work. 

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"