From 714cfc2313c8851d6cd00058246f88aae103809d Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期五, 12 九月 2025 15:15:34 +0800
Subject: [PATCH] 1. SG精度检保存检测结果(保存到文件) 2. SG精度检字符串需要UTF-8(BOM)

---
 SourceCode/Bond/SGMeasurement/resource.h                              |    2 
 SourceCode/Bond/SGMeasurement/PLCSignalListener.h                     |    2 
 SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h                      |   10 +++
 SourceCode/Bond/SGMeasurement/framework.h                             |    2 
 SourceCode/Bond/SGMeasurement/pch.h                                   |    2 
 SourceCode/Bond/SGMeasurement/Logger.cpp                              |    2 
 SourceCode/Bond/SGMeasurement/CCLinkPerformance/CCLinkIEControl.cpp   |    2 
 SourceCode/Bond/SGMeasurement/CCLinkPerformance/PerformanceMelsec.cpp |    2 
 SourceCode/Bond/SGMeasurement/pch.cpp                                 |    2 
 SourceCode/Bond/SGMeasurement/targetver.h                             |    2 
 SourceCode/Bond/SGMeasurement/CCLinkPerformance/PerformanceMelsec.h   |    2 
 SourceCode/Bond/SGMeasurement/SGMeasurement.cpp                       |    2 
 SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj                   |    2 
 SourceCode/Bond/SGMeasurement/ProductResultStorage.h                  |   18 ++++++
 SourceCode/Bond/SGMeasurement/SGMeasurement.h                         |    2 
 SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.filters           |    6 ++
 SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp                    |    6 +
 SourceCode/Bond/SGMeasurement/CCLinkPerformance/CCLinkIEControl.h     |    2 
 SourceCode/Bond/SGMeasurement/Logger.h                                |    2 
 SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp                   |   25 +++++---
 SourceCode/Bond/SGMeasurement/ProductResultStorage.cpp                |   70 +++++++++++++++++++++++
 21 files changed, 140 insertions(+), 25 deletions(-)

diff --git a/SourceCode/Bond/SGMeasurement/CCLinkPerformance/CCLinkIEControl.cpp b/SourceCode/Bond/SGMeasurement/CCLinkPerformance/CCLinkIEControl.cpp
index ee59ea8..3d4c965 100644
--- a/SourceCode/Bond/SGMeasurement/CCLinkPerformance/CCLinkIEControl.cpp
+++ b/SourceCode/Bond/SGMeasurement/CCLinkPerformance/CCLinkIEControl.cpp
@@ -1,4 +1,4 @@
-#include "pch.h"
+锘�#include "pch.h"
 #include "CCLinkIEControl.h"
 
 CCCLinkIEControl::CCCLinkIEControl() : CPerformanceMelsec(BoardType::CC_LINK_IE_CONTROL) {}
diff --git a/SourceCode/Bond/SGMeasurement/CCLinkPerformance/CCLinkIEControl.h b/SourceCode/Bond/SGMeasurement/CCLinkPerformance/CCLinkIEControl.h
index 5782c45..1baef51 100644
--- a/SourceCode/Bond/SGMeasurement/CCLinkPerformance/CCLinkIEControl.h
+++ b/SourceCode/Bond/SGMeasurement/CCLinkPerformance/CCLinkIEControl.h
@@ -1,4 +1,4 @@
-#ifndef CCLINKIECONTROL_H
+锘�#ifndef CCLINKIECONTROL_H
 #define CCLINKIECONTROL_H
 
 #include "PerformanceMelsec.h"
diff --git a/SourceCode/Bond/SGMeasurement/CCLinkPerformance/PerformanceMelsec.cpp b/SourceCode/Bond/SGMeasurement/CCLinkPerformance/PerformanceMelsec.cpp
index 24b6104..e677b00 100644
--- a/SourceCode/Bond/SGMeasurement/CCLinkPerformance/PerformanceMelsec.cpp
+++ b/SourceCode/Bond/SGMeasurement/CCLinkPerformance/PerformanceMelsec.cpp
@@ -1,4 +1,4 @@
-// PerformanceMelsec.cpp: implementation of the CPerformanceMelsec class.
+锘�// PerformanceMelsec.cpp: implementation of the CPerformanceMelsec class.
 //
 //////////////////////////////////////////////////////////////////////
 #include "pch.h"
