From 829fe6c6bc33d53fda9c31fd45a37e1df87befff Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期五, 30 一月 2026 11:16:24 +0800
Subject: [PATCH] Merge branch 'clh' into liuyang

---
 SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp |  269 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 240 insertions(+), 29 deletions(-)

diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
index 94363a7..ae284a7 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
@@ -17,6 +17,7 @@
 #define DeviceID 0
 
 // 鎵樼洏鍥炬爣 ID 涓庢秷鎭畯
+#define ID_TRAY_OPEN_DIR	   2000				// 鎵撳紑鐩綍
 #define ID_TRAY_RESTORE		   2001				// 鎭㈠绐楀彛
 #define ID_TRAY_EXIT		   2002				// 閫�鍑虹▼搴�
 #define WM_TRAY_ICON_NOTIFY    (WM_USER + 1000) // 鎵樼洏鍥炬爣鍥炶皟娑堟伅 ID
@@ -94,6 +95,7 @@
 	, m_nTrayIconID(0)
 	, m_bTrayIconCreated(FALSE)
 	, m_bExitingFromTray(FALSE)
+	, m_nAutoStart(TRUE)
 {
 	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
 }
@@ -131,6 +133,9 @@
 	}
 
 	m_plcListener.Stop();
+
+	// 淇濆瓨閰嶇疆鏂囦欢
+	SaveConfig(GetConfigPath());
 
 	DestroyWindow();
 	CDialogEx::OnClose();
@@ -247,7 +252,7 @@
 	strFullLogLine.Format(_T("%s %s"), strLevel, strContent);
 
 	// 鍐欏叆鏃ュ織鏂囦欢
-	// LOG_LINE(strFullLogLine);
+	LOG_LINE(strFullLogLine);
 }
 
 void CSGMeasurementDlg::HighlightAllMatches(const CString& strSearch, COLORREF clrHighlight/* = RGB(255, 165, 0)*/)
@@ -301,6 +306,149 @@
 			strLine.Empty();
 		}
 	}
