给zigbee的串口发送一个数据后,一直触发回调函数的解决办法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36249516/article/details/78799182

在学习zigbee的过程中,通过串口助手给串口发送一个数据后,按我原先的理解,就会执行一次回调函数  代码如下:

#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include <string.h>
#include "Coordinator.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"

unsigned char rmsg[16];
static void ReceiveMessage(uint8 port,uint8 event);
uint16 stringTohex(unsigned char rmsg[16]);

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;  
byte GenericApp_TransID;

void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
void GenericApp_SendTheMessage( void );

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

  // Device hardware initialization can be added here or in main() (Zmain.c).
  // If the hardware is application specific - add it here.
  // If the hardware is other parts of the device add it in main().

  //GenericApp_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
  //GenericApp_DstAddr.endPoint = 0;
  //GenericApp_DstAddr.addr.shortAddr = 0;

  // Fill out the endpoint description.
  GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;
  GenericApp_epDesc.task_id = &GenericApp_TaskID;
  GenericApp_epDesc.simpleDesc
            = (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
  GenericApp_epDesc.latencyReq = noLatencyReqs;

  // Register the endpoint description with the AF
  afRegister( &GenericApp_epDesc );
  uartConfig.configured  =TRUE;
  uartConfig.baudRate    =HAL_UART_BR_115200;
  uartConfig.flowControl =FALSE;
  uartConfig.callBackFunc=ReceiveMessage;      //串口回调函数的初始化
  HalUARTOpen(0,&uartConfig);
 
}

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 AF_INCOMING_MSG_CMD:
          GenericApp_MessageMSGCB(MSGpkt);
          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);
  }
 
  // Discard unknown events
  return 0;
}

 void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
  //unsigned char buffer[2] ={0x0A,0X0D};
  RFTX rftx;
  //unsigned char buffer[10];
  switch ( pkt->clusterId )
  {
    case GENERICAPP_CLUSTERID:
      //osal_memcpy(buffer,pkt->cmd.Data,10);
      //HalUARTWrite(0,buffer,10);
      osal_memcpy(&rftx,pkt->cmd.Data,sizeof(rftx));
      HalUARTWrite(0,(uint8*)&rftx,sizeof(rftx));
      //HalUARTWrite(0,buffer,2);
     break;
  }
}

static void ReceiveMessage(uint8 port,uint8 event)
{
  HalUARTRead(0,rmsg,16);
  if(rmsg[0]=='&'&&rmsg[15]=='&')
  {
    afAddrType_t my_DstAddr;
    my_DstAddr.addrMode=(afAddrMode_t)Addr16Bit;
    my_DstAddr.endPoint=GENERICAPP_ENDPOINT;
    my_DstAddr.addr.shortAddr=stringTohex(rmsg);
    AF_DataRequest(&my_DstAddr,
                 &GenericApp_epDesc,
                 GENERICAPP_CLUSTERID,
                 sizeof(rmsg),
                 //1,
                 (uint8 *)&rmsg,
                 //theMessageData,
                 //(uint8 *)&x,
                 &GenericApp_TransID,
                 AF_DISCV_ROUTE,
                 AF_DEFAULT_RADIUS);
  }
  //x++;
}

uint16 stringTohex(unsigned char rmsg[16])
{
  uint16 hex;
  
    if(rmsg[4]<='9'&&rmsg[4]>='0')
      hex=(rmsg[4]-'0')*16*16*16;
    if(rmsg[4]<='f'&&rmsg[4]>='a')
      hex=((rmsg[4]-'a')+10)*16*16*16;
    if(rmsg[4]<='F'&&rmsg[4]>='A')
      hex=((rmsg[4]-'A')+10)*16*16*16;

    if(rmsg[5]<='9'&&rmsg[5]>='0')
      hex=(rmsg[5]-'0')*16*16+hex;
    if(rmsg[5]<='f'&&rmsg[5]>='a')
      hex=((rmsg[5]-'a')+10)*16*16+hex;
    if(rmsg[5]<='F'&&rmsg[5]>='A')
      hex=((rmsg[5]-'A')+10)*16*16+hex;
    
    if(rmsg[6]<='9'&&rmsg[6]>='0')
      hex=(rmsg[6]-'0')*16+hex;
    if(rmsg[6]<='f'&&rmsg[6]>='a')
      hex=((rmsg[6]-'a')+10)*16+hex;
    if(rmsg[6]<='F'&&rmsg[6]>='A')
      hex=((rmsg[6]-'A')+10)*16+hex;
    
    if(rmsg[7]<='9'&&rmsg[7]>='0')
      hex=(rmsg[7]-'0')+hex;
    if(rmsg[7]<='f'&&rmsg[7]>='a')
      hex=((rmsg[7]-'a')+10)+hex;
    if(rmsg[7]<='F'&&rmsg[7]>='A')
      hex=((rmsg[7]-'A')+10)+hex;
    
    return hex;
}


但是下载到板子之后,我却发现发送一次数据给串口,会一直不停的调用回调函数

解决办法  :
 找到static void HalUARTPollDMA(void)函数  
将该函数中的///防止串口回调函数一直执行  
  if (dmaCfg.txMT)
  {
    dmaCfg.txMT = FALSE;
    evt |= HAL_UART_TX_EMPTY;
  }

注释掉就行

猜你喜欢

转载自blog.csdn.net/qq_36249516/article/details/78799182