在进行程序编译时出现了下面的错误
1、在将数据接受屏蔽后出现了First-chance exception in Quiver4_19.exe (KERNEL32.DLL): 0xC0000005: Access Violation.的错误
2、在开启数据接受的时候出现了HEAP[Quiver4_19.exe]: Invalid heap signature for heap at 130000, passed to RtlLockHeap
能请高手解答么,谢谢啊
以下是代码
进程函数负责监视
UINT CSerialPort::CommThread(LPVOID pParam)
{
// Cast the void pointer passed to the thread back to
// a pointer of CSerialPort class
CSerialPort *port = (CSerialPort*)pParam;
// Set the status variable in the dialog class to
// TRUE to indicate the thread is running.
port->m_bThreadAlive = TRUE;
// Misc. variables
DWORD BytesTransfered = 0;
DWORD Event = 0;
DWORD CommEvent = 0;
DWORD dwError = 0;
COMSTAT comstat;
unsigned int cnt1=0;
CString strcnt1;
BOOL bResult = TRUE;
//AfxMessageBox("i am in the Thread!");
// Clear comm buffers at startup
if (port->m_hComm) // check if the port is opened
PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
// begin forever loop. This loop will run as long as the thread is alive.
for (;;)
{
bResult = WaitCommEvent(port->m_hComm, &Event, &port->m_ov);
if (!bResult)
{
// If WaitCommEvent() returns FALSE, process the last error to determin
// the reason..
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
// This is a normal return value if there are no bytes
// to read at the port.
// Do nothing and continue
break;
}
case 87:
{
// Under Windows NT, this value is returned for some reason.
// I have not investigated why, but it is also a valid reply
// Also do nothing and continue.
break;
}
default:
{
// All other error codes indicate a serious error has
// occured. Process this error.
port->ProcessErrorMessage("WaitCommEvent()");
break;
}
}
}
else
{
bResult = ClearCommError(port->m_hComm, &dwError, &comstat);
if (comstat.cbInQue == 0)
continue;
} // end if bResult
Event = WaitForMultipleObjects(3, port->m_hEventArray, FALSE, INFINITE);//返回有信号事件索引
switch (Event)
{
case 0:
{
// Shutdown event. This is event zero so it will be
// the higest priority and be serviced first.
port->m_bThreadAlive = FALSE;
// 取消此进程
AfxEndThread(100);
break;
}
case 1: // 读串口事件
{
if (CommEvent & EV_RXCHAR)
// 从串口接收字符事件
{
ReceiveChar(port, comstat); //处理事件,传入串口极其状态
cnt1++;
strcnt1.Format("%d",cnt1);
TRACE("Get the EV_RXCHAR EVENT\n");
// AfxMessageBox(strcnt1); //调试
}
break;
}
case 2: // 写串口事件
{
// Write character event from port
WriteChar(port);
break;
}
} // end switch
} // close forever loop
return 0;
}
处理函数
ReceiveChar()处理消息
void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat)
{
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
unsigned int cnt=0;
unsigned char RXBuff;
for (;;)
{
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);
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();
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);
TRACE("I am in ReciveChar Function!\n");
// notify parent that a byte was received
::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM) port->m_nPortNr);
CString strcnt;
cnt++;
strcnt.Format("%d",cnt);
//AfxMessageBox(strcnt); //调试
} // end forever loop
}
响应的消息函数LONG CQuiver4_19Dlg::OnRecive(WPARAM wParam,LPARAM lParam)
{
unsigned int ser_ch;
ser_ch=wParam;
tempcnt++;
CString temp_message;
temp_message.Format("%d",tempcnt);
switch(RCnt) //接收数据(当屏蔽时发出第一个错误)
{
case 0:
if(ser_ch==255) RCnt++;TRACE("THE NUMBER 1\n");return 0;
case 5:
ser_buff[WriteCnt*5+RCnt-1]=ser_ch;RCnt=0;
WriteCnt++;if(WriteCnt>818) WriteCnt=0;TRACE("THE NUMBER 2\n");return 0;
default:
ser_buff[WriteCnt*5+RCnt-1]=ser_ch;RCnt++;TRACE("THE NUMBER 3\n");return 0;
}
return 0;
}