+}
+
+CString CSGMeasurementDlg::GetAppDirectory()
+{
+	TCHAR szPath[MAX_PATH] = { 0 };
+	GetModuleFileName(NULL, szPath, MAX_PATH);
+
+	CString strPath = szPath;
+	int pos = strPath.ReverseFind('\\');
+	if (pos != -1) {
+		strPath = strPath.Left(pos + 1);
+	}
+
+	return strPath;
+}
+
+CString CSGMeasurementDlg::GetConfigPath()
+{
+	return GetAppDirectory() + _T("SGConfig.ini");
+}
+
+bool CSGMeasurementDlg::LoadConfig(const CString& strFile)
+{
+	CString strSection = _T("StorageConfig");
+	TCHAR buf[256];
+
+	GetPrivateProfileString(strSection, _T("UseTrigger"), _T("0"), buf, 256, strFile);
+	m_nUseTrigger = _ttoi(buf);
+
+	GetPrivateProfileString(strSection, _T("SavePointCount"), _T("100000"), buf, 256, strFile);
+	m_nSavePointCount = _ttoi(buf);
+
+	// 杈撳嚭绔彛
+	GetPrivateProfileString(strSection, _T("OutputPort"), _T("OUT1"), buf, 256, strFile);
+	{
+		int idx = m_comboOutputPort.FindStringExact(-1, buf);
+		if (idx != CB_ERR) {
+			m_comboOutputPort.SetCurSel(idx);
+		}
+		else {
+			m_comboOutputPort.SetCurSel(0);
+		}
+	}
+
+	// 璺冲彉妫�娴嬪弬鏁�
+	GetPrivateProfileString(strSection, _T("JumpThreshold"), _T("0.2"), buf, 256, strFile);
+	m_fJumpThreshold = static_cast<float>(_tstof(buf));
+
+	GetPrivateProfileString(strSection, _T("JumpWindow"), _T("3"), buf, 256, strFile);
+	m_nJumpWindow = _ttoi(buf);
+
+	GetPrivateProfileString(strSection, _T("ValleyMargin"), _T("0"), buf, 256, strFile);
+	m_nValleyMargin = _ttoi(buf);
+
+	GetPrivateProfileString(strSection, _T("MinGlass1Count"), _T("10"), buf, 256, strFile);
+	m_nMinGlass1Count = _ttoi(buf);
+
+	// 绋冲畾鍖哄煙鍙傛暟
+	GetPrivateProfileString(strSection, _T("FixedCount"), _T("5"), buf, 256, strFile);
+	m_nFixedCount = _ttoi(buf);
+
+	GetPrivateProfileString(strSection, _T("MaxDelta"), _T("0.05"), buf, 256, strFile);
+	m_fMaxDelta = static_cast<float>(_tstof(buf));
+
+	// 鑷惎鍔�
+	GetPrivateProfileString(strSection, _T("AutoStart"), _T("1"), buf, 256, strFile);
+	m_nAutoStart = _ttoi(buf);
+
+	return true;
+}
+
+bool CSGMeasurementDlg::SaveConfig(const CString& strFile)
+{
+	CString strSection = _T("StorageConfig");
+
+	WritePrivateProfileString(strSection, _T("UseTrigger"), std::to_wstring(m_nUseTrigger).c_str(), strFile);
+	WritePrivateProfileString(strSection, _T("SavePointCount"), std::to_wstring(m_nSavePointCount).c_str(), strFile);
+
+	// 杈撳嚭绔彛涓嬫媺妗�
+	CString strPort;
+	m_comboOutputPort.GetWindowText(strPort);
+	WritePrivateProfileString(strSection, _T("OutputPort"), strPort, strFile);
+
+	// 璺冲彉妫�娴�
+	WritePrivateProfileString(strSection, _T("JumpThreshold"), std::to_wstring(m_fJumpThreshold).c_str(), strFile);
+	WritePrivateProfileString(strSection, _T("JumpWindow"), std::to_wstring(m_nJumpWindow).c_str(), strFile);
+	WritePrivateProfileString(strSection, _T("ValleyMargin"), std::to_wstring(m_nValleyMargin).c_str(), strFile);
+	WritePrivateProfileString(strSection, _T("MinGlass1Count"), std::to_wstring(m_nMinGlass1Count).c_str(), strFile);
+
+	// 绋冲畾鍖�
+	WritePrivateProfileString(strSection, _T("FixedCount"), std::to_wstring(m_nFixedCount).c_str(), strFile);
+	WritePrivateProfileString(strSection, _T("MaxDelta"), std::to_wstring(m_fMaxDelta).c_str(), strFile);
+
+	// 鑷惎鍔�
+	WritePrivateProfileString(strSection, _T("AutoStart"), std::to_wstring(m_nAutoStart).c_str(), strFile);
+
+	return true;
+}
+
+bool CSGMeasurementDlg::SetAutoStart(bool bEnable)
+{
+	// 鑾峰彇褰撳墠绋嬪簭璺緞
+	TCHAR szPath[MAX_PATH] = { 0 };
+	GetModuleFileName(NULL, szPath, MAX_PATH);
+	CString strAppPath = szPath;
+
+	// 鑾峰彇搴旂敤绋嬪簭鍚嶇О
+	CString strAppName = ::PathFindFileName(strAppPath);
+	strAppName = strAppName.Left(strAppName.ReverseFind('.'));
+
+	HKEY hKey;
+	LONG lRet = RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_READ | KEY_WRITE, &hKey);
+
+	if (lRet != ERROR_SUCCESS) { 
+		return false;
+	}
+
+	DWORD dwType = 0;
+	TCHAR szValue[MAX_PATH] = { 0 };
+	DWORD dwSize = sizeof(szValue);
+	lRet = RegQueryValueEx(hKey, strAppName, 0, &dwType, (LPBYTE)szValue, &dwSize);
+
+	if (bEnable) {
+		if (lRet != ERROR_SUCCESS || _tcsicmp(szValue, strAppPath) != 0) {
+			// 璁剧疆鑷惎
+			lRet = RegSetValueEx(hKey, strAppName, 0, REG_SZ, (BYTE*)(LPCTSTR)strAppPath, (strAppPath.GetLength() + 1) * sizeof(TCHAR));
+		}
+		else {
+			lRet = ERROR_SUCCESS;
+		}
+	}
+	else {
+		if (lRet == ERROR_SUCCESS) {
+			// 鍙栨秷鑷惎
+			lRet = RegDeleteValue(hKey, strAppName);
+		}
+		else {
+			lRet = ERROR_SUCCESS;
+		}
+	}
+
+	RegCloseKey(hKey);
+	return (lRet == ERROR_SUCCESS);
 }
 
 bool CSGMeasurementDlg::ConnectToDevice()
@@ -639,7 +787,8 @@
 
 	fAvg1 = CalcAverage(vecGlass1);
 	fAvg2 = CalcAverage(vecGlass2);
