LAPTOP-T815PCOQ\25526
2025-01-13 9ea5eea16629a2ab4473727e6b064f83f71aa46c
SourceCode/Bond/BoounionPLC/PLC.cpp
@@ -1,7 +1,10 @@
#include "stdafx.h"
#include "PLC.h"
#include "Log.h"
#include "ToolUnits.h"
#define ADDR_NIGHT_SHIFT_CAPACTITY      1627
void CALLBACK TimerFileProc(UINT uID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{
@@ -31,6 +34,9 @@
   m_nTimerId = 0;
   m_hTimeEvent = nullptr;
   m_bMute = false;
   m_pPlcData = new char[4096];
   m_nVelocityRatio = 0;
   InitializeCriticalSection(&m_criticalSection);
}
CPLC::CPLC(const char* pszName, const char* pszIp, const unsigned int port)
@@ -42,6 +48,7 @@
   m_state = PLCSTATE::READY;
   m_listener.onStateChanged = nullptr;
   m_listener.onMonitorData = nullptr;
   m_listener.onAlarm = nullptr;
   m_nUnHeartBeat = 0;
   m_hTimeEvent = nullptr;
   m_hMcMonitorStop = nullptr;
@@ -49,10 +56,32 @@
   m_mcMonitorThrdaddr = 0;
   m_nTimerId = 0;
   m_hTimeEvent = nullptr;
   m_pPlcData = new char[4096];
   m_nVelocityRatio = 0;
   m_dTactTime = 0.0;
   m_nDayShiftCapacity = 0;
   m_nNightShiftCapacity = 0;
   for (int i = 0; i < 7; i++) {
      m_bBlBtnsStates[i] = false;
   }
   InitializeCriticalSection(&m_criticalSection);
}
CPLC::~CPLC()
{
   if (m_pPlcData != nullptr) {
      delete[] m_pPlcData;
      m_pPlcData = nullptr;
   }
   DeleteCriticalSection(&m_criticalSection);
}
void CPLC::setListener(PLCListener& listener)
{
   m_listener.onStateChanged = listener.onStateChanged;
   m_listener.onMonitorData = listener.onMonitorData;
   m_listener.onAlarm = listener.onAlarm;
}
void CPLC::setWorkDir(const char* pszDir)
@@ -82,7 +111,32 @@
CAlarmMonitor* CPLC::getAlarmMonitor()
{
   return (CAlarmMonitor*)getComponent("PLC(1)");
   return (CAlarmMonitor*)getComponent("警告信息");
}
int CPLC::addMonitor(int id, int beginAddr, int endAddr, MC::SOFT_COMPONENT softComponent, char* pszRecvBuffer)
{
   // 检查是否有重复的
   Lock();
   for (auto& m : m_monitors) {
      if (m.id == id) {
         Unlock();
         return -1;
      }
   }
   MONITOR m;
   memset(&m, 0, sizeof(MONITOR));
   m.id = id;
   m.beginAddr = beginAddr;
   m.readLen = (endAddr - beginAddr + 1) * 2;
   m.softComponent = softComponent;
   m.szRecvBuffer = pszRecvBuffer;
   m.hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
   m_monitors.push_back(m);
   Unlock();
   return 0;
}
void CPLC::init()
@@ -127,11 +181,12 @@
      m_pChannel->setChannelListener(&m_mcChannellistener);
      m_pChannel->setActionInterval(m_nActionInterval);
   }
   addMonitor(MONITOR_ID_ALARM, 10001, 10064, MC::SOFT_COMPONENT::M, &m_pPlcData[600]);
   // 警告监控
   CString strAlarmFile;
   strAlarmFile.Format(_T("%s\\%s\\AlarmList.txt"), m_strWorkDir.c_str(), m_strName.c_str());
   strAlarmFile.Format(_T("%s\\AlarmList.txt"), m_strWorkDir.c_str());
   CAlarmMonitor* pAlarmMonitor = new CAlarmMonitor();
   pAlarmMonitor->setName("警告信息");
   pAlarmMonitor->setDescription("警告信息监控");
@@ -284,9 +339,24 @@
   WaitForSingleObject(monitor.hEvent, INFINITE);
   ResetEvent(monitor.hEvent);
   if (bReadOk) {
      ASSERT(m_listener.onMonitorData);
      onMonitorData(monitor);
   }
}
int CPLC::onMonitorData(MONITOR& monitor)
{
   // 转发到警告模块处理解释数据
   if (monitor.id == MONITOR_ID_ALARM) {
      for (auto c : m_components) {
         c->onData(monitor.id, monitor.szRecvBuffer, monitor.readLen);
      }
   }
   if (m_listener.onMonitorData) {
      m_listener.onMonitorData(this, monitor.id);
   }
   return 0;
}
int CPLC::readWord(MC::SOFT_COMPONENT softComponent, unsigned int addr,
@@ -368,3 +438,125 @@
   }
}
void CPLC::readPLCDataRegularly()
{
   if (!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);
            }
         }
      };
      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);
            }
         }
      };
      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 - 1612 + 1) * 2 && flag == 0) {
            int nDayShiftCapacity = CToolUnits::toInt16(&pData[0]);
            int nNightShiftCapacity = CToolUnits::toInt16(&pData[(ADDR_NIGHT_SHIFT_CAPACTITY - 1612) * 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);
            }
         }
      };
      readData(MC::ZR, 1612, (ADDR_NIGHT_SHIFT_CAPACTITY - 1612 + 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);
            }
         }
      };
      readData(MC::M, nStartAddress, nReadSize, funOnReadData);
   }
}
int CPLC::getVelocityRatio()
{
   return m_nVelocityRatio;
}
double CPLC::getTackTime()
{
   return m_dTactTime;
}
int CPLC::getDayShiftCapacity()
{
   return m_nDayShiftCapacity;
}
int CPLC::getNightShiftCapacity()
{
   return m_nNightShiftCapacity;
}