基于GenericAPP无线收发实验

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);
}



  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_45725659

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值