串口通訊進行時WriteFile函數報錯
GetLastError捕捉錯誤代碼如下:997 Overlapped I/O operation is in progress. ERROR_IO_PENDING 我的問題是:
1、這種錯誤通常情況下是怎麽造成的?
2、爲什麽錯誤出現後,被程序操作的串口就只能接受數據而不能發送數據且reboot系統以後才變正常?請高手賜教!
GetLastError捕捉錯誤代碼如下:997 Overlapped I/O operation is in progress. ERROR_IO_PENDING 我的問題是:
1、這種錯誤通常情況下是怎麽造成的?
2、爲什麽錯誤出現後,被程序操作的串口就只能接受數據而不能發送數據且reboot系統以後才變正常?請高手賜教!
解决方案 »
- socket bind()调用出现问题了。。。。。。。。。
- 如何将CString转化到指定元素起始位置的char数组中?
- 关于ONHSCROLL
- 请问大家,下面的代码为什么不能把ICON加入到list ctrl?
- 200分相送,Custom AppWizard设置Project\Settings的相关选项的问题
- 怎么样才能做到一个exe文件随着系统启动的时候就自己运行呀?
- 如何读取CPU的序列号或则其他信息???
- 关于Slider控件重绘问题
- 生成最简单窗口的问题
- VC封装ADO类出现问题,因该是这段出了问题,各位大虾帮忙呀
- 如何把其他程序中的.cpp和相应的.h加入自己的程序
- 如何使CView上的Static变成透明?
采用异步方式 WriteFile 会立即返回出错的 但是这个情况是正常的
后面要加一个等待程序
给我一个行吗?
[email protected]
{
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 != strlen((char*)port->m_szWriteBuffer))
{
// 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, (WPARAM) RXBuff, (LPARAM) port->m_nPortNr);
::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_TXEMPTY_DETECTED,0,(LPARAM) port->m_nPortNr);}