这个实验我用原版代码修改没有成功,也许是资料里做了一些修改没有说明,我先用资料里的理论写一遍吧。
实验现象:
三个节点,一个节点做协调器,另外两个节点做终端。按终端 S1 键时,协调器 Led2 状态改 变同时终端自身的 Led2 状态也改变,提示发送成功。修改其中一个终端的组编号,编译下 载后,按此终端 S1 键时,此时看协调器 Led2 状态是否会改变。
第一步:
在SampleApp_ProcessEven函数找到如下代码:
case ZDO_STATE_CHANGE: SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status); 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 ); }
因为没有周期性事件,下面的三行注释掉了。
第二步:
找到按键处理函数SampleApp_HandleKeys。
/********************************************************************* * @fn SampleApp_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */void SampleApp_HandleKeys( uint8 shift, uint8 keys ){ (void)shift; // Intentionally unreferenced parameter if ( keys & HAL_KEY_SW_6 ) { #if defined(ZDO_COORDINATOR) //协调器只接收数据 #else //路由器和终端才发送数据 SampleApp_SendFlashMessage(0); //以组播方式发数据 #endif } if ( keys & HAL_KEY_SW_1 ) { /* The Flashr Command is sent to Group 1. * This key toggles this device in and out of group 1. * If this device doesn't belong to group 1, this application * will not receive the Flash command sent to group 1. */ aps_Group_t *grp; grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); if ( grp ) { // Remove from the group aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); } else { // Add to the flash group aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); } }}
第三步:
接收数据:
/********************************************************************* * @fn SampleApp_MessageMSGCB * * @brief Data message processor callback. This function processes * any incoming data - probably from other devices. So, based * on cluster ID, perform the intended action. * * @param none * * @return none */void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ){ uint8 data; switch ( pkt->clusterId ) { case SAMPLEAPP_PERIODIC_CLUSTERID: break; case SAMPLEAPP_FLASH_CLUSTERID: data = (uint8)pkt->cmd.Data[0]; if(data == 0) HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); else HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); break; }}
第四步:
组播发送数据:找到SampleApp_SendFlashMessage函数
/********************************************************************* * @fn SampleApp_SendFlashMessage * * @brief Send the flash message to group 1. * * @param flashTime - in milliseconds * * @return none */void SampleApp_SendFlashMessage( uint16 flashTime ){ LedState = ~LedState; if ( AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc, SAMPLEAPP_FLASH_CLUSTERID, 1, &LedState, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) { if(LedState == 0) HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); else HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); } else { // Error occurred in request to send. }}
如果想用其他组通讯,修改下面代码:
// Setup for the flash command's destination address - Group 1 SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup; SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP; //组号1 //SampleApp_Flash_DstAddr.addr.shortAddr = 0x0002; //组号2
这组实验没有成功,之后等看完视频,理论更加扎实了之后我在回来弄懂它。