做串口通信时,如何实现程序的等待延时。。
单片机与PC通信时
PC发送一条命令给单片机,必须等待单片机的回应才能继续发送命令。那么这等待回应的动作怎么实现呢。。。
1。
延时函数
1) Sleep();//程序不能响应别的消息。
2) DelayTime()
void __fastcall TMainForm::DelayTime(DWORD DT)
{
long tt;
tt = GetTickCount();
while (GetTickCount()-tt<DT)
{
Application->ProcessMessages();
if ((GetTickCount()-tt)<=0)
tt = GetTickCount();
}
}
CPU的使用率100%
2查询某个标志的变量Com(此变量用来标志缓冲区是否受到单片机返回数据的结尾符)
void __fastcall TMainForm::WaitCom()
{
long tt;
tt = GetTickCount();
while(1)
{
if(Com == 1)//receive the end of character
{
Err = 0; // no error
break;
}
if(GetTickCount() - tt > 10000)
{
Err = 1;// out time error
break;
}
}
}
但这样CPU的占有率在WaitCom()还是很高。。
各位高手。。。。有没有更好的方法啊。。。。。。
CSDN 有没有大虾能解决。。。。