From 65f7136c6dfebde14a07c89c4366fb8bed2fe37f Mon Sep 17 00:00:00 2001
From: QuakeGod <QuakeGod@sina.com>
Date: 星期二, 10 五月 2022 14:22:45 +0800
Subject: [PATCH] add factory config mode

---
 MTerm2/KLink.cpp |  201 ++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 162 insertions(+), 39 deletions(-)

diff --git a/MTerm2/KLink.cpp b/MTerm2/KLink.cpp
index 6e3fdfc..53ba599 100644
--- a/MTerm2/KLink.cpp
+++ b/MTerm2/KLink.cpp
@@ -238,6 +238,18 @@
 		PkgLen1 = sizeof(stKLReqPacket) + Datalen;
 		break;
 	case KLCmdGetInfo:
+	case KLCmdGetSN:
+		Datalen = 0;
+		PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
+		break;
+	case KLCmdGetFactoryData:
+		Datalen = 0;
+		PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
+		break;
+	case KLCmdWriteFactoryData:
+		Datalen = nCount;
+		memcpy(pPacket->Params + 4, pData, Datalen);
+		PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
 		break;
 	case KLCmdRead:
 		Datalen = 0;
@@ -442,12 +454,21 @@
 	case KLCmdPing:
 	case KLCmdPingReply:
 		*nCount = Datalen;
-		memcpy(pPacket->Datas, pData, Datalen);
+		memcpy(pData, pPacket->Datas, Datalen);
 		break;
 	case KLCmdGetInfo:
 	case KLCmdVerInfo:
 		*nCount = Datalen;
-		memcpy(pPacket->Datas, pData, Datalen);
+		memcpy(pData, pPacket->Datas, Datalen);
+		break;
+	case KLCmdGetUid:
+	case KLCmdGetSN:
+	case KLCmdGetFactoryData:
+		*nCount = Datalen;
+		memcpy(pData, pPacket->Datas, Datalen);
+		break;
+	case KLCmdWriteFactoryData:
+		*nCount = Datalen;
 		break;
 	case KLCmdRead:
 	case KLCmdReadReply:
@@ -534,7 +555,7 @@
 
 	int len2=RecvPacket(m_RecvBuf, numToRead);
 	unsigned char nCmd;
-	unsigned short nCount;
+	unsigned short nCount=0;
 	ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf);
 	Value[0] = m_DataBuf[0];
 	return KL_OK;
@@ -547,23 +568,23 @@
 	SendPacket(m_Packetbuf, len1);
 	int len2 = RecvPacket(m_RecvBuf, 64);
 	unsigned char nCmd;
-	unsigned short nCount;
+	unsigned short nCount=0;
 	ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf);
 	return KL_OK;
 }
-int KLink::ReadBits(UCHAR nDst, UCHAR nBitCount, UCHAR nType, USHORT nBitAddr, UCHAR * Values)
+int KLink::ReadBits(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR nBitCount, UCHAR * Values)
 {
 	return KL_OK;
 }
-int KLink::WriteBits(UCHAR nDst, UCHAR nBitCount, UCHAR nType, USHORT nBitAddr, UCHAR * Values)
+int KLink::WriteBits(UCHAR nDst,  UCHAR nType, USHORT nBitAddr, UCHAR nBitCount, UCHAR * Values)
 {
 	return KL_OK;
 }
-int KLink::ReadBitsByWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT *Values)
+int KLink::ReadBitsByWord(UCHAR nDst,  UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT *Values)
 {
 	return KL_OK;
 }
-int KLink::WriteBitsByWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT *Values)
+int KLink::WriteBitsByWord(UCHAR nDst,  UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT *Values)
 {
 	return KL_OK;
 }
@@ -575,7 +596,7 @@
 	return m_nSeq;
 }
 
-int KLink::ReadDataByte(UCHAR nDst, UCHAR nByteCount, UCHAR nType, USHORT nByteAddr, UCHAR * Values)
+int KLink::ReadDataByte(UCHAR nDst, UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, UCHAR * Values)
 {
 	m_Dst = nDst;
 	m_resultStr.Empty();
@@ -598,7 +619,7 @@
 		m_resultStr += s1;
 	}
 	unsigned char nCmd  ;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	if (res == KL_OK)
 	{
@@ -613,22 +634,24 @@
 
 	return KL_OK;
 }
