DiosUnitComm_V3.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <cassert>
  2. #include "DiosUnitComm_V3.h"
  3. #include "ResDataObject.h"
  4. //-----------------------------------------------------------------------------
  5. // DiosConnect
  6. //-----------------------------------------------------------------------------
  7. namespace DIOS::Dev::Communication
  8. {
  9. DiosConnect::DiosConnect(int millisecond)
  10. {
  11. //m_DataThread = nullptr;
  12. m_tbFunction = nullptr;
  13. m_iWaitSCFTimeOut = millisecond; // 如果子类不重新设置, 默认就是 0.2 秒
  14. }
  15. DiosConnect::~DiosConnect()
  16. {
  17. std::this_thread::sleep_for(100ms);
  18. Disconnect();
  19. }
  20. void DiosConnect::Prepare(std::string DllName)
  21. {
  22. assert(DllName.length());
  23. try {
  24. m_SCF = nsSCF::SCF::FromDLL(DllName.c_str());
  25. m_SCF.OnPassiveDisconnected = [this]()
  26. {
  27. this->m_NewPacket.Set();
  28. };
  29. m_SCF.Queue.OnNewPacket = [this]()
  30. {
  31. this->m_NewPacket.Set();
  32. };
  33. }
  34. catch (const std::exception& exp)
  35. {
  36. printf("get SCF err:%s",exp.what());
  37. }
  38. catch (...)
  39. {
  40. printf("get SCF Mould failed");
  41. }
  42. }
  43. bool DiosConnect::Connect(std::string strProfile, tPacketPredate Pr, _SCF_Transfertype TransferType, DWORD msTimeOut)
  44. {
  45. m_tConnectParam = strProfile;
  46. auto erCode = m_SCF.Connect(m_tConnectParam, Pr, SCF_PACKET_TRANSFER, 3000);
  47. if (erCode != SCF_ERR::SCF_SUCCEED)
  48. {
  49. return false;
  50. }
  51. return true;
  52. }
  53. bool DiosConnect::Run(tbDataFun Dequeue)
  54. {
  55. m_tbFunction = Dequeue;
  56. auto THReadPacket = [this]()
  57. {
  58. auto msWait = std::chrono::milliseconds(m_iWaitSCFTimeOut);
  59. while (true)
  60. {
  61. auto rc = m_NewPacket.Wait(msWait);
  62. if (!m_SCF.isConnected())
  63. return;
  64. if (!rc) continue; //等待超时
  65. if (!m_SCF.isConnected())
  66. return;
  67. while (true) //有数据到达, 全部读完, 然后等待下一个事件
  68. {
  69. if (!m_SCF.isConnected())
  70. return;
  71. if (m_SCF.Queue.isEmpty())
  72. break;
  73. char cMsg[CMD_LEN_MAX];
  74. memset(cMsg, 0, sizeof(cMsg));
  75. int PacketLength = CMD_LEN_MAX;
  76. int len = m_SCF.Queue.Dequeue(cMsg, PacketLength, 20);
  77. if (m_tbFunction != nullptr && len > 0)
  78. m_tbFunction(cMsg, len);
  79. }
  80. }
  81. };
  82. auto THFunc = [this, THReadPacket]()
  83. {
  84. THReadPacket();
  85. m_TID = decltype (m_TID) (); // ! 记得在这里复位 ThreadID
  86. };
  87. if (m_TID == decltype (m_TID) ())
  88. {
  89. auto TH = std::thread(THFunc);
  90. m_TID = TH.get_id();
  91. TH.detach();
  92. }
  93. m_SCF.DecodePack(isActDecodeFun);
  94. return true;
  95. }
  96. bool DiosConnect::DecodePack(bool action)
  97. {
  98. isActDecodeFun = action;
  99. return isActDecodeFun;
  100. }
  101. void DiosConnect::Disconnect()
  102. {
  103. m_SCF.Disconnect();
  104. m_NewPacket.Set();
  105. }
  106. bool DiosConnect::isConnected()
  107. {
  108. return m_SCF.isConnected();
  109. }
  110. bool DiosConnect::TryReconnect()
  111. {
  112. Disconnect();
  113. }
  114. }