循环事件中用睡眠定时函数
时间:10-02
整理:3721RD
点击:
问题出在这一段代码中
if(events & SEND_DATA_EVENT) //发送事件
{
int i=0;
for(i;i<3;i++) halSleep(1000);
GenericApp_SendTheMessage();//发送函数
osal_start_timerEx(GenericApp_TaskID,SEND_DATA_EVENT,2000); //每个6s,重复这次事件
return(events ^ SEND_DATA_EVENT);
}
这里会导致终端连续发送两段数据,如
后面带的‘dht110000’这个尾巴,我完全不知道是怎么冒出来的,前面的数据是我需要的,但这尾巴不知从哪而降的
感谢各位大大,观看
最后附上终端代码
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include <string.h>
#include "Coordinator.h"
#include "DebugTrace.h"
#include "OnBoard.h"
/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"
#include "aps_groups.h"
#include "DHT11.h"
#define SHOW_INFO_EVENT 0x01
#define SEND_DATA_EVENT 0x0001
const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] =
{
GENERICAPP_CLUSTERID
};
const SimpleDescriptionFormat_t GenericApp_SimpleDesc = //简单节点描述符
{
GENERICAPP_ENDPOINT,
GENERICAPP_PROFID,
GENERICAPP_DEVICEID,
GENERICAPP_DEVICE_VERSION,
GENERICAPP_FLAGS,
0,
(cId_t*)NULL,
GENERICAPP_MAX_CLUSTERS,
(cId_t *)GenericApp_ClusterList
};
endPointDesc_t GenericApp_epDesc; //本节点的函数
byte GenericApp_TaskID;
byte GenericApp_TransID;
devStates_t GenericApp_NwkState;
void GenericApp_MessageMSGCB(afIncomingMSGPacket_t *pckt);
void GenericApp_SendTheMessage(void);
void GenericApp_Init( byte task_id ) //节点的初始化
{
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;
// Register the endpoint description with the AF
afRegister( &GenericApp_epDesc );
}
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: //当节点网络状态发生改变时,触发此事件
GenericApp_NwkState = (devStates_t)(MSGpkt->hdr.status); //获取节点当前状态,MSGpkt->hdr.status这个涉及到协议栈底层
if (GenericApp_NwkState == DEV_END_DEVICE) //当节点状态是终端时,触发下列处理机制
{
osal_set_event(GenericApp_TaskID,SEND_DATA_EVENT);// 设置一个新的事件
}
break;
default:
break;
}
osal_msg_deallocate( (uint8 *)MSGpkt ); //清缓存
// Next
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
if(events & SEND_DATA_EVENT) //发送事件
{
int i=0;
for(i;i<3;i++)
halSleep(1000);
GenericApp_SendTheMessage();//发送函数
osal_start_timerEx(GenericApp_TaskID,SEND_DATA_EVENT,2000); //每个6s,重复这次事件
return(events ^ SEND_DATA_EVENT);
}
return 0;
}
void GenericApp_SendTheMessage(void) //发送函数,包括读取DHT11的数据,发送数据
{
char temp[2],humidity[2],strTemp[10];
DHT11();
temp[0] = wendu_shi+0x30;
temp[1] = wendu_ge+0x30;
humidity[0] = shidu_shi+0x30;
humidity[1] = shidu_ge+0x30; //上面是读取事件,以及定义一些变量
osal_memcpy(strTemp,"dht11",5);
osal_memcpy(&strTemp[5], temp, 2);
osal_memcpy(&strTemp[7], humidity, 2);//数据整合
afAddrType_t my_DstAddr;
my_DstAddr.addrMode=(afAddrMode_t)Addr16Bit;
my_DstAddr.endPoint = GENERICAPP_ENDPOINT;
my_DstAddr.addr.shortAddr=0x0000;//上面是发送准备工作
AF_DataRequest(&my_DstAddr,&GenericApp_epDesc,
GENERICAPP_CLUSTERID,
10,
(uint8*)strTemp,
&GenericApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS);
}//发送函数
你好,不建议在程序直接添加halSleep做休眠。
协议栈会自动调度实现休眠,可以通过定时器进行定时唤醒。
OK,谢谢。下回我会注意图片的

