CCOS.Dev.Generator.CPI.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372
  1. // CCOS.Dev.GEN.CPI.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include <assert.h>
  4. #include <functional>
  5. #include <unordered_map>
  6. #include <fstream>
  7. #include <set>
  8. #include <unistd.h>
  9. #include <linux/limits.h>
  10. #include "LogicDevice.h"
  11. #include "CCOS.Dev.Generator.CPI.h"
  12. #include "Helper.JSON.hpp"
  13. #include "LogLocalHelper.h"
  14. #include "Log4CPP.h"
  15. using namespace std::placeholders;
  16. using namespace CCOS::Dev::Detail::Generator;
  17. namespace nsGEN = CCOS::Dev::Detail::Generator;
  18. static const int msTimeOut_Lock = 500;
  19. // Linux 环境下的 SCF 库文件名
  20. static const auto COM_SCFDllName = "libSerialSCF.so";
  21. static const auto TCP_SCFDllName = "libTcpipSCF.so";
  22. // 定义 Sleep 宏(如果头文件中没有定义)
  23. #ifndef Sleep
  24. #define Sleep(ms) std::this_thread::sleep_for(std::chrono::milliseconds(ms))
  25. #endif
  26. //-----------------------------------------------------------------------------
  27. // CPIDevice
  28. //-----------------------------------------------------------------------------
  29. float g_MA_List[] = { 8, 10, 12.5, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 160, 200, 250, 320, 400, 500, 640 };
  30. float g_MAS_List[] = { 1, 1.25, 1.6, 2, 2.5, 3.2, 4, 5, 6.4, 8, 10, 12.5, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 160, 200, 250, 320, 400, 500, 640, 800, 1000 };
  31. float g_MS_List[] = { 1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 160, 200, 250, 320, 400, 500, 640, 800, 1000, 1250, 1600, 2000, 2500, 3200, 4000, 5000, 6400, 8000, 10000 };
  32. int g_AECFIELD_List[] = { 1, 10, 100, 11, 101, 111 };
  33. std::vector <float> gf_MA_List = { 8, 10, 12.5, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 160, 200, 250, 320, 400, 500, 640 };
  34. string GetProcessDirectory()
  35. {
  36. char result[PATH_MAX] = { 0 };
  37. ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
  38. if (count == -1)
  39. {
  40. return "";
  41. }
  42. string fullpath = string(result, count);
  43. string::size_type lastSlash = fullpath.find_last_of('/');
  44. if (lastSlash == string::npos)
  45. {
  46. return "";
  47. }
  48. return fullpath.substr(0, lastSlash);
  49. }
  50. nsGEN::CPIDevice::CPIDevice(std::shared_ptr <IOEventCenter> center, std::shared_ptr<SCFWrapper> SCF, string configfile)
  51. : super(center)
  52. , superGen()
  53. , m_SCF(SCF)
  54. {
  55. assert(EventCenter);
  56. FINFO("version:3.0.0.5");
  57. m_DoseUnit.m_KV.reset(new KVMould(0.0, 40.0, 120.0, 1.0));
  58. m_DoseUnit.m_MA.reset(new MAMould(0.0, 1.0, 1000.0, 0.1));
  59. m_DoseUnit.m_MS.reset(new MSMould(0.0, 1.0, 10000.0, 0.01));
  60. m_DoseUnit.m_MAS.reset(new MASMould(0.0, 0.5, 1000.0, 0.01));
  61. m_DoseUnit.m_Techmode.reset(new TECHMODEMould(0, 0, 2, 1));
  62. m_DoseUnit.m_WS.reset(new WORKSTATIONMould(1, 0, 5, 1));
  63. m_DoseUnit.m_Focus.reset(new FOCUSMould(1, 0, 1, 1));
  64. m_DoseUnit.m_AECField.reset(new AECFIELDMould(0, 0, 111, 1));
  65. m_DoseUnit.m_AECFilm.reset(new AECFILMMould(0, 0, 2, 1));
  66. m_DoseUnit.m_AECDensity.reset(new AECDENSITYMould(0, -4, 4, 1));
  67. m_DoseUnit.m_HE.reset(new TUBEHEATMould(0, 0, 100, 1));
  68. m_DoseUnit.m_PostKV.reset(new POSTKVMould(0.0, 40.0, 120.0, 1.0));
  69. m_DoseUnit.m_PostMA.reset(new POSTMAMould(0.0, 1.0, 1000.0, 0.1));
  70. m_DoseUnit.m_PostMS.reset(new POSTMSMould(0.0, 1.0, 10000.0, 0.01));
  71. m_DoseUnit.m_PostMAS.reset(new POSTMASMould(0.0, 0.5, 1000.0, 0.01));
  72. m_DoseUnit.m_GenSynState.reset(new GENSYNSTATEMould(0, AttrKey::GENERATOR_SYNC_ERR, AttrKey::GENERATOR_SYNC_MAX, 1));
  73. m_DoseUnit.m_GenState.reset(new GENSTATEMould(0, AttrKey::GENERATOR_STATUS_SHUTDOWN, AttrKey::GENERATOR_STATUS_MAX, 1));
  74. m_DoseUnit.m_GenTotalExpNumber.reset(new TOTALEXPNUMMould(0, 0, 9999, 1));
  75. m_DoseUnit.m_GenTotalAcqTimes.reset(new TOTALACQTIMESMould(0, 0, 9999, 1));
  76. m_DoseUnit.m_GenTubeCoolWaitTimes.reset(new TUBECOOLTIMEMould(0, 0, 9999, 1));
  77. m_DoseUnit.m_GenTubeOverLoadNumber.reset(new TUBEOVERLOADNUMMould(0, 0, 9999, 1));
  78. m_DoseUnit.m_GenCurrentExpNumber.reset(new CUREXPNUMMould(0, 0, 9999, 1));
  79. m_DoseUnit.m_ExpMode.reset(new EXPMODEMould(AttrKey::EXPMODE_TYPE::Single));
  80. m_DoseUnit.m_FrameRate.reset(new FRAMERATEMould(0, 0, 16, 1));
  81. m_DoseUnit.m_FLMode.reset(new FLUModeMould(AttrKey::GENERATOR_FLUMode::GENERATOR_FLMODE_NOTFLU));
  82. m_MSGUnit.reset(new nsDetail::MSGUnit(center, nsGEN::GeneratorUnitType));
  83. m_DAP.reset(new DevDAP::DOSEMould(0.0, 0.0, 1000.0, 0.01));
  84. m_hGenPostEvent = LinuxEvent::CreateEvent(LinuxEvent::MANUAL_RESET, false);
  85. m_bExtraFlag = true; // 启用硬件状态查询线程
  86. m_bWithCollimator = false;
  87. m_nDeviceType = 0;
  88. m_fMsLimit = 500;
  89. m_strConfigPath = configfile;
  90. ResDataObject temp;
  91. temp.loadFile(m_strConfigPath.c_str());
  92. m_GenConfig = temp["CONFIGURATION"];
  93. TransJsonText(m_GenConfig);
  94. m_nDeviceType = (int)(m_GenConfig["GenType"]);//默认设置为2. 带有coll的。
  95. if (m_nDeviceType == 2)
  96. {
  97. m_bWithCollimator = true;
  98. }
  99. m_nAECMode = (int)m_GenConfig["AECMODE"];
  100. m_fMsLimit = (float)m_GenConfig["LIMITMS"];//default 500
  101. m_nCtlMode = (int)m_GenConfig["GenCtrlMode"];//default 2
  102. char strStart1[6] = { 0x01,0x35,0x00,0x01,0x01,0x38 };
  103. char strStart2[7] = { 0x01,0x01,0x00,0x02,0x00,0x64,0x73 };
  104. char strStart0a[7] = { 0x01,0x06,0x00,0x02,0x01,0x00,0x0a };
  105. char strStart1b[7] = { 0x01,0x06,0x00,0x02,0x01,0x01,0x0b };
  106. char strStart2c[7] = { 0x01,0x06,0x00,0x02,0x01,0x02,0x0c };
  107. char strStart3d[7] = { 0x01,0x06,0x00,0x02,0x01,0x03,0x0d };
  108. char strStart4e[7] = { 0x01,0x06,0x00,0x02,0x01,0x04,0x0e };
  109. char strStart5f[7] = { 0x01,0x06,0x00,0x02,0x01,0x05,0x0f };
  110. char strStart00c[8] = { 0x01,0x07,0x00,0x03,0x01,0x00,0x00,0x0c };
  111. char strStart01d[8] = { 0x01,0x07,0x00,0x03,0x01,0x00,0x01,0x0d };
  112. char strStart02e[8] = { 0x01,0x07,0x00,0x03,0x01,0x00,0x02,0x0e };
  113. char strStart03f[8] = { 0x01,0x07,0x00,0x03,0x01,0x00,0x03,0x0f };
  114. char strStart040[8] = { 0x01,0x07,0x00,0x03,0x01,0x00,0x04,0x10 };
  115. char strEnd1[8] = { 0x43,0x41,0x30,0x03,0xb7,0x50,0x52,0x30 };
  116. char strEnd2[8] = { 0x03,0xd5,0x01,0x35,0x00,0x01,0x00,0x37 };
  117. if (m_nCtlMode == 2)
  118. {
  119. //SendCommands(strStart1, 6);
  120. HWSend(strStart1);
  121. //::Sleep(50);
  122. HWSend(strStart2);
  123. //::Sleep(50);
  124. HWSend(strStart0a);
  125. //::Sleep(50);
  126. HWSend(strStart1b);
  127. //::Sleep(50);
  128. HWSend(strStart2c);
  129. //::Sleep(50);
  130. HWSend(strStart3d);
  131. //::Sleep(50);
  132. HWSend(strStart4e);
  133. //::Sleep(50);
  134. HWSend(strStart5f);
  135. //::Sleep(50);
  136. HWSend(strStart0a);
  137. //::Sleep(50);
  138. HWSend(strStart00c);
  139. //::Sleep(50);
  140. HWSend(strStart01d);
  141. //::Sleep(50);
  142. HWSend(strStart02e);
  143. //::Sleep(50);
  144. HWSend(strStart03f);
  145. //::Sleep(50);
  146. HWSend(strStart040);
  147. //::Sleep(50);
  148. HWSend(strStart00c);
  149. //::Sleep(50);
  150. HWSend(strStart01d);
  151. //::Sleep(50);
  152. HWSend(strStart02e);
  153. //::Sleep(50);
  154. HWSend(strStart03f);
  155. //::Sleep(50);
  156. HWSend(strStart040);
  157. SetExpEnable();
  158. }
  159. m_bDAPEnable = false;
  160. m_bSaveMSMA = true;
  161. //SynMode default is what?
  162. OnCallBack();
  163. Register();
  164. Reset();
  165. Sleep(500);
  166. HWSend("DS");
  167. HWSend("ET?");
  168. HWSend("ST");
  169. RefreshData();
  170. HWSend("ATU?");//直到收到ATU1为止。
  171. m_bCheckATUStatus = true;
  172. StartHardwareStatusThread();
  173. }
  174. nsGEN::CPIDevice::~CPIDevice()
  175. {
  176. FINFO("========================================");
  177. FINFO("CPIDevice Destructor Starting...");
  178. FINFO("========================================");
  179. FINFO("Stopping hardware status thread...");
  180. m_bExtraFlag = false;
  181. if (m_pHardwareStatusThread.joinable()) {
  182. FINFO("Waiting for hardware status thread to join...");
  183. m_pHardwareStatusThread.join();
  184. FINFO("Hardware status thread joined successfully");
  185. } else {
  186. FINFO("Hardware status thread not joinable, skipping join");
  187. }
  188. FINFO("========================================");
  189. FINFO("CPIDevice Destructor Completed");
  190. FINFO("========================================");
  191. }
  192. std::string nsGEN::CPIDevice::GetGUID() const
  193. {
  194. FINFO("\n===============GetGUID : {$} ===================\n", GeneratorUnitType);
  195. return GeneratorUnitType;
  196. }
  197. void nsGEN::CPIDevice::Register()
  198. {
  199. auto Disp = m_Dispatch.Lock().As();
  200. superGen::Register(Disp);
  201. superGen::RegisterRAD(Disp);
  202. superGen::RegisterAEC(Disp);
  203. superGen::RegisterExpEnable(Disp);
  204. Disp->Get.Push(m_MSGUnit->GetKey().c_str(), [this](std::string& out) { out = m_MSGUnit->JSGet(); return RET_STATUS::RET_SUCCEED; });
  205. auto fun_Clear_DAP = [this](auto a, auto&)
  206. {
  207. return Clear_DAP();
  208. };
  209. Disp->Action.Push("Clear_DAP", fun_Clear_DAP);
  210. auto fun_GetValue_DAP = [this](auto a, auto& b)
  211. {
  212. float value = 0;
  213. RET_STATUS ret = GetValue_DAP(value);
  214. b = ToJSON(value);
  215. return ret;
  216. };
  217. Disp->Action.Push("GetValue_DAP", fun_GetValue_DAP);
  218. auto fun_StartMove = [this](auto a,auto& b)
  219. {
  220. return StartMove();
  221. };
  222. Disp->Action.Push("StartMove", fun_StartMove);
  223. auto fun_EndMove = [this](auto a, auto& b)
  224. {
  225. return EndMove();
  226. };
  227. Disp->Action.Push("EndMove", fun_EndMove);
  228. }
  229. RET_STATUS nsGEN::CPIDevice::IncKV()
  230. {
  231. if (!m_DoseUnit.m_KV->CanInc()) return RET_STATUS::RET_SUCCEED;
  232. return HWSend("KV+");
  233. }
  234. RET_STATUS nsGEN::CPIDevice::DecKV()
  235. {
  236. if (!m_DoseUnit.m_KV->CanDec()) return RET_STATUS::RET_SUCCEED;
  237. return HWSend("KV-");
  238. }
  239. RET_STATUS nsGEN::CPIDevice::SetKV(float value)
  240. {
  241. if (!m_DoseUnit.m_KV->Verify(value)) return RET_STATUS::RET_SUCCEED;
  242. char temp[50] = { 0 };
  243. snprintf(temp, sizeof(temp), "KV%03d", (int)value);
  244. return HWSend(temp);
  245. }
  246. RET_STATUS nsGEN::CPIDevice::IncMA()
  247. {
  248. if (!m_DoseUnit.m_MA->CanInc()) return RET_STATUS::RET_SUCCEED;
  249. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode == 1)
  250. {
  251. FINFO("\n In AEC mode, do not allowed INCMA");
  252. return RET_STATUS::RET_FAILED;
  253. }
  254. return HWSend("MA+");
  255. }
  256. RET_STATUS nsGEN::CPIDevice::DecMA()
  257. {
  258. if (!m_DoseUnit.m_MA->CanDec()) return RET_STATUS::RET_SUCCEED;
  259. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode == 1)
  260. {
  261. FINFO("\n In AEC mode, do not allowed DECMA");
  262. return RET_STATUS::RET_FAILED;
  263. }
  264. return HWSend("MA-");
  265. }
  266. RET_STATUS nsGEN::CPIDevice::SetMA(float value)
  267. {
  268. if (!m_DoseUnit.m_MA->Verify(value)) return RET_STATUS::RET_SUCCEED;
  269. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode == 1)
  270. {
  271. //logfile->WriteLog("In AEC mode, do not allowed SetMA", LOG_INFORMATION, LOG_DEBUG, true);
  272. FINFO("\n In AEC mode, do not allowed SetMA");
  273. return RET_STATUS::RET_FAILED;
  274. }
  275. char temp[50] = { 0 };
  276. snprintf(temp, sizeof(temp), "MA%05d", (int)(value * 10));
  277. return HWSend(temp);
  278. }
  279. RET_STATUS nsGEN::CPIDevice::IncMS()
  280. {
  281. if (!m_DoseUnit.m_MS->CanInc()) return RET_STATUS::RET_SUCCEED;
  282. /*
  283. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)
  284. {
  285. PRINTA_INFO("\n Techmode is MAS, can't inc MS");
  286. return RET_STATUS::RET_FAILED;
  287. }
  288. */
  289. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode != 2)
  290. {
  291. //logfile->WriteLog("In AEC mode, do not allowed SetMA", LOG_INFORMATION, LOG_DEBUG, true);
  292. FINFO("\n In AEC mode, do not allowed INCMS");
  293. return RET_STATUS::RET_FAILED;
  294. }
  295. return HWSend("MS+");
  296. }
  297. RET_STATUS nsGEN::CPIDevice::DecMS()
  298. {
  299. if (!m_DoseUnit.m_MS->CanDec()) return RET_STATUS::RET_SUCCEED;
  300. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode != 2)
  301. {
  302. FINFO("\n Techmode is MAS, can't dec MS");
  303. return RET_STATUS::RET_FAILED;
  304. }
  305. return HWSend("MS-");
  306. }
  307. RET_STATUS nsGEN::CPIDevice::SetMS(float value)
  308. {
  309. if (!m_DoseUnit.m_MA->Verify(value)) return RET_STATUS::RET_SUCCEED;
  310. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode != 2)
  311. {
  312. FINFO("\n In AEC mode, do not allowed SetMS");
  313. return RET_STATUS::RET_FAILED;
  314. }
  315. char temp[50] { 0 };
  316. snprintf(temp, sizeof(temp), "MS%05d", (int)(value * 10));
  317. return HWSend(temp);
  318. }
  319. RET_STATUS nsGEN::CPIDevice::IncMAS()
  320. {
  321. if (!m_DoseUnit.m_MAS->CanInc()) return RET_STATUS::RET_SUCCEED;
  322. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode != 1)
  323. {
  324. FINFO("\n Techmode is not MAS, can't inc MAS");
  325. return RET_STATUS::RET_FAILED;
  326. }
  327. return HWSend("MX+");
  328. }
  329. RET_STATUS nsGEN::CPIDevice::DecMAS()
  330. {
  331. if (!m_DoseUnit.m_MAS->CanDec()) return RET_STATUS::RET_SUCCEED;
  332. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode != 1)
  333. {
  334. FINFO("\n Techmode is not MAS, can't dec MAS");
  335. return RET_STATUS::RET_FAILED;
  336. }
  337. return HWSend("MX-");
  338. }
  339. RET_STATUS nsGEN::CPIDevice::SetMAS(float value)
  340. {
  341. if (!m_DoseUnit.m_MA->Verify(value)) return RET_STATUS::RET_SUCCEED;
  342. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode != 1)
  343. {
  344. FINFO("\n Techmode is not MAS, can't set MAS");
  345. return RET_STATUS::RET_FAILED;
  346. }
  347. char temp[50] = { 0 };
  348. snprintf(temp, sizeof(temp), "MX%05d", (int)(value * 10));
  349. return HWSend(temp);
  350. }
  351. RET_STATUS nsGEN::CPIDevice::SetTechmode(int value)
  352. {
  353. if (!m_DoseUnit.m_Techmode->Verify(value)) return RET_STATUS::RET_SUCCEED;
  354. //char temp[50] = { 0 };
  355. //sprintf_s(temp, "ET%01d", (int)value);
  356. //return HWSend(temp);
  357. //CPI设置ET mode有点特殊
  358. float fms, fma;
  359. m_nPreET = m_DoseUnit.m_Techmode->Get();
  360. char temp[50] = { 0 };
  361. snprintf(temp, sizeof(temp), "ET%01d", (int)value);
  362. HWSend(temp);
  363. if (!m_bSaveMSMA)
  364. return RET_STATUS::RET_FAILED;
  365. if (value == AttrKey::TECHMODE_V2TYPE::ET_TIME)
  366. {
  367. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)//NOTE: use m_DoseUnit.m_Techmode->Get() not accurate. m_nPreET more better.!!!!
  368. {
  369. // mas - time
  370. if ((m_fPreMA > 0.1))
  371. {
  372. Sleep(50);
  373. SetMA(m_fPreMA);
  374. }
  375. Sleep(50);
  376. SetMS(m_PrefMS);
  377. }
  378. else if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC)
  379. {
  380. // aec - time
  381. if (m_bSaveMSMA)
  382. {
  383. fms = m_PrefMS;
  384. fma = m_fPreMA;
  385. Sleep(50);
  386. SetMS(fms);
  387. Sleep(50);
  388. SetMA(fma);
  389. }
  390. }
  391. }
  392. else if (value == AttrKey::TECHMODE_V2TYPE::ET_MAS) //mas
  393. {
  394. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_TIME)
  395. {
  396. // time - mas save
  397. if (m_bSaveMSMA)
  398. {
  399. m_PrefMS = m_DoseUnit.m_MS->Get();
  400. m_fPreMA = m_DoseUnit.m_MA->Get();
  401. }
  402. }
  403. else if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC)
  404. {
  405. // aec - mas
  406. if (m_bSaveMSMA)
  407. {
  408. fms = m_PrefMS;
  409. fma = m_fPreMA;
  410. }
  411. SetMAS(m_PrefMS * m_fPreMA / 1000); //mas mode : set dose to ma * ms
  412. }
  413. }
  414. else if (value == AttrKey::TECHMODE_V2TYPE::ET_AEC) //aec
  415. {
  416. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_TIME)
  417. {
  418. // time - aec save
  419. if (m_bSaveMSMA)
  420. {
  421. m_PrefMS = m_DoseUnit.m_MS->Get();
  422. m_fPreMA = m_DoseUnit.m_MA->Get();
  423. }
  424. }
  425. else if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)
  426. {
  427. // mas - aec
  428. if ((m_fPreMA > 0.1))
  429. {
  430. Sleep(50);
  431. if (m_nAECMode != 1)
  432. SetMA(m_fPreMA);
  433. }
  434. }
  435. Sleep(50);
  436. if (m_nAECMode == 2)
  437. SetMS(m_fMsLimit);
  438. //ysj++:不知道为什么要这么干,不过此处需要一个设置MS的语句
  439. //if (m_nWS == GEN_WS_TOMO)
  440. //{
  441. // if (m_nAECMode == 2)
  442. // SetMS(m_PrefMS);
  443. //}
  444. //else
  445. //{
  446. // if (m_nAECMode == 2)
  447. // SetMS(m_fMsLimit);
  448. //}
  449. }
  450. return RET_STATUS::RET_SUCCEED;
  451. }
  452. RET_STATUS nsGEN::CPIDevice::SetFocus(int value)
  453. {
  454. if (!m_DoseUnit.m_Focus->Verify(value)) return RET_STATUS::RET_SUCCEED;
  455. char temp[50] = { 0 };
  456. snprintf(temp, sizeof(temp), "FO%01d", (int)value);
  457. return HWSend(temp);
  458. }
  459. RET_STATUS nsGEN::CPIDevice::SetAECDensity(int value)
  460. {
  461. FINFO("Enter SetAECDensity {$}", value);
  462. if (!m_DoseUnit.m_AECDensity->Verify(value)) return RET_STATUS::RET_SUCCEED;
  463. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC) return RET_STATUS::RET_FAILED;
  464. int nAECDensity = m_DoseUnit.m_AECDensity->Get();
  465. if (value < m_DoseUnit.m_AECDensity->Get())
  466. {
  467. if (value < m_DoseUnit.m_AECDensity->Get() - 1)
  468. {
  469. nAECDensity = (int)value;
  470. }
  471. else
  472. {
  473. nAECDensity = nAECDensity - 1;
  474. }
  475. }
  476. else if (value > m_DoseUnit.m_AECDensity->Get())
  477. {
  478. if (value > m_DoseUnit.m_AECDensity->Get() + 1)
  479. {
  480. nAECDensity = (int)value;
  481. }
  482. else
  483. {
  484. nAECDensity = nAECDensity + 1;
  485. }
  486. }
  487. char temp[50] = { 0 };
  488. if (nAECDensity >= 0)
  489. {
  490. snprintf(temp, sizeof(temp), "FN+%01d", (int)nAECDensity);
  491. }
  492. else
  493. {
  494. snprintf(temp, sizeof(temp), "FN-%01d", (int)nAECDensity);
  495. }
  496. return HWSend(temp);
  497. }
  498. RET_STATUS nsGEN::CPIDevice::SetAECField(int value)
  499. {
  500. if (!m_DoseUnit.m_AECField->Verify(value)) return RET_STATUS::RET_SUCCEED;
  501. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS) return RET_STATUS::RET_FAILED;
  502. char temp[50] = { 0 };
  503. snprintf(temp, sizeof(temp), "FI%03d", (int)value);
  504. return HWSend(temp);
  505. }
  506. RET_STATUS nsGEN::CPIDevice::SetAECFilm(int value)
  507. {
  508. if (!m_DoseUnit.m_AECFilm->Verify(value)) return RET_STATUS::RET_SUCCEED;
  509. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS) return RET_STATUS::RET_FAILED;
  510. char temp[50] = { 0 };
  511. snprintf(temp, sizeof(temp), "FS%03d", (int)value);
  512. return HWSend(temp);
  513. }
  514. RET_STATUS nsGEN::CPIDevice::SetWS(const string value)
  515. {
  516. FINFO("Enter SetWS {$}", value);
  517. int tempws = 0;
  518. if (value == "Table") tempws = (int)m_GenConfig["WSTable"];
  519. else if (value == "Wall") tempws = (int)m_GenConfig["WSWall"];
  520. else if (value == "Direct") tempws = (int)m_GenConfig["WSConventional"];
  521. else if (value == "Free") tempws = (int)m_GenConfig["WSFree"];
  522. else if (value == "Tomo") tempws = (int)m_GenConfig["WSTomo"];
  523. char temp[50] = { 0 };
  524. snprintf(temp, sizeof(temp), "WS%01d", tempws);
  525. return HWSend(temp);
  526. }
  527. RET_STATUS nsGEN::CPIDevice::SetAPR(const _tAPRArgs& t)
  528. {
  529. m_t = t;
  530. FINFO("*********************Enter SetAPR**********************");
  531. FINFO("APR:t.fKV={$}, t.fMA={$}, t.fMAS={$}, t.fMS={$}, t.nAECDensity={$}, t.nAECField={$}, t.nAECFilm={$}, t.nFocus={$}, t.nTechmode={$}", t.fKV, t.fMA, t.fMAS, t.fMS, t.nAECDensity, t.nAECField, t.nAECFilm, t.nFocus, t.nTechmode);
  532. /// //////////////////////////////////////////////begin
  533. //SetWS(); //ysj++:缺少传递ws的apr结构体
  534. //GetTechMode(); //ysj++:理论上,init时,RR指令已经获取ET了。
  535. HWSend("ET?");
  536. if (m_DoseUnit.m_Focus->Get() == 0)
  537. {
  538. SetFocus(t.nFocus);
  539. }
  540. /*
  541. * //not add this logic: because struct _tAPRArgs ,not include WS item.
  542. if ((nWS != GEN_WS_TABLE) && (nWS != GEN_WS_WALL) && (nET == GEN_ET_AEC))
  543. {
  544. if (m_nET != GEN_ET_AEC)
  545. {
  546. nET = m_nET;
  547. }
  548. else
  549. {
  550. nET = GEN_ET_MATIME;
  551. }
  552. }
  553. */
  554. m_PrefMS = t.fMS;
  555. m_fPreMA = t.fMA;
  556. m_bSaveMSMA = false;
  557. if (t.nTechmode == AttrKey::TECHMODE_V2TYPE::ET_AEC)
  558. {
  559. // aec
  560. SetTechmode(t.nTechmode);
  561. SetAECField(t.nAECField);
  562. SetAECDensity(t.nAECDensity);
  563. if (t.nAECFilm == 0)
  564. {
  565. SetAECFilm(10);
  566. }
  567. else
  568. {
  569. SetAECFilm(t.nAECFilm);
  570. }
  571. if (m_nAECMode == 2)
  572. {
  573. if (t.fKV > m_DoseUnit.m_KV->Get())
  574. {
  575. SetMA(t.fMA);
  576. SetKV(t.fKV);
  577. }
  578. else
  579. {
  580. if (t.fMA < m_DoseUnit.m_MA->Get())
  581. {
  582. SetMA(t.fMA);
  583. SetKV(t.fKV);
  584. }
  585. else
  586. {
  587. SetKV(t.fKV);
  588. SetMA(t.fMA);
  589. }
  590. }
  591. Sleep(50);
  592. SetMS(m_fMsLimit);//int V2 mode.m_fMsLimit is set by UI.but in CCOS2.0 it set by config file.
  593. }
  594. else if (m_nAECMode == 1)
  595. {
  596. if (t.fKV > m_DoseUnit.m_KV->Get())
  597. {
  598. SetMA(t.fMA);
  599. Sleep(50);
  600. SetKV(t.fKV);
  601. }
  602. else
  603. {
  604. if (t.fMA < m_DoseUnit.m_MA->Get())
  605. {
  606. SetMA(t.fMA);
  607. Sleep(50);
  608. SetKV(t.fKV);
  609. }
  610. else
  611. {
  612. SetKV(t.fKV);
  613. Sleep(50);
  614. SetMA(t.fMA);
  615. }
  616. }
  617. Sleep(50);
  618. SetMAS(t.fMAS);
  619. }
  620. else
  621. {
  622. if (t.fMA < m_DoseUnit.m_MA->Get())
  623. {
  624. SetMA(t.fMA);
  625. Sleep(50);
  626. SetKV(t.fKV);
  627. }
  628. else
  629. {
  630. SetKV(t.fKV);
  631. Sleep(50);
  632. SetMA(t.fMA);
  633. }
  634. }
  635. }
  636. else if (t.nTechmode == AttrKey::TECHMODE_V2TYPE::ET_MAS)
  637. {
  638. // mas
  639. SetTechmode(t.nTechmode);
  640. SetKV(t.fKV);
  641. const float EPSINON = 0.000001;
  642. if ((t.fMAS >= -EPSINON) && (t.fMAS <= EPSINON))
  643. {
  644. SetMAS(t.fMA * t.fMS / 1000);
  645. }
  646. else
  647. {
  648. SetMAS(t.fMAS);
  649. }
  650. }
  651. else if(t.nTechmode == AttrKey::TECHMODE_V2TYPE::ET_TIME)
  652. {
  653. // time
  654. SetTechmode(t.nTechmode);
  655. Sleep(50);
  656. if (t.fKV > m_DoseUnit.m_KV->Get())
  657. {
  658. SetMA(t.fMA);
  659. Sleep(50);
  660. SetKV(t.fKV);
  661. }
  662. else
  663. {
  664. if (t.fMA < m_DoseUnit.m_MA->Get())
  665. {
  666. SetMA(t.fMA);
  667. Sleep(50);
  668. SetKV(t.fKV);
  669. }
  670. else
  671. {
  672. SetKV(t.fKV);
  673. Sleep(50);
  674. SetMA(t.fMA);
  675. }
  676. }
  677. Sleep(80);
  678. SetMS(t.fMS);
  679. }
  680. if (m_DoseUnit.m_Focus->Get() == 1)
  681. {
  682. SetFocus(t.nFocus);
  683. }
  684. m_bSaveMSMA = true;
  685. ///////////////////////end
  686. return RET_STATUS::RET_SUCCEED;
  687. }
  688. RET_STATUS nsGEN::CPIDevice::QueryDAP()
  689. {
  690. if (m_bDAPEnable)
  691. {
  692. return HWSend("DA");
  693. }
  694. return RET_STATUS::RET_SUCCEED;
  695. }
  696. RET_STATUS nsGEN::CPIDevice::QueryHE(int& value)
  697. {
  698. value = m_DoseUnit.m_HE->Get();
  699. return RET_STATUS::RET_SUCCEED;
  700. }
  701. RET_STATUS nsGEN::CPIDevice::QueryPostKV(float& value)
  702. {
  703. value = m_DoseUnit.m_PostKV->Get();
  704. return RET_STATUS::RET_SUCCEED;
  705. }
  706. RET_STATUS nsGEN::CPIDevice::QueryPostMA(float& value)
  707. {
  708. value = m_DoseUnit.m_PostMA->Get();
  709. return RET_STATUS::RET_SUCCEED;
  710. }
  711. RET_STATUS nsGEN::CPIDevice::QueryPostMS(float& value)
  712. {
  713. value = m_DoseUnit.m_PostMS->Get();
  714. return RET_STATUS::RET_SUCCEED;
  715. }
  716. RET_STATUS nsGEN::CPIDevice::QueryPostMAS(float& value)
  717. {
  718. value = m_DoseUnit.m_PostMAS->Get();
  719. return RET_STATUS::RET_SUCCEED;
  720. }
  721. RET_STATUS nsGEN::CPIDevice::Clear_DAP()
  722. {
  723. if (m_bDAPEnable)
  724. {
  725. return HWSend("DZ");
  726. }
  727. else
  728. return RET_STATUS::RET_SUCCEED;
  729. }
  730. RET_STATUS nsGEN::CPIDevice::GetValue_DAP(float& value)
  731. {
  732. value = m_DAP->Get();
  733. return RET_STATUS::RET_SUCCEED;
  734. }
  735. RET_STATUS nsGEN::CPIDevice::StartMove()
  736. {
  737. FINFO("Enter startMove");
  738. FINFO("end startmove");
  739. return RET_STATUS::RET_SUCCEED;
  740. }
  741. RET_STATUS nsGEN::CPIDevice::EndMove()
  742. {
  743. FINFO("Enter endmove");
  744. FINFO("end EndMove");
  745. return RET_STATUS::RET_SUCCEED;
  746. }
  747. RET_STATUS nsGEN::CPIDevice::SetCollimatorSize(int height, int width)
  748. {
  749. if (!m_bWithCollimator)
  750. {
  751. return RET_STATUS::RET_SUCCEED;
  752. }
  753. char temp[50] = { 0 };
  754. snprintf(temp, sizeof(temp), "ACH%05d", (int)(height * 100));
  755. HWSend(temp);
  756. snprintf(temp, sizeof(temp), "ACW%05d", (int)(width * 100));
  757. HWSend(temp);
  758. return RET_STATUS::RET_SUCCEED;
  759. }
  760. RET_STATUS nsGEN::CPIDevice::SetFilter(int filterType)
  761. {
  762. if (!m_bWithCollimator)
  763. {
  764. return RET_STATUS::RET_SUCCEED;
  765. }
  766. if (filterType > 3)
  767. {
  768. filterType -= 3;
  769. }
  770. char temp[50] = { 0 };
  771. snprintf(temp, sizeof(temp), "FIL%02d", (int)(filterType));
  772. return HWSend(temp);
  773. }
  774. RET_STATUS nsGEN::CPIDevice::SetGenSynState(int value)
  775. {
  776. if (AttrKey::GENERATOR_RAD_XRAYON == value) //��ͬ��,�յ�XR1ʱ����ϵͳ���ΪXRAYON״̬�������ô˴���Ȼ�������̻ظ�XR1����������Ȼ����������ߡ�
  777. {
  778. FINFO("SetGenSynState be call.this is soft syn mode.");
  779. HWSend("XR1");
  780. m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_XRAYON);
  781. FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
  782. }
  783. return RET_STATUS::RET_SUCCEED;
  784. }
  785. RET_STATUS nsGEN::CPIDevice::SetGenState(int value)
  786. {
  787. return RET_STATUS::RET_SUCCEED;
  788. }
  789. RET_STATUS nsGEN::CPIDevice::SetExpMode(std::string value)
  790. {
  791. FINFO("Enter SetExpMode...{$}",value);
  792. m_DoseUnit.m_ExpMode->Update(value);
  793. return RET_STATUS::RET_SUCCEED;
  794. }
  795. RET_STATUS nsGEN::CPIDevice::SetFLFMode(std::string value)
  796. {
  797. return RET_STATUS::RET_SUCCEED;
  798. //char temp[50]{ 0 };
  799. //sprintf_s(temp, "RTS%s",value.c_str());
  800. //return HWSend(temp);
  801. }
  802. RET_STATUS nsGEN::CPIDevice::SetEXAMMode(std::string value)
  803. {
  804. return RET_STATUS::RET_SUCCEED;
  805. }
  806. RET_STATUS nsGEN::CPIDevice::SetFrameRate(float frameRate)
  807. {
  808. m_DoseUnit.m_FrameRate->Update(frameRate);
  809. return RET_STATUS::RET_SUCCEED;
  810. }
  811. RET_STATUS nsGEN::CPIDevice::SetRPS(int rps)
  812. {
  813. return RET_STATUS::RET_SUCCEED;
  814. }
  815. RET_STATUS nsGEN::CPIDevice::RefreshData()
  816. {
  817. if (m_nDeviceType == 1)
  818. {
  819. HWSend("RRX");
  820. }
  821. else
  822. {
  823. //HWSend("RR");
  824. HWSend("KV?");
  825. HWSend("MA?");
  826. HWSend("MS?");
  827. HWSend("MX?");
  828. }
  829. return RET_STATUS::RET_SUCCEED;
  830. }
  831. RET_STATUS nsGEN::CPIDevice::SetExpEnable()
  832. {
  833. m_bExpEnable = true;
  834. return RET_STATUS::RET_SUCCEED;
  835. }
  836. RET_STATUS nsGEN::CPIDevice::SetExpDisable()
  837. {
  838. m_bExpEnable = false;
  839. return RET_STATUS::RET_SUCCEED;
  840. }
  841. RET_STATUS nsGEN::CPIDevice::Reset()
  842. {
  843. return HWSend("RE");
  844. }
  845. RET_STATUS nsGEN::CPIDevice::SetCollimatorDev(OemCollimator* dev)
  846. {
  847. if (dev)
  848. m_pCollDev.reset(dev);
  849. return RET_STATUS::RET_SUCCEED;
  850. }
  851. RET_STATUS nsGEN::CPIDevice::SetMechDev(OemMechanical* dev)
  852. {
  853. //if (dev)
  854. //m_pMechDev.reset(dev);
  855. return RET_STATUS::RET_SUCCEED;
  856. }
  857. RET_STATUS nsGEN::CPIDevice::SetDapDev(OemDap* dev)
  858. {
  859. if (dev)
  860. m_pDapDev.reset(dev);
  861. return RET_STATUS::RET_SUCCEED;
  862. }
  863. RET_STATUS nsGEN::CPIDevice::HWSend(const char* strCommand, int nTimeOut)
  864. {
  865. if (!m_SCF) {
  866. FINFO("Failed - Serial communication interface not initialized");
  867. if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN))
  868. {
  869. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  870. FINFO("Generator status updated to {$}", static_cast<int>(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN));
  871. }
  872. return RET_STATUS::RET_FAILED;
  873. }
  874. if (!m_SCF->IsConnected())
  875. {
  876. FERROR("Failed - Device not connected");
  877. if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN))
  878. {
  879. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  880. FINFO("Generator status updated to {$}", static_cast<int>(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN));
  881. }
  882. return RET_STATUS::RET_FAILED;
  883. }
  884. // 计算命令长度
  885. int cmdLen = strlen(strCommand);
  886. // 检查缓冲区大小,数据包 = 命令 + ETX(1字节) + CheckSum(1字节)
  887. const int maxCmdLen = 256; // 增大缓冲区以支持更长的命令
  888. if (cmdLen > maxCmdLen - 2)
  889. {
  890. FERROR("Command too long: {$} bytes, max allowed: {$} bytes\n", cmdLen, maxCmdLen - 2);
  891. return RET_STATUS::RET_FAILED;
  892. }
  893. char strSendCommand[maxCmdLen] = { 0 };
  894. // 计算校验和
  895. int tmpSum = 0;
  896. for (int i = 0; i < cmdLen; i++)
  897. {
  898. tmpSum += (unsigned char)strCommand[i];
  899. }
  900. char checkSum = char(tmpSum + 3);
  901. // 构建数据包:命令 + ETX + CheckSum
  902. memcpy(strSendCommand, strCommand, cmdLen);
  903. strSendCommand[cmdLen] = 0x03;
  904. strSendCommand[cmdLen + 1] = checkSum;
  905. int totalLen = cmdLen + 2; // 实际发送的总长度
  906. // 打印完整数据包的十六进制和ASCII(含ETX和CheckSum)便于调试
  907. FINFO("==OUT== CMD={$} LEN={$}", strCommand, totalLen);
  908. // 使用 SCFWrapper 发送数据
  909. unsigned int retLength = 0;
  910. if (m_SCF->Lock(1000) == WAIT_OBJECT_0)
  911. {
  912. int result = m_SCF->SendPacket(strSendCommand, (unsigned int)totalLen, (unsigned int)nTimeOut, retLength);
  913. m_SCF->Unlock();
  914. if (result != SCF_SUCCEED)
  915. {
  916. FERROR("SendPacket failed, result={$}", result);
  917. return RET_STATUS::RET_FAILED;
  918. }
  919. }
  920. else
  921. {
  922. FERROR("Failed to lock SCF");
  923. return RET_STATUS::RET_FAILED;
  924. }
  925. Sleep(nTimeOut);
  926. return RET_STATUS::RET_SUCCEED;
  927. }
  928. //-----------------------------------------------------------------------------
  929. // ProcessCmd
  930. //-----------------------------------------------------------------------------
  931. void nsGEN::CPIDevice::FireNotify(std::string key, std::string content)
  932. {
  933. EventCenter->OnNotify(1, key, content);
  934. }
  935. struct tFrameMapping
  936. {
  937. static const int MaxLen = 5; // ǰ׺���ܳ����� 5 ���ַ� !
  938. using cbFun = std::function <void(const char*, int)>;
  939. char strHead[MaxLen];
  940. int NbOfCharOfHead;
  941. cbFun fun;
  942. tFrameMapping(const char* str, int len, cbFun f)
  943. {
  944. assert(len < MaxLen); //len���ֻ����4
  945. //strHead[0] = 0x02; //STX
  946. //for (int i = 0; i < len; i++) //��strHead��ֵ
  947. // strHead[i + 1] = str[i];
  948. //NbOfCharOfHead = len + 1;
  949. //fun = f;
  950. //assert(len < MaxLen);
  951. for (int i = 0; i < len; i++)
  952. strHead[i] = str[i];
  953. NbOfCharOfHead = len;
  954. fun = f;
  955. }
  956. };
  957. static std::list <tFrameMapping> arFrame;
  958. static bool DecodeFrame(const char* strFrame, int length);
  959. //KV075 MA05000 MS00800 MX00400
  960. // ==IN==:KV075 MA05000 MS00800 MX00400
  961. void nsGEN::CPIDevice::OnCallBack()
  962. {
  963. auto HWNotProcess = [](const char* value, int length) -> void
  964. {
  965. FINFO("\n This commands didn't need to process!\n");
  966. };
  967. auto HWKV = [this](const char* value, int length) -> void
  968. {
  969. assert(value);
  970. if (m_DoseUnit.m_KV->Update(atof(value)))
  971. FireNotify(AttrKey::KV, m_DoseUnit.m_KV->JSGet());
  972. };
  973. auto HWMAS = [this](const char* value, int length)
  974. {
  975. assert(value);
  976. float fmas = atof(value) / 10.0;
  977. //if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_TIME)
  978. //{
  979. // //if (m_DoseUnit.m_MAS->Update(0))
  980. // // FireNotify(AttrKey::MAS, m_DoseUnit.m_MAS->JSGet());
  981. //}
  982. //else
  983. //{
  984. // if (m_DoseUnit.m_MAS->Update(fmas))
  985. // FireNotify(AttrKey::MAS, m_DoseUnit.m_MAS->JSGet());
  986. //}
  987. if (m_DoseUnit.m_MAS->Update(fmas))
  988. FireNotify(AttrKey::MAS, m_DoseUnit.m_MAS->JSGet());
  989. };
  990. auto HWMA = [this](const char* value, int length)
  991. {
  992. assert(value);
  993. float fma = atof(value) / 10.0;
  994. if (m_DoseUnit.m_MA->Update(fma))
  995. FireNotify(AttrKey::MA, m_DoseUnit.m_MA->JSGet());
  996. };
  997. auto HWMS = [this](const char* value, int length)
  998. {
  999. assert(value);
  1000. if (m_DoseUnit.m_MS->Update(atof(value) / 10.0))
  1001. FireNotify(AttrKey::MS, m_DoseUnit.m_MS->JSGet());
  1002. };
  1003. auto HWFocus = [this](const char* value, int length)
  1004. {
  1005. assert(value);
  1006. if (m_DoseUnit.m_Focus->Update(atoi(value)))
  1007. FireNotify(AttrKey::FOCUS, m_DoseUnit.m_Focus->JSGet());
  1008. };
  1009. auto HWTechmode = [this](const char* value, int length)
  1010. {
  1011. assert(value);
  1012. if (m_DoseUnit.m_Techmode->Update(atoi(value)))
  1013. FireNotify(AttrKey::TECHMODE, m_DoseUnit.m_Techmode->JSGet());
  1014. };
  1015. auto HWAECField = [this](const char* value, int length)
  1016. {
  1017. assert(value);
  1018. int nvalue = atoi(value);
  1019. if (m_DoseUnit.m_AECField->Update(nvalue))
  1020. FireNotify(AttrKey::AECFIELD, m_DoseUnit.m_AECField->JSGet());
  1021. };
  1022. auto HWAECFilm = [this](const char* value, int length)
  1023. {
  1024. assert(value);
  1025. if (m_DoseUnit.m_AECFilm->Update(atoi(value)))
  1026. FireNotify(AttrKey::AECFILM, m_DoseUnit.m_AECFilm->JSGet());
  1027. };
  1028. auto HWAECDensity = [this](const char* value, int length)
  1029. {
  1030. assert(value);
  1031. if (m_DoseUnit.m_AECDensity->Update(atoi(value)))
  1032. FireNotify(AttrKey::AECDENSITY, m_DoseUnit.m_AECDensity->JSGet());
  1033. };
  1034. auto HWWS = [this](const char* value, int length)
  1035. {
  1036. assert(value);
  1037. //ysj++:理论上 此处得到WS的值,需要转为对应的数字,转换后的数字,能给UI一致,从而得出正确的ws并显示出来。
  1038. int nValue = atoi(value);
  1039. if (m_DoseUnit.m_WS->Update(nValue))
  1040. {
  1041. FireNotify(m_DoseUnit.m_WS->GetKey(), m_DoseUnit.m_WS->JSGet());
  1042. }
  1043. };
  1044. auto HWPR = [this](const char* value, int length)
  1045. {
  1046. assert(value);
  1047. int nValue = atoi(value);
  1048. if (nValue == 2)
  1049. {
  1050. m_bInExpState = true;
  1051. m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_READY);
  1052. m_hGenPostEvent->ResetEvent();
  1053. HWSend("PR2");
  1054. }
  1055. else if (nValue == 1)
  1056. {
  1057. m_bInExpState = true;
  1058. m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_PREPARE);
  1059. HWSend("PR1");
  1060. }
  1061. else if (nValue == 0)
  1062. {
  1063. m_bInExpState = false;;
  1064. m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_OFF);
  1065. HWSend("PR0");
  1066. }
  1067. FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
  1068. };
  1069. auto HWXR = [this](const char* value, int length)
  1070. {
  1071. assert(value);
  1072. int nValue = atoi(value);
  1073. if (nValue == 1)
  1074. {
  1075. if ((this->m_nCtlMode == 2) && m_bExpEnable)
  1076. {
  1077. if (EXPOSURE_SOFTWARE_NOSYNBOX == (int)m_GenConfig["SynMode"] || EXPOSURE_NOSYNBOX_DIRCETCONNECT_DETECTOR_GEN == (int)m_nCtlMode["SynMode"])
  1078. {
  1079. FINFO("recv XR1 ,in nosynbox or in derect mode ,do nothing,and wait invoke be call,then send XR1,and send XRAYON msg.");
  1080. //m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_XRAYON);// ysj++ 我们认为此处不应该发这个消息,准确的时机 应该是invoke被调用的时候,
  1081. }
  1082. if (EXPOSURE_SOFTWARE_NOSYNBOX != (int)m_GenConfig["SynMode"])
  1083. {
  1084. FINFO("recv XR1 ,in hardware or in derect mode ,so response XR1.");
  1085. HWSend("XR1");
  1086. }
  1087. }
  1088. else
  1089. {
  1090. if (2 != this->m_nCtlMode)
  1091. {
  1092. if (EXPOSURE_SOFTWARE_NOSYNBOX == (int)m_GenConfig["SynMode"] || EXPOSURE_NOSYNBOX_DIRCETCONNECT_DETECTOR_GEN == (int)m_nCtlMode["SynMode"])
  1093. {
  1094. //::SendMessage(this->m_hWnd, MSG_SYN_STATUS, SYN_GEN_EIPON, 1); //------------------------------------------------ò��û����ͬ������Ϣ
  1095. FINFO("recv XR1 : ctrlmode !=2, in No SYNbox or in directly mode, Send Msg SYN_GEN_EIPON");
  1096. }
  1097. }
  1098. }
  1099. }
  1100. else if (nValue == 0)
  1101. {
  1102. if (m_nCtlMode == 2)
  1103. {
  1104. HWSend("XR0");
  1105. }
  1106. HWSend("DA");
  1107. HWSend("AP?");
  1108. HWSend("AT?");
  1109. m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_OFF);
  1110. }
  1111. FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
  1112. };
  1113. auto HWAPDOSE = [this](const char* value, int length)//post mas
  1114. {
  1115. assert(value);
  1116. if(m_DoseUnit.m_PostMAS->Update(atof(value) / 10.0))
  1117. FireNotify(m_DoseUnit.m_PostMAS->GetKey(), m_DoseUnit.m_PostMAS->JSGet());
  1118. //m_hGenPostEvent->SetEvent();
  1119. };
  1120. auto HWKW = [this](const char* value, int length)
  1121. {
  1122. assert(value);
  1123. FINFO("Recv KW={$}", atoi(value));
  1124. };
  1125. auto HWATDOSE = [this](const char* value, int length)
  1126. {
  1127. assert(value);
  1128. if(m_DoseUnit.m_PostMS->Update(atof(value)/10.0))
  1129. FireNotify(m_DoseUnit.m_PostMS->GetKey(), m_DoseUnit.m_PostMS->JSGet());
  1130. //m_hGenPostEvent->SetEvent();
  1131. };
  1132. auto HWDAP = [this](const char* value, int length)
  1133. {
  1134. assert(value);
  1135. FINFO("Recv DAP ={$}", atof(value) / 100.0);
  1136. //ysj++:理论上此处应该调用DAP设备。
  1137. if (m_pDapDev)
  1138. {
  1139. m_pDapDev->UpdateMammo_DAP(atof(value) / 100.0);
  1140. }
  1141. };
  1142. auto HWERROR = [this](const char* value, int length)
  1143. {
  1144. assert(value);
  1145. int nValue = atoi(value);
  1146. if (nValue != 0)
  1147. {
  1148. char ErrorCode[10]{ "" };
  1149. snprintf(ErrorCode, sizeof(ErrorCode), "CPI_ERR_%d", nValue);
  1150. int level = 1;
  1151. m_MSGUnit->AddErrorMessage(ErrorCode, level, "");
  1152. //去配置文件中找error对应的信息
  1153. }
  1154. else
  1155. {
  1156. int level = 1;
  1157. char ErrorCode[10]{ "" };
  1158. m_MSGUnit->DelErrorMessage(ErrorCode, level, "");
  1159. }
  1160. };
  1161. auto HWWARN = [this](const char* value, int length)
  1162. {
  1163. assert(value);
  1164. int nValue = atoi(value);
  1165. if (nValue != 0)
  1166. {
  1167. char WarnCode[20]{ "" };
  1168. snprintf(WarnCode, sizeof(WarnCode), "CPI_WARN_%d", nValue);
  1169. int level = 1;
  1170. m_MSGUnit->AddWarnMessage(WarnCode, level, "");
  1171. }
  1172. else
  1173. {
  1174. int level = 1;
  1175. char WarnCode[10]{ "" };
  1176. m_MSGUnit->AddWarnMessage(WarnCode, level, "");
  1177. }
  1178. };
  1179. auto HWEHE = [this](const char* value, int length)
  1180. {
  1181. assert(value);
  1182. if(m_DoseUnit.m_HE->Update(atoi(value)))
  1183. FireNotify(m_DoseUnit.m_HE->GetKey(), m_DoseUnit.m_HE->JSGet());
  1184. };
  1185. auto HWFLK = [this](const char* value, int length)
  1186. {
  1187. assert(value);
  1188. if (m_DoseUnit.m_KV->Update(atof(value)))
  1189. FireNotify(AttrKey::KV, m_DoseUnit.m_KV->JSGet());
  1190. };
  1191. auto HWFLM = [this](const char* value, int length)
  1192. {
  1193. assert(value);
  1194. if (m_DoseUnit.m_MA->Update(atof(value)/10.0))
  1195. FireNotify(AttrKey::MA, m_DoseUnit.m_MA->JSGet());
  1196. };
  1197. auto HWFLS = [this](const char* value, int length)
  1198. {
  1199. assert(value);
  1200. if (m_DoseUnit.m_FrameRate->Update(atof(value)/10.0))
  1201. FireNotify(AttrKey::FRAMERATE, m_DoseUnit.m_FrameRate->JSGet());
  1202. };
  1203. auto HWFLX = [this](const char* value, int length)
  1204. {
  1205. assert(value);
  1206. int nValue = atoi(value);
  1207. if (nValue == 1)
  1208. {
  1209. m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_XRAYON);
  1210. }
  1211. else if (nValue == 0)
  1212. {
  1213. m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_XRAYOFF);
  1214. }
  1215. FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
  1216. };
  1217. auto HWFLP = [this](const char* value, int length)
  1218. {
  1219. assert(value);
  1220. int nValue = atoi(value);
  1221. if (nValue == 1)
  1222. {
  1223. //PRINTA_INFO("\n recv FLP1\n");
  1224. }
  1225. else if (nValue == 4)
  1226. {
  1227. m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_READY);
  1228. }
  1229. else if (nValue == 0)
  1230. {
  1231. m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_OFF);
  1232. }
  1233. FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
  1234. };
  1235. auto HWACH = [this](const char* value, int length)
  1236. {
  1237. assert(value);
  1238. if (m_pCollDev)
  1239. {
  1240. m_pCollDev->UpdateCollimatorXSize(atoi(value) / 100.0);
  1241. }
  1242. };
  1243. auto HWACW = [this](const char* value, int length)
  1244. {
  1245. assert(value);
  1246. if (m_pCollDev)
  1247. {
  1248. m_pCollDev->UpdateCollimatorYSize(atoi(value) / 100.0);
  1249. }
  1250. };
  1251. auto HWFIL = [this](const char* value, int length)
  1252. {
  1253. assert(value);
  1254. if (m_pCollDev)
  1255. {
  1256. m_pCollDev->SetCollimatorFilter(atoi(value));
  1257. }
  1258. };
  1259. auto HWATU = [this](const char* value, int length)
  1260. {
  1261. assert(value);
  1262. if (1 == atoi(value))
  1263. {
  1264. m_bCheckATUStatus = false;//not need continue check status
  1265. }
  1266. else if (0 == atoi(value))
  1267. {
  1268. m_bCheckATUStatus = true;
  1269. }
  1270. };
  1271. auto HWDS = [this](const char* value, int length)
  1272. {
  1273. assert(value);
  1274. m_bDAPEnable = (bool)atoi(value);
  1275. };
  1276. arFrame.clear();
  1277. arFrame.push_back(tFrameMapping("KV", 2, HWKV));
  1278. arFrame.push_back(tFrameMapping("MX", 2, HWMAS));
  1279. arFrame.push_back(tFrameMapping("MA", 2, HWMA));
  1280. arFrame.push_back(tFrameMapping("MS", 2, HWMS));
  1281. arFrame.push_back(tFrameMapping("ET", 2, HWTechmode));
  1282. arFrame.push_back(tFrameMapping("FO", 2, HWFocus));
  1283. arFrame.push_back(tFrameMapping("FI", 2, HWAECField));
  1284. arFrame.push_back(tFrameMapping("FS", 2, HWAECFilm));
  1285. arFrame.push_back(tFrameMapping("FN", 2, HWAECDensity));
  1286. arFrame.push_back(tFrameMapping("WS", 2, HWWS));
  1287. arFrame.push_back(tFrameMapping("PR", 2, HWPR));
  1288. arFrame.push_back(tFrameMapping("XR", 2, HWXR));
  1289. arFrame.push_back(tFrameMapping("AP", 2, HWAPDOSE));
  1290. arFrame.push_back(tFrameMapping("AT", 2, HWATDOSE));
  1291. arFrame.push_back(tFrameMapping("EL", 2, HWERROR));
  1292. arFrame.push_back(tFrameMapping("ER", 2, HWWARN));
  1293. arFrame.push_back(tFrameMapping("HE", 2, HWEHE));
  1294. arFrame.push_back(tFrameMapping("DA", 2, HWDAP));
  1295. arFrame.push_back(tFrameMapping("DV", 2, HWDAP));
  1296. arFrame.push_back(tFrameMapping("DS", 2, HWDS));
  1297. arFrame.push_back(tFrameMapping("ACH", 3, HWACH));
  1298. arFrame.push_back(tFrameMapping("ACW", 3, HWACW));
  1299. arFrame.push_back(tFrameMapping("FIL", 3, HWFIL));
  1300. arFrame.push_back(tFrameMapping("ATU", 3, HWATU));
  1301. }
  1302. bool nsGEN::CPIDevice::StartHardwareStatusThread()
  1303. {
  1304. FINFO("enter Start HardwareStatus Thread ");
  1305. try {
  1306. if (!m_pHardwareStatusThread.joinable()) {
  1307. m_pHardwareStatusThread = std::thread(HardwareStatusThread, this);
  1308. FINFO("Hardware status thread started successfully");
  1309. return true;
  1310. } else {
  1311. FINFO("Hardware status thread already running");
  1312. return true;
  1313. }
  1314. }
  1315. catch (const std::exception& e) {
  1316. FERROR("Start HardwareStatus Thread Failed: {$}", e.what());
  1317. return false;
  1318. }
  1319. }
  1320. void nsGEN::CPIDevice::HardwareStatusThread(CPIDevice* pParam)
  1321. {
  1322. if (pParam == NULL)
  1323. {
  1324. FERROR("HardwareStatusThread: Invalid parameter");
  1325. return;
  1326. }
  1327. FINFO("HardwareStatusThread start");
  1328. int cnt = 0;
  1329. while (pParam->m_bExtraFlag)
  1330. {
  1331. Sleep(2000);
  1332. if (!pParam->m_bExtraFlag) {
  1333. break;
  1334. }
  1335. pParam->HWSend("HE?");
  1336. if (pParam->m_bCheckATUStatus)
  1337. {
  1338. cnt++;
  1339. if (cnt == 10)
  1340. {
  1341. pParam->HWSend("ATU?");
  1342. cnt = 0;
  1343. }
  1344. }
  1345. else
  1346. {
  1347. cnt = 0;
  1348. }
  1349. }
  1350. FINFO("HardwareStatusThread stop");
  1351. }
  1352. //-----------------------------------------------------------------------------
  1353. // CPIDriver
  1354. //-----------------------------------------------------------------------------
  1355. nsGEN::CPIDriver::CPIDriver()
  1356. {
  1357. m_bDemoConnected = false;
  1358. m_pDriGenDev = nullptr;
  1359. m_pDriCollDev = nullptr;
  1360. m_pDriDapDev = nullptr;
  1361. m_pAttribute.reset(new ResDataObject());
  1362. m_pDescription.reset(new ResDataObject());
  1363. }
  1364. nsGEN::CPIDriver::~CPIDriver()
  1365. {
  1366. }
  1367. auto nsGEN::CPIDriver::CreateDevice(int index) -> std::unique_ptr<IODevice>
  1368. {
  1369. if (index == 0)
  1370. {
  1371. m_pDriGenDev = new CPIDevice(EventCenter, m_scfWrapper, m_ConfigFileName);
  1372. auto dev = std::unique_ptr<IODevice>(new IODevice(m_pDriGenDev));
  1373. return dev;
  1374. }
  1375. else if (index == 1)
  1376. {
  1377. FINFO("Enter CreateDevice COLL");
  1378. m_pDriCollDev = new OemCollimator(std::shared_ptr<IOEventCenter>(new IOEventCenter()));
  1379. auto dev = std::unique_ptr<IODevice>(new IODevice(m_pDriCollDev));
  1380. m_pDriCollDev->SetCtrlDev(m_pDriGenDev);
  1381. m_pDriGenDev->SetCollimatorDev(m_pDriCollDev);
  1382. FINFO("Leave CreateDevice COLL");
  1383. return dev;
  1384. }
  1385. // else if (index == 2) //------------------------------因为机械部分没有可获取的信息,所以此处屏蔽机械设备的创建
  1386. // {
  1387. // FINFO("Enter CreateDevice Mech");
  1388. // m_pDriMechDev = new OemMechanical(std::shared_ptr<IOEventCenter>(new IOEventCenter()));
  1389. // auto dev = std::unique_ptr<IODevice>(new IODevice(m_pDriMechDev));
  1390. // m_pDriMechDev->SetCtrlDev(m_pDriGenDev);
  1391. // m_pDriGenDev->SetMechDev(m_pDriMechDev);
  1392. // FINFO("Leave CreateDevice Mech");
  1393. // return dev;
  1394. // }
  1395. else if (index == 2) //----------------------------------可能DAP设备也是如此
  1396. {
  1397. FINFO("Enter CreateDevice DAP");
  1398. m_pDriDapDev = new OemDap(std::shared_ptr<IOEventCenter>(new IOEventCenter()));
  1399. auto dev = std::unique_ptr<IODevice>(new IODevice(m_pDriDapDev));
  1400. m_pDriDapDev->SetCtrlDev(m_pDriGenDev);
  1401. m_pDriGenDev->SetDapDev(m_pDriDapDev);
  1402. FINFO("Leave CreateDevice DAP");
  1403. return dev;
  1404. }
  1405. // 处理所有未匹配的index,确保函数有返回值
  1406. return nullptr;
  1407. }
  1408. void nsGEN::CPIDriver::FireNotify(int code, std::string key, std::string content)
  1409. {
  1410. EventCenter->OnNotify(code, key, content);
  1411. }
  1412. void nsGEN::CPIDriver::Prepare()
  1413. {
  1414. FINFO("========================================");
  1415. FINFO("CPIDriver::Prepare() Starting...");
  1416. FINFO("========================================");
  1417. // 初始化日志系统
  1418. std::string strLogPath = GetProcessDirectory() + R"(/Conf/log_config.xml)";
  1419. std::string LogHost = "DevCPI";
  1420. std::string moduleName = "DevCPI";
  1421. FINFO("Initializing log module...");
  1422. FINFO("Log config path: {$}", strLogPath.c_str());
  1423. FINFO("Log host: {$}", LogHost.c_str());
  1424. FINFO("Module name: {$}", moduleName.c_str());
  1425. bool ret = initLogModule(
  1426. LogHost, // 主机名(用于日志路径中的{host}占位符)
  1427. moduleName, // 唯一模块名
  1428. strLogPath, // 配置文件路径
  1429. true // 是否输出到控制台(可选)
  1430. );
  1431. if (!ret) {
  1432. std::cerr << "Log init failed!" << std::endl;
  1433. FERROR("Failed to initialize log module");
  1434. return;
  1435. }
  1436. FINFO("Log module initialized successfully");
  1437. CPI_SetLocalModuleName(moduleName);
  1438. FINFO("Getting connection DLL from config: {$}", m_ConfigFileName.c_str());
  1439. m_SCFDllName = GetConnectDLL(m_ConfigFileName);
  1440. FINFO("SCF DLL name: {$}", m_SCFDllName.c_str());
  1441. FINFO("Calling parent Prepare()...");
  1442. super::Prepare();
  1443. FINFO("========================================");
  1444. FINFO("CPIDriver::Prepare() Completed");
  1445. FINFO("========================================");
  1446. }
  1447. bool DATA_ACTION nsGEN::CPIDriver::Connect()
  1448. {
  1449. std::lock_guard<std::mutex> lock(m_connectionMutex);
  1450. const auto currentState = m_connectionState.load();
  1451. auto now = std::chrono::steady_clock::now();
  1452. // 1. 处理可重试的失败状态
  1453. if (currentState == ConnectionState::Failed) {
  1454. if ((now - m_lastConnectionAttempt) >= RETRY_INTERVAL && m_connectionRetryCount < MAX_RETRY_COUNT) {
  1455. m_connectionState = ConnectionState::Disconnected;
  1456. }
  1457. else {
  1458. return false; // 不满足重试条件,直接返回
  1459. }
  1460. }
  1461. // 2. 检查无效状态(正在连接/已连接但实际有效)
  1462. if (currentState == ConnectionState::Connecting) {
  1463. FINFO("Already connecting (type: {$})",
  1464. m_currentConnType == ConnectionType::Serial ? "Serial" : "Ethernet");
  1465. return true;
  1466. }
  1467. if (currentState == ConnectionState::Connected && m_scfWrapper && m_scfWrapper->IsConnected()) {
  1468. FINFO("Already connected (type: {$})",
  1469. m_currentConnType == ConnectionType::Serial ? "Serial" : "Ethernet");
  1470. return true;
  1471. }
  1472. // 3. 检查重试间隔
  1473. if (m_connectionRetryCount > 0 && (now - m_lastConnectionAttempt) < RETRY_INTERVAL) {
  1474. FINFO("Retry in {$}s (type: {$})",
  1475. std::chrono::duration_cast<std::chrono::seconds>(RETRY_INTERVAL - (now - m_lastConnectionAttempt)).count(),
  1476. m_currentConnType == ConnectionType::Serial ? "Serial" : "Ethernet");
  1477. return false;
  1478. }
  1479. ResDataObject connParam = GetConnectParam(m_ConfigFileName);
  1480. std::string connPortStr = "";
  1481. std::string connTypeStr = (std::string)connParam["type"]; // 从配置读取type字段
  1482. m_currentConnType = (connTypeStr == "COM") ? ConnectionType::Serial : ConnectionType::Ethernet;
  1483. if (m_currentConnType == ConnectionType::Serial)
  1484. {
  1485. connPortStr = (std::string)connParam["port"];// 从配置读取port字段
  1486. // 查找配置端口在现有端口列表中的位置
  1487. auto it = std::find(m_serialPorts.begin(), m_serialPorts.end(), connPortStr);
  1488. if (it == m_serialPorts.end()) {
  1489. // 配置的端口不在列表中,添加到首位
  1490. FINFO("Configured serial port {$} not found, adding to front of port list", connPortStr);
  1491. m_serialPorts.insert(m_serialPorts.begin(), connPortStr);
  1492. }
  1493. else if (it != m_serialPorts.begin()) {
  1494. // 配置的端口存在但不在首位,移动到首位
  1495. FINFO("Moving configured serial port {$} to front of port list", connPortStr);
  1496. m_serialPorts.erase(it);
  1497. m_serialPorts.insert(m_serialPorts.begin(), connPortStr);
  1498. }
  1499. }
  1500. // 4. 执行连接流程
  1501. m_connectionState = ConnectionState::Connecting;
  1502. m_lastConnectionAttempt = now;
  1503. std::string connInfo;
  1504. try {
  1505. if (m_currentConnType == ConnectionType::Serial) {
  1506. // 串口连接:使用当前索引的端口
  1507. std::string currentPort = m_serialPorts[m_currentSerialPortIndex];
  1508. connParam.update("port", currentPort.c_str()); // 将当前尝试的端口写入参数
  1509. connInfo = "Serial (port: " + currentPort + ")";
  1510. }
  1511. else {
  1512. // 网口连接:直接使用配置参数
  1513. connInfo = "Ethernet (ip: " + std::string(connParam["ip"]) + ")";
  1514. }
  1515. FINFO("Enter Connect ({$}), config: {$}", connInfo, connParam.encode());
  1516. if (!m_scfWrapper->Initialize(m_SCFDllName)) {
  1517. FINFO("Init failed: {$}", m_scfWrapper->GetLastError());
  1518. m_connectionState = ConnectionState::Failed;
  1519. return false;
  1520. }
  1521. m_scfWrapper->SetDataReceivedCallback([this](const char* data, uint32_t length) {
  1522. this->Dequeue(data, length);
  1523. });
  1524. auto erCode = m_scfWrapper->Connect(connParam, &CPIDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000);
  1525. if (erCode != SCF_SUCCEED || !m_scfWrapper->StartAutoReceive()) {
  1526. FINFO("Connect failed (code: {$}) for {$}", erCode, connInfo);
  1527. m_scfWrapper->Disconnect();
  1528. m_connectionState = ConnectionState::Failed;
  1529. // 串口连接:未遍历完所有端口时,优先切换端口重试(不计入总重试次数)
  1530. if (m_currentConnType == ConnectionType::Serial) {
  1531. int nextIndex = (m_currentSerialPortIndex + 1) % m_serialPorts.size();
  1532. // 判断是否已遍历所有端口(当前索引是最后一个时,nextIndex会回到0)
  1533. bool allPortsTried = (nextIndex == 0);
  1534. if (!allPortsTried) {
  1535. // 未遍历完所有端口:切换到下一端口,不增加重试计数
  1536. m_currentSerialPortIndex = nextIndex;
  1537. m_connectionRetryCount = 0;
  1538. FINFO("Trying next serial port: {$}", m_serialPorts[nextIndex]);
  1539. return false; // 触发外部线程立即尝试下一端口
  1540. }
  1541. else {
  1542. // 已遍历所有端口:重置到第一个端口,增加重试计数(进入间隔等待)
  1543. m_currentSerialPortIndex = 0;
  1544. m_connectionRetryCount++;
  1545. FINFO("All serial ports tried, retry count: {$}/{$}", m_connectionRetryCount, MAX_RETRY_COUNT);
  1546. return false;
  1547. }
  1548. }
  1549. // 所有端口失败(串口)或网口失败,才增加总重试计数
  1550. m_connectionRetryCount++;
  1551. return false;
  1552. }
  1553. // 连接成功:重置状态
  1554. m_connectionState = ConnectionState::Connected;
  1555. m_connectionRetryCount = 0;
  1556. m_currentSerialPortIndex = 0; // 重置串口端口索引
  1557. FINFO("Connected successfully ({$})", connInfo);
  1558. return true;
  1559. }
  1560. catch (const std::exception& e) {
  1561. FINFO("Exception for {$}: {$}", connInfo, e.what());
  1562. m_connectionState = ConnectionState::Failed;
  1563. m_connectionRetryCount++;
  1564. return false;
  1565. }
  1566. }
  1567. void nsGEN::CPIDriver::Disconnect()
  1568. {
  1569. FINFO("========================================");
  1570. FINFO("CPIDriver::Disconnect() Entry");
  1571. if (m_scfWrapper) {
  1572. FINFO("Stopping auto receive...");
  1573. m_scfWrapper->StopAutoReceive();
  1574. FINFO("Auto receive stopped");
  1575. FINFO("Disconnecting SCF wrapper...");
  1576. m_scfWrapper->Disconnect();
  1577. FINFO("SCF wrapper disconnected");
  1578. } else {
  1579. FWARN("SCF wrapper is null, nothing to disconnect");
  1580. }
  1581. FINFO("CPIDriver::Disconnect() Completed");
  1582. FINFO("========================================");
  1583. }
  1584. bool nsGEN::CPIDriver::isConnected() const
  1585. {
  1586. const auto state = m_connectionState.load();
  1587. // 1. 连接中/实际已连接:返回true(无需重连)
  1588. if (state == ConnectionState::Connecting || (m_scfWrapper && m_scfWrapper->IsConnected())) {
  1589. //FINFO(state == ConnectionState::Connecting ? "Connecting in progress" : "Already connected");
  1590. return true;
  1591. }
  1592. // 2. 失败状态处理:判断是否允许重试
  1593. if (state == ConnectionState::Failed) {
  1594. auto now = std::chrono::steady_clock::now();
  1595. const auto timeSinceLast = now - m_lastConnectionAttempt;
  1596. // 2.1 达到最大重试次数,但超过重置间隔:重置计数,允许重新重试
  1597. if (m_connectionRetryCount >= MAX_RETRY_COUNT && timeSinceLast >= RESET_RETRY_AFTER) {
  1598. FINFO("Max retries reached, resetting count after {$}s",
  1599. std::chrono::duration_cast<std::chrono::seconds>(timeSinceLast).count());
  1600. m_connectionRetryCount = 0; // 重置计数(因mutable修饰,const函数可修改)
  1601. }
  1602. // 2.2 不适合重连(时间未到 或 次数仍超限):返回true阻止重连
  1603. if (timeSinceLast < RETRY_INTERVAL || m_connectionRetryCount >= MAX_RETRY_COUNT) {
  1604. FINFO(timeSinceLast < RETRY_INTERVAL ?
  1605. "Retry later ({$}s)" : "Max retries, waiting {$}s to reset",
  1606. std::chrono::duration_cast<std::chrono::seconds>(
  1607. timeSinceLast < RETRY_INTERVAL ? RETRY_INTERVAL - timeSinceLast : RESET_RETRY_AFTER - timeSinceLast
  1608. ).count()
  1609. );
  1610. return true;
  1611. }
  1612. }
  1613. // 3. 其他情况(适合重连):返回false触发Connect()
  1614. return false;
  1615. }
  1616. std::string nsGEN::CPIDriver::DriverProbe()
  1617. {
  1618. ResDataObject r_config, HardwareInfo;
  1619. if (r_config.loadFile(m_ConfigFileName.c_str()))
  1620. {
  1621. HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
  1622. HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
  1623. HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
  1624. HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
  1625. HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
  1626. }
  1627. else
  1628. {
  1629. HardwareInfo.add("MajorID", "Generator");
  1630. HardwareInfo.add("MinorID", "Dr");
  1631. HardwareInfo.add("VendorID", "CPI");
  1632. HardwareInfo.add("ProductID", "HF");
  1633. HardwareInfo.add("SerialID", "Drv");
  1634. }
  1635. string ret = HardwareInfo.encode();
  1636. return ret;
  1637. }
  1638. std::string nsGEN::CPIDriver::DeviceProbe()
  1639. {
  1640. ResDataObject r_config, HardwareInfo;
  1641. if (r_config.loadFile(m_ConfigFileName.c_str()))
  1642. {
  1643. HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
  1644. HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
  1645. HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
  1646. HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
  1647. HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
  1648. }
  1649. else
  1650. {
  1651. HardwareInfo.add("MajorID", "Generator");
  1652. HardwareInfo.add("MinorID", "Dr");
  1653. HardwareInfo.add("VendorID", "CPI");
  1654. HardwareInfo.add("ProductID", "HF");
  1655. HardwareInfo.add("SerialID", "Drv");
  1656. }
  1657. string ret = HardwareInfo.encode();
  1658. return ret;
  1659. }
  1660. bool nsGEN::CPIDriver::GetDeviceConfig(std::string& Cfg)
  1661. {
  1662. Cfg = m_DeviceConfigSend.encode();
  1663. //printf("GetDeviceConfig over");
  1664. printf("GetDeviceConfig over , %s", Cfg.c_str());
  1665. return true;
  1666. }
  1667. bool nsGEN::CPIDriver::SetDeviceConfig(std::string Cfg)
  1668. {
  1669. FINFO("--Func-- SetDeviceConfig {$}\n", Cfg.c_str());
  1670. ResDataObject DeviceConfig;
  1671. DeviceConfig.decode(Cfg.c_str());
  1672. ResDataObject DescriptionTempEx;
  1673. DescriptionTempEx = DeviceConfig["DeviceConfig"]["Attribute"];
  1674. FDEBUG("Attribute:{$}", DescriptionTempEx.encode());
  1675. bool bSaveFile = false;
  1676. string strAccess = "";
  1677. for (int i = 0; i < DescriptionTempEx.size(); i++)
  1678. {
  1679. string strKey = DescriptionTempEx.GetKey(i);
  1680. FINFO("{$}", strKey.c_str());
  1681. try
  1682. {
  1683. if (m_pAttribute->GetFirstOf(strKey.c_str()) >= 0)
  1684. {
  1685. strAccess = (string)(*m_pDescription)[strKey.c_str()]["Access"];
  1686. if ("RW" == strAccess)
  1687. {
  1688. (*m_pAttribute)[strKey.c_str()] = DescriptionTempEx[i];
  1689. int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  1690. FINFO("nConfigInfoCount {$}", nConfigInfoCount);
  1691. string strTemp = "";
  1692. for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  1693. {
  1694. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  1695. if (strTemp == strKey)
  1696. {
  1697. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  1698. break;
  1699. }
  1700. }
  1701. if (SetDeviceConfigValue(m_Configurations, strTemp.c_str(), 1, DescriptionTempEx[i]))
  1702. {
  1703. FDEBUG("SetDeviceConfigValue over");
  1704. bSaveFile = true;
  1705. }
  1706. }
  1707. else
  1708. {
  1709. FINFO("{$} is not a RW configuration item", strKey.c_str());
  1710. }
  1711. }
  1712. else
  1713. {
  1714. FINFO("without this attribute {$}", strKey.c_str());
  1715. }
  1716. }
  1717. catch (ResDataObjectExption& e)
  1718. {
  1719. FERROR("SetDriverConfig crashed: {$}", e.what());
  1720. return false;
  1721. }
  1722. }
  1723. if (bSaveFile)
  1724. {
  1725. SaveConfigFile(true);
  1726. }
  1727. return true;
  1728. }
  1729. bool nsGEN::CPIDriver::SaveConfigFile(bool bSendNotify)
  1730. {
  1731. m_ConfigAll["CONFIGURATION"] = m_Configurations;
  1732. bool bRt = m_ConfigAll.SaveFile(m_ConfigFileName.c_str());
  1733. FINFO("SaveConfigFile over {$}", bRt);
  1734. return true;
  1735. }
  1736. bool nsGEN::CPIDriver::GetDeviceConfigValue(ResDataObject config, const char* pInnerKey, int nPathID, string& strValue)
  1737. {
  1738. strValue = "";
  1739. string strTemp = pInnerKey;
  1740. if (1 == nPathID)
  1741. {
  1742. int pos = 0;
  1743. ResDataObject resTemp = config;
  1744. while ((pos = strTemp.find_first_of(',')) != string::npos)
  1745. {
  1746. string Key = strTemp.substr(0, pos);
  1747. string TempValue = resTemp[Key.c_str()].encode();
  1748. // printf("-TempValue=== %s", TempValue.c_str());
  1749. resTemp.clear();
  1750. resTemp.decode(TempValue.c_str());
  1751. strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1);
  1752. //printf("-************--%s", strTemp.c_str());
  1753. }
  1754. if (strTemp != "")
  1755. {
  1756. strValue = (string)resTemp[strTemp.c_str()];
  1757. }
  1758. else
  1759. {
  1760. strValue = (string)resTemp;
  1761. }
  1762. }
  1763. //printf("------------%s", strValue.c_str());
  1764. return true;
  1765. }
  1766. bool nsGEN::CPIDriver::SetDeviceConfigValue(ResDataObject& config, const char* pInnerKey, int nPathID, const char* szValue)
  1767. {
  1768. string strTemp = pInnerKey;
  1769. FDEBUG("Begin to change {$} item value to {$}", pInnerKey, szValue);
  1770. if (1 == nPathID) //��DriverConfig·����ÿ��DPC�Լ��������ļ���ȡ
  1771. {
  1772. try {
  1773. int pos = 0;
  1774. ResDataObject* resTemp = &config;
  1775. while ((pos = strTemp.find_first_of(',')) != string::npos)
  1776. {
  1777. string Key = strTemp.substr(0, pos);
  1778. resTemp = &(*resTemp)[Key.c_str()];
  1779. strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1);
  1780. }
  1781. if (strTemp != "")
  1782. {
  1783. (*resTemp)[strTemp.c_str()] = szValue;
  1784. //if ((strTemp.compare("WSTable") == 0) ||
  1785. // (strTemp.compare("WSWall") == 0) ||
  1786. // (strTemp.compare("WSFree") == 0) ||
  1787. // (strTemp.compare("WSTomo") == 0) ||
  1788. // (strTemp.compare("WSConventional") == 0)
  1789. // )
  1790. //{
  1791. // int sum = (*szValue) + 1;
  1792. // (*resTemp)[strTemp.c_str()] = (char*)(&sum);
  1793. //}
  1794. //else
  1795. // (*resTemp)[strTemp.c_str()] = szValue;
  1796. }
  1797. else
  1798. {
  1799. *resTemp = szValue;
  1800. }
  1801. }
  1802. catch (ResDataObjectExption& e)
  1803. {
  1804. FERROR("SetDriverConfigvalue crashed: {$}", e.what());
  1805. return false;
  1806. }
  1807. }
  1808. return true;
  1809. }
  1810. std::string nsGEN::CPIDriver::GetResource()
  1811. {
  1812. ResDataObject r_config, temp;
  1813. if (!temp.loadFile(m_ConfigFileName.c_str()))
  1814. {
  1815. return "";
  1816. }
  1817. m_ConfigAll = temp;
  1818. r_config = temp["CONFIGURATION"];
  1819. m_Configurations = r_config;
  1820. ResDataObject DescriptionTemp;
  1821. ResDataObject DescriptionSend;
  1822. ResDataObject m_DescriptionSend;
  1823. ResDataObject ListTemp;
  1824. string strTemp = "";
  1825. string strIndex = "";
  1826. int nTemp = -1;
  1827. char sstream[10] = { 0 };
  1828. string strValue = "";
  1829. string strType = "";
  1830. /***
  1831. ***/
  1832. try
  1833. {
  1834. int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  1835. m_pAttribute->clear();
  1836. m_pDescription->clear();
  1837. for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  1838. {
  1839. DescriptionTemp.clear();
  1840. DescriptionSend.clear();
  1841. ListTemp.clear();
  1842. //AttributeType
  1843. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Type"];
  1844. DescriptionTemp.add(ConfKey::CcosType, strTemp.c_str());//CcosGeneratorAttribute
  1845. DescriptionSend.add(ConfKey::CcosType, strTemp.c_str());//CcosGeneratorAttribute
  1846. strType = strTemp;
  1847. //AttributeKey
  1848. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  1849. nTemp = (int)m_Configurations["ConfigToolInfo"][nInfoIndex]["PathID"];
  1850. GetDeviceConfigValue(r_config, strTemp.c_str(), nTemp, strValue);
  1851. //printf("********************************innerkey=%s --strValue = %s\n", strTemp.c_str(), strValue.c_str());
  1852. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  1853. if ("int" == strType)
  1854. {
  1855. (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str()));
  1856. }
  1857. else if ("float" == strType)
  1858. {
  1859. (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str()));
  1860. }
  1861. else
  1862. {
  1863. (*m_pAttribute).add(strTemp.c_str(), strValue.c_str());
  1864. }
  1865. //printf("********************************outkey =%s --strValue = %s\n", strTemp.c_str(), strValue.c_str());
  1866. //AttributeAccess
  1867. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Access"];
  1868. DescriptionTemp.add(ConfKey::CcosAccess, strTemp.c_str());
  1869. DescriptionSend.add(ConfKey::CcosAccess, strTemp.c_str());
  1870. /*
  1871. //AttributeRangeMin
  1872. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMin"];
  1873. if (strTemp != "")
  1874. {
  1875. DescriptionTemp.add(ConfKey::CcosRangeMin, strTemp.c_str());
  1876. }
  1877. //AttributeRangeMax
  1878. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMax"];
  1879. if (strTemp != "")
  1880. {
  1881. DescriptionTemp.add(ConfKey::CcosRangeMax, strTemp.c_str());
  1882. }
  1883. */
  1884. //AttributeList
  1885. nTemp = m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListNum"];
  1886. if (nTemp > 0)
  1887. {
  1888. for (int nListIndex = 0; nListIndex < nTemp; nListIndex++)
  1889. {
  1890. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListInfo"][nListIndex];
  1891. auto temKey = std::to_string(nListIndex);
  1892. ListTemp.add(temKey.c_str(), strTemp.c_str());
  1893. }
  1894. DescriptionTemp.add(ConfKey::CcosList, ListTemp);
  1895. DescriptionSend.add(ConfKey::CcosList, ListTemp.encode());
  1896. }
  1897. //AttributeRequired
  1898. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Required"];
  1899. DescriptionTemp.add(ConfKey::CcosRequired, strTemp.c_str());
  1900. DescriptionSend.add(ConfKey::CcosRequired, strTemp.c_str());
  1901. //AttributeDefaultValue
  1902. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["DefaultValue"];
  1903. if (strTemp != "") //����Ҫ��������Ϊ��
  1904. {
  1905. DescriptionTemp.add(ConfKey::CcosDefaultValue, strTemp.c_str());
  1906. DescriptionSend.add(ConfKey::CcosDefaultValue, strTemp.c_str());
  1907. }
  1908. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  1909. (*m_pDescription).add(strTemp.c_str(), DescriptionTemp);
  1910. m_DescriptionSend.add(strTemp.c_str(), DescriptionSend.encode());
  1911. }
  1912. }
  1913. catch (ResDataObjectExption& e)
  1914. {
  1915. FERROR("Get config error: {$}", e.what());
  1916. return "";
  1917. }
  1918. ResDataObject resDeviceResource;
  1919. resDeviceResource.add(ConfKey::CcosGeneratorAttribute, (*m_pAttribute));
  1920. resDeviceResource.add(ConfKey::CcosGeneratorDescription, (*m_pDescription));
  1921. ResDataObject DescriptionTempEx;
  1922. DescriptionTempEx.add(ConfKey::CcosGeneratorConfig, resDeviceResource);
  1923. m_DeviceConfig.clear();
  1924. m_DeviceConfig = DescriptionTempEx;
  1925. FERROR("local ************* get resource over {$}", DescriptionTempEx.encode());
  1926. printf("local ************* get resource over %s \n", DescriptionTempEx.encode());
  1927. resDeviceResource.clear();
  1928. resDeviceResource.add(ConfKey::CcosGeneratorAttribute, (*m_pAttribute));
  1929. resDeviceResource.add(ConfKey::CcosGeneratorDescription, m_DescriptionSend);
  1930. DescriptionTempEx.clear();
  1931. DescriptionTempEx.add(ConfKey::CcosGeneratorConfig, resDeviceResource);
  1932. m_DeviceConfigSend.clear();
  1933. m_DeviceConfigSend = DescriptionTempEx;
  1934. string res = m_DeviceConfigSend.encode();
  1935. //printf("%s", res.c_str());
  1936. FDEBUG("get resource over {$}", DescriptionTempEx.encode());
  1937. printf("************* get resource over %s \n", DescriptionTempEx.encode());
  1938. return res;
  1939. }
  1940. void nsGEN::CPIDriver::Dequeue(const char* Packet, DWORD Length)
  1941. {
  1942. DecodeFrame(Packet, Length);
  1943. }
  1944. PACKET_RET nsGEN::CPIDriver::callbackPackageProcess(const char* RecData, uint32_t nLength, uint32_t& PacketLength)
  1945. {
  1946. if (nLength < 1)
  1947. {
  1948. printf("nLength < 1, nLength==%d \n", nLength);
  1949. FINFO("nLength < 1, nLength=={$} \n", nLength);
  1950. return PACKET_USELESS;
  1951. }
  1952. for (uint32_t i = 0; i < nLength - 1; i++)
  1953. {
  1954. //1 需要寻找包头
  1955. if (RecData[i]==0x02)
  1956. {
  1957. if (i!=0)
  1958. {
  1959. PacketLength = i; //i之前的数据,全部丢弃
  1960. char strtemp[100] = { 0 };
  1961. memcpy(strtemp, RecData, i);
  1962. strtemp[PacketLength + 1] = 0;
  1963. printf("==IN error data ==:%s,PacketLength=%d,nLength=%d\n", strtemp, PacketLength, nLength);
  1964. //for test
  1965. //for (int j = 0; j < nLength; j++)
  1966. //{
  1967. // printf("%2X,", RecData[j]);
  1968. //}
  1969. //printf("\n");
  1970. return PACKET_USELESS;
  1971. }
  1972. }
  1973. if (RecData[i] == 0x03)
  1974. {
  1975. PacketLength = i + 2; //+2 because index + ETX + sum.
  1976. char strtemp[100] = { 0 };
  1977. memcpy(strtemp, RecData, i); //RKC005(not include 03 + sum); //only 保存 0x03之前的数据,因为打印时,忽略03 和sum 都不需要了。
  1978. strtemp[PacketLength + 1] = 0;
  1979. printf("==IN==:%s\n", strtemp);
  1980. FINFO("==IN==:{$}\n", strtemp);
  1981. return PACKET_ISPACKET;
  1982. }
  1983. }
  1984. return PACKET_NOPACKET;
  1985. }
  1986. //-----------------------------------------------------------------------------
  1987. // DecodeFrame
  1988. //-----------------------------------------------------------------------------
  1989. static bool DecodeFrame(const char* strFrame, int length)
  1990. {
  1991. auto pr = [strFrame, length](const tFrameMapping& Item)
  1992. {
  1993. for (int i = 0; i < Item.NbOfCharOfHead; i++)
  1994. {
  1995. if (strFrame[i] != Item.strHead[i])
  1996. {
  1997. return false;
  1998. }
  1999. }
  2000. return true;
  2001. };
  2002. auto found = std::find_if(arFrame.begin(), arFrame.end(), pr);
  2003. if (found == arFrame.end())
  2004. {
  2005. return false;
  2006. }
  2007. const auto& Item = *found;
  2008. auto pc = strFrame;
  2009. char data[100] = { 0 };
  2010. memcpy(data, strFrame + Item.NbOfCharOfHead, length - Item.NbOfCharOfHead);
  2011. Item.fun(data, length - Item.NbOfCharOfHead);
  2012. return true;
  2013. }
  2014. //-----------------------------------------------------------------------------
  2015. // GetIODriver & CreateIODriver
  2016. //-----------------------------------------------------------------------------
  2017. static nsGEN::CPIDriver gIODriver;
  2018. extern "C" CCOS::Dev::IODriver * GetIODriver()
  2019. {
  2020. return &gIODriver;
  2021. }
  2022. extern "C" CCOS::Dev::IODriver * CreateIODriver()
  2023. {
  2024. return new nsGEN::CPIDriver();
  2025. }