-	fOffset = std::fabs(fAvg2 - fAvg1);  // 绗簩鐗� - 绗竴鐗�
+	fOffset = fAvg2 - fAvg1;				// 绗簩鐗� - 绗竴鐗�
+	//fOffset = std::fabs(fAvg2 - fAvg1);	// 绗簩鐗� - 绗竴鐗�
 
 	CString strLog;
 	strLog.Format(_T("绗竴鐗囩幓鐠冨钩鍧囧��: %.3f锛岀浜岀墖鐜荤拑骞冲潎鍊�: %.3f锛屽亸绉婚噺: %.3f"), fAvg1, fAvg2, fOffset);
@@ -714,7 +863,7 @@
 	return false;
 }
 
-float CSGMeasurementDlg::AnalyzeStoredData(int nOutNo)
+double CSGMeasurementDlg::AnalyzeStoredData(int nOutNo)
 {
 	MEASURE_FUNC_START();
 
@@ -723,27 +872,27 @@
 	if (m_nUseTrigger) {
 		UpdateControlStatus(m_bConnected, m_bSaving);
 		AfxMessageBox(_T("褰撳墠鏄‖瑙﹀彂妯″紡锛岃妫�鏌ヨЕ鍙戝櫒鐘舵�併��"), MB_ICONINFORMATION);
-		return -1.0f;
+		return DBL_MAX;
 	}
 
 	if (!m_bConnected) {
 		AppendLogLineRichStyled(_T("璁惧鏈繛鎺ワ紝璇峰厛杩炴帴璁惧銆�"), LOG_COLOR_WARNING);
-		return -1.0f;
+		return DBL_MAX;
 	}
 
 	if (m_bSaving) {
 		AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍姝e湪杩涜涓紝璇峰厛鍋滄瀛樺偍銆�"), LOG_COLOR_WARNING);
-		return -1.0f;
+		return DBL_MAX;
 	}
 
 	if (nOutNo < 1 || nOutNo > 4) {
 		AppendLogLineRichStyled(_T("杈撳嚭绔彛缂栧彿鏃犳晥锛屽繀椤诲湪 1 鍒� 4 涔嬮棿銆�"), LOG_COLOR_ERROR);
-		return -1.0f;
+		return DBL_MAX;
 	}
 
 	if (m_nSavePointCount < 0) {
 		AppendLogLineRichStyled(_T("鏁版嵁鐐规暟蹇呴』澶т簬 0銆�"), LOG_COLOR_ERROR);
-		return -1.0f;
+		return DBL_MAX;
 	}
 
 	std::vector<float> vecBuffer(m_nSavePointCount, 0.0f);
@@ -754,7 +903,7 @@
 		CString strError;
 		strError.Format(_T("璇诲彇 OUT%d 鏁版嵁澶辫触锛岄敊璇爜锛�%#X"), nOutNo, nRet);
 		AppendLogLineRichStyled(strError, LOG_COLOR_ERROR);
-		return -1.0f;
+		return DBL_MAX;
 	}
 
 	vecBuffer.resize(nReceived);
@@ -763,7 +912,7 @@
 	std::vector<float> vecGlass1, vecGlass2;
 	if (!SplitGlassSegments(nOutNo, vecBuffer, vecGlass1, vecGlass2, m_fJumpThreshold, m_nJumpWindow, m_nValleyMargin, m_nMinGlass1Count)) {
 		AppendLogLineRichStyled(_T("鏈兘璇嗗埆鍑轰袱鐗囩幓鐠冪殑鏁版嵁銆�"), LOG_COLOR_WARNING);
-		return -1.0f;
+		return DBL_MAX;
 	}
 
 	std::vector<float> vecGlass1Filtered, vecGlass2Filtered;
@@ -814,6 +963,7 @@
 	ON_WM_DRAWITEM()
 	ON_WM_CLOSE()
 	ON_MESSAGE(WM_TRAY_ICON_NOTIFY, &CSGMeasurementDlg::OnTrayIconClick)
+	ON_COMMAND(ID_TRAY_OPEN_DIR, &CSGMeasurementDlg::OnTrayOpenDir)
 	ON_COMMAND(ID_TRAY_RESTORE, &CSGMeasurementDlg::OnTrayRestore)
 	ON_COMMAND(ID_TRAY_EXIT, &CSGMeasurementDlg::OnTrayExit)
 	ON_BN_CLICKED(IDC_BUTTON_CONNECT, &CSGMeasurementDlg::OnBnClickedButtonConnect)
@@ -887,13 +1037,13 @@
 		if (type == -1) {
 			AppendLogLineRichStyled(msg, LOG_COLOR_ERROR);
 		}