-int KLink::WriteDataByte(UCHAR nDst, UCHAR nByteCount, UCHAR nType, USHORT nByteAddr, UCHAR * Values)
+int KLink::WriteDataByte(UCHAR nDst,  UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, UCHAR * Values)
 {
 	m_Dst = nDst;
 	UCHAR nExpSeq = GetNextSeq();
 	int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteDataByte, nType, nByteAddr/2, nByteCount, Values);
 	SendPacket(m_Packetbuf, len1);
-	int len2 = RecvPacket(m_RecvBuf, 64);
-	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
+	nByteCount = 0;
+	int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
+	int len2 = RecvPacket(m_RecvBuf, numToRead);
+	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
 	if (len2 == 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	return res;
 }
 
-int KLink::ReadDataWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
+int KLink::ReadDataWord(UCHAR nDst,  UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
 {
 	m_Dst = nDst;
 	UCHAR nExpSeq = GetNextSeq();
@@ -640,7 +663,7 @@
 	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
 	if (len2 == 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	for (int i = 0; i < nnCount/2; i++)
 	{
@@ -649,23 +672,25 @@
 	}
 	return res;
 }
-int KLink::WriteDataWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
+int KLink::WriteDataWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
 {
 	m_Dst = nDst;
 	UCHAR nExpSeq = GetNextSeq();
 	int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteDataWord, nType, nWordAddr, nWordCount, Values);
 	SendPacket(m_Packetbuf, len1);
-	int len2 = RecvPacket(m_RecvBuf, 64);
-	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
+	nWordCount = 0;
+	int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
+	int len2 = RecvPacket(m_RecvBuf, nWordCount);
+	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, nWordCount); }
 	if (len2 == 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	Values[0] = m_DataBuf[0];
 	return res;
 }
 
