串口堵塞怎么解决

wangxiang5135 2012-10-12 11:01:52
大家好:
我使用串口调试助手里面的一个CSerialPort类进行读写串口的操作。一次发送和接受多个数据,发送一次等待接受后再发送下一个数据,循环做。我把这个类的接受地方改为多个字符接受完成后再发送消息。主要代码如下:

BOOL COperationPLC::InitPort(CWnd* pPortOwner, // the owner (CWnd) of the port (receives message)
UINT portnr, // portnumber (1..4)
UINT baud, // baudrate
char parity, // parity
UINT databits, // databits
UINT stopbits, // stopbits
DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc
UINT writebuffersize) // size to the writebuffer
{
没有改...........................
}
UINT COperationPLC::CommThread(LPVOID pParam)
{
// Cast the void pointer passed to the thread back to
// a pointer of CSerialPort class
COperationPLC *port = (COperationPLC*)pParam;

// Set the status variable in the dialog class to
// TRUE to indicate the thread is running.
port->m_bThreadAlive = TRUE;

// Misc. variables
DWORD BytesTransfered = 0;
DWORD Event = 0;
DWORD CommEvent = 0;
DWORD dwError = 0;
static COMSTAT comstat;
BOOL bResult = TRUE;
// Clear comm buffers at startup
if (port->m_hComm) // check if the port is opened
PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);

// begin forever loop. This loop will run as long as the thread is alive.
for (;;)
{

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

if (!bResult)
{
// If WaitCommEvent() returns FALSE, process the last error to determin
// the reason..
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
break;
}
case 87:
{
break;
}
default:
{
port->ProcessErrorMessage("WaitCommEvent()");
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:
{
CloseHandle(port->m_hComm);
port->m_hComm=NULL;
port->m_bThreadAlive = FALSE;

// Kill this thread. break is not needed, but makes me feel better.
AfxEndThread(100);

break;
}
case 1: // read event
{
GetCommMask(port->m_hComm, &CommEvent);
if (CommEvent & EV_RXCHAR)
// Receive character event from port.
ReceiveChar(port, comstat);
break;
}
case 2: // write event
{
// Write character event from port
WriteChar(port);
break;
}

} // end switch

} // close forever loop

return 0;
}
//发送到串口
void COperationPLC::WriteChar(COperationPLC* port)
{
BOOL bWrite = TRUE;
BOOL bResult = TRUE;

DWORD BytesSent = 0;

ResetEvent(port->m_hWriteEvent);

// Gain ownership of the critical section
EnterCriticalSection(&port->m_csCommunicationSync);

if (bWrite)
{
// Initailize variables
port->m_ov.Offset = 0;
port->m_ov.OffsetHigh = 0;

// Clear buffer
PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);

bResult = WriteFile(port->m_hComm, // Handle to COMM Port
port->m_szWriteBuffer, // Pointer to message buffer in calling finction
// strlen((char*)port->m_szWriteBuffer), // Length of message to send
port->m_nWriteSize, // Length of message to send
&BytesSent, // Where to store the number of bytes sent
&port->m_ov); // Overlapped structure

// deal with any error codes
if (!bResult)
{
DWORD dwError = GetLastError();
switch (dwError)
{
case ERROR_IO_PENDING:
{
// continue to GetOverlappedResults()
BytesSent = 0;
bWrite = FALSE;
break;
}
default:
{
// all other error codes
port->ProcessErrorMessage("WriteFile()");
}
}
}
else
{
LeaveCriticalSection(&port->m_csCommunicationSync);
}
} // end if(bWrite)

if (!bWrite)
{
bWrite = TRUE;

bResult = GetOverlappedResult(port->m_hComm, // Handle to COMM port
&port->m_ov, // Overlapped structure
&BytesSent, // Stores number of bytes sent
TRUE); // Wait flag

LeaveCriticalSection(&port->m_csCommunicationSync);

// deal with the error code
// if (!bResult)
{
// port->ProcessErrorMessage("GetOverlappedResults() in WriteFile()");
}
} // end if (!bWrite)

//Verify that the data size send equals what we tried to send
if (BytesSent != port->m_nWriteSize) // Length of message to send)
{
TRACE("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d\n", BytesSent, strlen((char*)port->m_szWriteBuffer));
}
// ::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_TXEMPTY_DETECTED,0,(LPARAM) port->m_nPortNr);
}

//接受数据
void COperationPLC::ReceiveChar(COperationPLC* port, COMSTAT comstat)
{
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;

for (;;)
{
// Gain ownership of the comm port critical section.
// This process guarantees no other part of this program
// is using the port object.

EnterCriticalSection(&port->m_csCommunicationSync);

// ClearCommError() will update the COMSTAT structure and
// clear any other errors.

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

LeaveCriticalSection(&port->m_csCommunicationSync);

if (comstat.cbInQue == 0)
{
// break out when all bytes have been read
break;
}

EnterCriticalSection(&port->m_csCommunicationSync);

if (bRead)
{
int i=sizeof(RXBuff);//*************这里我修改为接受1024个字节
memset(RXBuff,0,1024);
bResult = ReadFile(port->m_hComm, // Handle to COMM port
&RXBuff, // RX Buffer Pointer
i, // Read one byte
&BytesRead, // Stores number of bytes read
&port->m_ov); // pointer to the m_ov structure
// deal with the error code
if (!bResult)
{
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
bRead = FALSE;
break;
}
default:
{
// Another error has occured. Process this error.
port->ProcessErrorMessage("ReadFile()");
break;
}
}
}
else
{
// ReadFile() returned complete. It is not necessary to call GetOverlappedResults()
bRead = TRUE;
}
} // close if (bRead)

if (!bRead)
{
bRead = TRUE;
bResult = GetOverlappedResult(port->m_hComm, // Handle to COMM port
&port->m_ov, // Overlapped structure
&BytesRead, // Stores number of bytes read
TRUE); // Wait flag

// deal with the error code
if (!bResult)
{
port->ProcessErrorMessage("GetOverlappedResults() in ReadFile()");
}
} // close if (!bRead)

LeaveCriticalSection(&port->m_csCommunicationSync);

// notify parent that a byte was received
//::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM)&RXBuff, (LPARAM) port->m_nPortNr);
CString str1=CString(RXBuff);
ReceiveData(str1);
} // end forever loop

}

void COperationPLC::WriteToPort(LPCTSTR string)
{
assert(m_hComm != 0);

memset(m_szWriteBuffer, 0, sizeof(m_szWriteBuffer));
strcpy(m_szWriteBuffer, string);
m_nWriteSize=strlen(string);

// set event for write
SetEvent(m_hWriteEvent);
}

上面就是我使用的网上的一个很流行的类,但是读写一段时间后(时间不固定,长短不一),串口会堵死,需要重启串口,各位高手看看这个有什么问题吗?谢谢!
...全文
471 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

2,641

社区成员

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

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