From 89cd7441c184d37f90d4d5311b348e938119765f Mon Sep 17 00:00:00 2001
From: zxd <zxdvslxy@gmail.com>
Date: 星期六, 21 十月 2023 12:28:04 +0800
Subject: [PATCH] 修改最后一个单元格不会被读取的问题

---
 MTerm2/MTerm2Doc.cpp |  195 ++++++++++++++++++++++++++++++++++++------------
 1 files changed, 144 insertions(+), 51 deletions(-)

diff --git a/MTerm2/MTerm2Doc.cpp b/MTerm2/MTerm2Doc.cpp
index 9ee69b6..2622a2a 100644
--- a/MTerm2/MTerm2Doc.cpp
+++ b/MTerm2/MTerm2Doc.cpp
@@ -26,7 +26,12 @@
 #include "MTerm2CommDevView.h"
 #include <propkey.h>
 
-#include "CDialogCommSet1.h"
+#include "DialogCommSet1.h"
+#include "DialogStatusShow.h"
+#include "DialogSysRegSet.h"
+#include "DialogFactCfg.h"
+#include "DialogDateTime.h"
+#include "DialogEventLog.h"
 #include "HvSerialPort.h"
 #include <functional>
 
@@ -35,7 +40,7 @@
 #endif
 
 // CMTerm2Doc
-CMTerm2Doc::stTypeDef CMTerm2Doc::CoilTypeDef[] =
+CMTerm2Doc::stTypeNameDef CMTerm2Doc::CoilTypeNameDef[] =
 {
 	{KLCoilTypeX,"X"},
 	{KLCoilTypeY,"Y"}, 
@@ -47,9 +52,9 @@
 	{KLCoilTypeC,"C"},
 	{KLCoilTypeT,"T"},
 };
-int CMTerm2Doc::nCoilTypeDefCount = sizeof(CMTerm2Doc::CoilTypeDef) / sizeof(stTypeDef);
+int CMTerm2Doc::nCoilTypeDefCount = sizeof(CMTerm2Doc::CoilTypeNameDef) / sizeof(stTypeNameDef);
 
-CMTerm2Doc::stTypeDef CMTerm2Doc::DataTypeDef[] =
+CMTerm2Doc::stTypeNameDef CMTerm2Doc::DataTypeNameDef[] =
 {
 	{KLDataTypeDEC,"K"},
 	{KLDataTypeHEX,"H"},
@@ -65,9 +70,9 @@
 	{KLDataTypeSV,"SV"},
 	{KLDataTypeLD,"LD"},
 };
-int CMTerm2Doc::nDataTypeDefCount = sizeof(CMTerm2Doc::DataTypeDef) / sizeof(stTypeDef);
+int CMTerm2Doc::nDataTypeDefCount = sizeof(CMTerm2Doc::DataTypeNameDef) / sizeof(stTypeNameDef);
 
-CMTerm2Doc::stProgDef CMTerm2Doc::ProgDef[] =
+CMTerm2Doc::stOpDef CMTerm2Doc::OpDef[] =
 {
 	{OP_NOP,"NOP",0},
 	{OP_ST,"ST",1,KLParamCoil},
@@ -122,11 +127,8 @@
 	{OP_MUL,"MUL",3,KLParamWord,"X"},
 	{OP_DIV,"DIV",3,KLParamWord,"/"},
 
-
-
-
 };
-int CMTerm2Doc::nProgDefCount = sizeof(CMTerm2Doc::ProgDef) / sizeof(stProgDef);
+int CMTerm2Doc::nOpDefCount = sizeof(CMTerm2Doc::OpDef) / sizeof(stOpDef);
 
 IMPLEMENT_DYNCREATE(CMTerm2Doc, CDocument)
 
@@ -150,8 +152,14 @@
 	ON_COMMAND(ID_MENU_SHOW_CONSOLE, &CMTerm2Doc::OnMenuShowConsole)
 
 	ON_COMMAND(ID_MENU_COMM_TEST, &CMTerm2Doc::OnMenuCommTest)
