to 楼主: CSerialPort类确实有这种问题,我以前解决过下面的贴子有解决, http://expert.csdn.net/Expert/topic/1505/1505867.xml?temp=.2344019 ///////////////////////////////////////////////////////////////////// 回复人: AthlonxpX86(一滴水) ( ) 信誉:106 2003-3-8 17:01:07 得分:0 你把void CSerialPort::ReceiveChar稍微改一下,我觉得他的效率有些差,他每接收到一个字节数据就会给你的程序发消息,这样你的程序每秒钟要处理19200个消息,这样当然慢。我改了一下这个程序,你看看怎样,当然你的程序也需要改变,你自己的代码还是你改比较合适,我只提醒你注意LONG CXsTrendCtrl::OnCommunication(WPARAM ch, LPARAM port)的第一个参数将变成一个指向30个字符的字符传指针。后面就看你的了void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat) { BOOL bRead = TRUE; BOOL bResult = TRUE; DWORD dwError = 0; DWORD BytesRead = 0; char *RXBuff=NEW char[30];//改成30个字符 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); // start forever loop. I use this type of loop because I // do not know at runtime how many loops this will have to // run. My solution is to start a forever loop and to // break out of it when I have processed all of the // data available. Be careful with this approach and // be sure your loop will exit. // My reasons for this are not as clear in this sample // as it is in my production code, but I have found this // solutiion to be the most efficient way to do this.
if (comstat.cbInQue == 0) { // break out when all bytes have been read break; }
EnterCriticalSection(&port->m_csCommunicationSync); if (bRead) { bResult = ReadFile(port->m_hComm, // Handle to COMM port RXBuff, // RX Buffer Pointer 30, // Read 30 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: { // asynchronous i/o is still in progress // Proceed on to GetOverlappedResults(); 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); } // end forever loop}//////////////////////////////////////////////////不过按照你的要求还要注意一些问题,在你的消息函数中要是释放指针,
希望资源多多共享!
如果你能送给我([email protected]),
我将不甚感激!!!
qrlvls兄,你用的类能否也借给小弟参考一下,谢了!!!!:)
c0der() ,一滴水你们提供的程序都收到了,可能消化还要一二天
以后还会遇到更多的问题!
给我一个:[email protected]
问题还是没处理好,当接收信息是通过WM_COMM_RXCHAR消息触发
但我要急时显示这一次接收到的信息时,是每接到一个字节就触发
显示一下接收的信息,感觉处理不得不好,要时能接收完这次的信息
后再显示更新接收的信息就好了.
CSerialPort类确实有这种问题,我以前解决过下面的贴子有解决,
http://expert.csdn.net/Expert/topic/1505/1505867.xml?temp=.2344019
///////////////////////////////////////////////////////////////////// 回复人: AthlonxpX86(一滴水) ( ) 信誉:106 2003-3-8 17:01:07 得分:0 你把void CSerialPort::ReceiveChar稍微改一下,我觉得他的效率有些差,他每接收到一个字节数据就会给你的程序发消息,这样你的程序每秒钟要处理19200个消息,这样当然慢。我改了一下这个程序,你看看怎样,当然你的程序也需要改变,你自己的代码还是你改比较合适,我只提醒你注意LONG CXsTrendCtrl::OnCommunication(WPARAM ch, LPARAM port)的第一个参数将变成一个指向30个字符的字符传指针。后面就看你的了void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat)
{
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
char *RXBuff=NEW char[30];//改成30个字符 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); // start forever loop. I use this type of loop because I
// do not know at runtime how many loops this will have to
// run. My solution is to start a forever loop and to
// break out of it when I have processed all of the
// data available. Be careful with this approach and
// be sure your loop will exit.
// My reasons for this are not as clear in this sample
// as it is in my production code, but I have found this
// solutiion to be the most efficient way to do this.
if (comstat.cbInQue == 0)
{
// break out when all bytes have been read
break;
}
EnterCriticalSection(&port->m_csCommunicationSync); if (bRead)
{
bResult = ReadFile(port->m_hComm, // Handle to COMM port
RXBuff, // RX Buffer Pointer
30, // Read 30 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:
{
// asynchronous i/o is still in progress
// Proceed on to GetOverlappedResults();
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);
} // end forever loop}//////////////////////////////////////////////////不过按照你的要求还要注意一些问题,在你的消息函数中要是释放指针,
改成一次读30个字节,不过CSerialPort类中的
一些代码还是没完全看懂,对线程的了解不够.