Zigbee网络通信之点播

点播即点对点通信,也就是在两个设备之间进行通信,不允许有第三个设备收到信息。

点播描述的就是网络中两个节点之间相互通信的过程,通过16bit短地址来确定通信对象。

  1. step 1 定义点播方式
    打开SampleApp.c文件,找到组播、广播的定义
afAddrType_t SampleApp_Periodic_DstAddr;
afAddrType_t SampleApp_Flash_DstAddr;

按照上述格式添加点播定义

afAddrType_t SampleApp_PointToPoint_DstAddr;//定义点对点通信

在函数void SampleApp_Init( uint8 task_id )中配置SampleApp_PointToPoint_DstAddr的相关参数(可参考对SampleApp_Periodic_DstAddrSampleApp_Flash_DstAddr的配置):

 SampleApp_PointToPoint_DstAddr.addrMode = (afAddrMode_t)afAddr16Bit;//地址类型为点播地址
  SampleApp_PointToPoint_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
  SampleApp_PointToPoint_DstAddr.addr.shortAddr = 0x0000;//发送给协调器
  1. step 2 添加点对点发送函数
      复制函数void SampleApp_SendPeriodicMessage( void ),修改相应的变量即可
/*********************************************************************
 * @fn      SampleApp_SendPointToPointMessage
 *
 * @brief   Send the point to pont message .
 *
 * @param   none
 *
 * @return  none
 */
void SampleApp_SendPointToPointMessage()
{
    uint8 data[10]={'0','1','2','3','4','5','6','7','8','9'};//定义发送内容
   // uint8 data[10]="0123456789";//上述方式二选一 
if(AF_DataRequest(&SampleApp_PointToPoint_DstAddr,&SampleApp_epDesc,
                      SAMPLEAPP_POINTTOPOINT_CLUSTERID,
                      10,
                      data,
                      &SampleApp_TransID,
                      AF_DISCV_ROUTE,
                      AF_DEFAULT_RADIUS) == afStatus_SUCCESS)
    {
    }
    else
    {
      // Error occurred in request to send.
    }
}

函数实现后请在SampleApp.c文件开头添加函数声明

void SampleApp_SendPointToPointMessage(void);

除此之外,还要添加SAMPLEAPP_POINTTOPOINT_CLUSTERID的定义,具体位置可通过在SAMPLEAPP_PERIODIC_CLUSTERID上右键Go to definition of SAMPLEAPP_PERIODIC_CLUSTERID来确定。

#define SAMPLEAPP_POINTTOPOINT_CLUSTERID  3

将宏定义SAMPLEAPP_MAX_CLUSTERS的值更改为3

#define SAMPLEAPP_MAX_CLUSTERS       3

找到函数uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events ),在周期性事件SAMPLEAPP_SEND_PERIODIC_MSG_EVT中调用点对点通信发送函数,同时注释掉原有的SampleApp_SendPeriodicMessage();函数:

if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
  {
    // Send the periodic message
    //SampleApp_SendPeriodicMessage();//周期广播发送函数
    SampleApp_SendPeriodicMessage();  //点对点通信发送函数

    // Setup to send message again in normal period (+ a little jitter)
    osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
        (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );

    // return unprocessed events
    return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
  }
  1. 修改消息接收ID
      在函数void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )中,修改clusterId,将原有周期发送的SAMPLEAPP_PERIODIC_CLUSTERID更改为点对点通信的SAMPLEAPP_POINTTOPOINT_CLUSTERID,然后将接收的数据打印到串口,具体代码如下:
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
    uint16 flashTime;
    
    switch ( pkt->clusterId )
    {
        //case SAMPLEAPP_PERIODIC_CLUSTERID:
        //break;
    case SAMPLEAPP_POINTTOPOINT_CLUSTERID:
        HalUARTWrite(0,"Point_To_Point Communication\n",29);
        //此处不要用sizeof计算字符串长度,否则会导致串口显示的数据出错或者不完整
        HalUARTWrite(0,pkt->cmd.Data,pkt->cmd.DataLength);//打印收到的数据
        HalUARTWrite(0,"\n",1);//换行
        break;
        
    case SAMPLEAPP_FLASH_CLUSTERID:
        flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] );
        HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) );
        break;
    }
}
  1. 注释协调器的周期事件
      协调器不需要周期性发送数据,在函数uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )注释掉协调器的周期性事件
if ( //(SampleApp_NwkState == DEV_ZB_COORD)||//协调器的周期性事件
               (SampleApp_NwkState == DEV_ROUTER)
              || (SampleApp_NwkState == DEV_END_DEVICE) )
          {
            // Start sending the periodic message in a regular interval.
            osal_start_timerEx( SampleApp_TaskID,
                              SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
                              SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );
          }
  1. 上电测试
     将修改后的程序分别以CoordinatorEBRouterEBEndDeviceEB的方式分别下载到三个节点中,并通过串口与PC相连,在串口中查看接收到的信息如下图。
Zigbee网络通信之点播_第1张图片
串口测试结果


  注意一下定义的区别和用法:

类型 定义
整型数组 int num[10]={0,1,2,3,4,5,6,7,8,9};
字符数组 char ch[10]={'0','1','2','3','4','5','6','7','8','9'};
字符串 char string[10]="0123456789";
字符串 char string[10]={"0123456789"};
字符串数组 char *week[10]={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};

你可能感兴趣的:(Zigbee网络通信之点播)