From 4cda72771765720a6a8721bd8844945c2403afe6 Mon Sep 17 00:00:00 2001
From: QuakeGod <quakegod@sina.com>
Date: 星期一, 23 十月 2023 08:52:12 +0800
Subject: [PATCH] fix vcxproj version

---
 MTerm2/HvSerialPort.cpp |  116 +++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 95 insertions(+), 21 deletions(-)

diff --git a/MTerm2/HvSerialPort.cpp b/MTerm2/HvSerialPort.cpp
index 6780f9b..ac70a9a 100644
--- a/MTerm2/HvSerialPort.cpp
+++ b/MTerm2/HvSerialPort.cpp
@@ -118,7 +118,7 @@
 	GetCommTimeouts(hCom1, &comtimeout1);
 
 	comtimeout1.ReadIntervalTimeout = 1;	//两个字符之间的时间
-	comtimeout1.ReadTotalTimeoutMultiplier = 1;	//每个字符等待的时间,读多个字符则等待时间为单个时间乘以字符个数
+	comtimeout1.ReadTotalTimeoutMultiplier = 0;	//每个字符等待的时间,读多个字符则等待时间为单个时间乘以字符个数
 	comtimeout1.ReadTotalTimeoutConstant = 2;		//读取时,再另外多等待的时间。
 	comtimeout1.WriteTotalTimeoutMultiplier = 0;	//写入时,每个字符等待的时间,写入多个字符则等待时间为单个时间乘以字符个数。
 	comtimeout1.WriteTotalTimeoutConstant = 0;	//写入时,再另外多等待的时间。
@@ -228,7 +228,7 @@
 	HANDLE hEventArr[2];
 	memset(&osRead, 0, sizeof(OVERLAPPED));
 	osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
-	SetCommMask(hCom1, EV_RXCHAR| EV_TXEMPTY);
+	SetCommMask(hCom1, EV_RXCHAR | EV_TXEMPTY);
 
 	hEventArr[0] = osRead.hEvent;
 	//	hEventArr[1] = *g_OutPutList.GetEvent();
@@ -253,33 +253,49 @@
 	int LastSpeedTime = (int)GetTimemS();
 	int nThisRecv = 0;
 
-//	COMSTAT cs;
-//	OVERLAPPED os;
-//	DWORD dwTrans;
-//	DWORD dwReaded;
+	COMSTAT cs;
+	OVERLAPPED os;
+	DWORD dwTrans;
+	DWORD dwReaded;
 	for (; MyThreadProc1ToRun == 1;)
 	{
 
 		DWORD dwEvtMask = 0;
-/*
-		BOOL result = WaitCommEvent(hCom1, &dwEvtMask, &os);
-		if (!result)
+///*
+//		BOOL result = WaitCommEvent(hCom1, &dwEvtMask, NULL); // &os);
+//		if (!result)
 		{
-			if (GetLastError() == ERROR_IO_PENDING)
+//			if (GetLastError() == ERROR_IO_PENDING)
+//			{
+//				GetOverlappedResult(hCom1, &os, &dwTrans, true);
+
+//			}
+//			if (dwEvtMask & EV_RXCHAR)
+			if (1) { Sleep(1); } else
 			{
-				GetOverlappedResult(hCom1, &os, &dwTrans, true);
-				if (dwEvtMask & EV_RXCHAR)
+				ClearCommError(hCom1, &m_dwError, &cs);
+				int bytes = cs.cbInQue;
+				dwReaded = 0;
+				if (bytes > 0)
 				{
-					ClearCommError(hCom1, &m_dwError, &cs);
-					int bytes = cs.cbInQue;
-					dwReaded = 0;
-					ReadFile(hCom1,RecvBuf,bytes,&dwReaded,NULL);
+					int j = ReadFile(hCom1, RecvBuf2, bytes, &dwReaded, NULL);
+					if (j > 0)
+					{
+						m_CS1.Lock();
+						memcpy(RecvBuf + RecvBufDataLen, RecvBuf2, dwReaded);
+						RecvBufDataLen += dwReaded;
+						m_CS1.Unlock();
+					}
+				}
+				else
+				{
+					Sleep(1);
 				}
 			}
 		}
+		if (RecvBufDataLen > 1024) { RecvBufDataLen = 0; }
 		//EscapeCommFunction(hCom1, SETDTR);
 //*/		
-		Sleep(1);
 	}
 	//SysLog(_T("跳出接收循环\r\n"));
 	//	hcurDC.SelectObject(def_font); 
@@ -289,6 +305,13 @@
 	return 0;
 }
 
+int HvSerialPort::ClearBuf()
+{
+	PurgeComm(hCom1, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_TXCLEAR);
+	RecvBufDataLen = 0;
+	RecvBufPos = 0;
+	return 0;
+}
 
 int HvSerialPort::Send(void * pBuf, int len1)
 {
@@ -321,9 +344,60 @@
 
 	return R_OK;
 }
-
-
 int HvSerialPort::Recv(void * pBuf, int len1)
+{
+	return RecvFromCom(pBuf, len1);
+}
+
+int HvSerialPort::RecvFromBuf(void * pBuf, int len1)
+{
+	CString s1;
+	int i, j;
+	DWORD numreaded = 0;
+	int numtoread = len1;
+	m_strResult.Format(_T("ToRead %d "), len1);
+	for (i = 0; i < 20; i++)
+	{
+		int k = m_CS1.Lock();
+		if (RecvBufDataLen)
+		{
+			if (RecvBufDataLen >= numtoread)
+			{
+				memcpy((char *)pBuf+numreaded, RecvBuf, numtoread);
+				numreaded += numtoread;
+				RecvBufDataLen -= numtoread;
+				numtoread = 0;
+				memmove(RecvBuf, RecvBuf + numtoread, RecvBufDataLen);
+			}
+			else
+			{
+				memcpy((char *)pBuf + numreaded, RecvBuf, RecvBufDataLen);
+				numreaded += RecvBufDataLen;
+				RecvBufDataLen = 0;
+				RecvBufPos = 0;
+				numtoread -= RecvBufDataLen;
+			}
+		}
+		m_CS1.Unlock();
+		if (numtoread == 0) break;
+		Sleep(1);
+	}
+	if (numreaded > 0) {
+		m_strResult.AppendFormat(_T("Read From COM%d OK %dB "), this->m_nPort, numreaded);
+		for (i = 0; (unsigned)i < numreaded; i++)
+		{
+			m_strResult.AppendFormat(_T("%c"), ((char *)pBuf)[i]);
+		}
+		m_strResult.AppendFormat(_T("\r\n"));
+	}
+	else {
+		m_strResult.Format(_T("Read From COM%d failed \r\n"), this->m_nPort);
+	}
+	m_strResult.AppendFormat(_T("ToRun %d, Running %d"), MyThreadProc1ToRun, MyThreadProc1Running);
+	
+	return numreaded;
+}
+int HvSerialPort::RecvFromCom(void * pBuf, int len1)
 {
 	// TODO: 在此处添加实现代码.
 	CString s1;
@@ -348,10 +422,10 @@
 		else {
 			if (len2 > 0) {
 				nTryCount2++;
-				if (nTryCount2 >= m_nCountToWait) break;
+				if (nTryCount2 > m_nCountToWait) break;
 			}
 		}
-		if (len2 > 0 && nTryCount2 >= m_nCountToWait) break;
+		if (len2 > 0 && nTryCount2 > m_nCountToWait) break;
 	}
 	numreaded = len2;
 	if (numreaded > 0) {

--
Gitblit v1.9.1