1、合并master与2021.1

This commit is contained in:
xiejunjie
2021-12-03 11:25:35 +08:00
parent dc07b8879b
commit 4bd7d1b80b
981 changed files with 384342 additions and 6470 deletions
@@ -7,7 +7,7 @@
//////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
#define LONG_TIMEOUT 5000
#define MAXBLOCK 4096
/*
/////////////////////////////////////////////////////////////////////////////
// Code - Text for debug window
@@ -70,13 +70,14 @@ CPSerial::CPSerial()
m_hWaitCMMResponse = CreateEvent( NULL, TRUE, FALSE, NULL );
m_hNewRx = CreateEvent( NULL, TRUE, FALSE, NULL ); // to trigger OnRx
m_ReadOLap.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
//m_RXTempPtr = NULL;
m_DebugInPtr = 0;
m_DebugCount = 0;
m_Item = 0;
m_MaxTXRetries = 5;
m_iRecvCount=252;
/*
// CriticalSection for locking lists
InitializeCriticalSection( &m_QueueLock );
@@ -157,14 +158,14 @@ DWORD CPSerial::Open()
// Setup the timeouts
CommTimeOut.ReadIntervalTimeout = 25;
CommTimeOut.ReadTotalTimeoutMultiplier = 1;
CommTimeOut.ReadTotalTimeoutConstant = 0;
CommTimeOut.WriteTotalTimeoutMultiplier = 0;
CommTimeOut.ReadTotalTimeoutConstant = 1;
CommTimeOut.WriteTotalTimeoutMultiplier = 1;
CommTimeOut.WriteTotalTimeoutConstant = m_TXTimeout;
if( SetCommTimeouts( m_PortHandle, &CommTimeOut ) )
{
// Setup the buffer sizes
if( SetupComm( m_PortHandle, 2048, 2048 ) )
{
{
// Setup the event masks for the monitoring task
if( SetCommMask( m_PortHandle, EV_RXCHAR | EV_TXEMPTY | EV_BREAK |
EV_CTS | EV_DSR | EV_ERR | EV_RLSD ) )
@@ -263,6 +264,14 @@ void CPSerial::GetPortData(int *Port,int *Baud,char *Parity,int *Bits,int *StopB
*HandShake = m_HandShake;
}
DWORD CPSerial::SendWriteFile(const char *Buffer, DWORD Bytes)
{
DWORD BytesWritten;
BOOL WriteState;
WriteState = WriteFile(m_PortHandle, &Buffer[0], Bytes, &BytesWritten,
&m_WriteOLap);
return WriteState;
}
/////////////////////////////////////////////////////////////////////////////
// ClosePort() : Close the port and shut down the monitoring thread
@@ -379,7 +388,7 @@ DWORD CPSerial::WritePort(const char *Buffer,DWORD Bytes)
&m_WriteOLap );
if( !WriteState )
{
Sleep(50);
Sleep(5);
// Ensure the write is going on in the background
if( GetLastError() == ERROR_IO_PENDING )
{
@@ -413,6 +422,43 @@ DWORD CPSerial::WritePort(const char *Buffer,DWORD Bytes)
return( TotalWritten );
}
//==========================================================================
int CPSerial::ReadBlock(BYTE *abIn, int MaxLength)
{
BOOL JudgeRead;
COMSTAT ComStat;
DWORD dwErrorFlags, dwLength;
ClearCommError(m_PortHandle, &dwErrorFlags, &ComStat);
if (dwErrorFlags>0)
{
PurgeComm(m_PortHandle, PURGE_RXABORT | PURGE_RXCLEAR);
return 0;
}
dwLength = ((DWORD)MaxLength<ComStat.cbInQue ? MaxLength : ComStat.cbInQue);
memset(abIn, 0, MaxLength);
//如果有字符即读入
if (dwLength)
{
JudgeRead = ReadFile(m_PortHandle, abIn, dwLength, &dwLength, &m_ReceiveOLap);//读出字符至abIn处
if (!JudgeRead)
{
//如果重叠操作未完成,等待直到操作完成
if (GetLastError() == ERROR_IO_PENDING)
{
// WaitForSingleObject(m_osRead.hEvent,INFINITE);
GetOverlappedResult(m_PortHandle, &m_ReceiveOLap, &dwLength, TRUE);
m_ReceiveOLap.Offset = 0;
// m_osRead.Offset=(m_osRead.Offset+dwLength)%MAXBLOCK;
}
else
dwLength = 0;
}
}
return dwLength;
}
/////////////////////////////////////////////////////////////////////////////
// ReceiveTask() : Internal function, this runs as a thread and provides the
// OnRecieve and OnTransmit events
@@ -425,52 +471,63 @@ void CPSerial::ReceiveTask( void )
do
{
Events=0;
// Wait for a comm event
State=WaitCommEvent(m_PortHandle,&Events,&m_ReceiveOLap);
if(!State)
{
// Since we are using overlapping IO we may have to wait
// for the result
if(GetLastError() == ERROR_IO_PENDING)
GetOverlappedResult(m_PortHandle,&m_ReceiveOLap,&State,TRUE);
}
//m_iRecvState=FALSE;
// If we have a result then OK otherwise the event was probable
// the serial port being closed and we shall exit the loop
if( State && IsOpen( ) )
{
// Check the events and act accordingly
if( Events & EV_RXCHAR )
Sleep(2);
BYTE abIn[MAXBLOCK];
int len;
len = ReadBlock(abIn, MAXBLOCK);
if ((len>0) && (len<MAX_RECIEVE_BUFFER_SIZE))
{
OnReceive( );
//memset(m_RecvData, 0, m_iRecvBytes);
memcpy(m_RecvData, abIn, len);
m_iRecvBytes = len;
m_iRecvState = TRUE;
}
//ZH
/*
if( Events & EV_TXEMPTY )
{
if( m_TXHead )
{
GetOverlappedResult(m_PortHandle,&m_TransmitOLap,&BytesWritten,TRUE);
if( BytesWritten )
AddToDebug( m_TXHead->Buffer, BytesWritten, 2 );
OnTransmit( 0, BytesWritten );
SendBuffer(TRUE);
}
}
*/
if(Events & EV_BREAK)
TRACE(_T("Break detected\n"));
if(Events & EV_CTS)
TRACE(_T("CTS Changed State\n"));
if(Events & EV_DSR)
TRACE(_T("DSR Changed State\n"));
if(Events & EV_ERR)
TRACE(_T("Line error\n"));
if(Events & EV_RLSD)
TRACE(_T("EV_RLSD error\n"));
}
// Events=0;
//
// // Wait for a comm event
// State=WaitCommEvent(m_PortHandle,&Events,&m_ReceiveOLap);
// if(!State)
// {
// // Since we are using overlapping IO we may have to wait
// // for the result
// if(GetLastError() == ERROR_IO_PENDING)
// GetOverlappedResult(m_PortHandle,&m_ReceiveOLap,&State,TRUE);
// }
////m_iRecvState=FALSE;
// // If we have a result then OK otherwise the event was probable
// // the serial port being closed and we shall exit the loop
// if( State && IsOpen( ) )
// {
// // Check the events and act accordingly
// if( Events & EV_RXCHAR )
// {
// OnReceive( );
// }
// //ZH
// /*
// if( Events & EV_TXEMPTY )
// {
// if( m_TXHead )
// {
// GetOverlappedResult(m_PortHandle,&m_TransmitOLap,&BytesWritten,TRUE);
// if( BytesWritten )
// AddToDebug( m_TXHead->Buffer, BytesWritten, 2 );
// OnTransmit( 0, BytesWritten );
// SendBuffer(TRUE);
// }
// }
// */
// if(Events & EV_BREAK)
// TRACE(_T("Break detected\n"));
// if(Events & EV_CTS)
// TRACE(_T("CTS Changed State\n"));
// if(Events & EV_DSR)
// TRACE(_T("DSR Changed State\n"));
// if(Events & EV_ERR)
// TRACE(_T("Line error\n"));
// if(Events & EV_RLSD)
// TRACE(_T("EV_RLSD error\n"));
// }
// Go round while the port is open
}
@@ -512,10 +569,10 @@ void CPSerial::OnReceive()
}
else
{
int num = ReadPort(s, 252);
int num = ReadPort(s, m_iRecvCount);
if ((num>0) && (num<MAX_RECIEVE_BUFFER_SIZE))
{
memset(m_RecvData,0,m_iRecvBytes);
// memset(m_RecvData,0,m_iRecvBytes);
memcpy(m_RecvData,s, num);
m_iRecvBytes=num;
m_iRecvState=TRUE;
@@ -544,7 +601,7 @@ DWORD CPSerial::ReadPort(char *Buffer,DWORD Bytes)
ReadState = ReadFile(m_PortHandle,Buffer,Bytes,&BytesRead,&m_ReadOLap);
if( !ReadState )
{
Sleep(50);
Sleep(3);
// the specifed number of bytes were not available so
// the read will continue in the background aslong as
// GetLastError() returns ERROR_IO_PENDING
@@ -666,6 +723,7 @@ int CPSerial::ProgramPort(int Port,int Baud,char Parity,int Bits,int StopBits,in
SerialDCB.fOutxDsrFlow=FALSE;
SerialDCB.XonLim=2048;
SerialDCB.XoffLim=512;
break;
case CS_HANDSHAKE_FOR_TRESASTR_E:
SerialDCB.EofChar = 26;
SerialDCB.XonChar = 17;