diff --git a/SourceCode/Bond/SGMeasurement/CCLinkPerformance/PerformanceMelsec.h b/SourceCode/Bond/SGMeasurement/CCLinkPerformance/PerformanceMelsec.h
index f171550..836f4da 100644
--- a/SourceCode/Bond/SGMeasurement/CCLinkPerformance/PerformanceMelsec.h
+++ b/SourceCode/Bond/SGMeasurement/CCLinkPerformance/PerformanceMelsec.h
@@ -1,4 +1,4 @@
-#ifndef PERFORMANCE_MELSEC_H
+锘�#ifndef PERFORMANCE_MELSEC_H
 #define PERFORMANCE_MELSEC_H
 
 #include "Mdfunc.h"
diff --git a/SourceCode/Bond/SGMeasurement/Logger.cpp b/SourceCode/Bond/SGMeasurement/Logger.cpp
index 34a0dd1..ecc94fd 100644
--- a/SourceCode/Bond/SGMeasurement/Logger.cpp
+++ b/SourceCode/Bond/SGMeasurement/Logger.cpp
@@ -1,4 +1,4 @@
-#include "pch.h"
+锘�#include "pch.h"
 #include "Logger.h"
 
 CLogger& CLogger::Instance()
diff --git a/SourceCode/Bond/SGMeasurement/Logger.h b/SourceCode/Bond/SGMeasurement/Logger.h
index 1649714..2b9f84d 100644
--- a/SourceCode/Bond/SGMeasurement/Logger.h
+++ b/SourceCode/Bond/SGMeasurement/Logger.h
@@ -1,4 +1,4 @@
-#pragma once
+锘�#pragma once
 #include <afx.h>
 
 class CLogger
diff --git a/SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp b/SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp
index 4176b59..ab32e61 100644
--- a/SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp
+++ b/SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp
@@ -1,4 +1,4 @@
-#include "pch.h"
+锘�#include "pch.h"
 #include "PLCSignalListener.h"
 
 // === 鏃ュ織鎵撳嵃绫诲瀷 ===
@@ -247,13 +247,10 @@
 				// 涓婂崌娌胯Е鍙�
 				switch (i) {
 				case 0:
+					// Start 鍛戒护
 					if (m_cbStart) {
 						m_cbStart();
 						WriteOutValues(OutValuesArray{ 0.0, 0.0, 0.0, 0.0 });
-						if (m_pPlc->SetBitDeviceEx(m_station, PLC_BIT_DEVICE_TYPE, PLC_ACK_BASE_BIT + i) == 0) {
-							m_vecAckSent[i] = true;
-							m_vecAckCounter[i] = 0;
-						}
 
 						std::string strProductID;
 						if (ReadProductID(strProductID)) {
@@ -262,21 +259,31 @@
 							LOG_MSG(msg, LOG_TYPE_SUCCESS);
 						}
 					}
+
+					// 鍙戦�佸簲绛斾俊鍙�
+					if (m_pPlc->SetBitDeviceEx(m_station, PLC_BIT_DEVICE_TYPE, PLC_ACK_BASE_BIT + i) == 0) {
+						m_vecAckSent[i] = true;
+						m_vecAckCounter[i] = 0;
+					}
 					break;
 
 				case 1:
+					// Stop 鍛戒护
 					if (m_cbStop) {
 						m_cbStop();
-						if (m_pPlc->SetBitDeviceEx(m_station, PLC_BIT_DEVICE_TYPE, PLC_ACK_BASE_BIT + i) == 0) {
-							m_vecAckSent[i] = true;
-							m_vecAckCounter[i] = 0;
-						}
 					}
 
+					// Analyze 鍛戒护
 					if (m_cbAnalyze) {
 						auto results = m_cbAnalyze();
 						WriteOutValues(results);
 					}
+
+					// 鍙戦�佸簲绛斾俊鍙�
+					if (m_pPlc->SetBitDeviceEx(m_station, PLC_BIT_DEVICE_TYPE, PLC_ACK_BASE_BIT + i) == 0) {
+						m_vecAckSent[i] = true;
+						m_vecAckCounter[i] = 0;
+					}
 					break;
 				}
 			}
