LAPTOP-T815PCOQ\25526
2025-01-13 9ea5eea16629a2ab4473727e6b064f83f71aa46c
SourceCode/Bond/BoounionPLC/Model.cpp
@@ -5,7 +5,7 @@
#include "ToolUnits.h"
// 常量
#define ADDR_NIGHT_SHIFT_CAPACTITY      2027
CModel* g_pModel = NULL;
void CALLBACK ModerTimerProc(HWND hWnd, UINT nMsg, UINT nTimerid, DWORD dwTime)
@@ -313,6 +313,13 @@
         }
      }
   }
   // 读取产能信息
   CPLC* pPlc = getCurrentPlc();
   if (pPlc != nullptr) {
      pPlc->readPLCDataRegularly();
   }
}
std::map<std::string, CPLC*>& CModel::getPlcMap()
@@ -324,7 +331,32 @@
{
   auto iter = m_mapPlc.find(pszName);
   if (iter != m_mapPlc.end()) return -1;
   CString strPlcDir;
   strPlcDir.Format(_T("%s\\Plcs\\%s"), (LPTSTR)(LPCTSTR)m_strWorkDir, pszName);
   CPLC* pPLC = new CPLC(pszName, pszIp, port);
   pPLC->setWorkDir((LPTSTR)(LPCTSTR)strPlcDir);
   PLCListener listener;
   listener.onStateChanged = [&](void* pFrom, int state) -> void {
      LOGD("PLC状态改变,%d", state);
   };
   listener.onMonitorData = [&](void* pFrom, int id) -> void {
      LOGD("PLConMonitorData,%d", id);
   };
   listener.onAlarm = [&](void* pFrom, CAlarm* pAlarm, int flag) -> void {
      LOGE("onAlarm,%d %s", pAlarm->getId(), flag != 0 ? "ON" : "Off");
      if (flag == 1) {
         pAlarm->addRef();
         notifyObjAndPtr(RX_CODE_ALARM_ON, pAlarm, pFrom);
         pAlarm->release();
      }
      else {
         pAlarm->addRef();
         notifyObjAndPtr(RX_CODE_ALARM_OFF, pAlarm, pFrom);
         pAlarm->release();
      }
   };
   pPLC->setListener(listener);
   pPLC->init();
   m_mapPlc[pszName] = pPLC;
@@ -371,108 +403,4 @@
   }
   return nullptr;
}
void CModel::readPLCDataRegularly()
{
   CPLC* pPlc = getCurrentPlc();
   if (pPlc == nullptr || !pPlc->isConnected()) return;
   {
      auto funOnReadData = [this](IMcChannel* pChannel, int addr, char* pData, unsigned int nDataSize, int flag) -> void {
         if (nDataSize == 2 && flag == 0) {
            int nVelocityRatio = CToolUnits::toInt16(&pData[0]);
            if (nVelocityRatio != m_nVelocityRatio) {
               m_nVelocityRatio = nVelocityRatio;
               notifyInt(RX_CODE_VELOCITY_RATIO, m_nVelocityRatio);
            }
         }
      };
      pPlc->readData(MC::D, 530, 2, funOnReadData);
   }
   {
      auto funOnReadData = [this](IMcChannel* pChannel, int addr, char* pData, unsigned int nDataSize, int flag) -> void {
         if (nDataSize == 2 && flag == 0) {
            double dTactTime = (double)CToolUnits::toInt16(&pData[0]);
            if (dTactTime != m_dTactTime) {
               m_dTactTime = dTactTime;
               notifyDouble(RX_CODE_TACT_TIME, m_dTactTime);
            }
         }
      };
      pPlc->readData(MC::ZR, 1500, 2, funOnReadData);
   }
   {
      auto funOnReadData = [this](IMcChannel* pChannel, int addr, char* pData, unsigned int nDataSize, int flag) -> void {
         if (nDataSize == (ADDR_NIGHT_SHIFT_CAPACTITY - 2012 + 1) * 2 && flag == 0) {
            int nDayShiftCapacity = CToolUnits::toInt16(&pData[0]);
            int nNightShiftCapacity = CToolUnits::toInt16(&pData[(ADDR_NIGHT_SHIFT_CAPACTITY - 2012) * 2]);
            if (nDayShiftCapacity != m_nDayShiftCapacity) {
               m_nDayShiftCapacity = nDayShiftCapacity;
               notifyInt(RX_CODE_DAY_SHIFT_CAPACTITY, nDayShiftCapacity);
            }
            if (nNightShiftCapacity != m_nNightShiftCapacity) {
               m_nNightShiftCapacity = nNightShiftCapacity;
               notifyInt(RX_CODE_NIGHT_SHIFT_CAPACTITY, nNightShiftCapacity);
            }
         }
      };
      pPlc->readData(MC::ZR, 2012, (ADDR_NIGHT_SHIFT_CAPACTITY - 2012 + 1) * 2, funOnReadData);
   }
   {
      int nStartAddress = 1000;
      int nEndAddress = 1200;
      int nReadSize = (nEndAddress - nStartAddress + 1) * 2;
      auto funOnReadData = [this, nStartAddress, nReadSize](IMcChannel* pChannel, int addr, char* pData, unsigned int nDataSize, int flag) -> void {
         if (nDataSize == nReadSize && flag == 0) {
            bool bRun = CToolUnits::toInt16(&pData[(1103 - nStartAddress) * 2]) != 0;      // 启动
            bool bAuto = CToolUnits::toInt16(&pData[(1100 - nStartAddress) * 2]) != 0;      // 自动
            bool bPuase = CToolUnits::toInt16(&pData[(1104 - nStartAddress) * 2]) != 0;      // 暂停
            bool bManual = CToolUnits::toInt16(&pData[(1100 - nStartAddress) * 2]) != 0;   // 手动
            bool bBeep = CToolUnits::toInt16(&pData[(1003 - nStartAddress) * 2]) != 0;      // 静音
            bool bResetting = CToolUnits::toInt16(&pData[(1150 - nStartAddress) * 2]) != 0; // 复位
            bool bStop = CToolUnits::toInt16(&pData[(1114 - nStartAddress) * 2]) != 0;      // ֹͣ
            if (m_bBlBtnsStates[0] != bRun) {
               m_bBlBtnsStates[0] = bRun;
               notifyInt(RX_CODE_ACTIVATE, bRun);
            }
            if (m_bBlBtnsStates[1] != bAuto) {
               m_bBlBtnsStates[1] = bAuto;
               notifyInt(RX_CODE_AUTO, bAuto);
            }
            if (m_bBlBtnsStates[2] != bPuase) {
               m_bBlBtnsStates[2] = bPuase;
               notifyInt(RX_CODE_PUASE, bPuase);
            }
            if (m_bBlBtnsStates[3] != bManual) {
               m_bBlBtnsStates[3] = bManual;
               notifyInt(RX_CODE_MANUAL, bManual);
            }
            if (m_bBlBtnsStates[4] != bBeep) {
               m_bBlBtnsStates[4] = bBeep;
               notifyInt(RX_CODE_BEEP, bBeep);
            }
            if (m_bBlBtnsStates[5] != bResetting) {
               m_bBlBtnsStates[5] = bResetting;
               notifyInt(RX_CODE_RESETTING, bResetting);
            }
            if (m_bBlBtnsStates[6] != bStop) {
               m_bBlBtnsStates[6] = bStop;
               notifyInt(RX_CODE_STOP, bStop);
            }
         }
      };
      pPlc->readData(MC::M, nStartAddress, nReadSize, funOnReadData);
   }
}