我编写了一个串口通讯的程序,从电脑到plc读取数据,有个疑问,每次读取信息用getTickCount()函数测了一下,需要大概1秒钟的时间,感觉不对劲,不知道哪里写的不对,用commix1.4只要30多毫秒就能读到
time=GetTickCount();
readState=ReadFile(this->hCom,lpReadBuffer,numberOfByteToRead,&numberOfByteToReadLength,&this->overlapped);
time1=GetTickCount();
cout<<"读取时间:"<<time1-time<<endl;
time=time1;
if (!readState)
{
if (GetLastError()==ERROR_IO_PENDING)
{
WaitForSingleObject(this->overlapped.hEvent,1000);
time1=GetTickCount();
cout<<"wait时间:"<<time1-time<<endl;
time=time1;
if (!GetOverlappedResult(this->hCom,&this->overlapped,&numberOfByteToReadLength,true))
{
time1=GetTickCount();
cout<<"等待时间:"<<time1-time<<endl;
cout<<"不成功"<<endl;
return 0;
}else
{
time1=GetTickCount();
cout<<"等待时间:"<<time1-time<<endl;
cout<<"成功"<<endl;
cout<<(char*)lpReadBuffer<<endl;
clearComBuffer(PURGE_RXCLEAR);
return numberOfByteToReadLength;
}
}
}
主程序是这样写的
char a[12];
memset(a,'\0',12);
a[0]=0x2;
a[1]='0';
a[2]='0';
a[3]='8';
a[4]='0';
a[5]='0';
a[6]='0';
a[7]='2';
a[8]=0x3;
a[9]='5';
a[10]='D';
time=GetTickCount();
comm.writeCom(a,12);//写入plc最多15毫秒左右,有时候0毫秒
time1=GetTickCount();
cout<<"写入PLC时间:"<<time1-time<<endl;
time=time1;
Eevent=0;
//comm.clearComError(&dwErrorFlags,&comstate);
while (Eevent!=EV_RXCHAR/*||comstate.cbInQue==0*/)
{
// comm.clearComError(&dwErrorFlags,&comstate);
WaitCommEvent(comm.getHCom(),&Eevent,comm.getOverlapped());//大概需要15、6毫秒最多30毫秒左右
}
time1=GetTickCount();
cout<<"等待plc相应时间:"<<time1-time<<endl;
time=time1;
//Sleep(100);
memset(b,'\0',12);
comm.readCom(b,12);//最费时间的地方大概1秒
time1=GetTickCount();
cout<<"PLC读取时间:"<<time1-time<<endl;
WaitForSingleObject(this->overlapped.hEvent,1000);时间基本都耗费在这一步。plc是三菱fx0s
请各位帮帮忙,看看怎么能读写快一些,谢谢 |