如何去掉阻塞在串口上的读操作?

山的那边还是山~ 2008-11-21 01:46:45
串口的打开方式:(非重叠方式打开)
hPort = CreateFile ("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

SetCommMask (hPort, EV_RXCHAR); //事件,读

程序然后阻塞在这里(位于启动的一个线程里头):
WaitCommEvent(hPort, &commStatus, 0);等待串口上的写数据发生,以便在程序中读取。

问题就是现在欲关闭串口的线程,程序却一直仍阻塞在这里。
...全文
165 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
rookieme 2008-11-22
  • 打赏
  • 举报
回复
OVERLAPPED方式在EVC中是不支持滴!
如果是在VC中可以
ilovedrv 2008-11-22
  • 打赏
  • 举报
回复
如果坚持用这种阻塞方式,那就没办法了,只能用最极端的方法,调用TerminateThread终止线程

建议还是用异步方式读取数据,OVERLAPPED方式
schlafenhamster 2008-11-22
  • 打赏
  • 举报
回复
用异步读:
//1>. an work thread to monitor the _CTS and RX_CHAR //可以去_CTS
UINT CUload::CommWatchRead(LPVOID lpParam)
{ // return 0=OK 1=Error
CUload *pUload=(CUload*)lpParam;
OVERLAPPED os;
DWORD dwEventMask,dwTransfer;

memset(&os,0,sizeof(OVERLAPPED));
os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);// attrb,Manual,init,name
if (os.hEvent==NULL)
{
AfxMessageBox("Can't create Event",MB_ICONSTOP);
return 1;// error
}
// set 2 events !
if (!SetCommMask(pUload->m_idComDev,EV_RXCHAR|EV_CTS)) return 1;
while (pUload->m_bConnected)
{ // break,only if m_bConnected=NULL

dwEventMask=0;
if(!WaitCommEvent(pUload->m_idComDev,&dwEventMask,&os))
{ // function fails
if (GetLastError()==ERROR_IO_PENDING)
{// TRUE=WaitForSingleObject() should be called inside
GetOverlappedResult(pUload->m_idComDev,&os,&dwTransfer,TRUE);
os.Offset+=dwTransfer;
}
}
if((dwEventMask & EV_RXCHAR)==EV_RXCHAR)
{ // some chats received.
ResetEvent(pUload->m_hPostEventRead);// first reset
pUload->PostMessage(WM_COMMNOTIFY,
(WPARAM)1, // EV_RXCHAR
(LPARAM)0); // not used
// wait answer
WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF);
}
// if not at same time,use else if
if((dwEventMask & EV_CTS)==EV_CTS)
{ // CTS changed
ResetEvent(pUload->m_hPostEventRead);// first reset
pUload->PostMessage(WM_COMMNOTIFY,
(WPARAM)0, // EV_CTS
(LPARAM)0); // not used
// wait answer
WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF);
}
}
CloseHandle(os.hEvent);
return 0;// OK
}
//2>.一个消息
// use ON_MESSAGE(a,b)
void CUload::OnCommNotify(WPARAM wParam,LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
int nLength;
BYTE abIn[6000];// for 115200 needs 4096 and more
DWORD totalrecv;
CString txt;
switch(wParam)
{
case 0:// from CommWatchCTS()
SetCTSIcon();
SetEvent(m_hPostEventRead);// tell watcher that CommNotify has been done
break;
case 1:// from CommWatchRead()
// append on each entry
CFile uldFile(UpLoadFileName,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);
uldFile.SeekToEnd( );
if (m_bConnected)
{
if(nLength=ReadCommBlock(abIn,BUFLEN))// 38400 needs 1024
{ // save into file
if (!m_bStart)
{ // only 1st
m_bStart=1;
InforOut("Receiving "+UpLoadFileName);
}
uldFile.Write(abIn,nLength);
}
}
uldFile.Close();
// get total bytes received
m_uldINs.GetText(0,txt);
totalrecv=atol(txt.GetBuffer(40));
totalrecv+=nLength;
txt.Format("%d",totalrecv);
m_uldINs.ResetContent();
m_uldINs.AddString(txt);
if (nLength>128)
{
Animate();
// reset "time waiting"
m_Elapse.ResetContent();
m_Elapse.AddString("0 s");
}
// tell watcher that CommNotify has been done
SetEvent(m_hPostEventRead);
break;
}
}
//3>.实际读
int CUload::ReadCommBlock(BYTE *pBlock,int nBlockLen)
{ //return 0 if error
int WaitErr;
BOOL fReadStart;
COMSTAT ComStat;
DWORD dwErrorFlags,dwLength;

if (!m_bConnected) return 0;

if (ClearCommError(m_idComDev,&dwErrorFlags,&ComStat))
{ //ComStat filled
if (dwErrorFlags)
{
if (dwErrorFlags & CE_RXOVER) AfxMessageBox("Receive Queue overflow");
else if(dwErrorFlags & CE_OVERRUN) AfxMessageBox("Receive Overrun Error");
else if(dwErrorFlags & CE_RXPARITY) AfxMessageBox("Receive Parity Error");
else if(dwErrorFlags & CE_FRAME ) AfxMessageBox("Receive Framing error");
else if(dwErrorFlags & CE_BREAK) AfxMessageBox("Break Detected");
else AfxMessageBox("CE_OTHERS");
}
}
// nBlockLen may >,=,< ComStat.cbInQue !
dwLength=min((DWORD)nBlockLen,ComStat.cbInQue);
if(dwLength>0)
{ // read required
fReadStart=ReadFile(m_idComDev,pBlock,dwLength,&dwLength,&m_osRead);
if(!fReadStart)
{ // if there was a problem, or the async. operation's still pending ...
if(GetLastError()==ERROR_IO_PENDING)
{ // asynchronous i/o is still in progress
if (WaitErr=WaitForSingleObject(m_osRead.hEvent,60000))// 1 minute
{ // time over
if(WaitErr==WAIT_TIMEOUT)
{ // time out
dwLength=0;
AfxMessageBox("Time out !");
}// end time out
}// end waiterr
} // end io_pending
else
{ // WaitErr=0.if you SetEvent(m_osRead.hEvent) anywhere else
GetOverlappedResult(m_idComDev,&m_osRead,&dwLength,FALSE);
m_osRead.Offset +=dwLength;
}
}
} //end if dwLength>0
return dwLength;
}
//4.线程退出
置m_bConnected=FALSE
m_bConnected必须是volatile的
dirdirdir3 2008-11-21
  • 打赏
  • 举报
回复
既然需要关闭,使用overlapped比较方便.........
gamezealot 2008-11-21
  • 打赏
  • 举报
回复
同意楼上的,很久以前做过,就是用OVERLAPPED方式做的。你可以参考下MSDN,WaitCommEvent这个函数的说明下面有个例子叫做:
see Monitoring Communications Events.
whatabig 2008-11-21
  • 打赏
  • 举报
回复
in your main thread,
WriteFile(hPort,"close port cmd")
thus release
WaitCommEvent(hPort, &commStatus, 0);

while(!isclose)
{
WaitCommEvent(hPort, &commStatus, 0);
ReadFile(hPort, pbuf,len);
if(pbuf=="close port comd")
isclose=false;
}


cleanup routine
zaodt 2008-11-21
  • 打赏
  • 举报
回复
楼主,你还是考虑 重叠 方式吧。

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