文章目录[隐藏]
GenericAPP无线收发基础实验中,协调器代码与终端基本一样
1、协调器主要函数
2、终端主要函数
3、GenericApp_SendTheMessage函数中代码的不同之处
协调器
终端
4、GenericApp_MessageMSGCB函数中代码的不同之处
协调器
终端
5、GenericApp_ProcessEvent函数中代码的不同之处
协调器
终端
6、调试程序出现的问题
解决未定义buf
实验现象:上电后协调器组网,终端联网后发“D1”,协调器收到数据“D1”后led1闪烁。协调器也发“D1”给终端,终端收到后led1也闪烁。
终端程序(Enddevice.c)
/*********************************************************************
* INCLUDES
*/
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include "ZGlobals.h"
#include "Common.h"
#include "DebugTrace.h"
#if !defined( WIN32 )
#include "OnBoard.h"
#endif
/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"
/* RTOS */
#if defined( IAR_ARMCM3_LM )
#include "RTOS_App.h"
#endif
/*********************************************************************
* GLOBAL VARIABLES
*/
uint8 AppTitle[] = "Enddevice"; //应用程序名称
// This list should be filled with Application specific Cluster IDs.
const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] =
{
GENERICAPP_CLUSTERID
};
const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
GENERICAPP_ENDPOINT, // int Endpoint;
GENERICAPP_PROFID, // uint16 AppProfId[2];
GENERICAPP_DEVICEID, // uint16 AppDeviceId[2];
GENERICAPP_DEVICE_VERSION, // int AppDevVer:4;
GENERICAPP_FLAGS, // int AppFlags:4;
GENERICAPP_MAX_CLUSTERS, // byte AppNumInClusters;
(cId_t *)GenericApp_ClusterList, // byte *pAppInClusterList;
GENERICAPP_MAX_CLUSTERS, // byte AppNumInClusters;
(cId_t *)GenericApp_ClusterList // byte *pAppInClusterList;
};
endPointDesc_t GenericApp_epDesc;
/*********************************************************************
* LOCAL VARIABLES
*/
byte GenericApp_TaskID; // Task ID for internal task/event processing
// This variable will be received when
// GenericApp_Init() is called.
devStates_t GenericApp_NwkState;
byte GenericApp_TransID; // This is the unique message ID (counter)
afAddrType_t GenericApp_DstAddr;
/*********************************************************************
* LOCAL FUNCTIONS
*/
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
static void GenericApp_SendTheMessage( void );
void GenericApp_Init( uint8 task_id )
{
GenericApp_TaskID = task_id; //osal分配的任务ID随着用户添加任务的增多而改变
GenericApp_NwkState = DEV_INIT;
GenericApp_TransID = 0; //消息发送ID(多消息时有顺序之分)
// Fill out the endpoint description.(定义本设备用来通信的APS层端点描述符)
GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;//应用程序的端口号
GenericApp_epDesc.task_id = &GenericApp_TaskID; //描述符的任务ID
GenericApp_epDesc.simpleDesc //简单描述符
= (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
GenericApp_epDesc.latencyReq = noLatencyReqs; //延时策略
afRegister( &GenericApp_epDesc ); //向AF层登记描述符
}
uint16 GenericApp_ProcessEvent( uint8 task_id, uint16 events )
{
afIncomingMSGPacket_t *MSGpkt;
devStates_t GenericApp_NwkState;//设备状态
GenericApp_NwkState = DEV_INIT;
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;
case ZDO_STATE_CHANGE:
GenericApp_NwkState =(devStates_t)(MSGpkt->hdr.status);
if(GenericApp_NwkState == DEV_END_DEVICE)
{
GenericApp_SendTheMessage();
}
break;
default:
break;
}
// Release the memory
osal_msg_deallocate( (uint8 *)MSGpkt );
// Next
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
return 0;
}
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
unsigned char buf[3];
switch ( pkt->clusterId )
{
case GENERICAPP_CLUSTERID:
osal_memset(buf, 0, 3); //数组初始化
osal_memcpy(buf, pkt->cmd.Data, 2); //拷贝数据到数组中
if(buf[0]=='D' && buf[1]=='1') //比较数据
{
HalLedBlink(HAL_LED_1, 0, 50, 500); //led1闪烁
}
else
{
HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
}
break;
}
}
static void GenericApp_SendTheMessage( void )
{
byte SendData[3]="D1";
afAddrType_t devDstAddr; //设置发送模式、端点号、地址
devDstAddr.addrMode=(afAddrMode_t)Addr16Bit;
devDstAddr.endPoint=GENERICAPP_ENDPOINT;
devDstAddr.addr.shortAddr=0x0000;
//无线发送数据
AF_DataRequest(&devDstAddr,
&GenericApp_epDesc,
GENERICAPP_CLUSTERID,
2,
SendData,
&GenericApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS);
}
/*********************************************************************
*/
协调器程序(Coordinator.c)
/*********************************************************************
* INCLUDES
*/
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include "Common.h"
#include "DebugTrace.h"
#if !defined( WIN32 )
#include "OnBoard.h"
#endif
/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"
/* RTOS */
#if defined( IAR_ARMCM3_LM )
#include "RTOS_App.h"
#endif
uint8 AppTitle[] = "Coordinator"; //应用程序名称
// This list should be filled with Application specific Cluster IDs.
const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] =
{
GENERICAPP_CLUSTERID
};
const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
GENERICAPP_ENDPOINT, // int Endpoint;
GENERICAPP_PROFID, // uint16 AppProfId[2];
GENERICAPP_DEVICEID, // uint16 AppDeviceId[2];
GENERICAPP_DEVICE_VERSION, // int AppDevVer:4;
GENERICAPP_FLAGS, // int AppFlags:4;
GENERICAPP_MAX_CLUSTERS, // byte AppNumInClusters;
(cId_t *)GenericApp_ClusterList, // byte *pAppInClusterList;
GENERICAPP_MAX_CLUSTERS, // byte AppNumInClusters;
(cId_t *)GenericApp_ClusterList // byte *pAppInClusterList;
};
endPointDesc_t GenericApp_epDesc;
byte GenericApp_TaskID; // Task ID for internal task/event processing
devStates_t GenericApp_NwkState;
byte GenericApp_TransID; // This is the unique message ID (counter)
afAddrType_t GenericApp_DstAddr;
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
static void GenericApp_SendTheMessage( void );
void GenericApp_Init( uint8 task_id )
{
GenericApp_TaskID = task_id; //osal分配的任务ID随着用户添加任务的增多而改变
GenericApp_NwkState = DEV_INIT;
GenericApp_TransID = 0; //消息发送ID(多消息时有顺序之分)
// Fill out the endpoint description.(定义本设备用来通信的APS层端点描述符)
GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;//应用程序的端口号
GenericApp_epDesc.task_id = &GenericApp_TaskID; //描述符的任务ID
GenericApp_epDesc.simpleDesc //简单描述符
= (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
GenericApp_epDesc.latencyReq = noLatencyReqs; //延时策略
afRegister( &GenericApp_epDesc ); //向AF层登记描述符
}
uint16 GenericApp_ProcessEvent( uint8 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 );// Release the memory
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );// Next
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
return 0;
}
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
unsigned char buf[3];
switch ( pkt->clusterId )
{
case GENERICAPP_CLUSTERID:
osal_memset(buf, 0, 3); //数组初始化
osal_memcpy(buf, pkt->cmd.Data, 2);//拷贝数据到数组中
if(buf[0]=='D' && buf[1]=='1') //比较数据
{
HalLedBlink(HAL_LED_1, 0, 50, 500); //led1闪烁
GenericApp_SendTheMessage(); //发送数据
}
else
{
HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
}
break;
}
}
static void GenericApp_SendTheMessage( void )
{
byte SendData[3]={'D', '1'};
afAddrType_t devDstAddr; //设置发送模式、端点号、地址
devDstAddr.addrMode=(afAddrMode_t)Addr16Bit;
devDstAddr.endPoint=GENERICAPP_ENDPOINT;
devDstAddr.addr.shortAddr=0xFFFF;
//无线发送数据
AF_DataRequest(&devDstAddr,
&GenericApp_epDesc,
GENERICAPP_CLUSTERID,
2,
SendData,
&GenericApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS);
}
版权声明:本文为CSDN博主「qq_45725659」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_45725659/article/details/121677829
暂无评论