Home > Software engineering >  Vs2008 MFC serialport class ModBus PC communication program to send port abnormal shutdown
Vs2008 MFC serialport class ModBus PC communication program to send port abnormal shutdown

Time:10-09

In written in MFC serialport class ModBus PC communication program to send data port abnormal shutdown, switch off process, program crash problems, unable to take the next step of operation, but the next bit machine with a serial port debug tool to receive the upper machine data
To send data format is as follows:
 
Unsigned char buff [256].
Memset (buff, 0 x00, 256);
Buff [0]=0 x01;
Buff [1]=0 x03;
Buff [2]=0 x00;
Buff [3]=0 x00;
Buff [4]=0 x00;
Buff [5]=(unsigned char) RegCount;
Crc16=CalcCrcFast (buff, 6);
Buff [6]=(unsigned char) (crc16 & gt;> 8);
Buff [7]=(unsigned char) crc16;
//write data
If (m_bSerialPortOpened)
{
M_SerialPort. WriteToPort (buff (char *), 8).
}

When the serialport. The CPP run to the CommThread function is directly from the case2 directly into the event, then the function, then calculated the event becomes zero value, when performing case0, close port, shut off process, dialog box card dead, why is this? How to solve the problem?
 UINT CSerialPort: : CommThread (LPVOID pParam) 
{
//Cast the void pointer passed to the thread back to
//a pointer of CSerialPort class
PParam CSerialPort * port=(CSerialPort *);

//Set the status variable in the dialog class to
//TRUE to indicate the thread is running.
The port - & gt; M_bThreadAlive=TRUE;

//Misc variables
DWORD BytesTransfered=0;
DWORD Event=0;
DWORD CommEvent=0;
DWORD dwError=0;
The COMSTAT COMSTAT;
BOOL bResult=TRUE;

//the Clear comm buffers at startup
If (port - & gt; M_hComm)//check if the port is the opened
PurgeComm (port - & gt; M_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);

//the begin forever loop. This loop will run as long as the thread is alive.
For (;; )
{

//Make a call to WaitCommEvent (). This call will return immediatly
//because our port was created as an async port (FILE_FLAG_OVERLAPPED
//and the an m_OverlappedStructerlapped structure specified). This call will cause the
//element m_OverlappedStructerlapped m_OverlappedStruct hEvent, which is part of the m_hEventArray to
//be placed in a non - signeled state if there are no bytes available to be read,
//the or to a signeled state if there are bytes available. If this event handle
//is set to the non - signeled state, it will be set to signeled the when a
//character arrives at the port.

//we do this for each port!

BResult=WaitCommEvent (port - & gt; M_hComm, & amp; The Event, & amp; The port - & gt; M_ov);

if (! BResult)
{
//If WaitCommEvent () returns FALSE, process the last error to determin
//the reason..
The 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 according to, 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.
The port - & gt; ProcessErrorMessage (" WaitCommEvent () ");
break;
}
}
}
The else
{
//If WaitCommEvent () returns TRUE, check to be sure there are
//later bytes in the buffer to read.
//
//If you are reading more than one byte at a time from the buffer
//(which this program does not do) you will have the situation occur
//where the first byte to arrive will cause the WaitForMultipleObjects ()
//function to stop waiting. The WaitForMultipleObjects () function
.//resets the event handle in m_OverlappedStruct hEvent to the non - signelead state
//as it returns.
//
//If in the time between the reset of this event and the call to
//ReadFile () more bytes arrive, the m_OverlappedStruct. HEvent handle will be set again
//to the signeled state. When the call to ReadFile () occurs, it will
//read all of the bytes from the buffer, and the program will
//loop back around to WaitCommEvent (.)
//
//At this point you will be in the situation where m_OverlappedStruct. HEvent is set,
//but there are no bytes available to read. If you proceed and call
//ReadFile (), it will return immediatly due to the async port setup, but
//GetOverlappedResults () will not return until the next character arrives.
//
//It is not desirable for GetOverlappedResults () function to be in
//this state. The thread shutdown event (event 0) and The WriteFile ()
//event (Event2) will not work if the thread is blocked by GetOverlappedResults ().
//
//The solution to this is to check The buffer with a call to ClearCommError ().
//This call will reset the event handle, and if there are no bytes to read
//we can loop back through WaitCommEvent () again, then proceed.
//If there are really bytes to read, do nothing and proceed.

BResult=ClearCommError (port - & gt; M_hComm, & amp; DwError, & amp; The comstat);

If (comstat. CbInQue==0)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related