概述
数据传输点对点实验
两个zigbee节点进行点对点通信,Zigbee终端节点发送“LED””三个字符,协调器收到收据后,对接受到的数据进行判断,如果收到的数据是“LED”,则使开发板上的LED等闪烁。
zigbee协议栈简介
协议是一系列的通信标准,通信双方需要按照这一标准进行正常的数据发射和接收。协议栈是协议的具体实现形式,通俗讲协议栈就是协议和用户之间的一个接口,开发人员通过使用协议栈来使用这个协议,进而实现无线数据收发。
原理图:
节点流程图:
协调器编程:
协调器主要负责网络组建,维护,控制终端节点的加入等
//zigbee设备节点
const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
GENERICAPP_ENDPOINT,
GENERICAPP_PROFID,
GENERICAPP_DEVICEID,
GENERICAPP_DEVICE_VERSION,
GENERICAPP_FLAGS,
GENERICAPP_MAX_CLUSTERS,
(cId_t *)GenericApp_ClusterList,
0,
(cId_t *)NULL
};
//任务初始化函数
void GenericApp_Init(byte task_id)
{
GenericApp_TaskID = task_id;
GenericApp_TransID = 0;
GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;
GenericApp_epDesc.task_id = &GenericApp_TaskID;
GenericApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&GenericApp_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)
{
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;
}
//对数据进行判断,当接收到的字符是“LED”时,LED灯进行闪烁
void GenericApp_MessageMSGCB(afIncomingMSGPacket_t *pckt)
{
unsigned char buffer[4] = " ";
switch (pckt->clusterId)
{
case GENERICAPP_CLUSTERID:
osal_memcpy(buffer, pckt->cmd.Data,3);
if((buffer[0] == 'L') || (buffer[1] == 'E') || (buffer[2] == 'D'))
{
HalLedBlink(HAL_LED_2, 0, 50, 500);
}
else
{
HalLedSet(HAL_LED_2, HAL_LED_MODE_ON);
}
break;
}
}
终端节点编程:
路由器主要负责数据包的路由选择,终端节点负责数据的采集,不具备路由功能。
//初始化工作
const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] =
{
GENERICAPP_CLUSTERID
};
const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
GENERICAPP_ENDPOINT,
GENERICAPP_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;
void GenericApp_MessageMSGCB(afIncomingMSGPacket_t *pckt);
void GenericApp_SendTheMessage(void);
void GenericApp_Init(byte task_id)
{
GenericApp_TaskID = task_id;
GenericApp_NwkState = DEV_INIT;
GenericApp_TransID = 0;
GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;
GenericApp_epDesc.task_id = &GenericApp_TaskID;
GenericApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&GenericApp_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)
{
switch(MSGpkt->hdr.event)
{
case ZDO_STATE_CHANGE:
GenericApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
if(GenericApp_NwkState == DEV_END_DEVICE)
{
GenericApp_SendTheMessage();
}
break;
default:
break;
}
osal_msg_deallocate((uint8 *)MSGpkt);
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(GenericApp_TaskID);
}
return (events ^ SYS_EVENT_MSG);
}
return 0;
}
//发送函数
void GenericApp_SendTheMessage( void )
{
unsigned char theMessageData[4] = "LED";
afAddrType_t my_DstAddr;
my_DstAddr.addrMode= (afAddrMode_t) Addr16Bit;
my_DstAddr.endPoint= GENERICAPP_ENDPOINT;
my_DstAddr.addr.shortAddr= 0x0000;
//发送函数
AF_DataRequest( &my_DstAddr, &GenericApp_epDesc,
GENERICAPP_CLUSTERID,
3,
theMessageData,
&GenericApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS ) ;
HalLedBlink(HAL_LED_2,0,50,500);
}
协调器的网络地址为0,发送模式为单播
最后
以上就是傲娇火龙果为你收集整理的Zigbee(2) ---- 协议栈的初次使用的全部内容,希望文章能够帮你解决Zigbee(2) ---- 协议栈的初次使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复