3,248
社区成员
发帖
与我相关
我的任务
分享
在串口初始化里设置超时部分
m_CommTimeouts.ReadIntervalTimeout = 10; //两字符之间最大的延时
m_CommTimeouts.ReadTotalTimeoutMultiplier = 10; //指定比例因子(毫秒)
m_CommTimeouts.ReadTotalTimeoutConstant = 100; //一次读取串口数据的固定超时。
m_CommTimeouts.WriteTotalTimeoutMultiplier = 10; //写入每字符间的超时。
m_CommTimeouts.WriteTotalTimeoutConstant = 10; //一次写入串口数据的固定超时。
// configure
if (SetCommTimeouts(m_hComm, &m_CommTimeouts))
{
if (SetCommMask(m_hComm, dwCommEvents))
{
if (GetCommState(m_hComm, &m_dcb))
{
}
else
ProcessErrorMessage("GetCommState()");
}
else
ProcessErrorMessage("SetCommMask()");
}
else
ProcessErrorMessage("SetCommTimeouts()");
读串口
if (bRead)
{
bResult = ReadFile(port->m_hComm, // Handle to COMM port
pRXBuff, // RX Buffer Pointer
comstat.cbInQue, // Read one byte
&BytesRead, // Stores number of bytes read
&rol);//port->m_ov); // pointer to the m_ov structure
if (!bResult)
{
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
bRead = FALSE;
break;
}
default:
{
port->ProcessErrorMessage("ReadFile()");
break;
}
}
}
else
{
bRead = TRUE;
}
} // close if (bRead)
if (!bRead)
{
bRead = TRUE; //当调用ReadFile, WriteFile 函数的时候,该GetOverlappedResult会自动被置为无信号状态;当重叠操作完成后,该成员变量会自动被置为有信号状态。
bResult = GetOverlappedResult(port->m_hComm, // Handle to COMM port
&port->m_ov, // Overlapped structure
&BytesRead, // Stores number of bytes read
TRUE); // Wait flag
if (!bResult)
{
port->ProcessErrorMessage("GetOverlappedResults() in ReadFile()");
}
} // close if (!bRead)