diff --git a/SourceCode/Bond/SGMeasurement/PLCSignalListener.h b/SourceCode/Bond/SGMeasurement/PLCSignalListener.h
index 0a4030d..aa40df7 100644
--- a/SourceCode/Bond/SGMeasurement/PLCSignalListener.h
+++ b/SourceCode/Bond/SGMeasurement/PLCSignalListener.h
@@ -1,4 +1,4 @@
-#pragma once
+锘�#pragma once
 
 #include "CCLinkIEControl.h"
 
diff --git a/SourceCode/Bond/SGMeasurement/ProductResultStorage.cpp b/SourceCode/Bond/SGMeasurement/ProductResultStorage.cpp
new file mode 100644
index 0000000..51503e2
--- /dev/null
+++ b/SourceCode/Bond/SGMeasurement/ProductResultStorage.cpp
@@ -0,0 +1,70 @@
+锘�#include "pch.h"
+#include "ProductResultStorage.h"
+
+CProductResultStorage::CProductResultStorage()
+{
+	TCHAR szPath[MAX_PATH] = { 0 };
+	GetModuleFileName(NULL, szPath, MAX_PATH);
+	CString strExePath(szPath);
+	int pos = strExePath.ReverseFind(_T('\\'));
+	if (pos != -1) {
+		m_strBaseResultDir = strExePath.Left(pos) + _T("\\Data");
+	}
+	else {
+		m_strBaseResultDir = _T(".\\Data");
+	}
+}
+
+void CProductResultStorage::SaveAnalyzeResult(const CString& strProductID, const std::array<double, 4>& result)
+{
+	CString strFilePath = MakeResultCsvPath(_T("MeasurementResults"));
+	CString strHeader = _T("Timestamp,ProductID,OUT1,OUT2,OUT3,OUT4\r\n");
+
+	// 鏃堕棿鎴�
+	CTime now = CTime::GetCurrentTime();
+	CString strTimestamp;
+	strTimestamp.Format(_T("%02d:%02d:%02d"), now.GetHour(), now.GetMinute(), now.GetSecond());
+
+	// 缁撴灉琛�
+	CString strBody;
+	strBody.Format(_T("%s,%s,%.3f,%.3f,%.3f,%.3f\r\n"), strTimestamp, strProductID, result[0], result[1], result[2], result[3]);
+	AppendCsvWithHeader(strFilePath, strHeader, strBody);
+}
+
+CString CProductResultStorage::MakeResultCsvPath(const CString& strFileName)
+{
+    SYSTEMTIME st;
+    GetLocalTime(&st);
+
+    CString strDate;
+    strDate.Format(_T("%04d%02d%02d"), st.wYear, st.wMonth, st.wDay);
+
+    if (GetFileAttributes(m_strBaseResultDir) == INVALID_FILE_ATTRIBUTES) {
+        CreateDirectory(m_strBaseResultDir, NULL);
+    }
+
+    CString strFilePath;
+    strFilePath.Format(_T("%s\\%s_%s.csv"), m_strBaseResultDir, strFileName, strDate);
+
+    return strFilePath;
+}
+
+BOOL CProductResultStorage::AppendCsvWithHeader(const CString& strFilePath, const CString& strHeader, const CString& strBody)
+{
+    if (strBody.IsEmpty()) {
+        return FALSE;
+    }
+
+    CFile f;
+    CFileException ex;
+    if (!f.Open(strFilePath, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate | CFile::shareDenyWrite | CFile::shareDenyRead, &ex)) {
+        return FALSE;
+    }
+
+    if (f.SeekToEnd() == 0L) {
+        f.Write(strHeader, strHeader.GetLength() * sizeof(TCHAR));
+    }
+    f.Write(strBody, strBody.GetLength() * sizeof(TCHAR));
+    f.Close();
+    return TRUE;
+}
diff --git a/SourceCode/Bond/SGMeasurement/ProductResultStorage.h b/SourceCode/Bond/SGMeasurement/ProductResultStorage.h
new file mode 100644
index 0000000..6165555
--- /dev/null
+++ b/SourceCode/Bond/SGMeasurement/ProductResultStorage.h
@@ -0,0 +1,18 @@
+锘�#pragma once
+#include <array>
+
+class CProductResultStorage
+{
+public:
+	CProductResultStorage();
+    ~CProductResultStorage() = default;
+
+    void SaveAnalyzeResult(const CString& strProductID, const std::array<double, 4>& result);
+
+private:
+    CString MakeResultCsvPath(const CString& strFileName);
+    BOOL AppendCsvWithHeader(const CString& strFilePath, const CString& strHeader, const CString& strBody);
+
+    CString m_strBaseResultDir;
+};
+
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurement.cpp b/SourceCode/Bond/SGMeasurement/SGMeasurement.cpp
index 475924d..2c3c2e6 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurement.cpp
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurement.cpp
@@ -1,4 +1,4 @@
-
+锘�
 // SGMeasurement.cpp: 瀹氫箟搴旂敤绋嬪簭鐨勭被琛屼负銆�
 //
 
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurement.h b/SourceCode/Bond/SGMeasurement/SGMeasurement.h
index 6515a3d..3819dce 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurement.h
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurement.h
@@ -1,4 +1,4 @@
-
+锘�
 // SGMeasurement.h: PROJECT_NAME 搴旂敤绋嬪簭鐨勪富澶存枃浠�
 //
 
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj
index 1bab676..33c1c14 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj
@@ -209,6 +209,7 @@
     <ClInclude Include="Logger.h" />
     <ClInclude Include="pch.h" />
     <ClInclude Include="PLCSignalListener.h" />
