组播通信
功能要求
协调器创建网络,并加入一组,并向组内成员组播数据“I am coordinator device”
终端1加入网络,并加入与协调器相同的组,收到协调器发送而来的数据
终端2加入网络,并加入另外一组,不能收到协调器发来的数据
实现过程
复制工程模块
加入串口通信代码
typedef struct
{
uint16 ID; //组ID
uint8 name[APS_GROUP_NAME_LEN]; //祖名,首元素是组名长度,后面是组名
}aps_Group_t;
代码:
// SampleApp.c
// 添加串口头文件
#include "mt_uart.h"
#include "string.h"
// ...
aps_Group_t SampleApp_Group;
void SampleApp_Init(uint8 task_id)
{
//...
// 加入串口初始化
// 在mt_uart.h 找
MT_UartInit();
HalUARTWrite(0,"uart0 is ok\n",strlen("uart0 is ok\n"));
//...
/*
// 广播去除
SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;
SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;
*/
SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup;
SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; //端口号
SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP; //组播地址,与组ID一致
// ...
// By default ,all devices start out in Group 1
// 默认情况下,所有设备都在组1中启动
SampleApp_Group.ID = 0x0001;
// Group 1复制给SampleApp_Group.name 长度为 7
osal_memcpy(SampleApp_Group.name,"Group 1", 7);
//把相应的端口加入SampleApp_Group
aps_AddGroup(SAMPLEAPP_ENDPOINT, &SampleApp_Group);
}
// MT_UART.c
void MT_UartInit(void)
{
halUARTCfg_t uartConfig;
// ...
uartConfig.baudRate = MT_UART_DEFAULT_BAUDRATE // 波特率
uartConfig.flowControl = MT_UART_DEFAULT_OVERFLOW // 流控制
}
// mt_uart.h
#define MT_UART_DEFAULT_BAUDRATE HAL_UART_BR_115200
#define MT_UART_DEFAULT_OVERFLOW FALSE
// SampleApp.h
#define SAMPLEAPP_FLASH_GROUP 0x0001
// aps_groups.h
typedef struct
{
uint16 ID; // Unique to this table 组ID
uint8 name[APS_GROUP_NAME_LEN]; // Human readable name of group 首是长度,后为组名
}aps_Group_t;
// SampleApp.c
// 事件轮循
uint16 SampleApp_ProcessEvent(uint8 task_id,uint16 events)
{
afIncomingMSGPacket_t *MSGpkt;
(void)task_id;
if(event & SYS_EVENT_MSG)
{
MSGpkt = (afIncomingMSGPcker_t *)osal_msg_receive(SampleApp_TaskID);
while(MSGpkt)
{
switch(MSGpkt->hdr.event)
{
// ...
// Received vhenever the device changes state in the netvork
case ZDO_STATE_CHANGE: // 网络创建成功
SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status); //获取网络状态
if( (SampleApp_NwkState == DEV_ZB_COORD) )
{
osal_start_timerEx( SampleApp_TaskID,
SAMPLEAPP_SEND_PERIODIC_MSG_EVI, //事件
SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );
}
else
{}
break;
default:
break;
}
//release the memory
osal_msg_deallocate((uint8 *)MSGpkt);
//Next -if one is available
MSGpkt = (afIncomingMSGpacket_t *)osal_msg_receive(SampleApp_TaskID);
}
//return unprocessed events
// 返回未处理的事件
return (events ^ SYS_EVENT_MSG)
}
if(events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT)
{
SampleApp_SendPeriodicMessage();
}
}
void SampleApp_SendPeriodicMessage(void)
{
uint8 *buff = "I am coordinator device\n";
if(AF_DataRequest(&SampleApp_Flash_DstAddr, //组播的地址
&SampleApp_epDesc,
SAMPLEAPP_PERIODIC_CLUSTERID, //命令号
strlen(buff),
buff,
&SampleApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS) == afStatus_SUCCESS)
{
}
else
{
// Error occurred in request to send
// 发送请求时出错
}
}
CpuCode
发布了58 篇原创文章 · 获赞 8 · 访问量 8796
私信
关注