-int KLink::ReadData(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
+int KLink::ReadData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
 {
 	m_Dst = nDst;
 	UCHAR nExpSeq = GetNextSeq();
@@ -675,14 +700,14 @@
 	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
 	if (len2 == 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	for (int i=0;i<nnCount;i++)	Values[i] = m_DataBuf[i];
 	return res;
 
 	return KL_OK;
 }
-int KLink::WriteData(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
+int KLink::WriteData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
 {
 	m_Dst = nDst;
 	UCHAR nExpSeq = GetNextSeq();
@@ -692,30 +717,128 @@
 	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
 	if (len2 == 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 //	Values[0] = m_DataBuf[0];
 	return res;
 
 	return KL_OK;
 }
-int KLink::GetInfo(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
+int KLink::GetInfo(UCHAR nDst,  UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
 {
 	m_Dst = nDst;
 	UCHAR nExpSeq = GetNextSeq();
 	int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetInfo, nType, nWordAddr, nWordCount, Values);
+	CString s1;
+	for (int i = 0; i < len1; i++) {
+		s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
+	}
+	m_resultStr = s1;
 	SendPacket(m_Packetbuf, len1);
-	int len2 = RecvPacket(m_RecvBuf, 64);
-	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
+	int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
+	int len2 = RecvPacket(m_RecvBuf, numToRead);
+	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
 	if (len2 == 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
+	int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
+	//	Values[0] = m_DataBuf[0];
+	return res;
+
+	return KL_OK;
+}
+int KLink::GetSN(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
+{
+	m_Dst = nDst;
+	UCHAR nExpSeq = GetNextSeq();
+	int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetSN, nType, nWordAddr, nWordCount, Values);
+	CString s1;
+	for (int i = 0; i < len1; i++) {
+		s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
+	}
+	m_resultStr = s1;
+	SendPacket(m_Packetbuf, len1);
+	nWordCount = 4;
+	int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
+	int len2 = RecvPacket(m_RecvBuf, numToRead);
+	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
+	if (len2 == 0) return KL_ERR;
+	unsigned char nCmd;
+	unsigned short nnCount = 0;
+	int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
+	//	Values[0] = m_DataBuf[0];
+	return res;
+
+	return KL_OK;
+	return 0;
+}
+int KLink::GetUID(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
+{
+	m_Dst = nDst;
+	UCHAR nExpSeq = GetNextSeq();
+	int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetUid, nType, nWordAddr, nWordCount, Values);
+	CString s1;
+	for (int i = 0; i < len1; i++) {
+		s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
+	}
+	m_resultStr = s1;
+	SendPacket(m_Packetbuf, len1);
+	int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
+	int len2 = RecvPacket(m_RecvBuf, numToRead);
+	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
+	if (len2 == 0) return KL_ERR;
+	unsigned char nCmd;
+	unsigned short nnCount = 0;
+	int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
+	//	Values[0] = m_DataBuf[0];
+	return res;
+
+	return KL_OK;
+	return 0;
+}
+
+int KLink::GetFactoryData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
+{
+	m_Dst = nDst;
+	UCHAR nExpSeq = GetNextSeq();
+	int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetFactoryData, nType, nWordAddr, nWordCount, Values);
+	CString s1;
+	for (int i = 0; i < len1; i++) {
+		s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
+	}
+	m_resultStr = s1;
+	SendPacket(m_Packetbuf, len1);
+	int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
+	int len2 = RecvPacket(m_RecvBuf, numToRead);
+	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
+	if (len2 == 0) return KL_ERR;
+	unsigned char nCmd;
+	unsigned short nnCount = 0;
+	int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
+	//	Values[0] = m_DataBuf[0];
+	return res;
+	return KL_OK;
+}
+int KLink::WriteFactoryData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
+{
+	m_Dst = nDst;
+	UCHAR nExpSeq = GetNextSeq();
+	int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteFactoryData, nType, nWordAddr, nWordCount, Values);
+	SendPacket(m_Packetbuf, len1);
+	nWordCount = 0;
+	int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
+	int len2 = RecvPacket(m_RecvBuf, numToRead);
+	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
+	if (len2 == 0) return KL_ERR;
+	unsigned char nCmd;
+	unsigned short nnCount = 0;
 	int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	//	Values[0] = m_DataBuf[0];
 	return res;
 
 	return KL_OK;
 }
+
 int KLink::GetEventLogCount(UCHAR nDst, int * nCount)
 {
 	m_Dst = nDst;
@@ -726,7 +849,7 @@
 	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
 	if (len2 == 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	nCount[0] = *(int *)m_DataBuf;
 	return res;
@@ -744,7 +867,7 @@
 	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
 	if (len2 == 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	
 	memcpy(theEventLogs, m_DataBuf, nCount * sizeof(stEventLog));
@@ -757,7 +880,7 @@
 int  KLink::GetDateTime32(UCHAR nDst, UINT * pValue)
 {
 	int res = KL_OK;
-	res = ReadDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)pValue);
+	res = ReadDataByte(nDst,  KLDataTypeSDT, 36, 4, (UCHAR *)pValue);
 	return res;
 
 }
@@ -765,7 +888,7 @@
 int  KLink::SetDateTime32(UCHAR nDst, UINT  Value)
 {
 	int res = KL_OK;
-	res = WriteDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)&Value);
+	res = WriteDataByte(nDst, KLDataTypeSDT, 36, 4, (UCHAR *)&Value);
 	return res;
 }
 
@@ -781,7 +904,7 @@
 	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
 	if (len2 == 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	return res;
 
@@ -800,7 +923,7 @@
 	if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
 	if (len2 == 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	return res;
 
@@ -821,7 +944,7 @@
 	if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
 	if (len2 <= 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	unsigned char * values2 = (unsigned char *)Values;
 	for (int i = 0; i < nnCount; i++)	values2[i] = m_DataBuf[i];
@@ -840,7 +963,7 @@
 	if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6); }
 	if (len2 <= 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	return res;
 
@@ -857,7 +980,7 @@
 	if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6, 30); }
 	if (len2 <= 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	//	Values[0] = m_DataBuf[0];
 	return res;
@@ -875,7 +998,7 @@
 	if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6, 30); }
 	if (len2 <= 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	return res;
 }
@@ -893,7 +1016,7 @@
 	if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
 	if (len2 <= 0) return KL_ERR;
 	unsigned char nCmd;
-	unsigned short nnCount;
+	unsigned short nnCount=0;
 	res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
 	unsigned char * values2 = (unsigned char *)Values;
 	for (int i = 0; i < nnCount; i++)	values2[i] = m_DataBuf[i];

--
Gitblit v1.9.1