+    <ClInclude Include="ProductResultStorage.h" />
     <ClInclude Include="Resource.h" />
     <ClInclude Include="SGMeasurement.h" />
     <ClInclude Include="SGMeasurementDlg.h" />
@@ -225,6 +226,7 @@
       <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
     </ClCompile>
     <ClCompile Include="PLCSignalListener.cpp" />
+    <ClCompile Include="ProductResultStorage.cpp" />
     <ClCompile Include="SGMeasurement.cpp" />
     <ClCompile Include="SGMeasurementDlg.cpp" />
   </ItemGroup>
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.filters b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.filters
index 163f190..df340e5 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.filters
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.filters
@@ -48,6 +48,9 @@
     <ClInclude Include="PLCSignalListener.h">
       <Filter>澶存枃浠�</Filter>
     </ClInclude>
+    <ClInclude Include="ProductResultStorage.h">
+      <Filter>澶存枃浠�</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="SGMeasurement.cpp">
@@ -71,6 +74,9 @@
     <ClCompile Include="PLCSignalListener.cpp">
       <Filter>婧愭枃浠�</Filter>
     </ClCompile>
+    <ClCompile Include="ProductResultStorage.cpp">
+      <Filter>婧愭枃浠�</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="SGMeasurement.rc">
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
index a2afe06..8e45bb2 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
@@ -1,4 +1,4 @@
-
+锘�
 // SGMeasurementDlg.cpp: 瀹炵幇鏂囦欢
 //
 
@@ -932,6 +932,10 @@
 			result[i] = AnalyzeStoredData(i + 1); // OUT1 ~ OUT4
 		}
 
+		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;
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h
index ab23343..a1d8aae 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h
@@ -1,10 +1,11 @@
-
+锘�
 // SGMeasurementDlg.h: 澶存枃浠�
 //
 
 #pragma once
 
 #include "PLCSignalListener.h"
+#include "ProductResultStorage.h"
 
 #include <map>
 #include <vector>
@@ -340,4 +341,11 @@
 	 * @brief PLC 淇″彿鐩戝惉鍣ㄥ疄渚嬶紝鐢ㄤ簬澶勭悊 PLC 淇″彿浜嬩欢
 	 */
 	CPLCSignalListener m_plcListener;
