微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 无线和射频 > 射频无线通信设计 > 我在做的 串口控制LED灯,单播 点对点

我在做的 串口控制LED灯,单播 点对点

时间:10-02 整理:3721RD 点击:
做不出实验现象,不知道哪里出错了

协调器的程序:
#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"
#include "OSAL_Nv.h"

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;
0, // byte AppNumInClusters;
(cId_t *)NULL // byte *pAppInClusterList;
};
static uint8 SerialApp_TxLen;
endPointDesc_t GenericApp_epDesc;
byte GenericApp_TaskID;
byte GenericApp_TransID;
static void rxCB(uint8 port,uint8 event);
unsigned char Uartbuf[80];
void GenericApp_SendTheMessage(unsigned char theMessageData);

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; // 2x30 don't care - see uart driver.
uartConfig.baudRate = HAL_UART_BR_115200;
uartConfig.flowControl = FALSE;
uartConfig.flowControlThreshold = 64; // 2x30 don't care - see uart driver.
uartConfig.rx.maxBufSize = 128; // 2x30 don't care - see uart driver.
uartConfig.tx.maxBufSize = 128; // 2x30 don't care - see uart driver.
uartConfig.idleTimeout = 6; // 2x30 don't care - see uart driver.
uartConfig.intEnable = TRUE; // 2x30 don't care - see uart driver.
uartConfig.callBackFunc = rxCB;
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;
}
osal_msg_deallocate( (uint8 *)MSGpkt );
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
}
return (events ^ SYS_EVENT_MSG);
}
return 0;
}

static void rxCB(uint8 port,uint8 event)
{
if ((event & (HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT)) &&
#if SERIAL_APP_LOOPBACK
(SerialApp_TxLen < SERIAL_APP_TX_MAX))
#else
!SerialApp_TxLen)
#endif
{
SerialApp_TxLen = HalUARTRead(0, Uartbuf, 80);
if (SerialApp_TxLen)
{

HalUARTWrite(0,Uartbuf,SerialApp_TxLen);
GenericApp_SendTheMessage(Uartbuf[2]);

if(Uartbuf[2]=='O'&&Uartbuf[3]=='D'&&Uartbuf[4]=='1')
GenericApp_SendTheMessage(1);
else if(Uartbuf[2]=='C'&&Uartbuf[3]=='D'&&Uartbuf[4]=='1')
GenericApp_SendTheMessage(11);

else if(Uartbuf[2]=='O'&&Uartbuf[3]=='D'&&Uartbuf[4]=='2')
GenericApp_SendTheMessage(2);
else if(Uartbuf[2]=='C'&&Uartbuf[3]=='D'&&Uartbuf[4]=='2')
GenericApp_SendTheMessage(22);

else if(Uartbuf[2]=='O'&&Uartbuf[3]=='D'&&Uartbuf[4]=='3')
GenericApp_SendTheMessage(3);
else if(Uartbuf[2]=='C'&&Uartbuf[3]=='D'&&Uartbuf[4]=='3')
GenericApp_SendTheMessage(33);


SerialApp_TxLen=0;
}

}
}

void GenericApp_SendTheMessage(unsigned char theMessageData)
{
afAddrType_t my_DstAddr;
my_DstAddr.addrMode=(afAddrMode_t)Addr16Bit;
my_DstAddr.endPoint=GENERICAPP_ENDPOINT;
my_DstAddr.addr.shortAddr=(Uartbuf[0]<<8)|(Uartbuf[1]);

AF_DataRequest(&my_DstAddr
,&GenericApp_epDesc
,GENERICAPP_CLUSTERID
,1
,&theMessageData
,&GenericApp_TransID
,AF_DISCV_ROUTE
,AF_DEFAULT_RADIUS);
}

终端程序:
#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"

#define SEND_DATA_EVENT 0x01

// 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;

0, // byte AppNumInClusters;
(cId_t *)NULL, // byte *pAppInClusterList;
GENERICAPP_MAX_CLUSTERS, // byte AppNumInClusters;
(cId_t *)GenericApp_ClusterList // byte *pAppInClusterList;
};

endPointDesc_t GenericApp_epDesc;
byte GenericApp_TaskID;
byte GenericApp_TransID;
devStates_t GenericApp_NwkState;
void SampleApp_HandleKeys( uint8 shift, uint8 keys );
void GenericApp_MessageMSGCB(afIncomingMSGPacket_t *pkt);

void GenericApp_Init( byte task_id )
{
halUARTCfg_t uartConfig;//串口

GenericApp_TaskID = task_id;
GenericApp_NwkState=DEV_INIT;
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; // 2x30 don't care - see uart driver.
uartConfig.baudRate = HAL_UART_BR_115200;
uartConfig.flowControl = FALSE;
uartConfig.flowControlThreshold = 64; // 2x30 don't care - see uart driver.
uartConfig.rx.maxBufSize = 128; // 2x30 don't care - see uart driver.
uartConfig.tx.maxBufSize = 128; // 2x30 don't care - see uart driver.
uartConfig.idleTimeout = 6; // 2x30 don't care - see uart driver.
uartConfig.intEnable = TRUE; // 2x30 don't care - see uart driver.
uartConfig.callBackFunc = NULL;
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;
}
osal_msg_deallocate( (uint8 *)MSGpkt );
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
}

// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
return 0;
}
void GenericApp_MessageMSGCB(afIncomingMSGPacket_t *pkt)
{
unsigned char buffer;
switch ( pkt->clusterId )
{
case GENERICAPP_CLUSTERID:
osal_memcpy(&buffer,pkt->cmd.Data,1);
if(buffer==1)
HalLedSet(HAL_LED_1,HAL_LED_MODE_OFF);
else if(buffer==11)
HalLedSet(HAL_LED_1,HAL_LED_MODE_ON);

else if(buffer==2)
HalLedSet(HAL_LED_2,HAL_LED_MODE_OFF);
else if(buffer==22)
HalLedSet(HAL_LED_2,HAL_LED_MODE_ON);

else if(buffer==3)
HalLedSet(HAL_LED_3,HAL_LED_MODE_OFF);
else if(buffer==33)
HalLedSet(HAL_LED_3,HAL_LED_MODE_ON);
break;
}

}
用串口调试助手(应该不是软件出错)发送,没有现象,灯不受控制

你确定你的目标地址是对了吗?不需要转化为16进制吗?

在哪里看目标地址?我刚开始学习,不太知道,请教一下

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top