《ZigBee协议栈串口应用讲解.ppt》由会员分享,可在线阅读,更多相关《ZigBee协议栈串口应用讲解.ppt(32页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、ZigBee串口应用一、串口收发基础实验二、ZigBee协议栈串口应用扩展实验实验课题:串口数据收发基础实验实验目的:利用串口收发数据试验步骤:ZigBee协议栈中串口通信的配置使用一个结构体来实现,该结构体为hal_UARTCfg_t,不必关心该结构体的具体定义形式,只需要对其功能有个了解,该结构体将串口初始化的参数集合在一起,只需要初始化各个参数即可最后使用HalUARTOpen()函数对串口进行初始化,该函数将halUARTCfg_t类型的结构体变量作为相关参数回调函数:通过指针(函数地址)调用的函数。如果把函数的地址作为参数传递给另一个函数,当通过这个指针调用它所指向的函数时,称为函数
2、的回调。一、串口收发基础实验一、串口收发基础实验使用串口的基本步骤:初始化串口,包括设置波特率、中断等;向发送缓冲区发送数据或者从接收缓冲区读取数据。串口工作原理剖析在ZigBee协议栈中,halUARTCfg_t结构体是如何定义的;串口是如何初始化的;发送给串口的数据时如何接受的;串口是如何向PC机发送数据的。这些问题涉及如下三个函数ZigBee协议栈中提供的与串口操作有关的三个函数为(可在可在zmain.c和和hal_uart.c中查看相关函数中查看相关函数)uint8 HalUARTOpen(uint8 halUARTCfg_t*config);uint16 HalUARTRead(ui
3、nt8 port,uint8*buf,uint16 len);Uint16 HalUARTWrite(uint8 prot,uint*buf,uint16 len);Coordinator.c#include OSAL.h#include AF.h#include ZDApp.h#include ZDObject.h#include ZDProfile.h#include#include Coordinator.h#include DebugTrace.h#if!defined(WIN32)#include OnBoard.h#endif#include hal_lcd.h#include h
4、al_led.h#include hal_key.h#include hal_uart.h/包含头文件const cId_t GenericApp_ClusterListGENERICAPP_MAX_CLUSTERS=GENERICAPP_CLUSTERID;const SimpleDescriptionFormat_t GenericApp_SimpleDesc=GENERICAPP_ENDPOINT,GENERICAPP_PROFID,GENERICAPP_DEVICEID,GENERICAPP_DEVICE_VERSION,GENERICAPP_FLAGS,GENERICAPP_MAX_
5、CLUSTERS,(cId_t*)GenericApp_ClusterList,0,(cId_t*)NULL;endPointDesc_t GenericApp_epDesc;byte GenericApp_TaskID;byte GenericApp_TransID;/*typedef struct byte endPoint;byte*task_id;SimpleDescriptionFormat_t*simpleDesc;afNetworkLatencyReq_t latencyReq;endPointDesc_t;*/void GenericApp_MessageMSGCB(afInc
6、omingMSGPacket_t*pckt);void GenericApp_SendTheMessage(void);/UINT16 GenericApp_ProcessEvent(byte task_id,UINT16 events);static void rxCB(uint8 port,uint8 event);/任务初始化函数void GenericApp_Init(byte task_id)halUARTCfg_t uartConfig;GenericApp_TaskID=task_id;GenericApp_TransID=0;GenericApp_epDesc.endPoint
7、=GENERICAPP_ENDPOINT;GenericApp_epDesc.task_id=&GenericApp_TaskID;GenericApp_epDesc.simpleDesc=(SimpleDescriptionFormat_t*)&GenericApp_SimpleDesc;GenericApp_epDesc.latencyReq=noLatencyReqs;afRegister(&GenericApp_epDesc);uartConfig.configured=TRUE;uartConfig.baudRate=HAL_UART_BR_115200;uartConfig.flo
8、wControl=FALSE;uartConfig.callBackFunc=rxCB;HalUARTOpen(0,&uartConfig);/串口初始化/事件处理函数UINT16 GenericApp_ProcessEvent(byte task_id,UINT16 events)return 0;/本实验没有进行事件处理static void rxCB(uint8 port,uint8 event)unsigned char uartbuf128=;HalUARTRead(0,uartbuf,16);/调用函数,从串口读取数据并将其放在uartbuf数组中 if(osal_memcmp(u
9、artbuf,16)/使用osal_memcmp()判断接收到的数据是否是“”,返回TRUE,执行HalUARTWrite()函数 HalUARTWrite(0,uartbuf,16);/调用函数将接收到的字符输出到串口 /ZigBee协议栈使用条件编译,在GenericApp-Coordinator工程上右键单击,options/C/C+Compiler在Defined symbols:下拉框中添加HAL_UART=TRUE,点击OK即可。二、ZigBee协议栈串口应用扩展实验实验课题:ZigBee协议栈串口应用扩展实验实验目的:利用串口收发数据实验原理:协调器建立ZigBee无线网络,终端
10、节点自动加入该网络中,然后终端节点周期性地向协调器发送字符串“EndDevice”,协调器收到该字符串后,通过串口将其输出到用户PC机。EndDeviceEndDeviceZigBee网络EndDeviceCoordinatorRS232Coordinator.c#include OSAL.h#include AF.h#include ZDApp.h#include ZDObject.h#include ZDProfile.h#include#include Coordinator.h#include DebugTrace.h#if!defined(WIN32)#include OnBoard
11、.h#endif#include hal_lcd.h#include hal_led.h#include hal_key.h#include hal_uart.hconst cId_t GenericApp_ClusterListGENERICAPP_MAX_CLUSTERS=GENERICAPP_CLUSTERID;const SimpleDescriptionFormat_t GenericApp_SimpleDesc=GENERICAPP_ENDPOINT,GENERICAPP_PROFID,GENERICAPP_DEVICEID,GENERICAPP_DEVICE_VERSION,GE
12、NERICAPP_FLAGS,GENERICAPP_MAX_CLUSTERS,(cId_t*)GenericApp_ClusterList,0,(cId_t*)NULL;endPointDesc_t GenericApp_epDesc;byte GenericApp_TaskID;byte GenericApp_TransID;/unsigned char uartbuf128;/将这一行注释掉/*typedef struct byte endPoint;byte*task_id;SimpleDescriptionFormat_t*simpleDesc;afNetworkLatencyReq_
13、t latencyReq;endPointDesc_t;*/void GenericApp_MessageMSGCB(afIncomingMSGPacket_t*pckt);void GenericApp_SendTheMessage(void);/static void rxCB(uint8 port,uint8 event);/将这一行注释掉/任务初始化函数void GenericApp_Init(byte task_id)halUARTCfg_t uartConfig;GenericApp_TaskID=task_id;GenericApp_TransID=0;GenericApp_ep
14、Desc.endPoint=GENERICAPP_ENDPOINT;GenericApp_epDesc.task_id=&GenericApp_TaskID;GenericApp_epDesc.simpleDesc=(SimpleDescriptionFormat_t*)&GenericApp_SimpleDesc;GenericApp_epDesc.latencyReq=noLatencyReqs;afRegister(&GenericApp_epDesc);uartConfig.configured=TRUE;uartConfig.baudRate=HAL_UART_BR_115200;u
15、artConfig.flowControl=FALSE;uartConfig.callBackFunc=NULL;/置空,即无需回调函数 HalUARTOpen(0,&uartConfig);/串口初始化/事件处理函数UINT16 GenericApp_ProcessEvent(byte task_id,UINT16 events)afIncomingMSGPacket_t*MSGpkt;if(events&SYS_EVENT_MSG)MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(GenericApp_TaskID);while(MSGpkt)
16、switch(MSGpkt-hdr.event)case AF_INCOMING_MSG_CMD:GenericApp_MessageMSGCB(MSGpkt);/完成对接收数据的处理 break;default:break;osal_msg_deallocate(uint8*)MSGpkt);MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(GenericApp_TaskID);return(events SYS_EVENT_MSG);return 0;/当协调器收到终端节点发送来的数据后,首先使用osal_msg_receive()函数,从消息
17、队列接收到消息,然后调用GenericApp_/MessageMSGCB()函数中将接收到的数据通过串口发送给PC机void GenericApp_MessageMSGCB(afIncomingMSGPacket_t*pkt)unsigned char buffer10=;switch(pkt-clusterId)case GENERICAPP_CLUSTERID:osal_memcpy(buffer,pkt-cmd.Data,10);HalUARTWrite(0,buffer,10);break;/使用osal_memcpy()函数,将接收到的数据拷贝到buffer数组中,然后就可以将该数据
18、通过串口发送给PC机EndDevice.cOsal_start_timerEx()函数原型如下:调用 uint8 osal_start_timerEx(uint8 taskID,uint16 event_id,uint16 timeout_value)周期性发送(任务,事件,时间)#include OSAL.h#include AF.h#include ZDApp.h#include ZDObject.h#include ZDProfile.h#include#include Coordinator.h/使用该头文件的宏定义#include DebugTrace.h#if!defined(WI
19、N32)#include OnBoard.h#endif#include hal_lcd.h#include hal_led.h#include hal_key.h#include hal_uart.h#define SEND_DATA_EVENT 0 x01const cId_t GenericApp_ClusterListGENERICAPP_MAX_CLUSTERS=GENERICAPP_CLUSTERID;/简单设备描述符 结构体 常量const SimpleDescriptionFormat_t GenericApp_SimpleDesc=GENERICAPP_ENDPOINT,GE
20、NERICAPP_PROFID,GENERICAPP_DEVICEID,GENERICAPP_DEVICE_VERSION,GENERICAPP_FLAGS,0,(cId_t*)NULL,GENERICAPP_MAX_CLUSTERS,(cId_t*)GenericApp_ClusterList;/初始化端口描述符endPointDesc_t GenericApp_epDesc;/简单节点描述符byte GenericApp_TaskID;/任务优先级byte GenericApp_TransID;/数据发送序列号devStates_t GenericApp_NwkState;/节点状态保存变
21、量 枚举型 记录设备状态/定义结构体/*typedef struct byte endPoint;byte*task_id;SimpleDescriptionFormat_t*simpleDesc;afNetworkLatencyReq_t latencyReq;endPointDesc_t;*/声明函数void GenericApp_MessageMSGCB(afIncomingMSGPacket_t*pckt);void GenericApp_SendTheMessage(void);int8 readTemp(void);/定义任务初始化函数void GenericApp_Init(by
22、te task_id)GenericApp_TaskID=task_id;GenericApp_TransID=0;/累加数据发送序列号,可用于计算丢包率 GenericApp_NwkState=DEV_INIT;/设备状态初始化为DEV_INIT表示该节点没有连接到ZigBee网络 GenericApp_epDesc.endPoint=GENERICAPP_ENDPOINT;GenericApp_epDesc.task_id=&GenericApp_TaskID;GenericApp_epDesc.simpleDesc=(SimpleDescriptionFormat_t*)&Generic
23、App_SimpleDesc;GenericApp_epDesc.latencyReq=noLatencyReqs;afRegister(&GenericApp_epDesc);/节点描述符注册/以下是件处理函数UINT16 GenericApp_ProcessEvent(byte task_id,UINT16 events)afIncomingMSGPacket_t*MSGpkt;if(events&SYS_EVENT_MSG)MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(GenericApp_TaskID);while(MSGpkt)swi
24、tch(MSGpkt-hdr.event)/*case AF_INCOMING_MSG_CMD:GenericApp_MessageMSGCB(MSGpkt);break;*/case ZDO_STATE_CHANGE:GenericApp_NwkState=(devStates_t)(MSGpkt-hdr.status);if(GenericApp_NwkState=DEV_END_DEVICE)/对节点设备类型进行判断是否是终端节点DEV_END_DEVICE osal_set_event(GenericApp_TaskID,SEND_DATA_EVENT);break;default:b
25、reak;/如果事件SEND_DATA_EVENT发生,则events&SEND_DATA_EVENT非零,条件成立则执行GenericApp_SendTheMessage()/函数,向协调器发送数据,发送完数据后再定时1s,同时清除SEND_DATA_EVENT事件。osal_msg_deallocate(uint8*)MSGpkt);/释放消息在堆中的内存 MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(GenericApp_TaskID);return(events SYS_EVENT_MSG);if(events&SEND_DATA_EV
26、ENT)GenericApp_SendTheMessage();osal_start_timerEx(GenericApp_TaskID,SEND_DATA_EVENT,1000);/uint8 taskID-该参数表明定时时间到达后,哪个任务对其作出响应 /uint16 event_id-该参数是一个事件ID,定时时间到达后,该事件发生,因此 需要添加一个新的事件,该事件发生表明定时时间到达 /uint16 timeout_value-定时时间由timeout_value参数确定 return(events SEND_DATA_EVENT);/(异或运算)return 0;/定义数据发送函数
27、void GenericApp_SendTheMessage(void)unsigned char theMessageData10=EndDevice;/定义数组存放要发送的数据 afAddrType_t my_DstAddr;my_DstAddr.addrMode=(afAddrMode_t)Addr16Bit;/发送地址模式为单播 my_DstAddr.endPoint=GENERICAPP_ENDPOINT;/初始化目的端口号 my_DstAddr.addr.shortAddr=0 x0000;/在ZigBee网络中,协调器的网络地址是固定的,向协调器发送数据时可以直接指定协调器的网络地址 /调用数据请求发送函数 AF_DataRequest(&my_DstAddr,&GenericApp_epDesc,GENERICAPP_CLUSTERID,osal_strlen(EndDevice)+1,/发送数据的长度发送数据的长度,加加1是结尾字符是结尾字符 theMessageData,/指向要发送数据的缓冲区指针&GenericApp_TransID,AF_DISCV_ROUTE,AF_DEFAULT_RADIUS );讲述结束,谢谢!
限制150内