ZigBee學習三 UART通信
在使用串口時,只需掌握ZigBee協議棧提供的串口操作相關的三個函數即可。
uint8 HalUARTOpen(uint8 port,halUARTCfg_t *config);
uint16 HalUARTRead(uint8 port,uint8 *buf,uint16 len);
uint16 HalUARTWrite(uint8 port,uint8 *buf,uint16 len);
本實驗只對coordinator.c文件進行改動就可以實現串口的收發。
修改coordinator.c文件
byte GenericApp_TransID; // This is the unique message ID (counter)
afAddrType_t GenericApp_DstAddr;
unsigned char uartbuf[128];
/*********************************************************************
* LOCAL FUNCTIONS
*/
static void GenericApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg );
static void GenericApp_HandleKeys( byte shift, byte keys );
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
static void GenericApp_SendTheMessage( void );
static void rxCB(uint8 port,uint8 event);
void GenericApp_Init( uint8 task_id )
{
halUARTCfg_t uartConfig;
GenericApp_TaskID = task_id;
GenericApp_NwkState = DEV_INIT;
GenericApp_TransID = 0;
... ...
GenericApp_epDesc.simpleDesc= (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
GenericApp_epDesc.latencyReq = noLatencyReqs;
afRegister( &GenericApp_epDesc );
uartConfig.configured = TRUE;
uartConfig.baudRate = HAL_UART_BR_115200;
uartConfig.flowControl = FALSE;
uartConfig.callBackFunc = rxCB; //配置串口調用函數
//對串口進行初始化
HalUARTOpen(0,&uartConfig); //該函數將halUARTCfg_t類型的結構體變量作為參數,而halUARTCfg_t結構體變量包含了串口初始化相關的參數。
}
//該函數是一個空函數。因為本實驗並沒有進行事件處理,所有不需要任何代碼
uint16 GenericApp_ProcessEvent( uint8 task_id, uint16 events )
{
}
static void rxCB(uint8 port,uint8 event)
{
if (event & (HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT)) //接收到串口數據
{
HalUARTRead(0,uartbuf,16); //讀取數據並存放到uartbuf數組中
if(osal_memcmp(uartbuf,"www.wlwmaker.com",16)) //使用osal_memcmp()函數判斷接收到的數據是否是字符串"www.wlwmaker.com",如果是,執行{}
{
HalUARTWrite(0,uartbuf,16); //調用HalUARTWrite()函數將接收到的字符輸出到串口
}
}
}