-		if (type == 0) {
+		else if (type == 0) {
 			AppendLogLineRichStyled(msg, LOG_COLOR_SUCCESS);
 		}
 		else if (type == 1) {
 			AppendLogLineRichStyled(msg, LOG_COLOR_WARNING);
 		}
-		else {
+		else if (type == 2) {
 			AppendLogLineRichStyled(msg, LOG_COLOR_NORMAL);
 		}
 	});
@@ -903,9 +1053,14 @@
 
 	// 璁剧疆 PLC 鐩戝惉鍣ㄧ殑寮�濮嬮噰闆嗗洖璋冨嚱鏁�
 	m_plcListener.SetStartCallback([this]() {
-		InitDataStorage();
-		StartDataStorage();
-		UpdateControlStatus(m_bConnected, m_bSaving);
+		if (!m_bConnected) {
+			ConnectToDevice();
+		}
+
+		if (InitDataStorage()) {
+			StartDataStorage();
+			UpdateControlStatus(m_bConnected, m_bSaving);
+		}
 	});
 
 	// 璁剧疆 PLC 鐩戝惉鍣ㄧ殑鍋滄閲囬泦鍥炶皟鍑芥暟
@@ -916,21 +1071,46 @@
 
 	// 璁剧疆 PLC 鐩戝惉鍣ㄧ殑鍒嗘瀽鍥炶皟鍑芥暟
 	m_plcListener.SetAnalyzeCallback([this]() {
+		if (!m_bConnected) {
+			AppendLogLineRichStyled(_T("璁惧鏈繛鎺ワ紝璇峰厛杩炴帴璁惧銆�"), LOG_COLOR_WARNING);
+			return std::array<double, 4>{ DBL_MAX, DBL_MAX, DBL_MAX, DBL_MAX };
+		}
+
 		std::array<double, 4> result;
 		for (int i = 0; i < 4; ++i) {
 			result[i] = AnalyzeStoredData(i + 1); // OUT1 ~ OUT4
 		}
 
-		if (std::any_of(result.begin(), result.end(), [](double v) { return v < 0; })) {
-			AppendLogLineRichStyled(_T("鍒嗘瀽澶辫触锛屾煇浜涜緭鍑虹鍙f暟鎹棤鏁堛��"), LOG_COLOR_ERROR);
-			return std::array<double, 4>{ -1.0, -1.0, -1.0, -1.0 };
-		}
+		std::string strProductID;
+		m_plcListener.ReadProductID(strProductID);
+		m_resultStorage.SaveAnalyzeResult(CString(strProductID.c_str()), result);
 
 		CString strLog;
 		strLog.Format(_T("鍒嗘瀽缁撴灉锛歄UT1: %.3f, OUT2: %.3f, OUT3: %.3f, OUT4: %.3f"), result[0], result[1], result[2], result[3]);
 		return result;
 	});
 	m_plcListener.Start();
+
+	// 鍔犺浇閰嶇疆鏂囦欢
+	if (LoadConfig(GetConfigPath())) {
+		AppendLogLineRichStyled(_T("閰嶇疆宸蹭粠 SGConfig.ini 鍔犺浇鎴愬姛"), LOG_COLOR_SUCCESS);
+	}
+	else {
+		AppendLogLineRichStyled(_T("閰嶇疆鍔犺浇澶辫触锛屼娇鐢ㄩ粯璁ゅ弬鏁�"), LOG_COLOR_WARNING);
+	}
+
+	// 璁剧疆鑷姩鍚姩
+	if (SetAutoStart(m_nAutoStart)) {
+		if (m_nAutoStart) {
+			AppendLogLineRichStyled(_T("宸插惎鐢ㄥ紑鏈鸿嚜鍚姩"), LOG_COLOR_SUCCESS);
+		}
+		else {
+			AppendLogLineRichStyled(_T("宸插彇娑堝紑鏈鸿嚜鍚姩"), LOG_COLOR_WARNING);
+		}
+	}
+	else {
+		AppendLogLineRichStyled(_T("璁剧疆寮�鏈鸿嚜鍚姩澶辫触锛岃妫�鏌ユ潈闄�"), LOG_COLOR_ERROR);
+	}
 
 	// 鍒濆鍖栨棩蹇楁
 	AppendLogLineRichStyled(_T("鍑嗗灏辩华..."), LOG_COLOR_SUCCESS);
