This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2650如何实现串口的不定长字节接收

各位大侠,如何实现串口不定长字节的接收,接收完成后自动调用回调函数

协议栈为Zstack Home 1.2.2a Sampleswitch,TI-RTOS系统

  • 在你发送的 字节中加入协议头和协议尾,接收的时候进行判断就好了,不然的话,是不可能实现的。你没告诉接收多少,他怎么可能识别呢?

  • 协议栈本身就支持不定长发送的,建议使用网蜂的方法重写原先的MT层函数,替换下MT_UartProcessZToolData

    void MT_UartProcessZToolData ( uint8 port, uint8 event )
    {
      uint8 flag=0,i,j=0;   //flag是判断有没有收到数据,j记录数据长度
      uint8 buf[128];     //串口buffer最大缓冲默认是128,我们这里用128.
      (void)event;        // Intentionally unreferenced parameter  
      
      while ((Hal_UART_RxBufLen(port))&&(j<128)) //检测串口数据是否接收完成
        
      {
        HalUARTRead (port,&buf[j], 1);  //把数据接收放到buf中
        j++;                           //记录字符数
        flag=1;                         //已经从串口接收到信息
      } 
      
      if(flag==1)       //已经从串口接收到信息
        
      {     /* Allocate memory for the data */
        //分配内存空间,为机构体内容+数据内容+1个记录长度的数据
        pMsg = (mtOSALSerialData_t *)osal_msg_allocate( sizeof  
                                                       ( mtOSALSerialData_t )+j+1);
        //事件号用原来的CMD_SERIAL_MSG
        pMsg->hdr.event = CMD_SERIAL_MSG;
        pMsg->msg = (uint8*)(pMsg+1);  // 把数据定位到结构体数据部分
        pMsg->msg [0]= j;              //给上层的数据第一个是长度
        for(i=0;i<j;i++)                //从第二个开始记录数据 
          pMsg->msg [i+1]= buf[i];   
        osal_msg_send( App_TaskID, (byte *)pMsg );  //登记任务,发往上层
        /* deallocate the msg */
        osal_msg_deallocate ( (uint8 *)pMsg );      //释放内存
      }
    }