MAC地址通信

1.以MAC地址为寻址目标(8字节)

2.代码:

  • 获取网络和MAC地址
    首先在GenericAPP.c中定义两个变量,用以保存2个地址,之后引入头文件进行字符串拷贝工作
#include
#include "NLMEDE.h"
uint16 NwkAddr = 0xFFFF;//初始化网络地址
uint8 MacAddr[8] = {0}; //初始化MAC地址

变为协调器后

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) )
          {
            // Start sending "the" message in a regular interval.
//            osal_start_timerEx( GenericApp_TaskID,
//                                GENERICAPP_SEND_MSG_EVT,
//                              GENERICAPP_SEND_MSG_TIMEOUT );
          }
          if(GenericApp_NwkState == DEV_ZB_COORD)
          {
            LS164_BYTE(12);//如果为协调器,数码管显示C
            NwkAddr = NLME_GetShortAddr();//获取网络地址
            memcpy(MacAddr,NLME_GetExtAddr(),8);//获取MAC地址
          }else if(GenericApp_NwkState == DEV_ROUTER)
          {
            LS164_BYTE(13);//如果为路由器,数码管显示R
          }else if(GenericApp_NwkState == DEV_END_DEVICE)
          {
            LS164_BYTE(14);//如果为终端,数码管显示E
          }
  • 接收端代码(协调器)
void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
  switch ( pkt->clusterId )
  {
    case GENERICAPP_CLUSTERID:
      // "the" message
#if defined( LCD_SUPPORTED )
      HalLcdWriteScreen( (char*)pkt->cmd.Data, "rcvd" );
#elif defined( WIN32 )
      WPRINTSTR( pkt->cmd.Data );
#endif
      //于接收端添加如下一行即可,数码管显示
      LS164_BYTE(pkt->cmd.Data[0]);
      break;
  }
}
  • 发送端(终端)
    先定义一个数组存放目标MAC地址
uint8 DstMacAddr[8] = {0x0F,0x55,0x16,0x07,0x00,0x4B,0x12,0x00};
if(events & GENERICAPP_MY_EVT)
  {
    if(0 == P1_1)//如果按钮3按下
    {
      LS164_BYTE(3);
      GenericApp_DstAddr.addrMode = (afAddrMode_t)Addr64Bit;//MAC地址发送
      memcpy(GenericApp_DstAddr.addr.extAddr,DstMacAddr,8);//指定接收方MAC地址
      GenericApp_DstAddr.endPoint = GENERICAPP_ENDPOINT;//指定接收方端口,默认为10
      
      char theMessageData[] = "8";
      AF_DataRequest( &GenericApp_DstAddr, &GenericApp_epDesc,
                       GENERICAPP_CLUSTERID,
                       1,//(byte)osal_strlen( theMessageData ) + 1
                       (byte *)&theMessageData,
                       &GenericApp_TransID,
                       AF_DISCV_ROUTE, AF_DEFAULT_RADIUS );
       P1SEL &= 0xfe;
       P1DIR |= 0x01;
       P1_0 ^= 1;//灯翻转
    }else if(0 == P2_0)//如果按钮4按下
    {
      LS164_BYTE(4);
    }else if(0 == P0_5)//如果按钮5按下
    {
      LS164_BYTE(5);
    }
    return(events ^ GENERICAPP_MY_EVT);
  }

你可能感兴趣的:(MAC地址通信)