LAPTOP-SNT8I5JK\Boounion
2025-04-11 92dee0d8070594c351008d02fd9645c9e5de80a7
SourceCode/Bond/Servo/CMaster.cpp
@@ -15,6 +15,16 @@
namespace SERVO {
   CMaster* g_pMaster = NULL;
   unsigned __stdcall ReadBitsThreadFunction(LPVOID lpParam)
   {
      if (g_pMaster != NULL) {
         return g_pMaster->ReadBitsProc();
      }
      return 0;
   }
   void CALLBACK MasterTimerProc(HWND hWnd, UINT nMsg, UINT nTimerid, DWORD dwTime)
   {
      if (g_pMaster != NULL) {
@@ -26,6 +36,10 @@
   {
      m_listener = {nullptr, nullptr, nullptr};
      m_bDataModify = FALSE;
      m_hEventReadBitsThreadExit[0] = ::CreateEvent(NULL, TRUE, FALSE, NULL);
      m_hEventReadBitsThreadExit[1] = ::CreateEvent(NULL, TRUE, FALSE, NULL);
      m_hReadBitsThreadHandle = nullptr;
      m_nReadBitsThreadAddr = 0;
   }
   CMaster::~CMaster()
@@ -34,6 +48,16 @@
         delete item;
      }
      m_listEquipment.clear();
      if (m_hEventReadBitsThreadExit[0] != nullptr) {
         ::CloseHandle(m_hEventReadBitsThreadExit[0]);
         m_hEventReadBitsThreadExit[0] = nullptr;
      }
      if (m_hEventReadBitsThreadExit[1] != nullptr) {
         ::CloseHandle(m_hEventReadBitsThreadExit[1]);
         m_hEventReadBitsThreadExit[1] = nullptr;
      }
   }
   void CMaster::setListener(MasterListener listener)
@@ -98,12 +122,19 @@
      SetTimer(NULL, 1, 250, (TIMERPROC)MasterTimerProc);
      m_hReadBitsThreadHandle = (HANDLE)_beginthreadex(NULL, 0, SERVO::ReadBitsThreadFunction, this,
         0, &m_nReadBitsThreadAddr);
      LOGI("<Master>初始化完成.");
      return 0;
   }
   int CMaster::term()
   {
      SetEvent(m_hEventReadBitsThreadExit[0]);
      ::WaitForSingleObject(m_hEventReadBitsThreadExit[1], INFINITE);
      LOGI("<Master>正在结束程序.");
      for (auto item : m_listEquipment) {
         item->term();
@@ -111,6 +142,38 @@
      saveCache();
      return 0;
   }
   unsigned CMaster::ReadBitsProc()
   {
      while (1) {
         // 待退出信号或时间到
         int nRet = ::WaitForSingleObject(m_hEventReadBitsThreadExit[0], 1000);
         if (nRet == WAIT_OBJECT_0) {
            break;
         }
         for (auto item : m_listEquipment) {
            if (item->getID() == EQ_ID_Bonder1) {
               const StationIdentifier& station = item->getStation();
               MemoryBlock& block = item->getReadBitBlock();
               int nRet = m_cclink.ReadData2(station, (DeviceType)block.type,
                  block.start, block.size, block.buffer);
               if (0 == nRet) {
                  item->onReceiveLBData(block.buffer, block.size);
               }
            }
         }
      }
      SetEvent(m_hEventReadBitsThreadExit[1]);
      // _endthreadex(0);
      TRACE("CMaster::ReadBitsProc 线程退出\n");
      return 0;
   }
@@ -663,7 +726,8 @@
      pEquipment->setID(EQ_ID_Bonder1 + index);
      pEquipment->setName(index == 0 ? "Bonder 1" : "Bonder 2");
      pEquipment->setDescription(index == 0 ? "Bonder 1." : "Bonder 2.");
      pEquipment->setStation(1, index == 0 ? 3 : 4);
      // pEquipment->setStation(1, index == 0 ? 3 : 4);
      pEquipment->setStation(0, 255);
      pEquipment->setReadBitBlock(index == 0 ? 0x4600 : 0x4c00,
         index == 0 ? 0x4c00 : 0x5200);
      addToEquipmentList(pEquipment);
@@ -823,19 +887,23 @@
      // 按一定频率扫描LB数据
      static int i = 0;
      i++;
      /*
      if (i % (4 * 1) == 0) {
         for (auto item : m_listEquipment) {
            const StationIdentifier& station = item->getStation();
            MemoryBlock& block = item->getReadBitBlock();
            if (item->getID() == EQ_ID_Bonder1) {
               const StationIdentifier& station = item->getStation();
               MemoryBlock& block = item->getReadBitBlock();
            int nRet = m_cclink.ReadData2(station, (DeviceType)block.type,
               block.start, block.size, block.buffer);
            if (0 == nRet) {
               item->onReceiveLBData(block.buffer, block.size);
               int nRet = m_cclink.ReadData2(station, (DeviceType)block.type,
                  block.start, block.size, block.buffer);
               if (0 == nRet) {
                  item->onReceiveLBData(block.buffer, block.size);
               }
            }
         }
      }
      */
      // 自动保存缓存