美文网首页
协议栈中串口实验之串口接收

协议栈中串口实验之串口接收

作者: 羽墨志 | 来源:发表于2016-12-15 18:11 被阅读0次

目的借助于协议栈快速搭建自己的串口接收。
  前期的初始化和《协议栈中串口实验之串口发送》实验是相同的,简要回顾一下:
  一、在SampleApp.c文件添加所需头文件:

/* MT */
#include "MT.h"
#include "MT_UART.h"

二、在SampleApp_Init()函数中调用MT_UartInit()MT_UartRegisterTaskID(task_id)两个函数函数后就可以调用HalUARTWrite()函数进行串口打印了:

  SampleApp_TaskID = task_id;
  SampleApp_NwkState = DEV_INIT;
  SampleApp_TransID = 0;

  // Device hardware initialization can be added here or in main() (Zmain.c).
  // If the hardware is application specific - add it here.
  // If the hardware is other parts of the device add it in main().
  
  MT_UartInit();//串口初始化
  MT_UartRegisterTaskID(task_id);//登记串口任务号
  HalUARTWrite(0,"UartInit OK!\n",13);//打印字符串

【注意】记得修改串口初始化函数`MT_UartInit()`中的波特率,关闭流控制,同时取消project-->options-->C/C++ Compiler-->Preprocessor中关于MTLCD的预编译选项:

取消预编译选项
-----------------------我是分割线-------------------------------
  下面来实现协议栈中的串口接收。
  让我们先回到串口初始化函数MT_UartInit()。右键然后Go to the definition of MT_UartInit查看该函数的实现。
void MT_UartInit ()
{
  halUARTCfg_t uartConfig;

  /* Initialize APP ID */
  App_TaskID = 0;

  /* UART Configuration */
  uartConfig.configured           = TRUE;
  uartConfig.baudRate             = MT_UART_DEFAULT_BAUDRATE;//波特率
  uartConfig.flowControl          = MT_UART_DEFAULT_OVERFLOW;//流控制
  uartConfig.flowControlThreshold = MT_UART_DEFAULT_THRESHOLD;
  uartConfig.rx.maxBufSize        = MT_UART_DEFAULT_MAX_RX_BUFF;
  uartConfig.tx.maxBufSize        = MT_UART_DEFAULT_MAX_TX_BUFF;
  uartConfig.idleTimeout          = MT_UART_DEFAULT_IDLE_TIMEOUT;
  uartConfig.intEnable            = TRUE;
#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
  uartConfig.callBackFunc         = MT_UartProcessZToolData;
#elif defined (ZAPP_P1) || defined (ZAPP_P2)
  uartConfig.callBackFunc         = MT_UartProcessZAppData;
#else
  uartConfig.callBackFunc         = NULL;
#endif

  /* Start UART */
#if defined (MT_UART_DEFAULT_PORT)
  HalUARTOpen (MT_UART_DEFAULT_PORT, &uartConfig);
#else
  /* Silence IAR compiler warning */
  (void)uartConfig;
#endif

  /* Initialize for ZApp */
#if defined (ZAPP_P1) || defined (ZAPP_P2)
  /* Default max bytes that ZAPP can take */
  MT_UartMaxZAppBufLen  = 1;
  MT_UartZAppRxStatus   = MT_UART_ZAPP_RX_READY;
#endif

}

注意这两行代码:

#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
   uartConfig.callBackFunc         = MT_UartProcessZToolData;

在预编译选项中我们选择了ZTOOL_P1MT_UartProcessZToolData这个就是串口接收的回调处理函数。也就是说,当配置好协议栈的串口后,串口的接收和发送就已经可以用了。但因为协议栈的串口接收函数对串口进行了打包,必须按照固定的格式发送才可以。为了更快的实现自己的串口接收,自己写一个串口接收的回调函数代替MT_UartProcessZToolData()函数。
  自定义串口接收函数如下:

void rxCB( uint8 port, uint8 event )
{
    unsigned char Uartbuf[20];
    unsigned char buflen;
    
    buflen = HalUARTRead(0,Uartbuf,10);//读取串口数据,返回数据长度
    if(buflen)
    {
        HalUARTWrite(0,Uartbuf,buflen);//打印串口数据
        buflen = 0;
    }
}

这个函数实现了简单的回显功能。将回调函数替换为自己的函数

#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
   uartConfig.callBackFunc         = rxCB;

记得在MT_UART.c文件的开头添加函数的声明

void rxCB( uint8 port, uint8 event );

实验结果如下:

实验结果.png

相关文章

网友评论

      本文标题:协议栈中串口实验之串口接收

      本文链接:https://www.haomeiwen.com/subject/bidgmttx.html