+	
+	
 	ON_COMMAND(ID_SIMULATE, &CMTerm2Doc::OnSimulate)
 	ON_UPDATE_COMMAND_UI(ID_SIMULATE, &CMTerm2Doc::OnUpdateSimulate)
+	ON_COMMAND(ID_MENU_STATUS_SHOW, &CMTerm2Doc::OnMenuStatusShow)
+	ON_COMMAND(ID_MENU_FACT_CFG, &CMTerm2Doc::OnMenuFactCfg)
+	ON_COMMAND(ID_MENU_DATETIME_SET, &CMTerm2Doc::OnMenuDatetimeSet)
+	ON_COMMAND(ID_MENU_EVENT_LOG, &CMTerm2Doc::OnMenuEventLog)
 END_MESSAGE_MAP()
 
 
@@ -161,9 +169,21 @@
 {
 	// TODO: 鍦ㄦ娣诲姞涓�娆℃�ф瀯閫犱唬鐮�
 
-	MyKLink1.SetSendCallBackFunc(std::bind(&CMTerm2Doc::SendPacket, this, std::placeholders::_1, std::placeholders::_2));
-	MyKLink1.SetRecvCallBackFunc(std::bind(&CMTerm2Doc::RecvPacket, this, std::placeholders::_1, std::placeholders::_2));
-	StartTime = myHvSerialPort1.GetTimeMs();
+	KLink::stCallBackFuncs callbackfuncs;
+	callbackfuncs.OpenFunc = std::bind(&HvSerialPort::Open, &myHvSerialPort1);
+	callbackfuncs.CloseFunc = std::bind(&HvSerialPort::Close, &myHvSerialPort1);
+	callbackfuncs.ClearBufFunc = std::bind(&HvSerialPort::ClearBuf, &myHvSerialPort1);
+//	callbackfuncs.SendPkgFunc = std::bind(&CMTerm2Doc::SendPacket, this, std::placeholders::_1, std::placeholders::_2);
+//	callbackfuncs.RecvPkgFunc = std::bind(&CMTerm2Doc::RecvPacket, this, std::placeholders::_1, std::placeholders::_2);
+
+	callbackfuncs.SendPkgFunc = std::bind(&HvSerialPort::Send, &myHvSerialPort1, std::placeholders::_1, std::placeholders::_2);
+	callbackfuncs.RecvPkgFunc = std::bind(&HvSerialPort::Recv, &myHvSerialPort1, std::placeholders::_1, std::placeholders::_2);
+
+	MyKLink1.SetCallBackFuncs(&callbackfuncs);
+
+//	MyKLink1.SetSendCallBackFunc(std::bind(&CMTerm2Doc::SendPacket, this, std::placeholders::_1, std::placeholders::_2));
+//	MyKLink1.SetRecvCallBackFunc(std::bind(&CMTerm2Doc::RecvPacket, this, std::placeholders::_1, std::placeholders::_2));
+//	StartTime = myHvSerialPort1.GetTimemS();
 }
 
 CMTerm2Doc::~CMTerm2Doc()