+
+	// === 浜у搧缁撴灉瀛樺偍 ===
+
+	/**
+	 * @brief 浜у搧缁撴灉瀛樺偍瀹炰緥锛岀敤浜庝繚瀛樺拰绠$悊娴嬮噺缁撴灉
+	 */
+	CProductResultStorage m_resultStorage;
 };
diff --git a/SourceCode/Bond/SGMeasurement/framework.h b/SourceCode/Bond/SGMeasurement/framework.h
index d6f08b6..5672fb5 100644
--- a/SourceCode/Bond/SGMeasurement/framework.h
+++ b/SourceCode/Bond/SGMeasurement/framework.h
@@ -1,4 +1,4 @@
-#pragma once
+锘�#pragma once
 
 #ifndef VC_EXTRALEAN
 #define VC_EXTRALEAN            // 浠� Windows 澶翠腑鎺掗櫎鏋佸皯浣跨敤鐨勮祫鏂�
diff --git a/SourceCode/Bond/SGMeasurement/pch.cpp b/SourceCode/Bond/SGMeasurement/pch.cpp
index 10b8eaa..b6fb8f4 100644
--- a/SourceCode/Bond/SGMeasurement/pch.cpp
+++ b/SourceCode/Bond/SGMeasurement/pch.cpp
@@ -1,4 +1,4 @@
-// pch.cpp: 涓庨缂栬瘧鏍囧ご瀵瑰簲鐨勬簮鏂囦欢
+锘�// pch.cpp: 涓庨缂栬瘧鏍囧ご瀵瑰簲鐨勬簮鏂囦欢
 
 #include "pch.h"
 
diff --git a/SourceCode/Bond/SGMeasurement/pch.h b/SourceCode/Bond/SGMeasurement/pch.h
index fe3bd2d..9660927 100644
--- a/SourceCode/Bond/SGMeasurement/pch.h
+++ b/SourceCode/Bond/SGMeasurement/pch.h
@@ -1,4 +1,4 @@
-// pch.h: 杩欐槸棰勭紪璇戞爣澶存枃浠躲��
+锘�// pch.h: 杩欐槸棰勭紪璇戞爣澶存枃浠躲��
 // 涓嬫柟鍒楀嚭鐨勬枃浠朵粎缂栬瘧涓�娆★紝鎻愰珮浜嗗皢鏉ョ敓鎴愮殑鐢熸垚鎬ц兘銆�
 // 杩欒繕灏嗗奖鍝� IntelliSense 鎬ц兘锛屽寘鎷唬鐮佸畬鎴愬拰璁稿浠g爜娴忚鍔熻兘銆�
 // 浣嗘槸锛屽鏋滄澶勫垪鍑虹殑鏂囦欢涓殑浠讳綍涓�涓湪鐢熸垚涔嬮棿鏈夋洿鏂帮紝瀹冧滑鍏ㄩ儴閮藉皢琚噸鏂扮紪璇戙��
diff --git a/SourceCode/Bond/SGMeasurement/resource.h b/SourceCode/Bond/SGMeasurement/resource.h
index e083afe..9286258 100644
--- a/SourceCode/Bond/SGMeasurement/resource.h
+++ b/SourceCode/Bond/SGMeasurement/resource.h
@@ -1,4 +1,4 @@
-//{{NO_DEPENDENCIES}}
+锘�//{{NO_DEPENDENCIES}}
 // Microsoft Visual C++ 鐢熸垚鐨勫寘鍚枃浠躲��
 // 渚� SGMeasurement.rc 浣跨敤
 //
diff --git a/SourceCode/Bond/SGMeasurement/targetver.h b/SourceCode/Bond/SGMeasurement/targetver.h
index 51be421..79934a3 100644
--- a/SourceCode/Bond/SGMeasurement/targetver.h
+++ b/SourceCode/Bond/SGMeasurement/targetver.h
@@ -1,4 +1,4 @@
-#pragma once
+锘�#pragma once
 
 // 鍖呮嫭 SDKDDKVer.h 灏嗗畾涔夊彲鐢ㄧ殑鏈�楂樼増鏈殑 Windows 骞冲彴銆�
 

--
Gitblit v1.9.3