// #pragma once #include "stdafx.h" #include #include "IODeviceWithSCF.h" //----------------------------------------------------------------------------- // IODeviceWithSCF //----------------------------------------------------------------------------- namespace DIOS::Dev::Detail { template void IODriverWithSCF ::Prepare () { super::Prepare (); assert (m_SCFDllName.length ()); m_SCF = nsSCF::SCF::FromDLL (m_SCFDllName.c_str ()); m_SCF.OnPassiveDisconnected = [ this ] () { this->EventCenter->OnPassiveDisconnected(); this->OnNewPacket.Set(); }; m_SCF.Queue.OnNewPacket = [ this ] () { this->OnNewPacket.Set (); }; } template int IODriverWithSCF ::Connect () { auto THReadPacket = [ this ] () { auto msWait = std::chrono::milliseconds (m_msWaitSCFTimeOut); while (true) { auto rc = OnNewPacket.Wait (msWait); if (! m_SCF.isConnected ()) return; if (! rc) continue; // 等待超时 if (!m_SCF.isConnected ()) return; while (true) // 有数据到达, 全部读完, 然后等待下一个事件 { if (!m_SCF.isConnected ()) return; if (m_SCF.Queue.isEmpty ()) break; char cMsg [CMD_LEN_MAX]; memset (cMsg, 0, sizeof (cMsg)); int PacketLength = CMD_LEN_MAX; int len = m_SCF.Queue.Dequeue (cMsg, PacketLength, 20); if (len > 0) Dequeue (cMsg, len); } } }; auto THFunc = [this, THReadPacket] () { THReadPacket (); m_TID = decltype (m_TID) (); // ! 记得在这里复位 ThreadID }; if (m_TID == decltype (m_TID) ()) { auto TH = std::thread (THFunc); m_TID = TH.get_id (); TH.detach (); } return 2; } template void IODriverWithSCF ::Disconnect () { m_SCF.Disconnect (); OnNewPacket.Set (); super::Disconnect (); } template bool IODriverWithSCF ::isConnected () const { return m_SCF.isConnected (); } }