@@ -176,7 +196,8 @@
 }
 int CMTerm2Doc::RecvPacket(void * pBuf, int Len)
 {
-	int j = myHvSerialPort1.Recv((char *)pBuf, Len);
+	int j = 0;
+	j = myHvSerialPort1.Recv((char *)pBuf, Len);
 	if (j <= 0)
 	{
 		CString s1;
@@ -429,20 +450,20 @@
 
 int CMTerm2Doc::TxtToOp(CStringA optxt, int* ParamCount, int* ParamType)
 {
-	for (int i = 0; i < nProgDefCount; i++){
-		if (ProgDef[i].OpTxt == optxt)	{
-			*ParamCount = ProgDef[i].nParamCount;
-			*ParamType = ProgDef[i].nParamType;
-			return ProgDef[i].nOpType1;
+	for (int i = 0; i < nOpDefCount; i++){
+		if (OpDef[i].OpTxt == optxt)	{
+			*ParamCount = OpDef[i].nParamCount;
+			*ParamType = OpDef[i].nParamType;
+			return OpDef[i].nOpType1;
 		}
 	}
 	return -1;
 }
 int CMTerm2Doc::OpToTxt(int nOp, CStringA & OpTxt)
 {
-	for (int i = 0; i < nProgDefCount; i++){
-		if (ProgDef[i].nOpType1 == nOp) {
-			OpTxt = ProgDef[i].OpTxt;
+	for (int i = 0; i < nOpDefCount; i++){
+		if (OpDef[i].nOpType1 == nOp) {
+			OpTxt = OpDef[i].OpTxt;
 			return true;
 		}
 	}
@@ -451,9 +472,9 @@
 
 int CMTerm2Doc::OpToShowTxt(int nOp, CStringA & OpShowTxt)
 {
-	for (int i = 0; i < nProgDefCount; i++) {
-		if (ProgDef[i].nOpType1 == nOp) {
-			OpShowTxt = ProgDef[i].ShowTxt;
+	for (int i = 0; i < nOpDefCount; i++) {
+		if (OpDef[i].nOpType1 == nOp) {
+			OpShowTxt = OpDef[i].ShowTxt;
 			return true;
 		}
 	}
@@ -465,10 +486,10 @@
 	Typetxt.MakeUpper();
 	for (int i = 0; i < nCoilTypeDefCount; i++)
 	{
-		if (Typetxt.Find( CoilTypeDef[i].TypeTxt)==0) {
-			*nCoilType = CoilTypeDef[i].nType;
-			*nCoilAddr = atoi(Typetxt.Mid(CoilTypeDef[i].TypeTxt.GetLength()));
-			return CoilTypeDef[i].nType;
+		if (Typetxt.Find( CoilTypeNameDef[i].TypeTxt)==0) {
+			*nCoilType = CoilTypeNameDef[i].nType;
+			*nCoilAddr = atoi(Typetxt.Mid(CoilTypeNameDef[i].TypeTxt.GetLength()));
+			return CoilTypeNameDef[i].nType;
 		}
 	}
 	return false;;
@@ -478,8 +499,8 @@
 {
 	for (int i = 0; i < nCoilTypeDefCount; i++)
 	{
-		if (CoilTypeDef[i].nType == nType){
-			typeTxt = CoilTypeDef[i].TypeTxt;
+		if (CoilTypeNameDef[i].nType == nType){
+			typeTxt = CoilTypeNameDef[i].TypeTxt;
 			return true;
 		}
 	}
@@ -491,9 +512,9 @@
 	Typetxt.MakeUpper();
 	for (int i = 0; i < nDataTypeDefCount; i++)
 	{
-		if (Typetxt.Find(DataTypeDef[i].TypeTxt) == 0) {
-			*nDataType = DataTypeDef[i].nType;
-			*nDataAddr = atoi(Typetxt.Mid(DataTypeDef[i].TypeTxt.GetLength()));
+		if (Typetxt.Find(DataTypeNameDef[i].TypeTxt) == 0) {
+			*nDataType = DataTypeNameDef[i].nType;
+			*nDataAddr = atoi(Typetxt.Mid(DataTypeNameDef[i].TypeTxt.GetLength()));
 			return true;
 		}
 	}
@@ -505,8 +526,8 @@
 {
 	for (int i = 0; i < nDataTypeDefCount; i++)
 	{
-		if (DataTypeDef[i].nType == nType) {
-			typeTxt = DataTypeDef[i].TypeTxt;
+		if (DataTypeNameDef[i].nType == nType) {
+			typeTxt = DataTypeNameDef[i].TypeTxt;
 			return true;
 		}
 	}
@@ -558,7 +579,7 @@
 int CMTerm2Doc::SetAnno(unsigned short nType, unsigned short nAddr, CString sCoilName, CString sAnno)
 {
 	// TODO: 鍦ㄦ澶勬坊鍔犲疄鐜颁唬鐮�.
-// 浜屽垎娉� 鏌ユ壘銆�
+// 浜屽垎娉�? 鏌ユ壘銆�
 // map.
 	for (int i = 0; i < nCoilAnnoCount; i++) {
 		if (mCoilAnnos[i].nType == nType && mCoilAnnos[i].nAddr == nAddr) {
@@ -1358,6 +1379,23 @@
 void CMTerm2Doc::OnPlcSysregSet()
 {
 	// TODO: 鍦ㄦ娣诲姞鍛戒护澶勭悊绋嬪簭浠g爜
+	CDialogSysRegSet dialog1;
+	INT_PTR r = dialog1.DoModal();
+	if (r == IDOK)
+	{
+
+	}
+}
+
+void CMTerm2Doc::OnMenuFactCfg()
+{
+	// TODO: 鍦ㄦ娣诲姞鍛戒护澶勭悊绋嬪簭浠g爜
+	CDialogFactCfg dialog1;
+	INT_PTR r = dialog1.DoModal();
+	if (r == IDOK)
+	{
+
+	}
 }
 
 
@@ -1643,7 +1681,7 @@
 			return nDataAddr;
 		}
 
-		MyKLink1.ReadDataWord(1, 2, nDataType, nDataAddr, value);// (unsigned char *)&KMem.DT[nDataAddr]);
+		MyKLink1.ReadDataWord(1,  nDataType, nDataAddr, 2, value);// (unsigned char *)&KMem.DT[nDataAddr]);
 		svalue = value[0];
 		return svalue;
 	}
@@ -1728,7 +1766,7 @@
 		unsigned short svalue[10];
 		svalue[0]=nDataValue;
 		int res = 0;
-		res = MyKLink1.WriteDataWord(1, 2, nDataType, nDataAddr, svalue);
+		res = MyKLink1.WriteDataWord(1, nDataType, nDataAddr, 2, svalue);
 		return res;
 	}
 
@@ -1824,11 +1862,12 @@
 int CMTerm2Doc::DoPLCMonitor()
 {
 	// TODO: 鍦ㄦ澶勬坊鍔犲疄鐜颁唬鐮�.
+	CString s1;
 	if (m_bSimulate) {
 		int nDataType = MyKLink1.KLDataTypeWX;
 		int nDataAddr = 0;
 		int nDataCount = 4;
-		int res;
+//		int res;
 		for (int i = 0; i < nDataCount; i++) {
 			KMem.WX[i] = myKMachine1.KMem.WX[i];
 		}
@@ -1878,33 +1917,52 @@
 		int nDataAddr = 0;
 		int nDataCount = 4;
 		int res;
-		res = MyKLink1.ReadDataWord(1, nDataCount, nDataType, nDataAddr, KMem.WX);// (unsigned char *)&KMem.DT[nDataAddr]);
+		res = MyKLink1.ReadDataWord(1, nDataType, nDataAddr, nDataCount, KMem.WX);// (unsigned char *)&KMem.DT[nDataAddr]);
+		if (res != MyKLink1.KL_OK) {
+			s1.Format(_T("R:= %d %s  \r\n"), res, MyKLink1.m_resultStr);
+			SysLog(s1);
+		}
 
 		nDataType = MyKLink1.KLDataTypeWY;
 		nDataAddr = 0;
 		nDataCount = 4;
-		res = MyKLink1.ReadDataWord(1, nDataCount, nDataType, nDataAddr, KMem.WY);// (unsigned char *)&KMem.DT[nDataAddr]);
-
+		res = MyKLink1.ReadDataWord(1, nDataType, nDataAddr, nDataCount, KMem.WY);// (unsigned char *)&KMem.DT[nDataAddr]);
+		if (res != MyKLink1.KL_OK) {
+			s1.Format(_T("R:= %d %s  \r\n"), res, MyKLink1.m_resultStr);
+			SysLog(s1);
+		}
 		nDataType = MyKLink1.KLDataTypeWR;
 		nDataAddr = 0;
 		nDataCount = 10;
-		res = MyKLink1.ReadDataWord(1, nDataCount, nDataType, nDataAddr, KMem.WR);// (unsigned char *)&KMem.DT[nDataAddr]);
-
+		res = MyKLink1.ReadDataWord(1, nDataType, nDataAddr, nDataCount, KMem.WR);// (unsigned char *)&KMem.DT[nDataAddr]);
+		if (res != MyKLink1.KL_OK) {
+			s1.Format(_T("R:= %d %s  \r\n"), res, MyKLink1.m_resultStr);
+			SysLog(s1);
+		}
 		nDataType = MyKLink1.KLDataTypeDT;
 		nDataAddr = 0;
 		nDataCount = 80;
-		res = MyKLink1.ReadDataWord(1, nDataCount, nDataType, nDataAddr, KMem.DT);// (unsigned char *)&KMem.DT[nDataAddr]);
-
+		res = MyKLink1.ReadDataWord(1, nDataType, nDataAddr, nDataCount, KMem.DT);// (unsigned char *)&KMem.DT[nDataAddr]);
+		if (res != MyKLink1.KL_OK) {
+			s1.Format(_T("R:= %d %s  \r\n"), res, MyKLink1.m_resultStr);
+			SysLog(s1);
+		}
 		nDataType = MyKLink1.KLDataTypeSV;
 		nDataAddr = 0;
 		nDataCount = 40;
-		res = MyKLink1.ReadDataWord(1, nDataCount, nDataType, nDataAddr, KMem.SV);// (unsigned char *)&KMem.DT[nDataAddr]);
-
+		res = MyKLink1.ReadDataWord(1, nDataType, nDataAddr, nDataCount, KMem.SV);// (unsigned char *)&KMem.DT[nDataAddr]);
+		if (res != MyKLink1.KL_OK) {
+			s1.Format(_T("R:= %d %s  \r\n"), res, MyKLink1.m_resultStr);
+			SysLog(s1);
+		}
 		nDataType = MyKLink1.KLDataTypeEV;
 		nDataAddr = 0;
 		nDataCount = 40;
-		res = MyKLink1.ReadDataWord(1, nDataCount, nDataType, nDataAddr, KMem.EV);// (unsigned char *)&KMem.DT[nDataAddr]);
-
+		res = MyKLink1.ReadDataWord(1, nDataType, nDataAddr, nDataCount, KMem.EV);// (unsigned char *)&KMem.DT[nDataAddr]);
+		if (res != MyKLink1.KL_OK) {
+			s1.Format(_T("R:= %d %s  \r\n"), res, MyKLink1.m_resultStr);
+			SysLog(s1);
+		}
 	}
 
 	return 0;
@@ -1912,12 +1970,47 @@
 
 void CMTerm2Doc::OnTimer(UINT_PTR nIDEvent)
 {
+	CString s1;
+//	s1.Format(_T("Doc::OntTimer"));
+//	SysLog(s1);
 	if (m_bSimulate && m_bPlcRunning)
 	{
 		//ProcessPLCProg(Progs, m_nProgSteps);
 		myKMachine1.ProcPLC();
 
 		// update PLC Stat
+	}
+}
+
+void CMTerm2Doc::OnMenuStatusShow()
+{
+	// TODO: 鍦ㄦ娣诲姞鍛戒护澶勭悊绋嬪簭浠g爜
+	CDialogStatusShow dialog1;
+	INT_PTR r = dialog1.DoModal();
+	if (r == IDOK)
+	{
+
+	}
+}
+
+void CMTerm2Doc::OnMenuDatetimeSet()
+{
+	// TODO: 鍦ㄦ娣诲姞鍛戒护澶勭悊绋嬪簭浠g爜
+	CDialogDateTime dialog1;
+	INT_PTR r = dialog1.DoModal();
+	if (r == IDOK)
+	{
+
+	}
+}
+
+void CMTerm2Doc::OnMenuEventLog()
+{
+	// TODO: 鍦ㄦ娣诲姞鍛戒护澶勭悊绋嬪簭浠g爜
+	CDialogEventLog dialog1;
+	INT_PTR r = dialog1.DoModal();
+	if (r == IDOK)
+	{
 
 	}
 }

--
Gitblit v1.9.1