mrDarker
2025-09-13 4a487efb06d6c314fd46ba52a2ba921618c676dd
SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp
@@ -1,4 +1,4 @@
#include "pch.h"
#include "pch.h"
#include "PLCSignalListener.h"
// === 日志打印类型 ===
@@ -247,35 +247,42 @@
            // 上升沿触发
            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)) {
                     CString msg;
                     msg.Format(_T("读取到产品ID:%s"), strProductID);
                     msg.Format(_T("读取到产品ID:%s"), CString(strProductID.c_str()));
                     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;
            }
@@ -302,15 +309,22 @@
   }
   for (int i = 0; i < PLC_RESULT_ADDR_COUNT; ++i) {
      // 放大1000倍并四舍五入,转为PLC整数
      int32_t  nScaled = static_cast<int32_t>(std::round(values[i] * 1000.0));
      DWordContainer vec = { static_cast<uint32_t>(nScaled) };
      double dVal = values[i];
      int32_t nScaled = 0;
      if (dVal == DBL_MAX || dVal == DBL_MIN || std::isnan(dVal)) {
         nScaled = static_cast<int32_t>(dVal);
      }
      else {
         nScaled = static_cast<int32_t>(std::round(dVal * 1000.0));
      }
      short nTargetAddr = PLC_RESULT_ADDR_START + i * 2;
      DWordContainer vec = { static_cast<uint32_t>(nScaled) };
      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]);
         msg.Format(_T("写入OUT%d到地址%d失败,值=%.2f"), i + 1, nTargetAddr, dVal);
         LOG_MSG(msg, LOG_TYPE_ERROR);
         return false;
      }
@@ -353,4 +367,43 @@
   }
   return true;
}
bool CPLCSignalListener::WriteProductID(const std::string& strProductID)
{
   if (!m_pPlc || !m_bConnected) {
      LOG_MSG(_T("PLC未连接或未初始化,无法写入产品ID。"), LOG_TYPE_ERROR);
      return false;
   }
   WordContainer vec;
   vec.reserve(PLC_PRODUCT_ID_WORDS);
   for (size_t i = 0; i < strProductID.size();) {
      unsigned char c1 = static_cast<unsigned char>(strProductID[i]);
      unsigned char c2 = 0;
      if (i + 1 < strProductID.size()) {
         c2 = static_cast<unsigned char>(strProductID[i + 1]);
      }
      uint16_t w = static_cast<uint16_t>(c2 << 8 | c1);
      vec.push_back(w);
      i += 2;
   }
   while (vec.size() < PLC_PRODUCT_ID_WORDS) {
      vec.push_back(0);
   }
   int ret = m_pPlc->WriteWordDataEx(m_station, PLC_WORD_DEVICE_TYPE, PLC_PRODUCT_ID_ADDR, vec);
   if (ret != 0) {
      CString msg;
      msg.Format(_T("写入产品ID失败,错误码=%d"), ret);
      LOG_MSG(msg, LOG_TYPE_ERROR);
      return false;
   }
   return true;
}