加载中…
个人资料
zhonglovc
zhonglovc
  • 博客等级:
  • 博客积分:0
  • 博客访问:229
  • 关注人气:0
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

zigbee 组网发送

(2018-01-11 22:48:02)
标签:

zigbee

组网发送

//协调器部分代码
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDProfile.h"
#include "ZDObject.h"
#include
#include "Coordinator.h"
#include "DebugTrace.h"

#if !defined(WIN32)
#include "OnBoard.h"
#endif

#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"
#include "OSAL_Nv.h"
#include "aps_groups.h"

#define SEND_TO_ALL_EVENT 0X01

const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS]  
{
  GENERICAPP_CLUSTERID_1,GENERICAPP_CLUSTERID_2
};

const SimpleDescriptionFormat_t GenericApp_SimpleDesc = {
  GENERICAPP_ENDPOINT,
  GENERICAPP_PROFID,
  GENERICAPP_DEVICEID,
  GENERICAPP_DEVICE_VERSION,
  GENERICAPP_FLAGS,
  GENERICAPP_MAX_CLUSTERS,
  (cId_t *)GenericApp_ClusterList,
  0,
  (cId_t *)NULL
};


endPointDesc_t GenericApp_epDesc;
devStates_t GenericApp_NwkState;
byte GenericApp_TaskID;
byte GenericApp_TransID;
uint8 nodenum = 0;
uint16 info;


void GenericApp_MessageMSGCB(afIncomingMSGPacket_t *pckt);
void GenericApp_SendTheMessage(void);
static void rxCB(uint8 port,uint8 event);

void GenericApp_Init(byte task_id)
{
  halUARTCfg_t uartConfig;
  GenericApp_TaskID       = task_id;
  GenericApp_TransID      = 0;
  GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;
  GenericApp_epDesc.task_id = &GenericApp_TaskID;
  GenericApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
  GenericApp_epDesc.latencyReq = noLatencyReqs;
  afRegister(&GenericApp_epDesc);
  
  uartConfig.configured     = TRUE;
  uartConfig.baudRate       = HAL_UART_BR_115200;//定义波特率
  uartConfig.flowControl    = FALSE;
  uartConfig.callBackFunc   = rxCB;
  HalUARTOpen(0,&uartConfig);
  
  HalLedBlink(HAL_LED_2,0,50,250);
}

UINT16 GenericApp_ProcessEvent(byte 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 ZDO_STATE_CHANGE:
          HalLedSet(HAL_LED_2,HAL_LED_MODE_ON);
        break;
      case AF_INCOMING_MSG_CMD:  //A设备用函数ZDO_RegisterForZDOMsg注册了特定消息,A设备才能用ZDO_CB_MSG消息接收解析此特定消息;
                        //应用绑定例子:A设备发送绑定请求,B设备收到后发送匹配响应,A设备收到响应触发ZDO_CB_MSG事件。
       GenericApp_MessageMSGCB(MSGpkt);
        break;
      default:
        break;
      }
      osal_msg_deallocate((uint8 *)MSGpkt);
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(GenericApp_TaskID);
    }
    return (events ^ SYS_EVENT_MSG);
  }
  return 0;
}

void GenericApp_MessageMSGCB(afIncomingMSGPacket_t *pkt)//收数据
{
  char buf[30] = 0;
  unsigned char buffer[2] = {0x0A,0x0D};
  switch(pkt->clusterId)
  {
  case GENERICAPP_CLUSTERID_1:
    osal_memcpy(&info,pkt->cmd.Data,2);
    break;
  case GENERICAPP_CLUSTERID_2:
    osal_memcpy(buf,pkt->cmd.Data,pkt->cmd.DataLength);
    HalUARTWrite(0,buf,pkt->cmd.DataLength);//向串口写数据显示到屏幕
    break;
  }
}

static void rxCB(uint8 port,uint8 event)//发送消息
{
  unsigned char changeling[2] = {0x0A,0x0D};
  uint8 buf[8];
  uint8 len = 0;
  len = HalUARTRead(0,buf,8);//读数据
  if(len)
  {
    afAddrType_t my_DstAddr;
    my_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
    my_DstAddr.endPoint = GENERICAPP_ENDPOINT;
    my_DstAddr.addr.shortAddr = info;
    AF_DataRequest(&my_DstAddr,&GenericApp_epDesc,GENERICAPP_CLUSTERID_2,
                   len,buf,&GenericApp_TransID,AF_DISCV_ROUTE,AF_DEFAULT_RADIUS);//发
   
  }
}

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有