CCOS.Dev.Generator.CPI.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  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. FINFO("========================================");
  1450. FINFO("CPIDriver::Connect() Starting...");
  1451. FINFO("========================================");
  1452. // 获取连接参数
  1453. ResDataObject connParam = GetConnectParam(m_ConfigFileName);
  1454. FINFO("Connection parameters: {$}", connParam.encode());
  1455. // 创建 SCFWrapper 实例
  1456. if (!m_scfWrapper) {
  1457. try {
  1458. // 创建 SCFWrapper
  1459. m_scfWrapper = std::make_shared<SCFWrapper>();
  1460. // 初始化 SCF 库
  1461. if (!m_scfWrapper->Initialize(m_SCFDllName)) {
  1462. FERROR("Failed to initialize SCF library: {$}", m_scfWrapper->GetLastError());
  1463. return false;
  1464. }
  1465. FINFO("SCFWrapper initialized successfully");
  1466. }
  1467. catch (const std::exception& e) {
  1468. FERROR("Exception creating SCFWrapper: {$}", e.what());
  1469. return false;
  1470. }
  1471. }
  1472. // 设置数据接收回调
  1473. m_scfWrapper->SetDataReceivedCallback([this](const char* data, uint32_t length) {
  1474. this->Dequeue(data, length);
  1475. });
  1476. // 连接 SCF
  1477. FINFO("Connecting to device...");
  1478. auto erCode = m_scfWrapper->Connect(
  1479. connParam,
  1480. &nsGEN::CPIDriver::callbackPackageProcess,
  1481. SCF_PACKET_TRANSFER,
  1482. 3000
  1483. );
  1484. if (erCode != SCF_SUCCEED || !m_scfWrapper->StartAutoReceive()) {
  1485. FERROR("Failed to connect to device (error code: {$})", erCode);
  1486. m_scfWrapper->Disconnect();
  1487. return false;
  1488. }
  1489. FINFO("Device connected successfully");
  1490. // 调用父类的 Connect
  1491. FINFO("Calling parent Connect()...");
  1492. auto rc = super::Connect();
  1493. if (!rc) {
  1494. FERROR("Parent Connect() failed");
  1495. m_scfWrapper->Disconnect();
  1496. return false;
  1497. }
  1498. FINFO("========================================");
  1499. FINFO("CPIDriver::Connect() Completed Successfully");
  1500. FINFO("========================================");
  1501. return true;
  1502. }
  1503. void nsGEN::CPIDriver::Disconnect()
  1504. {
  1505. FINFO("========================================");
  1506. FINFO("CPIDriver::Disconnect() Entry");
  1507. if (m_scfWrapper) {
  1508. FINFO("Stopping auto receive...");
  1509. m_scfWrapper->StopAutoReceive();
  1510. FINFO("Auto receive stopped");
  1511. FINFO("Disconnecting SCF wrapper...");
  1512. m_scfWrapper->Disconnect();
  1513. FINFO("SCF wrapper disconnected");
  1514. } else {
  1515. FWARN("SCF wrapper is null, nothing to disconnect");
  1516. }
  1517. super::Disconnect();
  1518. m_bDemoConnected = false;
  1519. FINFO("CPIDriver::Disconnect() Completed");
  1520. FINFO("========================================");
  1521. }
  1522. bool nsGEN::CPIDriver::isConnected() const
  1523. {
  1524. if (m_scfWrapper && m_scfWrapper->IsConnected()) {
  1525. return true;
  1526. }
  1527. return super::isConnected();
  1528. }
  1529. std::string nsGEN::CPIDriver::DriverProbe()
  1530. {
  1531. ResDataObject r_config, HardwareInfo;
  1532. if (r_config.loadFile(m_ConfigFileName.c_str()))
  1533. {
  1534. HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
  1535. HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
  1536. HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
  1537. HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
  1538. HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
  1539. }
  1540. else
  1541. {
  1542. HardwareInfo.add("MajorID", "Generator");
  1543. HardwareInfo.add("MinorID", "Dr");
  1544. HardwareInfo.add("VendorID", "CPI");
  1545. HardwareInfo.add("ProductID", "HF");
  1546. HardwareInfo.add("SerialID", "Drv");
  1547. }
  1548. string ret = HardwareInfo.encode();
  1549. return ret;
  1550. }
  1551. std::string nsGEN::CPIDriver::DeviceProbe()
  1552. {
  1553. ResDataObject r_config, HardwareInfo;
  1554. if (r_config.loadFile(m_ConfigFileName.c_str()))
  1555. {
  1556. HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
  1557. HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
  1558. HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
  1559. HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
  1560. HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
  1561. }
  1562. else
  1563. {
  1564. HardwareInfo.add("MajorID", "Generator");
  1565. HardwareInfo.add("MinorID", "Dr");
  1566. HardwareInfo.add("VendorID", "CPI");
  1567. HardwareInfo.add("ProductID", "HF");
  1568. HardwareInfo.add("SerialID", "Drv");
  1569. }
  1570. string ret = HardwareInfo.encode();
  1571. return ret;
  1572. }
  1573. bool nsGEN::CPIDriver::GetDeviceConfig(std::string& Cfg)
  1574. {
  1575. Cfg = m_DeviceConfigSend.encode();
  1576. //printf("GetDeviceConfig over");
  1577. printf("GetDeviceConfig over , %s", Cfg.c_str());
  1578. return true;
  1579. }
  1580. bool nsGEN::CPIDriver::SetDeviceConfig(std::string Cfg)
  1581. {
  1582. FINFO("--Func-- SetDeviceConfig {$}\n", Cfg.c_str());
  1583. ResDataObject DeviceConfig;
  1584. DeviceConfig.decode(Cfg.c_str());
  1585. ResDataObject DescriptionTempEx;
  1586. DescriptionTempEx = DeviceConfig["DeviceConfig"]["Attribute"];
  1587. FDEBUG("Attribute:{$}", DescriptionTempEx.encode());
  1588. bool bSaveFile = false;
  1589. string strAccess = "";
  1590. for (int i = 0; i < DescriptionTempEx.size(); i++)
  1591. {
  1592. string strKey = DescriptionTempEx.GetKey(i);
  1593. FINFO("{$}", strKey.c_str());
  1594. try
  1595. {
  1596. if (m_pAttribute->GetFirstOf(strKey.c_str()) >= 0)
  1597. {
  1598. strAccess = (string)(*m_pDescription)[strKey.c_str()]["Access"];
  1599. if ("RW" == strAccess)
  1600. {
  1601. (*m_pAttribute)[strKey.c_str()] = DescriptionTempEx[i];
  1602. int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  1603. FINFO("nConfigInfoCount {$}", nConfigInfoCount);
  1604. string strTemp = "";
  1605. for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  1606. {
  1607. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  1608. if (strTemp == strKey)
  1609. {
  1610. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  1611. break;
  1612. }
  1613. }
  1614. if (SetDeviceConfigValue(m_Configurations, strTemp.c_str(), 1, DescriptionTempEx[i]))
  1615. {
  1616. FDEBUG("SetDeviceConfigValue over");
  1617. bSaveFile = true;
  1618. }
  1619. }
  1620. else
  1621. {
  1622. FINFO("{$} is not a RW configuration item", strKey.c_str());
  1623. }
  1624. }
  1625. else
  1626. {
  1627. FINFO("without this attribute {$}", strKey.c_str());
  1628. }
  1629. }
  1630. catch (ResDataObjectExption& e)
  1631. {
  1632. FERROR("SetDriverConfig crashed: {$}", e.what());
  1633. return false;
  1634. }
  1635. }
  1636. if (bSaveFile)
  1637. {
  1638. SaveConfigFile(true);
  1639. }
  1640. return true;
  1641. }
  1642. bool nsGEN::CPIDriver::SaveConfigFile(bool bSendNotify)
  1643. {
  1644. m_ConfigAll["CONFIGURATION"] = m_Configurations;
  1645. bool bRt = m_ConfigAll.SaveFile(m_ConfigFileName.c_str());
  1646. FINFO("SaveConfigFile over {$}", bRt);
  1647. return true;
  1648. }
  1649. bool nsGEN::CPIDriver::GetDeviceConfigValue(ResDataObject config, const char* pInnerKey, int nPathID, string& strValue)
  1650. {
  1651. strValue = "";
  1652. string strTemp = pInnerKey;
  1653. if (1 == nPathID)
  1654. {
  1655. int pos = 0;
  1656. ResDataObject resTemp = config;
  1657. while ((pos = strTemp.find_first_of(',')) != string::npos)
  1658. {
  1659. string Key = strTemp.substr(0, pos);
  1660. string TempValue = resTemp[Key.c_str()].encode();
  1661. // printf("-TempValue=== %s", TempValue.c_str());
  1662. resTemp.clear();
  1663. resTemp.decode(TempValue.c_str());
  1664. strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1);
  1665. //printf("-************--%s", strTemp.c_str());
  1666. }
  1667. if (strTemp != "")
  1668. {
  1669. strValue = (string)resTemp[strTemp.c_str()];
  1670. }
  1671. else
  1672. {
  1673. strValue = (string)resTemp;
  1674. }
  1675. }
  1676. //printf("------------%s", strValue.c_str());
  1677. return true;
  1678. }
  1679. bool nsGEN::CPIDriver::SetDeviceConfigValue(ResDataObject& config, const char* pInnerKey, int nPathID, const char* szValue)
  1680. {
  1681. string strTemp = pInnerKey;
  1682. FDEBUG("Begin to change {$} item value to {$}", pInnerKey, szValue);
  1683. if (1 == nPathID) //��DriverConfig·����ÿ��DPC�Լ��������ļ���ȡ
  1684. {
  1685. try {
  1686. int pos = 0;
  1687. ResDataObject* resTemp = &config;
  1688. while ((pos = strTemp.find_first_of(',')) != string::npos)
  1689. {
  1690. string Key = strTemp.substr(0, pos);
  1691. resTemp = &(*resTemp)[Key.c_str()];
  1692. strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1);
  1693. }
  1694. if (strTemp != "")
  1695. {
  1696. (*resTemp)[strTemp.c_str()] = szValue;
  1697. //if ((strTemp.compare("WSTable") == 0) ||
  1698. // (strTemp.compare("WSWall") == 0) ||
  1699. // (strTemp.compare("WSFree") == 0) ||
  1700. // (strTemp.compare("WSTomo") == 0) ||
  1701. // (strTemp.compare("WSConventional") == 0)
  1702. // )
  1703. //{
  1704. // int sum = (*szValue) + 1;
  1705. // (*resTemp)[strTemp.c_str()] = (char*)(&sum);
  1706. //}
  1707. //else
  1708. // (*resTemp)[strTemp.c_str()] = szValue;
  1709. }
  1710. else
  1711. {
  1712. *resTemp = szValue;
  1713. }
  1714. }
  1715. catch (ResDataObjectExption& e)
  1716. {
  1717. FERROR("SetDriverConfigvalue crashed: {$}", e.what());
  1718. return false;
  1719. }
  1720. }
  1721. return true;
  1722. }
  1723. std::string nsGEN::CPIDriver::GetResource()
  1724. {
  1725. ResDataObject r_config, temp;
  1726. if (!temp.loadFile(m_ConfigFileName.c_str()))
  1727. {
  1728. return "";
  1729. }
  1730. m_ConfigAll = temp;
  1731. r_config = temp["CONFIGURATION"];
  1732. m_Configurations = r_config;
  1733. ResDataObject DescriptionTemp;
  1734. ResDataObject DescriptionSend;
  1735. ResDataObject m_DescriptionSend;
  1736. ResDataObject ListTemp;
  1737. string strTemp = "";
  1738. string strIndex = "";
  1739. int nTemp = -1;
  1740. char sstream[10] = { 0 };
  1741. string strValue = "";
  1742. string strType = "";
  1743. /***
  1744. ***/
  1745. try
  1746. {
  1747. int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  1748. m_pAttribute->clear();
  1749. m_pDescription->clear();
  1750. for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  1751. {
  1752. DescriptionTemp.clear();
  1753. DescriptionSend.clear();
  1754. ListTemp.clear();
  1755. //AttributeType
  1756. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Type"];
  1757. DescriptionTemp.add(ConfKey::CcosType, strTemp.c_str());//CcosGeneratorAttribute
  1758. DescriptionSend.add(ConfKey::CcosType, strTemp.c_str());//CcosGeneratorAttribute
  1759. strType = strTemp;
  1760. //AttributeKey
  1761. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  1762. nTemp = (int)m_Configurations["ConfigToolInfo"][nInfoIndex]["PathID"];
  1763. GetDeviceConfigValue(r_config, strTemp.c_str(), nTemp, strValue);
  1764. //printf("********************************innerkey=%s --strValue = %s\n", strTemp.c_str(), strValue.c_str());
  1765. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  1766. if ("int" == strType)
  1767. {
  1768. (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str()));
  1769. }
  1770. else if ("float" == strType)
  1771. {
  1772. (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str()));
  1773. }
  1774. else
  1775. {
  1776. (*m_pAttribute).add(strTemp.c_str(), strValue.c_str());
  1777. }
  1778. //printf("********************************outkey =%s --strValue = %s\n", strTemp.c_str(), strValue.c_str());
  1779. //AttributeAccess
  1780. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Access"];
  1781. DescriptionTemp.add(ConfKey::CcosAccess, strTemp.c_str());
  1782. DescriptionSend.add(ConfKey::CcosAccess, strTemp.c_str());
  1783. /*
  1784. //AttributeRangeMin
  1785. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMin"];
  1786. if (strTemp != "")
  1787. {
  1788. DescriptionTemp.add(ConfKey::CcosRangeMin, strTemp.c_str());
  1789. }
  1790. //AttributeRangeMax
  1791. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMax"];
  1792. if (strTemp != "")
  1793. {
  1794. DescriptionTemp.add(ConfKey::CcosRangeMax, strTemp.c_str());
  1795. }
  1796. */
  1797. //AttributeList
  1798. nTemp = m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListNum"];
  1799. if (nTemp > 0)
  1800. {
  1801. for (int nListIndex = 0; nListIndex < nTemp; nListIndex++)
  1802. {
  1803. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListInfo"][nListIndex];
  1804. auto temKey = std::to_string(nListIndex);
  1805. ListTemp.add(temKey.c_str(), strTemp.c_str());
  1806. }
  1807. DescriptionTemp.add(ConfKey::CcosList, ListTemp);
  1808. DescriptionSend.add(ConfKey::CcosList, ListTemp.encode());
  1809. }
  1810. //AttributeRequired
  1811. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Required"];
  1812. DescriptionTemp.add(ConfKey::CcosRequired, strTemp.c_str());
  1813. DescriptionSend.add(ConfKey::CcosRequired, strTemp.c_str());
  1814. //AttributeDefaultValue
  1815. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["DefaultValue"];
  1816. if (strTemp != "") //����Ҫ��������Ϊ��
  1817. {
  1818. DescriptionTemp.add(ConfKey::CcosDefaultValue, strTemp.c_str());
  1819. DescriptionSend.add(ConfKey::CcosDefaultValue, strTemp.c_str());
  1820. }
  1821. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  1822. (*m_pDescription).add(strTemp.c_str(), DescriptionTemp);
  1823. m_DescriptionSend.add(strTemp.c_str(), DescriptionSend.encode());
  1824. }
  1825. }
  1826. catch (ResDataObjectExption& e)
  1827. {
  1828. FERROR("Get config error: {$}", e.what());
  1829. return "";
  1830. }
  1831. ResDataObject resDeviceResource;
  1832. resDeviceResource.add(ConfKey::CcosGeneratorAttribute, (*m_pAttribute));
  1833. resDeviceResource.add(ConfKey::CcosGeneratorDescription, (*m_pDescription));
  1834. ResDataObject DescriptionTempEx;
  1835. DescriptionTempEx.add(ConfKey::CcosGeneratorConfig, resDeviceResource);
  1836. m_DeviceConfig.clear();
  1837. m_DeviceConfig = DescriptionTempEx;
  1838. FERROR("local ************* get resource over {$}", DescriptionTempEx.encode());
  1839. printf("local ************* get resource over %s \n", DescriptionTempEx.encode());
  1840. resDeviceResource.clear();
  1841. resDeviceResource.add(ConfKey::CcosGeneratorAttribute, (*m_pAttribute));
  1842. resDeviceResource.add(ConfKey::CcosGeneratorDescription, m_DescriptionSend);
  1843. DescriptionTempEx.clear();
  1844. DescriptionTempEx.add(ConfKey::CcosGeneratorConfig, resDeviceResource);
  1845. m_DeviceConfigSend.clear();
  1846. m_DeviceConfigSend = DescriptionTempEx;
  1847. string res = m_DeviceConfigSend.encode();
  1848. //printf("%s", res.c_str());
  1849. FDEBUG("get resource over {$}", DescriptionTempEx.encode());
  1850. printf("************* get resource over %s \n", DescriptionTempEx.encode());
  1851. return res;
  1852. }
  1853. void nsGEN::CPIDriver::Dequeue(const char* Packet, DWORD Length)
  1854. {
  1855. DecodeFrame(Packet, Length);
  1856. }
  1857. PACKET_RET nsGEN::CPIDriver::callbackPackageProcess(const char* RecData, uint32_t nLength, uint32_t& PacketLength)
  1858. {
  1859. if (nLength < 1)
  1860. {
  1861. printf("nLength < 1, nLength==%d \n", nLength);
  1862. FINFO("nLength < 1, nLength=={$} \n", nLength);
  1863. return PACKET_USELESS;
  1864. }
  1865. for (uint32_t i = 0; i < nLength - 1; i++)
  1866. {
  1867. //1 需要寻找包头
  1868. if (RecData[i]==0x02)
  1869. {
  1870. if (i!=0)
  1871. {
  1872. PacketLength = i; //i之前的数据,全部丢弃
  1873. char strtemp[100] = { 0 };
  1874. memcpy(strtemp, RecData, i);
  1875. strtemp[PacketLength + 1] = 0;
  1876. printf("==IN error data ==:%s,PacketLength=%d,nLength=%d\n", strtemp, PacketLength, nLength);
  1877. //for test
  1878. //for (int j = 0; j < nLength; j++)
  1879. //{
  1880. // printf("%2X,", RecData[j]);
  1881. //}
  1882. //printf("\n");
  1883. return PACKET_USELESS;
  1884. }
  1885. }
  1886. if (RecData[i] == 0x03)
  1887. {
  1888. PacketLength = i + 2; //+2 because index + ETX + sum.
  1889. char strtemp[100] = { 0 };
  1890. memcpy(strtemp, RecData, i); //RKC005(not include 03 + sum); //only 保存 0x03之前的数据,因为打印时,忽略03 和sum 都不需要了。
  1891. strtemp[PacketLength + 1] = 0;
  1892. printf("==IN==:%s\n", strtemp);
  1893. FINFO("==IN==:{$}\n", strtemp);
  1894. return PACKET_ISPACKET;
  1895. }
  1896. }
  1897. return PACKET_NOPACKET;
  1898. }
  1899. //-----------------------------------------------------------------------------
  1900. // DecodeFrame
  1901. //-----------------------------------------------------------------------------
  1902. static bool DecodeFrame(const char* strFrame, int length)
  1903. {
  1904. auto pr = [strFrame, length](const tFrameMapping& Item)
  1905. {
  1906. for (int i = 0; i < Item.NbOfCharOfHead; i++)
  1907. {
  1908. if (strFrame[i] != Item.strHead[i])
  1909. {
  1910. return false;
  1911. }
  1912. }
  1913. return true;
  1914. };
  1915. auto found = std::find_if(arFrame.begin(), arFrame.end(), pr);
  1916. if (found == arFrame.end())
  1917. {
  1918. return false;
  1919. }
  1920. const auto& Item = *found;
  1921. auto pc = strFrame;
  1922. char data[100] = { 0 };
  1923. memcpy(data, strFrame + Item.NbOfCharOfHead, length - Item.NbOfCharOfHead);
  1924. Item.fun(data, length - Item.NbOfCharOfHead);
  1925. return true;
  1926. }
  1927. //-----------------------------------------------------------------------------
  1928. // GetIODriver & CreateIODriver
  1929. //-----------------------------------------------------------------------------
  1930. static nsGEN::CPIDriver gIODriver;
  1931. extern "C" CCOS::Dev::IODriver * GetIODriver()
  1932. {
  1933. return &gIODriver;
  1934. }
  1935. extern "C" CCOS::Dev::IODriver * CreateIODriver()
  1936. {
  1937. return new nsGEN::CPIDriver();
  1938. }