我写了一个用来读串口数据的线程,但是不知道为什么,程序每次跑起来都非常慢,一卡一卡的,点一个按钮后,半天才响应.请哪位高手指点一下,怎样解决这个问题,有人说在循环中加入Application->ProcessMessage(); 但我加入后出现以下错误[C++ Error] Unit1.cpp(122): E2247 '_fastcall TApplication::ProcessMessage(tagMSG &)' is not accessible
Unit1.H
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
//---------------------------------------------------------------------------
class TReadThread : public TThread
{
private:
void __fastcall TReadThread::ReadData();
protected:
void __fastcall Execute();
public:
__fastcall TReadThread(bool CreateSuspended);
};
//---------------------------------------------------------------------------
#endif
Unit1.cpp
#include
#pragma hdrstop
#include "Unit1.h"
#include "Unit1MainForm.h"
#pragma package(smart_init)
extern HANDLE hComm;
extern int Flag;
extern int TimeOut=0;
extern int Len;
extern DWORD BytesRead;
extern char inbuff[128];
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall TReadThread::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall TReadThread::TReadThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall TReadThread::Execute()
{
//---- Place thread code here ----
while (! Terminated) Synchronize(ReadData);
}
//---------------------------------------------------------------------------
void __fastcall TReadThread::ReadData()
{
if (hComm == INVALID_HANDLE_VALUE) return;
TimeOut++;
if(TimeOut>100)
{
if(MainForm->CWButton1->Value ==true)
{
MainForm->Label4->Caption =AnsiString(Now())+"PC与单片机断开连接";
MainForm->CWButton1->Value =false;
}
else
{
MainForm->CWButton1->Value =false;
}
TimeOut=0;
}
if(Flag==0)
{
ReadFile(hComm, inbuff,1,&BytesRead,NULL);
if(inbuff[0]==0x55)
{
Flag++;
return;
}
else
{
Flag=0;
return;
}
}
else if(Flag==1)
{
ReadFile(hComm, inbuff+1,1,&BytesRead,NULL);
if(inbuff[1]==0x55)
{
Flag++;
return;
}
else
{
Flag=0;
return;
}
}
else if(Flag==2)
{
ReadFile(hComm, inbuff+2,2,&BytesRead,NULL);
Len=inbuff[2];
Len=Len*256+inbuff[3];
Flag++;
return;
}
else
{
do
{
ReadFile(hComm, inbuff+4,Len-4,&BytesRead,NULL);
}
while(BytesRead<(Len-4));
Flag=0;
inbuff[Len]='\0';
//MainForm->Memo1->Text = MainForm->Memo1->Text + inbuff[0]+inbuff[1]+inbuff[4]+inbuff[6]+inbuff[7]+inbuff[8]+'S';
switch(inbuff[4])
{
case 0x41://单片机定时发送PC,表示已连接
MainForm->CWButton1->Value =true;
MainForm->Label4->Caption =AnsiString(Now())+" PC与单片机相连接";
TimeOut=0;
break;
//单片机将各股道的状态传送给PC unsigned int TrainRoadUnit[4]
//共32个股道每一位表示相应的股道的开(1)或关(0)
case 0x43:
MainForm->Label8->Caption="各股道的初始状态";
break;
}
memset(inbuff,0x00,sizeof(inbuff));
}
}
--------------------next---------------------
阅读(1496) | 评论(0) | 转发(0) |