From 1310b3764eafe085dd43a73aa5190a18e9f717c0 Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期四, 11 九月 2025 17:11:40 +0800
Subject: [PATCH] 1. 把数值放大1000倍 2. 发送PLC是双字
---
SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj | 2 +-
SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.user | 2 +-
SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp | 21 ++++++++-------------
SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp | 11 ++++++-----
4 files changed, 16 insertions(+), 20 deletions(-)
diff --git a/SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp b/SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp
index d5869c7..1de8be5 100644
--- a/SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp
+++ b/SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp
@@ -258,7 +258,7 @@
std::string strProductID;
if (ReadProductID(strProductID)) {
CString msg;
- msg.Format(_T("读取到产品ID:%s"), strProductID.c_str());
+ msg.Format(_T("读取到产品ID:%s"), strProductID);
LOG_MSG(msg, LOG_TYPE_SUCCESS);
}
}
@@ -302,12 +302,12 @@
}
for (int i = 0; i < PLC_RESULT_ADDR_COUNT; ++i) {
- // 放大100倍并四舍五入,转为PLC整数
- uint16_t nScaled = static_cast<uint16_t>(std::round(values[i] * 100.0));
- WordContainer vec = { nScaled };
+ // 放大1000倍并四舍五入,转为PLC整数
+ int32_t nScaled = static_cast<int32_t>(std::round(values[i] * 1000.0));
+ DWordContainer vec = { static_cast<uint32_t>(nScaled) };
short nTargetAddr = PLC_RESULT_ADDR_START + i * 2;
- int ret = m_pPlc->WriteWordDataEx(m_station, PLC_WORD_DEVICE_TYPE, nTargetAddr, vec);
+ int ret = m_pPlc->WriteDWordDataEx(m_station, PLC_WORD_DEVICE_TYPE, nTargetAddr, vec);
if (ret != 0) {
CString msg;
msg.Format(_T("写入OUT%d到地址%d失败,值=%.2f"), i + 1, nTargetAddr, values[i]);
@@ -336,6 +336,7 @@
}
strProductID.clear();
+ strProductID.reserve(PLC_PRODUCT_ID_WORDS * 2);
for (auto w : vec) {
char c1 = static_cast<char>(w & 0xFF); // 低字节
char c2 = static_cast<char>((w >> 8) & 0xFF); // 高字节
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj
index 05fa356..1bab676 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj
@@ -175,7 +175,7 @@
<PreprocessorDefinitions>_WINDOWS;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>.;..;.\DLL\64bit;.\CCLinkPerformance;..\MELSECSDK\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
- <Optimization>MaxSpeed</Optimization>
+ <Optimization>Disabled</Optimization>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.user b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.user
index 0cecbcc..110a872 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>WindowsLocalDebugger</DebuggerFlavor>
+ <DebuggerFlavor>WindowsRemoteDebugger</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 dcf3a01..4f7a030 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
@@ -724,27 +724,27 @@
if (m_nUseTrigger) {
UpdateControlStatus(m_bConnected, m_bSaving);
AfxMessageBox(_T("褰撳墠鏄‖瑙﹀彂妯″紡锛岃妫�鏌ヨЕ鍙戝櫒鐘舵�併��"), MB_ICONINFORMATION);
- return -1.0f;
+ return 0xFF;
}
if (!m_bConnected) {
AppendLogLineRichStyled(_T("璁惧鏈繛鎺ワ紝璇峰厛杩炴帴璁惧銆�"), LOG_COLOR_WARNING);
- return -1.0f;
+ return 0xFF;
}
if (m_bSaving) {
AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍姝e湪杩涜涓紝璇峰厛鍋滄瀛樺偍銆�"), LOG_COLOR_WARNING);
- return -1.0f;
+ return 0xFF;
}
if (nOutNo < 1 || nOutNo > 4) {
AppendLogLineRichStyled(_T("杈撳嚭绔彛缂栧彿鏃犳晥锛屽繀椤诲湪 1 鍒� 4 涔嬮棿銆�"), LOG_COLOR_ERROR);
- return -1.0f;
+ return 0xFF;
}
if (m_nSavePointCount < 0) {
AppendLogLineRichStyled(_T("鏁版嵁鐐规暟蹇呴』澶т簬 0銆�"), LOG_COLOR_ERROR);
- return -1.0f;
+ return 0xFF;
}
std::vector<float> vecBuffer(m_nSavePointCount, 0.0f);
@@ -755,7 +755,7 @@
CString strError;
strError.Format(_T("璇诲彇 OUT%d 鏁版嵁澶辫触锛岄敊璇爜锛�%#X"), nOutNo, nRet);
AppendLogLineRichStyled(strError, LOG_COLOR_ERROR);
- return -1.0f;
+ return 0xFF;
}
vecBuffer.resize(nReceived);
@@ -764,7 +764,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 0xFF;
}
std::vector<float> vecGlass1Filtered, vecGlass2Filtered;
@@ -924,17 +924,12 @@
m_plcListener.SetAnalyzeCallback([this]() {
if (!m_bConnected) {
AppendLogLineRichStyled(_T("璁惧鏈繛鎺ワ紝璇峰厛杩炴帴璁惧銆�"), LOG_COLOR_WARNING);
- return std::array<double, 4>{ -1.0, -1.0, -1.0, -1.0 };
+ return std::array<double, 4>{ 0xFF, 0xFF, 0xFF, 0xFF };
}
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 };
}
CString strLog;
--
Gitblit v1.9.3