我必須監視串行端口並處理其數據。作爲一個測試程序,我只使用select作爲一個端口。運行功能如下:選擇()不在線程中工作
void <ProtocolClass>::run()
{
int fd = mPort->GetFileDescriptor();
fd_set readfs;
int maxfd=1;
int res;
FD_ZERO(&readfs);
FD_SET(fd,&readfs);
struct timeval Timeout;
Timeout.tv_usec=0;
Timeout.tv_sec=3;
//BYTE ack_message_frame[ACKNOWLEDGE_FRAME_SIZE];
while(true)
{
usleep(10);
res=select(maxfd,&readfs,NULL,NULL,NULL);
if(res<0)
perror("\nselect failed");
else if(res==0)
puts("TIMEOUT");
else if(FD_ISSET(fd,&readfs))
{//IF INPUT RECEIVED
qDebug("************RECEIVED DATA****************");
FlushBuf();
qDebug("\nReading data into a read buffer");
int bytes_read=mPort->ReadPort(mBuf,1000);
mFrameReceived=false;
for(int i=0;i<bytes_read;i++)
{
qDebug("%x",mBuf[i]);
}
//if complete frame has been received, write the acknowledge message frame to the port.
if(bytes_read>0)
{
qDebug("\nAbout to Process Received bytes");
ProcessReceivedBytes(mBuf,bytes_read);
qDebug("\n Processed Received bytes");
if(mFrameReceived)
{
int no_bytes=mPort->WritePort(mAcknowledgeMessage,ACKNOWLEDGE_FRAME_SIZE);
}//if frame received
}//if bytes read > 0
} //if input received
}//end while
}
但問題是它似乎沒有任何反應。有人可以建議正確的方法來做到這一點。我想使用每個線程選擇。這是可行的嗎?你能給我一個示例代碼來做到這一點。我已經搜索了網絡,但這些例子非常簡單,只涉及主要功能。沒有C++特定的例子。順便說一句,我正在使用Qt線程。
感謝
試過那個男人,它沒有工作 – rocknroll 2009-06-30 11:42:03
它的工作,加上下面給出的建議。謝謝stefaanv – rocknroll 2009-06-30 11:45:50