接收一个String [英] Receive a String

查看:210
本文介绍了接收一个String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得字符串中的UART。我使用了AVR Studio 5 brays终端像这样的

How to received string in uart. I am using avr studio 5 and brays terminal like this one

同这张照片我使用的9600波特率跑到我尝试键入ABCDEF,只出来ABCF

Same as this picture I am using baudrate of 9600. Went I try to type "abcdef", it only come out "abcf" .

我的code是这样的--->

My code are like this --->

#include <avr/io.h>

void serial_init(void)
{
    UBRRH = 0x00;
    UBRRL = 95;   //baudrate 9600 and F_CPU 14745600UL
    UCSRB =  (1 << RXEN) | (1 << TXEN) | (1<<RXCIE); 
    UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0)|(1 << UCSZ1);
}

unsigned long long Usart_Receive(void)          
{
    while((UCSRA & (1 << RXC)) == 0) {};
    return UDR;
}
void USART_Transmit(unsigned long c)   
{
    PORTD= 0b00000100;  //RTS Enable
    while ((UCSRA & (1 << UDRE)) ==0) {};
    UDR = c;
    PORTD= 0b00000000;  //RTS Disable
}

int main(void)
{
    unsigned char data;
    serial_init();
    while (1)
    {
        data = Usart_Receive();
        _delay_ms(100);
        USART_Transmit(data);
    }
    return 0;   
}

就这么简单,但我找不到我的问题,为什么只出现在终端4封信。我希望有人能够帮助这一点。
谢谢你。

Simple as that, but I cannot find my problem why it only appear 4 letter at the terminal. I hope somebody can help about this. THANK.

推荐答案

下面是有工作的答​​案。

Here are the answer that are work.

int main(void)
 {

    char data[1][40];   
    int i1 = 0;
    int i2 = 0;
        serial_init();

        while (1)
    {

        for (i1=0;i1<1;i1++) 
            { 
               for (i2=0;i2<40;i2++) 
               { 
                  data[i1][i2] = Usart_Receive(); 
               } 
            } 

           for (i1=0;i1<1;i1++) 
           { 
               for (i2=0;i2<40;i2++) 
               { 
                  Usart_Transmit(data[i1][i2]); 
               } 
           }       

      }  
    return 0;   
}

感谢那些谁在这之前都在帮助我。

Thank for those who are helping me before this.

这篇关于接收一个String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