From 418cb35b936f21415807a2bcc21b2d75934bd4d2 Mon Sep 17 00:00:00 2001
From: QuakeGod <quakegod@sina.com>
Date: 星期五, 19 五月 2023 16:45:01 +0800
Subject: [PATCH] 2023-05-19

---
 MTerm2/KLink.h |   72 ++++++++++++++++++++++--------------
 1 files changed, 44 insertions(+), 28 deletions(-)

diff --git a/MTerm2/KLink.h b/MTerm2/KLink.h
index 42399c9..708a933 100644
--- a/MTerm2/KLink.h
+++ b/MTerm2/KLink.h
@@ -5,6 +5,7 @@
 */
 #pragma once
 #include "KDefine.h"
+#include "HvSerialPort.h"
 #include <functional>
 class KLink
 {
@@ -15,6 +16,7 @@
 public:
 	KLink();
 	virtual ~KLink();
+	HvSerialPort m_SerialPort;
 #define TYPECOIL 0x00
 #define TYPEDATA 0x80
 	enum enKLCoilTypes
@@ -194,8 +196,10 @@
 	KLCmdResetMachine,
 	KLCmdGetUid,
 	KLCmdSetTime,
+	KLCmdGetSN,
 
-	KLCmdMC,
+
+	KLCmdMC = 0x70, 
 	KLCmdMD,
 	KLCmdMG,
 
@@ -486,24 +490,26 @@
 //  pOpen OpenFunc;
 //  pClose CloseFunc;
 
-std::function<int (void *,int)> SendPkgFunc;
-std::function<int(void *, int)> RecvPkgFunc;
-std::function<int(int)> OpenFunc;
-std::function<int(int)> CloseFunc;
+typedef struct tagCallBackFuncs
+{
+	std::function<int(void)> OpenFunc;
+	std::function<int(void)> CloseFunc;
+	std::function<int(void)> ClearBufFunc;
+	std::function<int(void *, int)> SendPkgFunc;
+	std::function<int(void *, int)> RecvPkgFunc;
+}stCallBackFuncs, *pCallBackFuncs;
 
-
+stCallBackFuncs m_CallBackFuncs;
 
 int m_bSendCallBackSet = 0;
 int m_bRecvCallBackSet = 0;
 int m_bOpenCallBackSet = 0;
 int m_bCloseCallBackSet = 0;
+int m_bClearBufCallBackSet = 0;
 
 bool m_bOpened=false;
 
-int SetSendCallBackFunc(std::function<int(void *, int)> pSendFunc);
-int SetRecvCallBackFunc(std::function<int(void *, int)> pRecvFunc);
-int SetOpenCallBackFunc(std::function<int(int)> pOpenFunc);
-int SetCloseCallBackFunc(std::function<int(int)> pCloseFunc);
+int SetCallBackFuncs(pCallBackFuncs pFuncSt);
 
 int m_nErrCount = 0;
 int m_nContinueErrCount = 0;
@@ -511,7 +517,7 @@
 {
 	if (m_bOpenCallBackSet)
 	{
-		OpenFunc(OpenParam);
+		m_CallBackFuncs.OpenFunc();
 	}
 	m_bOpened = true;
 	return KL_OK;
@@ -520,18 +526,22 @@
 {
 	if (m_bCloseCallBackSet)
 	{
-		CloseFunc(CloseParam);
+		m_CallBackFuncs.CloseFunc();
 	}
 	m_bOpened = false;
 	return KL_OK;
 };
 int SendPacket(void * pBuf, int Len)
 {
-	if (m_bOpened && m_bSendCallBackSet) return SendPkgFunc(pBuf, Len);
+	if (m_bOpened && m_bSendCallBackSet)
+	{	
+		if (m_bClearBufCallBackSet) m_CallBackFuncs.ClearBufFunc();
+		return m_CallBackFuncs.SendPkgFunc(pBuf, Len);
+	}
 	else return KL_ERR;
 }
 
-int RecvPacket(void * pBuf, int LenToRead, int ReTryCount=3)
+int RecvPacket(void * pBuf, int LenToRead, int ReTryCount=1)
 {
 	
 	if (m_bOpened && m_bRecvCallBackSet)
@@ -540,7 +550,10 @@
 		char * pBufByte = (char *)pBuf;
 		for (int i = 0; i < ReTryCount; i++)
 		{
-			int len1= RecvPkgFunc(pBufByte + len2, LenToRead - len2);
+			int len1= m_CallBackFuncs.RecvPkgFunc(pBufByte + len2, LenToRead - len2);
+			if (len1 <= 0 || len1 > 256) {
+				continue;
+			}
 			len2 += len1;
 			if (len2 >= LenToRead) break;
 		}
@@ -578,9 +591,12 @@
 int ProcessPacket(void *pBuf, int nLen);
 int ParseRplyPacket(void *pBuf, int nPkgLen, UCHAR * nCmd, UCHAR * Status, USHORT* nCount, void * pData);
 
-int GetInfo(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values);
-int GetSN(UCHAR nDst, int * nCount);
-int GetUID(UCHAR nDst, int * nCount);
+int GetInfo(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values);
+int GetSN(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values);
+int GetUID(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values);
+int GetFactoryData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values);
+int WriteFactoryData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values);
+
 //int GetTime32(UCHAR nDst, int * nCount);
 //int SetTime32(UCHAR nDst, int * nCount);
 int GetDateTime32(UCHAR nDst, UINT * pValue);
@@ -593,19 +609,19 @@
 
 int ReadBit(UCHAR nDst, UCHAR nType,USHORT nBitAddr, UCHAR * Value);//Read 1 Single bit
 int WriteBit(UCHAR nDst, UCHAR nType,USHORT nBitAddr, UCHAR Value);//Write 1 Single bit
-int ReadBits(UCHAR nDst, UCHAR nBitCount, UCHAR nType, USHORT nBitAddr, UCHAR * Values);
-int WriteBits(UCHAR nDst, UCHAR nBitCount, UCHAR nType, USHORT nBitAddr, UCHAR * Values);
-int ReadBitsByWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT *Values);
-int WriteBitsByWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT *Values);
+int ReadBits(UCHAR nDst,  UCHAR nType, USHORT nBitAddr, UCHAR nBitCount, UCHAR * Values);
+int WriteBits(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR nBitCount, UCHAR * Values);
+int ReadBitsByWord(UCHAR nDst,  UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT *Values);
+int WriteBitsByWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT *Values);
 
-int ReadDataByte(UCHAR nDst, UCHAR nByteCount, UCHAR nType, USHORT nByteAddr, UCHAR * Values);
-int WriteDataByte(UCHAR nDst, UCHAR nByteCount, UCHAR nType, USHORT nByteAddr, UCHAR * Values);
+int ReadDataByte(UCHAR nDst,  UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, UCHAR * Values);
+int WriteDataByte(UCHAR nDst, UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, UCHAR * Values);
 
-int ReadDataWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values);
-int WriteDataWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values);
+int ReadDataWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values);
+int WriteDataWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values);
 
-int ReadData(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values);
-int WriteData(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values);
+int ReadData(UCHAR nDst,  UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values);
+int WriteData(UCHAR nDst,  UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values);
 
 int GetMode(UCHAR nDst, UCHAR nType);
 int ChangeMode(UCHAR nDst,  UCHAR nType);

--
Gitblit v1.9.1