SourceCode/Bond/SGMeasurement/PLCSignalListener.cpp
@@ -255,7 +255,7 @@
                  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);
                  }
               }
@@ -309,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;
      }
@@ -360,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;
}