请教串口中waitcommevent()事件运行死循环的问题?

chenyc 2003-09-07 03:40:35
初始化代码:
if (m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
}

// prepare port strings
//sprintf(szPort, "COM%d", portnr);
//sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopsbits);
m_dcb.BaudRate=baud;
m_dcb.Parity=NOPARITY;
m_dcb.ByteSize=databits;
m_dcb.StopBits=ONESTOPBIT;
m_dcb.fParity=FALSE;
m_dcb.fNull=FALSE;
// get a handle to the port
m_hComm = CreateFile(portnr, // communication port string (COMX)
GENERIC_READ | GENERIC_WRITE, // read/write types
0, // comm devices must be opened with exclusive access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
0, // Async I/O
0); // template must be 0 for comm devices

if (m_hComm == INVALID_HANDLE_VALUE)
{
// port not found
delete [] szPort;
delete [] szBaud;

return FALSE;
}

//Setup the port
SetupComm(m_hComm,4096,4096);
// flush the port
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
//

// set the timeout values
m_CommTimeouts.ReadIntervalTimeout = 0;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 0;
m_CommTimeouts.ReadTotalTimeoutConstant = 0;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 0;
m_CommTimeouts.WriteTotalTimeoutConstant = 0;

// configure
if (SetCommTimeouts(m_hComm, &m_CommTimeouts))
{
if (SetCommMask(m_hComm, dwCommEvents))
{
if (GetCommState(m_hComm, &m_dcb))
{
m_dcb.fRtsControl = RTS_CONTROL_ENABLE; // set RTS bit high!
//if (BuildCommDCB(szBaud, &m_dcb))
//{
if (SetCommState(m_hComm, &m_dcb))
// normal operation... continue
::SendMessage(m_pOwner->m_hWnd, WM_COMM_INIT_DETECTED, 0,0);
else
ProcessErrorMessage("SetCommState()");
//}
//else
//ProcessErrorMessage("BuildCommDCB()");
}
else
ProcessErrorMessage("GetCommState()");
}
else
ProcessErrorMessage("SetCommMask()");
}
else
ProcessErrorMessage("SetCommTimeouts()");

delete [] szPort;
delete [] szBaud;


// release critical section
LeaveCriticalSection(&m_csCommunicationSync);




取事件代码:

if (port->m_hComm)// check if the port is opened
PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR |PURGE_RXABORT | PURGE_TXABORT);
else
return 10;

for (;;)
{

bResult = WaitCommEvent(port->m_hComm, &Event, 0);

if (!bResult)
{
Switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
break;
}
case 87:
{
break;
}
default:
{
break;
}
}
}
else
{

bResult = ClearCommError(port->m_hComm, &dwError, &comstat);

if (comstat.cbInQue == 0)
continue;
} // end if bResult

Event = WaitForMultipleObjects(3, port->m_hEventArray, FALSE, INFINITE);

switch (Event)
{
case 0:
{
port->m_bThreadAlive = FALSE;

AfxEndThread(100);
break;
}
case 1: // read event
{
.
.
.
.
.

...全文
101 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cockhorse 2003-09-08
  • 打赏
  • 举报
回复
首先要确认确实有com event
另外,我觉得取事件代码写得不好
klbt 2003-09-07
  • 打赏
  • 举报
回复
自己设置一些断点,跟踪调试一下就清楚了。
chenyc 2003-09-07
  • 打赏
  • 举报
回复
help

19,502

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 嵌入开发(WinCE)
社区管理员
  • 嵌入开发(WinCE)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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