From 874993222ab785bedd1525ef3335f6714a3ca6e0 Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期四, 11 九月 2025 09:16:42 +0800
Subject: [PATCH] 1. SG精度检写入日志文件 2. SG精度检结果可以是负数 3. 添加读取产品ID功能

---
 SourceCode/Bond/SGMeasurement/PLCSignalListener.h        |    9 ++++
 SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.user |    2 
 SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp       |    9 ++--
 SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp      |   46 +++++++++++++++++++++++
 4 files changed, 61 insertions(+), 5 deletions(-)

diff --git a/SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp b/SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp
index bef7669..d5869c7 100644
--- a/SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp
+++ b/SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp
@@ -31,6 +31,10 @@
 #define PLC_RESULT_ADDR_START   0x37B0  // PLC结果寄存器起始地址(如W37B0)
 #define PLC_RESULT_ADDR_COUNT   4       // 结果寄存器数量(如W37B0, W37B2, W37B4, W37B6)
 
+// === PLC 产品ID配置(PLC -> PC)===
+#define PLC_PRODUCT_ID_ADDR      0x1B160   // 产品ID起始地址 (W1B160)
+#define PLC_PRODUCT_ID_WORDS     10        // 产品ID长度(10个Word)
+
 #define IS_RISING_EDGE(prev, curr) (!(prev) && (curr))
 
 CPLCSignalListener::CPLCSignalListener() = default;
@@ -250,6 +254,13 @@
                             m_vecAckSent[i] = true;
                             m_vecAckCounter[i] = 0;
                         }
+
+                        std::string strProductID;
+                        if (ReadProductID(strProductID)) {
+                            CString msg;
+                            msg.Format(_T("读取到产品ID:%s"), strProductID.c_str());
+                            LOG_MSG(msg, LOG_TYPE_SUCCESS);
+                        }
                     }
                     break;
 
@@ -306,4 +317,39 @@
     }
 
     return true;
+}
+
+bool CPLCSignalListener::ReadProductID(std::string& strProductID)
+{
+    if (!m_pPlc || !m_bConnected) {
+        LOG_MSG(_T("PLC未连接或未初始化,无法读取产品ID。"), LOG_TYPE_ERROR);
+        return false;
+    }
+
+    WordContainer vec;
+    int ret = m_pPlc->ReadWordDataEx(m_station, PLC_WORD_DEVICE_TYPE, PLC_PRODUCT_ID_ADDR, PLC_PRODUCT_ID_WORDS, vec);
+    if (ret != 0 || vec.size() != PLC_PRODUCT_ID_WORDS) {
+        CString msg;
+        msg.Format(_T("读取产品ID失败,错误码=%d"), ret);
+        LOG_MSG(msg, LOG_TYPE_ERROR);
+        return false;
+    }
+
+    strProductID.clear();
+    for (auto w : vec) {
+        char c1 = static_cast<char>(w & 0xFF);          // 低字节
+        char c2 = static_cast<char>((w >> 8) & 0xFF);   // 高字节
+
+        if (c1 == '\0') { 
+            break;
+        }
+        strProductID.push_back(c1);
+
+        if (c2 == '\0') { 
+            break;
+        }
+        strProductID.push_back(c2);
+    }
+
+    return true;
 }
\ No newline at end of file
diff --git a/SourceCode/Bond/SGMeasurement/PLCSignalListener.h b/SourceCode/Bond/SGMeasurement/PLCSignalListener.h
index fe8638b..592cfde 100644
--- a/SourceCode/Bond/SGMeasurement/PLCSignalListener.h
+++ b/SourceCode/Bond/SGMeasurement/PLCSignalListener.h
@@ -78,6 +78,15 @@
      */
     bool WriteOutValues(const OutValuesArray& values);
 
+	/**
+	 * @brief 读取 PLC 内部的产品 ID(字符串形式)。
+	 *
+	 * @param strProductID  输出参数,存储读取到的产品 ID。
+	 * @return true         读取成功。
+	 * @return false        读取失败。
+	 */
+    bool ReadProductID(std::string& strProductID);
+
 private:
     /**
      * @brief 输出日志信息(封装日志回调)。
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.user b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.user
index 110a872..0cecbcc 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.user
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.user
@@ -4,7 +4,7 @@
     <RESOURCE_FILE>SGMeasurement.rc</RESOURCE_FILE>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
-    <DebuggerFlavor>WindowsRemoteDebugger</DebuggerFlavor>
+    <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
     <RemoteDebuggerCommand>\\DESKTOP-IODBVIQ\SGMeasurement\$(ProjectName).exe</RemoteDebuggerCommand>
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
index a3f8234..dcf3a01 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
@@ -247,7 +247,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)*/)
@@ -639,7 +639,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);
@@ -760,10 +761,10 @@
 	vecBuffer.resize(nReceived);
 	CleanInvalidValuesInPlace(nOutNo, vecBuffer);
 
-	std::vector<float> vecGlass1, vecGlass2;
+ 	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 -1.0f; 
 	}
 
 	std::vector<float> vecGlass1Filtered, vecGlass2Filtered;

--
Gitblit v1.9.3