void CSerialPort::WriteChar(CSerialPort* port)
{
BOOL bWrite = TRUE;
BOOL bResult = FALSE;
DWORD BytesSent = 0;
ResetEvent(port->m_hWriteEvent);
EnterCriticalSection(&port->m_csCommunicationSync);
if (bWrite)
{
PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); port->m_ov.Offset = 0;
port->m_ov.OffsetHigh = 0;
bResult = WriteFile(port->m_hComm, // Handle to COMM Port
port->m_szWriteBuffer, // Pointer to message buffer in calling finction
strlen(port->m_szWriteBuffer), // Length of message to send
&BytesSent, // Where to store the number of bytes sent
&port->m_ov); // Overlapped structur if (!bResult)
{
DWORD dwError = GetLastError();
switch (dwError)
{
case ERROR_IO_PENDING:
{
BytesSent = 0;
bWrite = FALSE;
break;
}
default:
{
port->ProcessErrorMessage("WriteFile()");
}
}
}
else
{
//AfxMessageBox("22222");
CString s;
s.Format("m_szWriteBuffer=%s",port->m_szWriteBuffer);
AfxMessageBox(s);
LeaveCriticalSection(&port->m_csCommunicationSync);
}
}
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);
if (!bResult)
{
port->ProcessErrorMessage("GetOverlappedResults() in WriteFile()");
}
}
if (BytesSent != strlen((char*)port->m_szWriteBuffer))
{
TRACE("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d\n", BytesSent, strlen((char*)port->m_szWriteBuffer));
}
}
问题:这个发送返回值总是0,唉!找了半天也不知道为什么,更奇怪的是,我自发自收还能收到字符。服了,请大虾帮忙!!谢谢;
bResult = WriteFile(port->m_hComm, // Handle to COMM Port
port->m_szWriteBuffer, // Pointer to message buffer in calling finction
strlen(port->m_szWriteBuffer), // Length of message to send
&BytesSent, // Where to store the number of bytes sent
&port->m_ov);
{
BOOL bWrite = TRUE;
BOOL bResult = FALSE;
DWORD BytesSent = 0;
ResetEvent(port->m_hWriteEvent);
EnterCriticalSection(&port->m_csCommunicationSync);
if (bWrite)
{
PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); port->m_ov.Offset = 0;
port->m_ov.OffsetHigh = 0;
bResult = WriteFile(port->m_hComm, // Handle to COMM Port
port->m_szWriteBuffer, // Pointer to message buffer in calling finction
strlen(port->m_szWriteBuffer), // Length of message to send
&BytesSent, // Where to store the number of bytes sent
&port->m_ov); // Overlapped structur if (!bResult)
{
DWORD dwError = GetLastError();
switch (dwError)
{
case ERROR_IO_PENDING:
{
BytesSent = 0;
bWrite = FALSE;
break;
}
default:
{
port->ProcessErrorMessage("WriteFile()");
}
}
}
else
{
//AfxMessageBox("22222");
CString s;
s.Format("m_szWriteBuffer=%s",port->m_szWriteBuffer);
AfxMessageBox(s);
LeaveCriticalSection(&port->m_csCommunicationSync);
}
}
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);
if (!bResult)
{
port->ProcessErrorMessage("GetOverlappedResults() in WriteFile()");
}
}
if (BytesSent != strlen((char*)port->m_szWriteBuffer))
{
TRACE("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d\n", BytesSent, strlen((char*)port->m_szWriteBuffer));
}
}
问题:这个发送返回值总是0,唉!找了半天也不知道为什么,更奇怪的是,我自发自收还能收到字符。服了,请大虾帮忙!!谢谢;
bResult = WriteFile(port->m_hComm, // Handle to COMM Port
port->m_szWriteBuffer, // Pointer to message buffer in calling finction
strlen(port->m_szWriteBuffer), // Length of message to send
&BytesSent, // Where to store the number of bytes sent
&port->m_ov);
解决方案 »
- 如何编程控制一个线程的cpu占用率不超过10%?
- vc2005中,怎样每次release让程序的版本递增?
- 在对话框程序里面有没有什么办法实现动态画图,就像window资源管理器的CPU使用记录那样
- 关于<<c++编程思想>>的源码和答案
- 不用类向导写连接数据库的程序段,如何修改字段中的数据那?请各位大侠多指点(不用sql语句), 小弟深表感谢
- 请问VC下哪个函数可以获得屏幕的尺寸大小? 如:1024,768这些数据
- 请问窗口和视口的区别,请说浅显些,谢谢
- 关于校园网的建设问题:
- "error C2653: 'Cxxxxxx' : is not a class or namespace name"是怎么回事儿?
- 求救,求救,我的windows2000的ie主页被人改了,工具栏下的internet选项不能修改,这可如何办呀???
- 处理格式化的.txt文件
- 怎样mp3解码文件啊?
port->m_szWriteBuffer, // Pointer to message buffer in calling finction
strlen(port->m_szWriteBuffer), // Length of message to send
&BytesSent, // Where to store the number of bytes sent
&port->m_ov);
//overlap方式,操作不等执行完毕就立即返回,用GetLastError()查看具体错误,如果是ERROR_IO_PENDING,则说明串口操作正在进行中。
函数问题,先查msdn