@@ -988,11 +1168,21 @@
 		RC nRet = SGIF_GetCalcDataALL(DeviceID, value);
 		if (nRet == RC_OK) {
 			for (int i = 0; i < 4; ++i) {
-				m_dOutValues[i] = value[i].Value;
-			}
+				double dNew = value[i].Value;
+				if (fabs(m_dOutValues[i] - dNew) > 1e-6) {
+					m_dOutValues[i] = dNew;
 
-			// 鏇存柊缁戝畾鎺т欢
-			UpdateData(FALSE);
+					CString str;
+					str.Format(_T("%.3f"), dNew);
+
+					switch (i) {
+					case 0: GetDlgItem(IDC_EDIT_OUT1)->SetWindowText(str); break;
+					case 1: GetDlgItem(IDC_EDIT_OUT2)->SetWindowText(str); break;
+					case 2: GetDlgItem(IDC_EDIT_OUT3)->SetWindowText(str); break;
+					case 3: GetDlgItem(IDC_EDIT_OUT4)->SetWindowText(str); break;
+					}
+				}
+			}
 		}
 		else {
 			CString strError;
@@ -1033,6 +1223,10 @@
 
 	// 鍥炬爣
 	HICON hIcon = nullptr;
+	if (id == ID_TRAY_OPEN_DIR) {
+		hIcon = AfxGetApp()->LoadIcon(IDI_ICON_OPEN_DIR);
+	}
+
 	if (id == ID_TRAY_RESTORE) {
 		hIcon = AfxGetApp()->LoadIcon(IDI_ICON_RESTORE);
 	}
@@ -1047,6 +1241,10 @@
 
 	// 鏂囨湰
 	CString str;
+	if (id == ID_TRAY_OPEN_DIR) {
+		str = _T("鎵撳紑鐩綍");
+	}
+
 	if (id == ID_TRAY_RESTORE) { 
 		str = _T("鎭㈠鐣岄潰");
 	}
@@ -1090,22 +1288,23 @@
 			// 鍙抽敭鐐瑰嚮寮瑰嚭鑿滃崟
 			CMenu menu;
 			menu.CreatePopupMenu();
+			menu.AppendMenu(MF_OWNERDRAW, ID_TRAY_OPEN_DIR, (LPCTSTR)ID_TRAY_OPEN_DIR);
 			menu.AppendMenu(MF_OWNERDRAW, ID_TRAY_RESTORE, (LPCTSTR)ID_TRAY_RESTORE);
 			menu.AppendMenu(MF_OWNERDRAW, ID_TRAY_EXIT, (LPCTSTR)ID_TRAY_EXIT);
-
-			// 鍔犺浇鍥炬爣
-			HICON hIconRestore = (HICON)::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON_RESTORE), IMAGE_ICON, 16, 16, LR_SHARED);
-			HICON hIconExit = (HICON)::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON_EXIT), IMAGE_ICON, 16, 16, LR_SHARED);
 
 			// 璁剧疆鍥炬爣鍒拌彍鍗曢」
 			MENUITEMINFO mii = { sizeof(MENUITEMINFO) };
 			mii.fMask = MIIM_BITMAP;
 
-			// 鎭㈠鑿滃崟椤瑰浘鏍�
+			// 鎵撳紑鐩綍鑿滃崟椤瑰浘鏍�
+			mii.hbmpItem = HBMMENU_CALLBACK;
+			menu.SetMenuItemInfo(ID_TRAY_OPEN_DIR, &mii);
+
+			// 鎭㈠绐楀彛鑿滃崟椤瑰浘鏍�
 			mii.hbmpItem = HBMMENU_CALLBACK;
 			menu.SetMenuItemInfo(ID_TRAY_RESTORE, &mii);
 
-			// 閫�鍑鸿彍鍗曢」鍥炬爣
+			// 閫�鍑虹▼搴忚彍鍗曢」鍥炬爣
 			mii.hbmpItem = HBMMENU_CALLBACK;
 			menu.SetMenuItemInfo(ID_TRAY_EXIT, &mii);
 
@@ -1119,6 +1318,18 @@
 	return 0;
 }
 
+void CSGMeasurementDlg::OnTrayOpenDir()
+{
+	CString strDir = GetAppDirectory();
+	if (PathFileExists(strDir)) {
+		ShellExecute(NULL, _T("open"), strDir, NULL, NULL, SW_SHOWDEFAULT);
+		AppendLogLineRichStyled(_T("宸叉墦寮�绋嬪簭鐩綍"), LOG_COLOR_SUCCESS);
+	}
+	else {
+		AppendLogLineRichStyled(_T("鐩綍涓嶅瓨鍦紝鏃犳硶鎵撳紑"), LOG_COLOR_ERROR);
+	}
+}
+
 void CSGMeasurementDlg::OnTrayRestore()
 {
 	ShowWindow(SW_SHOW);  	// 鎭㈠绐楀彛

--
Gitblit v1.9.3