ZigBee学习四 无线+UART通信

ZigBee学习四 无线+UART通信

1) 协调器编程

修改coordinator.c文件

byte GenericApp_TransID; // This is the unique message ID (counter)

afAddrType_t GenericApp_DstAddr;

//unsigned char uartbuf[128];
/*********************************************************************
* LOCAL FUNCTIONS
*/
static void GenericApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg );
static void GenericApp_HandleKeys( byte shift, byte keys );
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
static void GenericApp_SendTheMessage( void );

//static void rxCB(uint8 port,uint8 event);

void GenericApp_Init( uint8 task_id )
{
  halUARTCfg_t uartConfig;
  GenericApp_TaskID = task_id;
  GenericApp_NwkState = DEV_INIT;
  GenericApp_TransID = 0;

  ... ...

  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  = NULL;  //配置串口调用函数

  //对串口进行初始化
  HalUARTOpen(0,&uartConfig);  //该函数将halUARTCfg_t类型的结构体变量作为参数,而halUARTCfg_t结构体变量包含了串口初始化相关的参数。

}

//该函数是一个空函数。因为本实验并没有进行事件处理,所有不需要任何代码

uint16 GenericApp_ProcessEvent( uint8 task_id, uint16 events )
{

... ...

  switch ( MSGpkt->hdr.event )
  {
    case ZDO_CB_MSG:
      break;

    case KEY_CHANGE:
      break;

    case AF_DATA_CONFIRM_CMD:

      break;

    case AF_INCOMING_MSG_CMD:

      GenericApp_MessageMSGCB( MSGpkt );
      break;

    case ZDO_STATE_CHANGE:
      break;

    default:
      break;
  }

... ...

}

/*

static void rxCB(uint8 port,uint8 event)
{
  HalUARTRead(0,uartbuf,16);  //读取数据并存放到uartbuf数组中
  if(osal_memcmp(uartbuf,"www.wlwmaker.com",16)) //使用osal_memcmp()函数判断接收到的数据是否是字符串"www.wlwmaker.com",如果是,执行{}
  {
    HalUARTWrite(0,uartbuf,16); //调用HalUARTWrite()函数将接收到的字符输出到串口
  }
}

*/

static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
    unsigned char buffer[10];
    switch ( pkt->clusterId )
    {
      case GENERICAPP_CLUSTERID:
        osal_memcpy(buffer,pkt->cmd.Data,10);
        HalUARTWrite(0,buffer,10);
        break;
    }

}

2)终端节点编程

修改enddevice.c文件

#include "hal_key.h"
#include "hal_uart.h"

#define SEND_DATA_EVENT 0x01

uint16 GenericApp_ProcessEvent( uint8 task_id, uint16 events )
{

... ...

  

  if ( events & SYS_EVENT_MSG )
  {
    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
    while ( MSGpkt )

    {

      switch ( MSGpkt->hdr.event )
      {
        case ZDO_CB_MSG:
          GenericApp_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
          break;

        case KEY_CHANGE:
          GenericApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
          break;

        case AF_DATA_CONFIRM_CMD:

          break;

        case AF_INCOMING_MSG_CMD:
          GenericApp_MessageMSGCB( MSGpkt );
          break;

        case ZDO_STATE_CHANGE:
          GenericApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
          if ( (GenericApp_NwkState == DEV_ZB_COORD)
          || (GenericApp_NwkState == DEV_ROUTER)
          || (GenericApp_NwkState == DEV_END_DEVICE) )
          {
            osal_set_event(GenericApp_TaskID,SEND_DATA_EVENT);
          }
          break;

        default:
          break;
      }

      osal_msg_deallocate( (uint8 *)MSGpkt );

      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
    }

    return (events ^ SYS_EVENT_MSG);
  }

  if(events & SEND_DATA_EVENT)
  {
    GenericApp_SendTheMessage();
    osal_start_timerEx(GenericApp_TaskID,SEND_DATA_EVENT,1000);
    return (events ^ SEND_DATA_EVENT);
  }

  return 0;

}

static void GenericApp_SendTheMessage( void )
{
  char theMessageData[] = "EndDevice";
  afAddrType_t my_DstAddr;
  my_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;//单播发送
  my_DstAddr.endPoint = GENERICAPP_ENDPOINT; //目的端口号
  my_DstAddr.addr.shortAddr = 0x0000; //协调器网络地址

  if ( AF_DataRequest( &my_DstAddr, &GenericApp_epDesc,
    GENERICAPP_CLUSTERID,
    (byte)osal_strlen( theMessageData ) + 1,
    (byte *)&theMessageData,
    &GenericApp_TransID,
    AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
  {
    // Successfully requested to be sent.
    HalLedBlink(HAL_LED_1,0,50,500);
  }else
  {
    // Error occurred in request to send.
  }
}

上一篇:逆袭之旅DAY15.东软实训.Oracle.约束、序列、视图、索引、用户管理、角色


下一篇:string strSQL = "Select * From Employees;Select * from Customers";执行两次查询