怎么用将串口读到的数据显示在MFC的编辑框
如何用将串口读到的数据显示在MFC的编辑框
c++新手,刚接手的代码,往下位机写命令已经完成了,我需要把下位机存在3000H的的信息读上来,请问怎么写呢,我一点头绪也没有,麻烦大家指导下,
读写串口用的是下面链接里的
http://blog.csdn.net/zmq5411/article/details/6542309
我是要用下面这一部分吗?
(4)读取数据操作
读取数据是一个异步操作,当有数据发来时,会触发读事件m_ov.hEvent,监视线程捕捉到事件后并获知是读事件,进入相关读处理,这里调用函数ReceiveChar
,ReceiveChar中调用ReadFile函数将串口数据读到Buffer缓冲中,相关代码如下:
view plaincopy to clipboardprint?
//接收数据
void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat)
{
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
unsigned char RXBuff;
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.
//更新COMSTAT
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
1, // 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:
{
// asynchronous i/o is still in progress
// Proceed on to GetOverlappedResults();
//异步IO仍在进行
bRead = FALSE;
break;
}
default:
{ //其他错误处理
// Another error has occured. Process this error.
port->ProcessErrorMessage("ReadFile()");
break;
}
}
}
else
{ //ReadFile返回TRUE
// ReadFile() returned complete. It is not necessary to call GetOverlappedResults()
bRead = TRUE;
}
} // close if (bRead)
//异步IO操作仍在进行,需要调用GetOverlappedResult查询
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
}
------解决思路----------------------
SetRel
ReplaceRel
c++新手,刚接手的代码,往下位机写命令已经完成了,我需要把下位机存在3000H的的信息读上来,请问怎么写呢,我一点头绪也没有,麻烦大家指导下,
读写串口用的是下面链接里的
http://blog.csdn.net/zmq5411/article/details/6542309
我是要用下面这一部分吗?
(4)读取数据操作
读取数据是一个异步操作,当有数据发来时,会触发读事件m_ov.hEvent,监视线程捕捉到事件后并获知是读事件,进入相关读处理,这里调用函数ReceiveChar
,ReceiveChar中调用ReadFile函数将串口数据读到Buffer缓冲中,相关代码如下:
view plaincopy to clipboardprint?
//接收数据
void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat)
{
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
unsigned char RXBuff;
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.
//更新COMSTAT
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
1, // 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:
{
// asynchronous i/o is still in progress
// Proceed on to GetOverlappedResults();
//异步IO仍在进行
bRead = FALSE;
break;
}
default:
{ //其他错误处理
// Another error has occured. Process this error.
port->ProcessErrorMessage("ReadFile()");
break;
}
}
}
else
{ //ReadFile返回TRUE
// ReadFile() returned complete. It is not necessary to call GetOverlappedResults()
bRead = TRUE;
}
} // close if (bRead)
//异步IO操作仍在进行,需要调用GetOverlappedResult查询
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
}
------解决思路----------------------
SetRel
ReplaceRel