功能描述
终端发送模块:端口号11 P0_4-key1 P0_5-key2 P0_6-key3
协调器接收模块:端口号7 0x0001 LED1 0x0002 LED2 ;端口号8 0X0001 LED3
key1按下 向端口号7发送 数据"KEY1按下",同时接收模块 LED1闪烁1次,串口打印接收到信息;
key2按下 向端口号7发送 数据"KEY2按下",同时接收模块 LED2闪烁1次,串口打印接收到信息;
key2按下 向端口号8发送 数据"KEY3按下",同时接收模块 LED3闪烁1次,串口打印接收到信息;
操作步骤
发送模块《EndDeviceEB》:
- 定义11号端点与应用层任务挂钩《LEDApp.c 》->
void LEDApp_Init( uint8 task_id )
里把LEDApp_epDesc.endPoint = LEDApp_ENDPOINT;
(LEDApp_ENDPOINT是10号端口)改为LEDApp_epDesc.endPoint = 11;
2.在之前if ( events & LEDApp_MY_EVT )
事件按键处理函数里修改(可以看ZigBee之ZStack协议(2)单播1)
if ( events & LEDApp_MY_EVT )
{
if(P0_4 == 0)//S1
{
char theMessageData[] = "KEY1按下\n";
LED02 =~LED02;
LEDApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
LEDApp_DstAddr.addr.shortAddr = 0x0000;//接收模块的网络地址
// Take the first endpoint, Can be changed to search through endpoints
LEDApp_DstAddr.endPoint = 7;//接收模块的端点号
//LEDApp_epDesc结构体 端点描述符有源端点的信息,延时10
//发送 (byte)osal_strlen( theMessageData ) + 1表示发送的字节
AF_DataRequest( &LEDApp_DstAddr, &LEDApp_epDesc,
0x0001,//目标端点簇,房间的连接端点数据宏是1,2个字节,所以在射频是0x0001
(byte)osal_strlen( theMessageData ) + 1,//发送字符串的长度
(byte *)&theMessageData,//字符串内容数组的首地址
&LEDApp_TransID,//记录我们应用层任务发送的数据包个数
AF_DISCV_ROUTE, AF_DEFAULT_RADIUS );
}
if(P0_5 == 0)//S2
{
char theMessageData1[] = "KEY2按下\n";
LED02 =~LED02;
LEDApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
LEDApp_DstAddr.addr.shortAddr = 0x0000;//接收模块的网络地址
// Take the first endpoint, Can be changed to search through endpoints
LEDApp_DstAddr.endPoint = 7;//接收模块的端点号
//LEDApp_epDesc结构体 端点描述符有源端点的信息,延时10
//发送 (byte)osal_strlen( theMessageData1 ) + 1表示发送的字节
AF_DataRequest( &LEDApp_DstAddr, &LEDApp_epDesc,
0x0002,//目标端点簇,房间的连接端点数据宏是1,2个字节,所以在射频是0x0001
(byte)osal_strlen( theMessageData1 ) + 1,//发送字符串的长度
(byte *)&theMessageData1,//字符串内容数组的首地址
&LEDApp_TransID,//记录我们应用层任务发送的数据包个数
AF_DISCV_ROUTE, AF_DEFAULT_RADIUS );
}
if(P0_6 == 0)//S3
{
char theMessageData[] = "KEY3按下\n";
LED02 =~LED02;
LEDApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
LEDApp_DstAddr.addr.shortAddr = 0x0000;//接收模块的网络地址
// Take the first endpoint, Can be changed to search through endpoints
LEDApp_DstAddr.endPoint = 8;//接收模块的端点号
//LEDApp_epDesc结构体 端点描述符有源端点的信息,延时10
//发送 (byte)osal_strlen( theMessageData ) + 1表示发送的字节
AF_DataRequest( &LEDApp_DstAddr, &LEDApp_epDesc,
0x0001,//目标端点簇,房间的连接端点数据宏是1,2个字节,所以在射频是0x0001
(byte)osal_strlen( theMessageData ) + 1,//发送字符串的长度
(byte *)&theMessageData,//字符串内容数组的首地址
&LEDApp_TransID,//记录我们应用层任务发送的数据包个数
AF_DISCV_ROUTE, AF_DEFAULT_RADIUS );
}
P0IFG = 0;
P0IF = 0;
return (events ^ LEDApp_MY_EVT);
}
接收模块《CoordinatorEB》:
- 定义endPointDesc_t LEDApp_epDesc1;作为端口8的描述符
- 定义11号端点与应用层任务挂钩《LEDApp.c 》->
void LEDApp_Init( uint8 task_id )
里修改
// Fill out the endpoint description.
LEDApp_epDesc.endPoint = 7;//LEDApp_ENDPOINT;
LEDApp_epDesc.task_id = &LEDApp_TaskID;
LEDApp_epDesc.simpleDesc
= (SimpleDescriptionFormat_t *)&LEDApp_SimpleDesc;
LEDApp_epDesc.latencyReq = noLatencyReqs;
// Register the endpoint description with the AF
afRegister( &LEDApp_epDesc );
// Fill out the endpoint description.
LEDApp_epDesc1.endPoint = 8;//LEDApp_ENDPOINT;
LEDApp_epDesc1.task_id = &LEDApp_TaskID;
LEDApp_epDesc1.simpleDesc
= (SimpleDescriptionFormat_t *)&LEDApp_SimpleDesc;
LEDApp_epDesc1.latencyReq = noLatencyReqs;
// Register the endpoint description with the AF
afRegister( &LEDApp_epDesc1 );
- 《LEDApp.c 》->
static void LEDApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
内部代码
static void LEDApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
if((pkt->endPoint)==7)
{//外来数据包
switch ( pkt->clusterId )
{
case 0x0001: LED01 = ~LED01;
UartTX_Send_String( pkt->cmd.Data,pkt->cmd.DataLength);
break;
case 0x0002: LED02 = ~LED02;
UartTX_Send_String( pkt->cmd.Data,pkt->cmd.DataLength);
break;
}
}
if((pkt->endPoint)==8)
{
switch ( pkt->clusterId )
{
case 0x0001: LED03 = ~LED03;
UartTX_Send_String( pkt->cmd.Data,pkt->cmd.DataLength);
break;
}
}
}