【问题标题】:How can I write code to receive whole string from a device on rs232?如何编写代码以从 rs232 上的设备接收整个字符串?
【发布时间】:2017-12-10 13:42:47
【问题描述】:

我想知道如何编写接收特定字符串的代码。例如,这个 OK ,这里我只需要“OK”字符串。

另一个字符串也一样OK 我已经在 keil c51 中为 at89s52 微控制器编写了代码,它可以工作,但我需要更可靠的代码。

我正在对来自 rs232 串行的 rx 数据使用中断。

void _esp8266_getch() interrupt 4      //UART Rx.{
    if(TI){
        TI=0;
        xmit_bit=0;
        return ;
    }
    else
    {
        count=0;
        do
        {
            while(RI==0);
            rx_buff=SBUF;
        if(rx_buff==rx_data1)           //rx_data1 = 0X0D /CR
            {
                RI=0;
                while(RI==0);
                rx_buff=SBUF;
                if(rx_buff==rx_data2)           // rx_data2 = 0x0A /LF
                {
                    RI=0;
                    data_in_buffer=1;
                    if(loop_conti==1)
                    {
                        if(rec_bit_flag==1)
                        {
                            data_in_buffer=0;
                            loop_conti=0;
                        }
                    }
                }
            }
            else
            {
                if(data_in_buffer==1)
                {

                    received[count]=rx_buff;  //my buffer in which storing string
                    rec_bit_flag=1;
                    count++;
                    loop_conti=1;
                    RI=0;
                }
                else
                {
                    loop_conti=0;
                    rec_bit_flag=0;
                    RI=0;
                }
            }
        }
        while(loop_conti==1);
    }
    rx_buff=0;
  }

【问题讨论】:

  • 不同的是,您不应该在中断例程中实现 do-while(任何循环)。中断程序应尽可能简洁
  • 你的意思是 uart 而不是 rs232?
  • 是的,rs 232 表示 uart
  • 是的@Sudhee。我不应该使用do-while。比我怎么写呢?有什么例子吗?

标签: embedded microcontroller 8051


【解决方案1】:

这是一个仅供参考,您需要根据您的需要进一步开发逻辑。此外,设计取决于接收到的值,是否有任何特定的模式和更多参数。这不是经过测试的代码,我试图给出我的设计想法,这里有这个免责声明是示例..

//Assuming you get - "OK<CR><LF>" in which <CR><LF> indicates the end of string steam

int nCount =0; 
int received[2][BUF_SIZE];              //used only 2 buffers, you can use more than 2, depending on how speed 
                                        //you receive and how fast you process it
int pingpong =0;
bool bRecFlag = FALSE;
int nNofbytes = 0;
void _esp8266_getch() interrupt 4      //UART Rx.
{
    if(TI){
        TI=0;
        xmit_bit=0;
        return ;
    }
    if(RI)                          // Rx interrupt
    {
        received[pingpong][nCount]=SBUF;    
        RI =0;                      
        if(nCount > 0)
        {
            //  check if you receive end of stream value
            if(received[pingpong][nCount-1] == 0x0D) && (received[pingpong][nCount] == 0x0A))
            {
                bRecFlag = TRUE;
                pingpong = (pingpong == 0);
                nNofbytes = nCount;
                nCount = 0;
                return;
            }
        }
        nCount++;
    }
    return;
}

int main()
{

    //  other stuff

    while(1)
    {
        // other stuff

        if(bRecFlag)        //String is completely received
        {
            buftouse = pingpong ? 0 : 1;    // when pingpong is 1, buff 0 will have last complete string  
                                            // when pingpong is 0, buff 1 will have last complete string  
            // copy to other buffer or do action on received[buftouse][] 
            bRecFlag = false;
        }

        // other stuff
    }

}

【讨论】:

  • 感谢@Sudhee,感谢您的参考,但我得到了以下任一格式字符串 1. "OK" OR 2."OK"
  • 如果您将 视为字符串的结尾,那么这将起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-03
  • 1970-01-01
  • 2021-06-18
相关资源
最近更新 更多