|
|
|
@@ -6,7 +6,7 @@
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace std;
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
#define LONG_TIMEOUT 5000
|
|
|
|
@@ -178,7 +178,8 @@ DWORD CPSerial::Open()
|
|
|
|
|
// Setup the port according to the stored parameters
|
|
|
|
|
if (ProgramPort(m_Port, m_Baud, m_Parity, m_Bits, m_StopBits, m_HandShake))
|
|
|
|
|
{
|
|
|
|
|
TRACE(_T("CmmIO> Port OPEN %s\n", m_Port), Ok);
|
|
|
|
|
TRACE(_T("CmmIO> Port OPEN %s, m_Baud\n", m_Port, m_Baud), Ok);
|
|
|
|
|
printf("m_Baud: %d\n", m_Baud);
|
|
|
|
|
// Setup the timeouts
|
|
|
|
|
CommTimeOut.ReadIntervalTimeout = 25;
|
|
|
|
|
CommTimeOut.ReadTotalTimeoutMultiplier = 1;
|
|
|
|
@@ -486,6 +487,76 @@ int CPSerial::ReadBlock(BYTE* abIn, int MaxLength)
|
|
|
|
|
// ReceiveTask() : Internal function, this runs as a thread and provides the
|
|
|
|
|
// OnRecieve and OnTransmit events
|
|
|
|
|
|
|
|
|
|
//void CPSerial::ReceiveTask(void)
|
|
|
|
|
//{
|
|
|
|
|
// //DWORD BytesWritten;
|
|
|
|
|
// DWORD Events;
|
|
|
|
|
// unsigned long State;
|
|
|
|
|
//
|
|
|
|
|
// do
|
|
|
|
|
// {
|
|
|
|
|
// Sleep(2);
|
|
|
|
|
// BYTE abIn[MAXBLOCK];
|
|
|
|
|
// int len;
|
|
|
|
|
// len = ReadBlock(abIn, MAXBLOCK);
|
|
|
|
|
// if ((len > 0) && (len < MAX_RECIEVE_BUFFER_SIZE))
|
|
|
|
|
// {
|
|
|
|
|
// //memset(m_RecvData, 0, m_iRecvBytes);
|
|
|
|
|
// memcpy(m_RecvData, abIn, len);
|
|
|
|
|
// m_iRecvBytes = len;
|
|
|
|
|
// m_iRecvState = TRUE;
|
|
|
|
|
// }
|
|
|
|
|
// // 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
|
|
|
|
|
// }
|
|
|
|
|
// while (IsOpen());
|
|
|
|
|
//}
|
|
|
|
|
void CPSerial::ReceiveTask(void)
|
|
|
|
|
{
|
|
|
|
|
//DWORD BytesWritten;
|
|
|
|
@@ -494,69 +565,56 @@ void CPSerial::ReceiveTask(void)
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
Sleep(2);
|
|
|
|
|
BYTE abIn[MAXBLOCK];
|
|
|
|
|
int len;
|
|
|
|
|
len = ReadBlock(abIn, MAXBLOCK);
|
|
|
|
|
if ((len > 0) && (len < MAX_RECIEVE_BUFFER_SIZE))
|
|
|
|
|
Events = 0;
|
|
|
|
|
|
|
|
|
|
// Wait for a comm event
|
|
|
|
|
State = WaitCommEvent(m_PortHandle, &Events, &m_ReceiveOLap);
|
|
|
|
|
if (!State)
|
|
|
|
|
{
|
|
|
|
|
//memset(m_RecvData, 0, m_iRecvBytes);
|
|
|
|
|
memcpy(m_RecvData, abIn, len);
|
|
|
|
|
m_iRecvBytes = len;
|
|
|
|
|
m_iRecvState = TRUE;
|
|
|
|
|
// 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"));
|
|
|
|
|
}
|
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
while (IsOpen());
|
|
|
|
|
} while (IsOpen());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// OnReceive() : Default OnReceive()
|
|
|
|
|
// V114
|
|
|
|
@@ -567,7 +625,12 @@ void CPSerial::OnReceive()
|
|
|
|
|
char s[MAX_RECIEVE_BUFFER_SIZE] = {0};
|
|
|
|
|
s[1] = '\0';
|
|
|
|
|
CurrentPointer = 0;
|
|
|
|
|
printf("MAX_RECIEVE_BUFFER_SIZE: %d m_HandShake:%d\n", MAX_RECIEVE_BUFFER_SIZE, m_HandShake);
|
|
|
|
|
int num = 0;
|
|
|
|
|
printf(" m_HandShake:%d\n", m_HandShake); //打印握手类型
|
|
|
|
|
|
|
|
|
|
Sleep(100); //延时100ms ,等待接收
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (m_HandShake == CS_HANDSHAKE_FOR_TRESASTR_E)
|
|
|
|
|
{
|
|
|
|
|
int num = ReadPort(s, MAX_RECIEVE_BUFFER_SIZE);
|
|
|
|
@@ -593,8 +656,8 @@ void CPSerial::OnReceive()
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int num = ReadPort(s, m_iRecvCount);
|
|
|
|
|
printf("----RECV: %d----\r\n", num);
|
|
|
|
|
num = ReadPort(s, m_iRecvCount);
|
|
|
|
|
printf("----Data received:: %d----\r\n", num);
|
|
|
|
|
if ((num > 0) && (num < MAX_RECIEVE_BUFFER_SIZE))
|
|
|
|
|
{
|
|
|
|
|
// memset(m_RecvData,0,m_iRecvBytes);
|
|
|
|
@@ -614,6 +677,92 @@ void CPSerial::OnReceive()
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// ReadPort() : Read the specifed number of bytes.
|
|
|
|
|
//
|
|
|
|
|
#if 0
|
|
|
|
|
DWORD CPSerial::ReadPort(char* Buffer, DWORD Bytes)
|
|
|
|
|
{
|
|
|
|
|
DWORD recieved, Error;
|
|
|
|
|
BOOL ReadState;
|
|
|
|
|
|
|
|
|
|
long len = 0;
|
|
|
|
|
int read_loop = 1;
|
|
|
|
|
// Check the port is open
|
|
|
|
|
if (IsOpen())
|
|
|
|
|
{
|
|
|
|
|
// Enter a critical section incase this is been used from multiple threads
|
|
|
|
|
// EnterCriticalSection(&m_ReadLock);
|
|
|
|
|
// Start the read
|
|
|
|
|
while (read_loop != 0)
|
|
|
|
|
{
|
|
|
|
|
//BOOL ReadFile(
|
|
|
|
|
// HANDLE hFile, //文件的句柄
|
|
|
|
|
// LPVOID lpBuffer, //用于保存读入数据的一个缓冲区
|
|
|
|
|
// DWORD nNumberOfBytesToRead, //要读入的字节数
|
|
|
|
|
// LPDWORD lpNumberOfBytesRead, //指向实际读取字节数的指针
|
|
|
|
|
// LPOVERLAPPED lpOverlapped
|
|
|
|
|
// //如文件打开时指定了FILE_FLAG_OVERLAPPED,那么必须,用这个参数引用一个特殊的结构。
|
|
|
|
|
// //该结构定义了一次异步读取操作。否则,应将这个参数设为NULL
|
|
|
|
|
//);
|
|
|
|
|
ReadState = ReadFile(m_PortHandle, Buffer, 1, &recieved, &m_ReadOLap);
|
|
|
|
|
printf("Bytes: %d\r\n", Bytes);
|
|
|
|
|
if (!ReadState)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
if (GetLastError() == ERROR_IO_PENDING)
|
|
|
|
|
{
|
|
|
|
|
// Wait for the read to complete
|
|
|
|
|
WaitForSingleObject(m_ReadOLap.hEvent, LONG_TIMEOUT); // GER
|
|
|
|
|
|
|
|
|
|
// get the result of the read
|
|
|
|
|
if (GetOverlappedResult(m_PortHandle, &m_ReadOLap, &recieved, FALSE) == 0)
|
|
|
|
|
Error = GetLastError();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Gone wrong so clear any erros
|
|
|
|
|
ClearCommError(m_PortHandle, &Error, nullptr);
|
|
|
|
|
recieved = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
len++;
|
|
|
|
|
printf("BytesRead:%d %s\r\n", len, std::to_string(recieved));
|
|
|
|
|
// recieved prompt, so end read loop
|
|
|
|
|
if ((len == 2) && (Buffer[0] == '6F') && (Buffer[1] == '6B'))
|
|
|
|
|
{
|
|
|
|
|
printf("1111");
|
|
|
|
|
read_loop = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// recieve nothing so quit
|
|
|
|
|
if (len <= 0) { read_loop = 0; }
|
|
|
|
|
|
|
|
|
|
if (Buffer[len - 1] != '\r')
|
|
|
|
|
{
|
|
|
|
|
printf("%C", Buffer[len - 1]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
len = 0;
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//ZH
|
|
|
|
|
/*
|
|
|
|
|
// Read some bytes so add then to the debug list
|
|
|
|
|
if( BytesRead )
|
|
|
|
|
AddToDebug( Buffer, BytesRead, 1 );
|
|
|
|
|
*/
|
|
|
|
|
// LeaveCriticalSection( &m_ReadLock );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (len);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DWORD CPSerial::ReadPort(char* Buffer, DWORD Bytes)
|
|
|
|
|
{
|
|
|
|
@@ -630,7 +779,7 @@ DWORD CPSerial::ReadPort(char* Buffer, DWORD Bytes)
|
|
|
|
|
ReadState = ReadFile(m_PortHandle, Buffer, Bytes, &BytesRead, &m_ReadOLap);
|
|
|
|
|
if (!ReadState)
|
|
|
|
|
{
|
|
|
|
|
Sleep(3);
|
|
|
|
|
Sleep(5);
|
|
|
|
|
// the specifed number of bytes were not available so
|
|
|
|
|
// the read will continue in the background aslong as
|
|
|
|
|
// GetLastError() returns ERROR_IO_PENDING
|
|
|
|
@@ -658,12 +807,11 @@ DWORD CPSerial::ReadPort(char* Buffer, DWORD Bytes)
|
|
|
|
|
AddToDebug( Buffer, BytesRead, 1 );
|
|
|
|
|
*/
|
|
|
|
|
// LeaveCriticalSection( &m_ReadLock );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (BytesRead);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// ReadPort() : Read the specifed number of bytes into a CString class.
|
|
|
|
|
//
|
|
|
|
|