DIOS.Dev.Mech.HanQing3D.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  1. #include "stdafx.h"
  2. #include <assert.h>
  3. #include "Helper.JSON.hpp"
  4. #include "DIOS.Dev.Mech.HanQing3D.hpp"
  5. using namespace DIOS::Dev::Detail::MECH;
  6. namespace nsMech = DIOS::Dev::Detail::MECH;
  7. //关闭无关警告
  8. #pragma warning (disable:4244) // warning C4244: “初始化”: 从“double”转换到“float”,可能丢失数据
  9. #pragma warning (disable:4305) // warning C4305: “参数”: 从“double”到“float”截断
  10. #pragma warning (disable:4267) // warning C4267 : “初始化”: 从“size_t”转换到“int”,可能丢失数据
  11. #pragma warning (disable:4805) // warning C4805: “!=”: 在操作中将类型“bool”与类型“int”混合不安全
  12. //设置相关常量
  13. #define HanQing3D_TIMEOUTVALUE 100
  14. #define HanQing3D_LoopDefHBTime 500
  15. static const int msTimeOut_Lock = 200; //通讯接口锁定时间
  16. #define HanQing3D_Com_NormalLen 128
  17. #define HanQing3D_STX 0x02
  18. #define HanQing3D_ETX 0x03
  19. #define HanQing3D_SynClientName "HanQing_Syn"
  20. #define HanQing3D_SynHandSwitch1 "I0"
  21. #define HanQing3D_SynHandSwitch2 "I1"
  22. /*
  23. *串口/以太网通信帧格式:
  24. <STX><num><data><ETX>
  25. 解释:
  26. <STX> = 1 ASCII 0x02 帧头
  27. <num> = 2 ASCII 字符表示的命令ID
  28. <data> = 命令参数
  29. <ETX> = 1 ASCII 0x03 帧尾
  30. 串口通信设置:
  31. . 19200 波特率
  32. . No Parity
  33. . 8 Data Bits
  34. . 0 Stop Bit
  35. . 无握手过程
  36. */
  37. Log4CPP::Logger* mLog::gLogger = nullptr;
  38. //指令操作关联结构
  39. nsMech::tFrameMapItem::tFrameMapItem()
  40. {
  41. m_fFun = NULL;
  42. }
  43. nsMech::tFrameMapItem::tFrameMapItem(cbFun f)
  44. {
  45. m_fFun = f;
  46. }
  47. nsMech::tFrameMapItem& nsMech::tFrameMapItem::operator =(const tFrameMapItem& value)
  48. {
  49. m_fFun = value.m_fFun;
  50. return *this;
  51. }
  52. //响应操作对照表
  53. static std::map <string, tFrameMapItem> arFrame;
  54. //查找响应操作对照表执行对应操作
  55. static char CMDHeader[HanQing3D_Com_NormalLen] = { 0 };
  56. static bool DecodeFrame(const char* strFrame, int length)
  57. {
  58. memset(CMDHeader,0, HanQing3D_Com_NormalLen-1);
  59. strncpy_s(CMDHeader, strFrame+1, 3);
  60. auto found = arFrame.find(CMDHeader);//此处pr用来在arFrame中找到对于的包头
  61. if (found == arFrame.end())
  62. {
  63. return false;
  64. }
  65. //found->second.tChangeWaitState(0, data);
  66. int cmdHead = found->first.length();
  67. memcpy(CMDHeader, strFrame + cmdHead+1, length - cmdHead-2);
  68. found->second.m_fFun(CMDHeader, length - cmdHead);//第二个参数 不重要
  69. return true;
  70. }
  71. void DataSpliToNumbers(const char* name, vector<int>& arry)
  72. {
  73. string strtemp = name;
  74. string strName = strtemp;
  75. std::size_t oldPos = 0;
  76. std::size_t findPos = strtemp.find(",");
  77. while (findPos != string::npos)
  78. {
  79. strName = strtemp.substr(oldPos, findPos - oldPos);
  80. arry.push_back(atoi(strName.c_str()));
  81. if (findPos != strtemp.length())
  82. {
  83. oldPos = findPos + 1;
  84. findPos = strtemp.find(",", oldPos);
  85. if (findPos == string::npos)
  86. {
  87. strName = strtemp.substr(oldPos, strtemp.length() - oldPos);
  88. break;
  89. }
  90. }
  91. else
  92. {
  93. break;
  94. }
  95. }
  96. strName.erase(strName.length()-1);
  97. arry.push_back(atoi(strName.c_str()));
  98. }
  99. //-----------------------------------------------------------------------------
  100. // HanQing3DDevice
  101. //-----------------------------------------------------------------------------
  102. nsSerialGPM::CDeliverModule nsMech::HanQing3DDevice::m_tDelivermodule;
  103. int nsMech::HanQing3DDevice::m_iLoopTime = HanQing3D_LoopDefHBTime;
  104. nsMech::HanQing3DDevice::HanQing3DDevice(std::shared_ptr <IOEventCenter> center, nsSCF::SCF SCF, string configfile /*= ""*/):super(center, SCF)
  105. {
  106. assert(EventCenter);
  107. string version;
  108. if (GetVersion(version, hMyModule))
  109. mLog::Debug("\n===============log begin : version:{$} ===================\n", version.c_str());
  110. else
  111. mLog::Debug("\n===============log begin : version:0.0.0.0 ===================\n");
  112. ResDataObject Conftemp;
  113. if (!configfile.empty())
  114. {
  115. Conftemp.loadFile(configfile.c_str());
  116. m_MechConfig = Conftemp["CONFIGURATION"];
  117. TransJsonText(m_MechConfig);
  118. }
  119. //Mechanical
  120. m_MECHUnit.m_MechState.reset(new MechStateMould(AttrKey::MACHINE_STATUS_STANDBY, AttrKey::MACHINE_STATUS_SHUTDOWN, AttrKey::MACHINE_STATUS_SERVICE, 1));
  121. m_MECHUnit.m_TID.reset(new TIDMould(3.5, 0, 10000, 1));
  122. m_MECHUnit.m_SID.reset(new SIDMould(123.5, 0, 10000, 1));
  123. if (m_MechConfig.GetKeyCount("MechanicalAngle") > 0)
  124. {
  125. if (m_MechConfig["MechanicalAngle"].GetKeyCount("min") > 0)
  126. m_nMinAngle = m_MechConfig["MechanicalAngle"]["min"];
  127. if (m_MechConfig["MechanicalAngle"].GetKeyCount("max") > 0)
  128. m_nMaxAngle = m_MechConfig["MechanicalAngle"]["max"];
  129. mLog::Debug("config:MechanicalAngle[min:{$},max:{$}]", m_nMinAngle, m_nMaxAngle);
  130. }
  131. m_MECHUnit.m_TubeAngle.reset(new TubeAngleMould(90, m_nMinAngle, m_nMaxAngle, 1));
  132. if (m_MechConfig.GetKeyCount("MechanicalHeight") > 0)
  133. {
  134. if (m_MechConfig["MechanicalHeight"].GetKeyCount("min") > 0)
  135. m_nMinHeight = m_MechConfig["MechanicalHeight"]["min"];
  136. if (m_MechConfig["MechanicalHeight"].GetKeyCount("max") > 0)
  137. m_nMaxHeight = m_MechConfig["MechanicalHeight"]["max"];
  138. mLog::Debug("config:MechanicalHeight[min:{$},max:{$}]", m_nMinHeight, m_nMaxHeight);
  139. }
  140. m_MECHUnit.m_TubeHeight.reset(new TubeHeightMould(150, m_nMinHeight, m_nMaxHeight, 1));
  141. m_MECHUnit.m_PostionNumber.reset(new POSITIONNUMBERMould(0, 0, 1000, 1));
  142. //TOMO
  143. m_MECHUnit.m_Tomo_Type.reset(new TOMOTypeMould(31, 0, 100, 1));
  144. m_MECHUnit.m_Tomo_ScanDirection.reset(new TOMOScanDirectionMould(AttrKey::TOMODIR_NULL, AttrKey::TOMODIR_NULL, AttrKey::Grid_MAX, 1));
  145. m_MECHUnit.m_Tomo_FrameCount.reset(new TOMOFrameCountMould(30, 0, 100, 1));
  146. m_MECHUnit.m_Tomo_FrameRate.reset(new TOMOFrameRateMould(4, 0, 60, 1));
  147. m_MECHUnit.m_Tomo_ScanAngle.reset(new TOMOScanAngleMould(15, -180, 180, 1));
  148. m_MECHUnit.m_Tomo_ScanImageNumber.reset(new TOMOScanImageNumberMould(29, 0, 100, 1));
  149. //拼接
  150. m_MECHUnit.m_StitchingState.reset(new StitchingStateMould(AttrKey::STITCHSTATE_ERR, AttrKey::STITCHSTATE_ERR, AttrKey::STITCHSTATE_MAX, 1));
  151. m_MECHUnit.m_StitchHeight.reset(new StitchHeightMould(0, 0, 1000, 1));
  152. m_MECHUnit.m_StitchLength.reset(new StitchLengthMould(0, 0, 1000, 1));
  153. m_MECHUnit.m_StitchOverLap.reset(new StitchOverLapMould(0, 0, 1000, 1));
  154. m_MECHUnit.m_StitchPID.reset(new StitchPIDMould(0, 0, 1000, 1));
  155. m_MECHUnit.m_StitchDirection.reset(new StitchDirectionMould(AttrKey::STITCHDIR_NULL, AttrKey::STITCHDIR_NULL, AttrKey::STITCHDIR_MAX, 1));
  156. m_MECHUnit.m_StitchType.reset(new StitchTypeMould(AttrKey::STITCHTYPE_NULL, AttrKey::STITCHTYPE_NULL, AttrKey::STITCHTYPE_MAX, 1));
  157. m_MECHUnit.m_StitchStepLength.reset(new StitchStepLengthMould(0, 0, 1000, 1));
  158. m_MECHUnit.m_FPDPosition.reset(new FPDPositionMould(0, 0, 100, 1));
  159. m_MECHUnit.m_TotalImageCount.reset(new TotalImageCountMould(0, 0, 100, 1));
  160. m_MECHUnit.m_CurrentImageNumber.reset(new CurrentImageNumberMould(0, 0, 100, 1));
  161. //上报错误消息
  162. m_MSGUnit.reset(new nsDetail::MSGUnit(center, MechUnitType));
  163. //串口数据处理类初始化
  164. m_tDelivermodule.InitSendModle(this ,&ProcessClientData, WriteLog);
  165. m_nCMDType_WaitTime = m_tDelivermodule.SetPriority(true, false, false, true, 100);
  166. m_nCMDType_HB = m_tDelivermodule.SetPriority(false, true, false, true, 100);
  167. m_nCMDType_WaitACK = m_tDelivermodule.SetPriority(true, false, 3, false, 0, true, 1000);
  168. mLog::Debug("m_nCMDType_WaitTime[{$}]m_nCMDType_HB[{$}] m_nCMDType_WaitACK[{$}]", m_nCMDType_WaitTime, m_nCMDType_HB, m_nCMDType_WaitACK);
  169. //配置响应操作对照表
  170. OnCallBack();
  171. //将机架可以对外提供的指令注册集进行补充
  172. Register();
  173. m_hExitEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  174. m_hMoveBeginEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  175. m_pHardwareStatusThread = NULL;
  176. //启动硬件状态轮询进程
  177. StartHardwareStatusThread();
  178. //设置初始参数
  179. RefreshData();
  180. }
  181. nsMech::HanQing3DDevice::~HanQing3DDevice()
  182. {
  183. #if Dios_V3
  184. if (m_pSynClient)
  185. {
  186. if (!m_pSynClient->IsClosed())
  187. {
  188. m_pSynClient->Close();
  189. }
  190. delete m_pSynClient;
  191. m_pSynClient = NULL;
  192. }
  193. #endif
  194. ResetEvent(m_hMoveBeginEvent);
  195. SetEvent(m_hExitEvent);
  196. Sleep(m_iLoopTime);
  197. m_tDelivermodule.EixtSendModle();
  198. mLog::Debug("\n===============log end ===================\n");
  199. }
  200. std::string HanQing3DDevice::GetGUID() const
  201. {
  202. mLog::Debug("\n===============GetGUID : {$} ===================\n", StitchMechUnitType);
  203. return StitchMechUnitType;
  204. }
  205. RET_STATUS nsMech::HanQing3DDevice::RefreshData()
  206. {
  207. #if Dios_V3
  208. m_pSynClient = new LogicClient(HanQing3D_SynClientName, "NSQ", "", false);
  209. if (m_pSynClient->Open("diosChannel", ALL_ACCESS))
  210. {
  211. mLog::Debug("Dios_V3 Create {$} Client success", HanQing3D_SynClientName);
  212. }
  213. else
  214. {
  215. mLog::Debug("Dios_V3 Create {$} Client failed", HanQing3D_SynClientName);
  216. }
  217. #endif
  218. if (m_MechConfig.GetKeyCount(AttrKey::TID) > 0)
  219. {
  220. float tempValue = (float)m_MechConfig[AttrKey::TID];
  221. m_MECHUnit.m_TID->Update(tempValue);
  222. }
  223. GetAllAxisPosition();
  224. return RET_STATUS::RET_SUCCEED;
  225. }
  226. void nsMech::HanQing3DDevice::Register()
  227. {
  228. auto Disp = &Dispatch;
  229. superMech::RegisterExamInfo(Disp);
  230. superMech::RegisterNormalControl(Disp);
  231. superMech::RegisterTOMOControl(Disp);
  232. superMech::RegisterStitchingControl(Disp);
  233. Disp->Get.Push(m_MSGUnit->GetKey().c_str(), [this](std::string& out) { out = m_MSGUnit->JSGet(); return RET_STATUS::RET_SUCCEED; });
  234. Disp->Get.Push("StitchPlanMinAngle", [this](std::string& out) {
  235. out = to_string(m_nMinAngle);
  236. return RET_STATUS::RET_SUCCEED; });
  237. Disp->Get.Push("StitchPlanMaxAngle", [this](std::string& out) {
  238. out = to_string(m_nMaxAngle);
  239. return RET_STATUS::RET_SUCCEED; });
  240. Disp->Get.Push("StitchPlanMinHeight", [this](std::string& out) {
  241. out = to_string(m_nMinHeight);
  242. return RET_STATUS::RET_SUCCEED; });
  243. Disp->Get.Push("StitchPlanMaxHeight", [this](std::string& out) {
  244. out = to_string(m_nMaxHeight);
  245. return RET_STATUS::RET_SUCCEED; });
  246. auto fun_SendStitchState = [this](auto in, auto& out)
  247. {
  248. FireNotify(AttrKey::STITCHINGSTATE, m_MECHUnit.m_StitchingState->Get());
  249. return RET_STATUS::RET_SUCCEED;
  250. };
  251. Disp->Action.Push("SendStitchState", fun_SendStitchState);
  252. //3D特有
  253. auto fun_StopAllAxis = [this](auto in, auto& out)
  254. {
  255. return StopAllAxis();
  256. };
  257. Disp->Action.Push("StopAllAxis", fun_StopAllAxis);
  258. auto fun_MoveToRelativeDistance = [this](auto in, auto& out)
  259. {
  260. int value[2]{0};
  261. ResDataObject json;
  262. json.decode(in.c_str());
  263. //mLog::Debug("MoveToRelativeDistance:[{$}]",json.encode());
  264. int paramCount = json.GetKeyCount("P0");
  265. for (int i = 0; i < paramCount; i++)
  266. {
  267. value[i] = json[i];
  268. }
  269. return MoveToRelativeDistance(value[0], value[1]);
  270. };
  271. Disp->Action.Push("MoveToRelativeDistance", fun_MoveToRelativeDistance);
  272. auto fun_MoveToPositionNumber = [this](auto in, auto& out)
  273. {
  274. int value[2]{ 0 };
  275. ResDataObject json;
  276. json.decode(in.c_str());
  277. int paramCount = json.GetKeyCount("P0");
  278. for (int i = 0; i < paramCount; i++)
  279. {
  280. value[i] = json[i];
  281. }
  282. return MoveToPositionNumber(value[0], value[1]);
  283. };
  284. Disp->Action.Push("MoveToPositionNumber", fun_MoveToPositionNumber);
  285. auto fun_GetAllAxisPosition = [this](auto in, auto& out)
  286. {
  287. return GetAllAxisPosition();
  288. };
  289. Disp->Action.Push("GetAllAxisPosition", fun_GetAllAxisPosition);
  290. auto fun_SetPositionNumber = [this](auto in, auto& out)
  291. {
  292. int value[5]{ 0 };
  293. ResDataObject json;
  294. json.decode(in.c_str());
  295. int paramCount = json.GetKeyCount("P0");
  296. for (int i = 0; i < paramCount; i++)
  297. {
  298. value[i] = json[i];
  299. }
  300. return SetAxisPositionNumber(value[0], value[1], value[2], value[3], value[4]);
  301. };
  302. Disp->Action.Push("SetAxisPosition", fun_SetPositionNumber);
  303. }
  304. void nsMech::HanQing3DDevice::FireNotify(std::string key, std::string content)
  305. {
  306. EventCenter->OnNotify(1, key, content);
  307. }
  308. void nsMech::HanQing3DDevice::FireNotify(std::string key, const int content)
  309. {
  310. std::string str = std::format("{:d}", content);
  311. EventCenter->OnNotify(1, key, str);
  312. }
  313. void nsMech::HanQing3DDevice::FireNotify(std::string key, const float content)
  314. {
  315. std::string str = std::format("{:f}", content);
  316. EventCenter->OnNotify(1, key, str);
  317. }
  318. void nsMech::HanQing3DDevice::FireErrorMessage(const bool Act, const int Code, const char* ResInfo)
  319. {
  320. string ErrorCode("Mech_ERR_");
  321. ErrorCode += std::to_string(Code);
  322. int level = OTC_MG_REGULATION_LEVEL::REG_ERRO;
  323. if (Act)
  324. {
  325. mLog::Error("add {$}:{$}", ErrorCode.c_str(), ResInfo);
  326. m_MSGUnit->AddErrorMessage(ErrorCode.c_str(), level, ResInfo);
  327. }
  328. else
  329. {
  330. mLog::Error("del {$}:{$}", ErrorCode.c_str(), ResInfo);
  331. if(Code == 0)
  332. m_MSGUnit->DelErrorMessage("0", level, ResInfo);
  333. else
  334. m_MSGUnit->DelErrorMessage(ErrorCode.c_str(), level, ResInfo);
  335. }
  336. }
  337. void nsMech::HanQing3DDevice::FireWarnMessage(const bool Act, const int Code, const char* ResInfo)
  338. {
  339. string ErrorCode("Mech_WAR_");
  340. ErrorCode += std::to_string(Code);
  341. int level = OTC_MG_REGULATION_LEVEL::REG_WARN;
  342. if (Act)
  343. {
  344. mLog::Error("add {$}:{$}", ErrorCode.c_str(), ResInfo);
  345. m_MSGUnit->AddWarnMessage(ErrorCode.c_str(), level, ResInfo);
  346. }
  347. else
  348. {
  349. mLog::Error("del {$}:{$}", ErrorCode.c_str(), ResInfo);
  350. m_MSGUnit->DelWarnMessage(ErrorCode.c_str(), level, ResInfo);
  351. }
  352. }
  353. RET_STATUS nsMech::HanQing3DDevice::HWSend(const char* strCommand, int lengh, bool reSend, int nTimeOut)
  354. {
  355. if (!m_SCF)
  356. return RET_STATUS::RET_FAILED;
  357. char strSendCommand[HanQing3D_Com_NormalLen] = { 0 };
  358. int copyCode = memcpy_s(strSendCommand + 1, HanQing3D_Com_NormalLen - 3, strCommand, lengh);
  359. if (copyCode)
  360. {
  361. mLog::Error("copy cmd[{$}][{$}] error[{$}]", strCommand, lengh, copyCode);
  362. return RET_STATUS::RET_FAILED;
  363. }
  364. strSendCommand[0] = HanQing3D_STX;
  365. strSendCommand[lengh + 1] = HanQing3D_ETX;
  366. if (strncmp(strCommand, "FFF", 3) != 0)
  367. {
  368. if (reSend)
  369. {
  370. mLog::Debug("==OUT==: [{$}] wait for ACK \n", strSendCommand);
  371. //find_item->second.tChangeWaitState(1, strCommand, lengh);
  372. }
  373. else
  374. {
  375. mLog::Debug("==OUT==: [{$}] \n", strSendCommand);
  376. }
  377. }
  378. int retLength;
  379. m_SCF.Lock(msTimeOut_Lock)
  380. .SendPacket(strSendCommand, lengh+2, nTimeOut, retLength);
  381. //Sleep(nTimeOut);
  382. return RET_STATUS::RET_SUCCEED;
  383. }
  384. void HanQing3DDevice::OnCallBack()
  385. {
  386. mLog::Debug("Enter OnCallBack");
  387. auto HWNotProcess = [](const char* value, int length) -> void
  388. {
  389. mLog::Warn(" This commands didn't need to process!");
  390. };
  391. auto HWAPS = [this](const char* value, int length) -> void
  392. {
  393. //assert(value && length >= 3);
  394. int data = atoi(value + 2);
  395. mLog::Debug("begin tomo[{$}]", data);
  396. if (data == 1)
  397. {
  398. FireNotify(AttrKey::TomoTID, m_MECHUnit.m_TID->Get());//3.5
  399. FireNotify(AttrKey::TomoSID, m_MECHUnit.m_SID->Get());//123.5
  400. FireNotify(AttrKey::TomoScanDirection, m_MECHUnit.m_Tomo_ScanDirection->Get());//m_nDirection
  401. FireNotify(AttrKey::TomoScanAngle, m_MECHUnit.m_Tomo_ScanAngle->Get());//15
  402. FireNotify(AttrKey::TomoScanImageNumber, m_MECHUnit.m_Tomo_FrameCount->Get() - 1);
  403. }
  404. else if (data == 2)
  405. {
  406. m_mTomoResult.clear();
  407. m_MECHUnit.m_Tomo_ScanImageNumber->Update(0);
  408. }
  409. else if (data == 0)
  410. {
  411. FireNotify(AttrKey::TomoScanImageNumber, m_MECHUnit.m_Tomo_ScanImageNumber->Get());
  412. }
  413. };
  414. auto HWAPE = [this](const char* value, int length) -> void
  415. {
  416. //assert(value && length >= 3);
  417. mLog::Debug("end tomo");
  418. FireNotify(AttrKey::TomoScanImageNumber, m_MECHUnit.m_Tomo_ScanImageNumber->Get());
  419. };
  420. auto HWSPA = [this](const char* value, int length) -> void
  421. {
  422. //assert(value && length >= 3);
  423. vector<int> data;
  424. DataSpliToNumbers(value+2, data);
  425. mLog::Debug("mechine position[{$}]", value + 2);
  426. FireNotify(AttrKey::TUBEHEIGHT, data[2]);
  427. FireNotify(AttrKey::TUBEANGLE, data[3]);
  428. for (int i = 0; i < AxisCount; i++)
  429. {
  430. MechStatus[i].SetPosition(data[i]);
  431. FireNotify("Pos" + MechStatus[i].GetName(), data[i]);
  432. }
  433. };
  434. auto HWERR = [this](const char* value, int length) -> void
  435. {
  436. //assert(value && length >= 3);
  437. int data = atoi(value + 2);
  438. mLog::Debug("Mech get error[{$}]", data);
  439. if (data > 0)
  440. {
  441. FireErrorMessage(true, data, "get Mech error");
  442. }
  443. else
  444. {
  445. FireErrorMessage(false, data, "del Mech error");
  446. }
  447. };
  448. auto HWSTA = [this](const char* value, int length) -> void
  449. {
  450. //assert(value && length >= 3);
  451. vector<int> data;
  452. DataSpliToNumbers(value + 2, data);
  453. mLog::Debug("mechine status[{$}]", value + 2);
  454. int moveCount = 0;
  455. for (int i = 0; i < AxisCount; i++)
  456. {
  457. MechStatus[i].SetState(data[i]);
  458. if (MechStatus[i].GetState() == Axis::AXIS_MOVE)
  459. {
  460. moveCount++;
  461. }
  462. FireNotify("Status"+ MechStatus[i].GetName(), data[i]);
  463. }
  464. if (moveCount == 0)
  465. {
  466. ResetEvent(m_hMoveBeginEvent);
  467. }
  468. else
  469. {
  470. SetEvent(m_hMoveBeginEvent);
  471. }
  472. };
  473. auto HWHAD = [this](const char* value, int length) -> void
  474. {
  475. //assert(value && length >= 3);
  476. int data = atoi(value + 2);
  477. HAND_SWITCH emHS = HAND_MAX;
  478. switch (data)
  479. {
  480. case 0:
  481. {
  482. emHS = HAND_OFF;
  483. SetFunToOtherUnit(HanQing3D_SynHandSwitch2, 0);
  484. SetFunToOtherUnit(HanQing3D_SynHandSwitch1, 0);
  485. }break;
  486. case 10:
  487. {
  488. emHS = HAND_DWON1;
  489. SetFunToOtherUnit(HanQing3D_SynHandSwitch1, 1);
  490. }break;
  491. case 11:
  492. {
  493. emHS = HAND_DWON2;
  494. SetFunToOtherUnit(HanQing3D_SynHandSwitch2,1);
  495. }break;
  496. }
  497. };
  498. auto HWINE = [this](const char* value, int length) -> void
  499. {
  500. //assert(value && length >= 3);
  501. vector<int> data;
  502. DataSpliToNumbers(value + 2, data);
  503. char index[3]{ 0 };
  504. strncpy(index, value, 2);
  505. int countNum = atoi(index);
  506. if (countNum >= 2)
  507. {
  508. mLog::Debug("tomo Exp info[{$}:{$},{$}]", countNum, data[0], data[1]);
  509. m_MECHUnit.m_Tomo_ScanImageNumber->Update(countNum - 1);
  510. m_mTomoResult[countNum - 1] = tTomoResult(data[0] / 1000.0 - 90, data[1] / 100.0);
  511. }
  512. };
  513. auto HWUSC = [this](const char* value, int length) -> void
  514. {
  515. //assert(value && length >= 3);
  516. int data = atoi(value);
  517. if (m_MECHUnit.m_CurrentImageNumber->Update(data))
  518. {
  519. mLog::Debug("stitching current image[{$}:{$}]", m_MECHUnit.m_CurrentImageNumber->Get(), m_MECHUnit.m_TotalImageCount->Get());
  520. FireNotify(AttrKey::CURRENTIMAGENUMBER, data);
  521. }
  522. if (m_MECHUnit.m_CurrentImageNumber->Get() >= m_MECHUnit.m_TotalImageCount->Get())
  523. {
  524. EndStitching();
  525. }
  526. };
  527. auto HWUSN = [this](const char* value, int length) -> void
  528. {
  529. //assert(value && length >= 3);
  530. int data = atoi(value);
  531. if (m_MECHUnit.m_TotalImageCount->Update(data))
  532. {
  533. mLog::Debug("stitching current image[{$}:{$}]", m_MECHUnit.m_CurrentImageNumber->Get(), m_MECHUnit.m_TotalImageCount->Get());
  534. FireNotify(AttrKey::CURRENTIMAGENUMBER, m_MECHUnit.m_CurrentImageNumber->Get());
  535. FireNotify(AttrKey::TOTALIMAGECOUNT, data);
  536. }
  537. };
  538. auto HWUST = [this](const char* value, int length) -> void
  539. {
  540. //assert(value && length >= 3);
  541. int data = atoi(value);
  542. if (m_MECHUnit.m_StitchingState->Update(data))
  543. {
  544. int status = -1;
  545. switch (data)
  546. {
  547. case 8: //拼接错误
  548. {
  549. status = AttrKey::STITCHSTATE_ERR;
  550. mLog::Error("get error");
  551. FireErrorMessage(true, 100, "get Mech error");
  552. }break;
  553. case 6: //拼接初始化,此时设置拼接相应参数
  554. {
  555. status = AttrKey::STITCHSTATE_INIT;
  556. m_MECHUnit.m_strInfo = "Stitch Init";
  557. }break;
  558. case 7: //拼接就绪,参数设置成功
  559. {
  560. status = AttrKey::STITCHSTATE_STANDBY;
  561. m_MECHUnit.m_strInfo = "Stitch Standby, Need Moving to the initial position";
  562. }break;
  563. case 1: //拼接开始
  564. {
  565. status = AttrKey::STITCHSTATE_START;
  566. m_MECHUnit.m_strInfo = "Stitch Start";
  567. }break;
  568. case 2: //拼接机架需要向指定位置运动
  569. {
  570. status = AttrKey::STITCHSTATE_NEED_MOVE;
  571. m_MECHUnit.m_strInfo = "Stitch Need Move";
  572. }break;
  573. case 3: //拼接机架正在运动
  574. {
  575. status = AttrKey::STITCHSTATE_MOVING;
  576. m_MECHUnit.m_strInfo = "Stitch Moving";
  577. }break;
  578. case 9: //拼接机架到达初始位置
  579. {
  580. status = AttrKey::STITCHSTATE_REACH_INIT;
  581. m_MECHUnit.m_strInfo = "Stitch Reach to the initial position";
  582. }break;
  583. case 4: //拼接机架到达下一个位置
  584. {
  585. status = AttrKey::STITCHSTATE_REACH_NEXT;
  586. m_MECHUnit.m_strInfo = "Stitch Reach to the next position";
  587. }break;
  588. case 11: //拼接机架到达额外位置
  589. {
  590. status = AttrKey::STITCHSTATE_REACH_EXTRA;
  591. m_MECHUnit.m_strInfo = "Stitch Reach to the Extra position";
  592. }break;
  593. case 5: //拼接完成
  594. {
  595. status = AttrKey::STITCHSTATE_END;
  596. m_MECHUnit.m_strInfo = "Stitch Finish";
  597. }break;
  598. default:
  599. break;
  600. }
  601. if (m_MECHUnit.m_StitchingState->Update(status))
  602. {
  603. FireNotify(AttrKey::STITCHINGSTATE, status);
  604. }
  605. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  606. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  607. if (data == 4 || data == 11)
  608. {
  609. m_MECHUnit.m_strInfo = "Stitch Ready";
  610. if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_READY))
  611. {
  612. Sleep(2000);
  613. FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_READY);
  614. }
  615. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  616. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  617. }
  618. }
  619. };
  620. // 有部分前缀是包含关系, 长的包含短的, 因此长的在前面, 短的在后面
  621. // Device 是个短寿命对象, 而 arFrame 是静态变量 !!!
  622. // 因此, 在添加到 arFrame 之前, 务必先清零 !!!
  623. arFrame.clear();
  624. arFrame["APS"] = tFrameMapItem(HWAPS);
  625. arFrame["APE"] = tFrameMapItem(HWAPE);
  626. arFrame["SPA"] = tFrameMapItem(HWSPA);
  627. arFrame["ERR"] = tFrameMapItem(HWERR);
  628. arFrame["STA"] = tFrameMapItem(HWSTA);
  629. arFrame["HAD"] = tFrameMapItem(HWHAD);
  630. arFrame["INE"] = tFrameMapItem(HWINE);
  631. arFrame["USC"] = tFrameMapItem(HWUSC);
  632. arFrame["USN"] = tFrameMapItem(HWUSN);
  633. arFrame["UST"] = tFrameMapItem(HWUST);
  634. arFrame["FFF"] = tFrameMapItem(HWNotProcess);//命令格式:校验和错误
  635. }
  636. //串口数据处理类对外接口
  637. void __stdcall nsMech::HanQing3DDevice::ProcessClientData(const char* pData, unsigned long nDataLength, void* lparam)
  638. {
  639. HanQing3DDevice* pCurMech = (HanQing3DDevice*)lparam;
  640. pCurMech->HWSend(pData, nDataLength);
  641. }
  642. void __stdcall nsMech::HanQing3DDevice::WriteLog(const char* pData, nsSerialGPM::LOG_V2_LEVEL level)
  643. {
  644. switch (level)
  645. {
  646. case nsSerialGPM::LOG_V2_FATAL:
  647. case nsSerialGPM::LOG_V2_ERROR:
  648. mLog::Error(pData);
  649. break;
  650. case nsSerialGPM::LOG_V2_WARNING:
  651. mLog::Warn(pData);
  652. break;
  653. case nsSerialGPM::LOG_V2_DEBUG:
  654. mLog::Debug(pData);
  655. break;
  656. case nsSerialGPM::LOG_V2_INFO:
  657. mLog::Debug(pData);
  658. break;
  659. default:
  660. break;
  661. }
  662. }
  663. RET_STATUS nsMech::HanQing3DDevice::HWSendWaittimeCMD(char* strCommand, int lengh, int headLengh)
  664. {
  665. return m_tDelivermodule.ProcessCommand(strCommand, lengh, m_nCMDType_WaitTime, headLengh);
  666. }
  667. RET_STATUS nsMech::HanQing3DDevice::HWSendHBCMD(char* strCommand, int lengh, int headLengh)
  668. {
  669. return m_tDelivermodule.ProcessCommand(strCommand, lengh, m_nCMDType_HB, headLengh);
  670. }
  671. RET_STATUS nsMech::HanQing3DDevice::HWSendWaitACKCMD(char* strCommand, int lengh, int headLengh)
  672. {
  673. return m_tDelivermodule.ProcessCommand(strCommand, lengh, m_nCMDType_WaitACK, headLengh);
  674. }
  675. bool nsMech::HanQing3DDevice::StartHardwareStatusThread()
  676. {
  677. mLog::Debug("Enter Start HardwareStatus Thread ");
  678. if (m_pHardwareStatusThread == NULL)
  679. {
  680. DWORD m_HardwareStatusID;
  681. m_pHardwareStatusThread = CreateThread(0, 0, HardwareStatusThread, this, 0, &m_HardwareStatusID);
  682. if (m_pHardwareStatusThread == NULL)
  683. {
  684. mLog::Error("Start HardwareStatus Thread Failed");
  685. return false;
  686. }
  687. }
  688. return true;
  689. }
  690. DWORD nsMech::HanQing3DDevice::HardwareStatusThread(LPVOID pParam)
  691. {
  692. HanQing3DDevice* pCurMech = (HanQing3DDevice*)pParam;
  693. if (pCurMech == NULL)
  694. {
  695. return false;
  696. }
  697. mLog::Debug("HardwareStatusThread start");
  698. if ((int)pCurMech->m_MechConfig["loopTime"] >= 0)
  699. {
  700. pCurMech->m_iLoopTime = (int)pCurMech->m_MechConfig["loopTime"];
  701. }
  702. mLog::Debug("loopTime = {$}", pCurMech->m_iLoopTime);
  703. HANDLE hEventArray[2];
  704. hEventArray[0] = pCurMech->m_hExitEvent;
  705. hEventArray[1] = pCurMech->m_hMoveBeginEvent;
  706. while (pCurMech->m_iLoopTime)
  707. {
  708. DWORD dwResult = WaitForMultipleObjects(2, hEventArray, FALSE, INFINITE);
  709. switch (dwResult)
  710. {
  711. case WAIT_OBJECT_0:
  712. {
  713. mLog::Debug("get exit event, HardwareStatusThread stop");
  714. return true;
  715. }break;
  716. case WAIT_OBJECT_0 + 1:
  717. {
  718. Sleep(pCurMech->m_iLoopTime);
  719. mLog::Debug("loop get pos");
  720. pCurMech->GetAllAxisPosition();
  721. }break;
  722. }
  723. }
  724. mLog::Debug("HardwareStatusThread stop");
  725. return true;
  726. }
  727. //检查信息接口
  728. RET_STATUS nsMech::HanQing3DDevice::SetStudyInfo(ResDataObject& pParam)
  729. {
  730. auto& P0 = pParam[0];
  731. SetPatientInfo(P0);
  732. return RET_STATUS::RET_SUCCEED;
  733. }
  734. RET_STATUS nsMech::HanQing3DDevice::SetViewInfo(ResDataObject& pParam)
  735. {
  736. return RET_STATUS::RET_SUCCEED;
  737. }
  738. RET_STATUS nsMech::HanQing3DDevice::SetPatientInfo(ResDataObject& pParam)
  739. {
  740. ECOM_PATIENT Info;
  741. Info.strPatientName = (string)pParam["PatientName"];
  742. Info.strSex = (string)pParam["Sex"];
  743. Info.strAge = (string)pParam["Age"];
  744. Info.strPatientID = (string)pParam["PatientID"];
  745. Info.strPatientSize = (string)pParam["PatientSize"];
  746. Info.strStudyDescription = (string)pParam["StudyDescription"];
  747. Info.strAccessionNumber = (string)pParam["AccessionNumber"];
  748. mLog::Debug("Enter SetPatientInfo:[Name:{$}],[Sex:{$}],[Age:{$}],[ID:{$}],[Size:{$}],[Description:{$}],[AccessionNumber:{$}]",
  749. Info.strPatientName.c_str(), Info.strSex.c_str(), Info.strAge.c_str(), Info.strPatientID.c_str(), Info.strPatientSize.c_str(),
  750. Info.strStudyDescription.c_str(), Info.strAccessionNumber.c_str());
  751. return RET_STATUS::RET_SUCCEED;
  752. }
  753. //基本
  754. RET_STATUS nsMech::HanQing3DDevice::Reset()
  755. {
  756. mLog::Debug("Enter Reset");
  757. FireErrorMessage(false, 0, "clear all Mech error");
  758. mLog::Debug("End Reset");
  759. return RET_STATUS::RET_SUCCEED;
  760. }
  761. RET_STATUS nsMech::HanQing3DDevice::SetTechParamsInfo(ResDataObject& pParam)
  762. {
  763. return RET_STATUS::RET_SUCCEED;
  764. }
  765. RET_STATUS nsMech::HanQing3DDevice::SetPositionNumber(int pn)
  766. {
  767. mLog::Debug("Enter SetPositionNumber:[{$}]", pn);
  768. m_MECHUnit.m_PostionNumber->Update(pn);
  769. char temp[50] = { 0 };
  770. sprintf_s(temp, "PSN99%05d", pn);
  771. return HWSendWaittimeCMD(temp, strlen(temp));
  772. FireNotify(AttrKey::POSITIONNUMBER, m_MECHUnit.m_PostionNumber->Get());
  773. return RET_STATUS::RET_SUCCEED;
  774. }
  775. RET_STATUS nsMech::HanQing3DDevice::SetSID(float value)
  776. {
  777. mLog::Debug("Enter SetSID:[{$}]", value);
  778. m_MECHUnit.m_SID->Update(value);
  779. return RET_STATUS::RET_SUCCEED;
  780. }
  781. RET_STATUS nsMech::HanQing3DDevice::SetSOD(float value)
  782. {
  783. return RET_STATUS::RET_SUCCEED;
  784. }
  785. RET_STATUS nsMech::HanQing3DDevice::MoveToHome(string& value)
  786. {
  787. int PosNum = atoi(value.c_str());
  788. mLog::Debug("Enter MoveToHome:[{$}]", PosNum);
  789. if (PosNum > 0 && PosNum < 100)
  790. {
  791. char temp[50] = { 0 };
  792. sprintf_s(temp, "HOM99%02d", PosNum);
  793. return HWSendWaittimeCMD(temp, strlen(temp));
  794. }
  795. return RET_STATUS::RET_FAILED;
  796. }
  797. RET_STATUS nsMech::HanQing3DDevice::MoveMech(string& value)
  798. {
  799. return RET_STATUS::RET_SUCCEED;
  800. }
  801. RET_STATUS nsMech::HanQing3DDevice::StopMech(string& value)
  802. {
  803. return RET_STATUS::RET_SUCCEED;
  804. }
  805. RET_STATUS nsMech::HanQing3DDevice::SetGrid(AttrKey::MECH_GRIDSTATE GridState)
  806. {
  807. return RET_STATUS::RET_SUCCEED;
  808. }
  809. RET_STATUS nsMech::HanQing3DDevice::SetAutoTracking(int nAutoTracking)
  810. {
  811. mLog::Debug("Enter MoveToHome:[{$}]", nAutoTracking);
  812. char temp[50] = { 0 };
  813. sprintf_s(temp, "USI");
  814. return HWSendWaittimeCMD(temp, strlen(temp));
  815. }
  816. //Tomo
  817. RET_STATUS nsMech::HanQing3DDevice::SetTomoEnable(bool enable)
  818. {
  819. if (m_MECHUnit.m_MechState->Update(AttrKey::MACHINE_STATUS_3DDR))
  820. FireNotify(AttrKey::MACHSTATE, m_MECHUnit.m_MechState->Get());
  821. mLog::Debug("Enter SetTomoEnable:[{$}]", enable);
  822. if (enable >= 0 && enable <= 1)
  823. {
  824. char temp[50] = { 0 };
  825. sprintf_s(temp, "APR99%02d", enable);
  826. return HWSendWaittimeCMD(temp, strlen(temp));
  827. }
  828. return RET_STATUS::RET_FAILED;
  829. }
  830. RET_STATUS nsMech::HanQing3DDevice::SetTomoExpMode(string& value)
  831. {
  832. mLog::Debug("Enter SetTomoExpMode:[{$}]", value.c_str());
  833. char temp[50] = { 0 };
  834. if (value == "Single")
  835. {
  836. sprintf_s(temp, "APM9920,00,00,00");
  837. return HWSendWaittimeCMD(temp, strlen(temp));
  838. }
  839. else if(value == "TOMO")
  840. {
  841. mLog::Debug("Enter SetTomoExpMode:[{$},{$},{$},{$}]",
  842. m_MECHUnit.m_Tomo_Type->Get(), m_MECHUnit.m_Tomo_ScanAngle->Get(), m_MECHUnit.m_Tomo_FrameCount->Get(), m_MECHUnit.m_Tomo_FrameRate->Get());
  843. sprintf_s(temp, "APM99%02d,%02d,%02d,%02d",
  844. m_MECHUnit.m_Tomo_Type->Get(), m_MECHUnit.m_Tomo_ScanAngle->Get(), m_MECHUnit.m_Tomo_FrameCount->Get(), m_MECHUnit.m_Tomo_FrameRate->Get());
  845. return HWSendWaittimeCMD(temp, strlen(temp));
  846. }
  847. return RET_STATUS::RET_SUCCEED;
  848. }
  849. RET_STATUS nsMech::HanQing3DDevice::SetTomoTechnical(string& value)
  850. {
  851. ResDataObject json;
  852. json.decode(value.c_str());
  853. mLog::Debug("Enter SetTomoTechnical:[{$}]", json.encode());
  854. string strValue = (string)json[AttrKey::TomoViewId];
  855. if (strValue == "2D")
  856. {
  857. m_MECHUnit.m_Tomo_Type->Update(20);
  858. }
  859. else if (strValue == "3D")
  860. {
  861. m_MECHUnit.m_Tomo_Type->Update(31);
  862. }
  863. m_MECHUnit.m_Tomo_ScanAngle->Update(atoi(((string)json[AttrKey::TomoRotateAngle]).c_str()));
  864. m_MECHUnit.m_Tomo_FrameCount->Update(atoi(((string)json[AttrKey::TomoFrameCount]).c_str()));
  865. m_MECHUnit.m_Tomo_FrameRate->Update(atoi(((string)json[AttrKey::TomoFrameRate]).c_str()));
  866. m_MECHUnit.m_SID->Update(atoi(((string)json[AttrKey::SID]).c_str()));
  867. char temp[50] = { 0 };
  868. sprintf_s(temp, "APM99%02d,%02d,%02d,%02d",
  869. m_MECHUnit.m_Tomo_Type->Get(), m_MECHUnit.m_Tomo_ScanAngle->Get(), m_MECHUnit.m_Tomo_FrameCount->Get(), m_MECHUnit.m_Tomo_FrameRate->Get());
  870. return HWSendWaittimeCMD(temp, strlen(temp));
  871. }
  872. RET_STATUS nsMech::HanQing3DDevice::GetTomoResults(std::string& result)
  873. {
  874. if (m_MECHUnit.m_MechState->Update(AttrKey::MACHINE_STATUS_STANDBY))
  875. FireNotify(AttrKey::MACHSTATE, m_MECHUnit.m_MechState->Get());
  876. mLog::Debug("Enter GetTomoResults");
  877. ResDataObject json;
  878. ResDataObject jsonAngle;
  879. ResDataObject jsonHeight;
  880. if (m_mTomoResult.empty())
  881. {
  882. mLog::Debug("use test TomoResults Data");
  883. float dataAngle[15] = { -7.2 ,-6.1 ,-5.0 ,-4.5 ,-3.4 ,-2.3 ,-1.3 ,0 ,1.3 ,2.4 ,3.5 ,4.5 ,5.1 ,6.1 ,7.2 };
  884. float dataHeight[15] = { 315,290,264,252,226,201,176,150,112,1087,162,1036,1024,998,973 };
  885. for (int i = 0; i < 15; i++)
  886. {
  887. jsonAngle.add(to_string(i).c_str(), dataAngle[i]);
  888. jsonHeight.add(to_string(i).c_str(), dataHeight[i]);
  889. }
  890. json.add("P0", jsonAngle);
  891. json.add("P1", jsonHeight);
  892. FireNotify(AttrKey::TomoScanImageNumber, 15);
  893. }
  894. else
  895. {
  896. for (auto& item : m_mTomoResult)
  897. {
  898. jsonAngle.add(to_string(item.first).c_str(), item.second.nAngle);
  899. jsonHeight.add(to_string(item.first).c_str(), item.second.nHeight);
  900. }
  901. json.add("P0", jsonAngle);
  902. json.add("P1", jsonHeight);
  903. FireNotify(AttrKey::TomoScanImageNumber, m_MECHUnit.m_Tomo_ScanImageNumber->Get());
  904. }
  905. mLog::Debug("TomoResults:[{$}]", json.encode());
  906. result = json.encode();
  907. return RET_STATUS::RET_SUCCEED;
  908. }
  909. bool nsMech::HanQing3DDevice::SetFunToOtherUnit(const char* Signal, int SWstatus)
  910. {
  911. bool res{ false };
  912. #if Dios_V3
  913. if (m_pSynClient != NULL)
  914. {
  915. if (!m_pSynClient->IsClosed())
  916. {
  917. ResDataObject Request, Response;
  918. Request.add("P0", Signal);
  919. Request.add("P0", SWstatus);
  920. m_pSynClient->Action("SimulateHandSwitchSignal", Request, Response, 4993, "DIOS/DEVICE/SyncBox");
  921. mLog::Debug("SimulateHandSwitchSignal[{$}:{$}] to SYN", Signal, SWstatus);
  922. res = true;
  923. }
  924. else
  925. {
  926. mLog::Debug("{$} Client is Close", HanQing3D_SynClientName);
  927. }
  928. }
  929. else
  930. {
  931. mLog::Debug("{$} Client is NULL", HanQing3D_SynClientName);
  932. }
  933. #endif
  934. return res;
  935. }
  936. //拼接相应接口
  937. RET_STATUS nsMech::HanQing3DDevice::BeginStitching()
  938. {
  939. if (m_MECHUnit.m_MechState->Update(AttrKey::MACHINE_STATUS_STITCH))
  940. FireNotify(AttrKey::MACHSTATE, m_MECHUnit.m_MechState->Get());
  941. mLog::Debug("Enter BeginStitching");
  942. char temp[50] = { 0 };
  943. sprintf_s(temp, "USS");
  944. HWSendWaittimeCMD(temp, strlen(temp));
  945. return RET_STATUS::RET_SUCCEED;
  946. }
  947. RET_STATUS nsMech::HanQing3DDevice::InitStitching()
  948. {
  949. mLog::Debug("Enter InitStitching");
  950. return RET_STATUS::RET_SUCCEED;
  951. }
  952. RET_STATUS nsMech::HanQing3DDevice::EndStitching()
  953. {
  954. if (m_MECHUnit.m_MechState->Update(AttrKey::MACHINE_STATUS_STANDBY))
  955. FireNotify(AttrKey::MACHSTATE, m_MECHUnit.m_MechState->Get());
  956. mLog::Debug("Enter EndStitching");
  957. char temp[50] = { 0 };
  958. sprintf_s(temp, "USE");
  959. return HWSendWaittimeCMD(temp, strlen(temp));
  960. }
  961. RET_STATUS nsMech::HanQing3DDevice::SetupStitching(string& value)
  962. {
  963. EndStitching();
  964. Sleep(500);
  965. ResDataObject json,P0;
  966. json.decode(value.c_str());
  967. if (json.GetKeyCount("P0") > 0)
  968. {
  969. P0 = json[0];
  970. }
  971. else
  972. P0 = json;
  973. int nInitHeight = atoi(((string)P0[AttrKey::STITCHHEIGHT]).c_str());
  974. int nTargetLength = atoi(((string)P0[AttrKey::STITCHLENGTH]).c_str());
  975. int nOverlap = atoi(((string)P0[AttrKey::STITCHOVERLAP]).c_str());
  976. int nSID = atoi(((string)P0[AttrKey::STITCHSID]).c_str());
  977. int nTID = atoi(((string)P0[AttrKey::STITCHTID]).c_str());
  978. int nPID = atoi(((string)P0[AttrKey::STITCHPID]).c_str());
  979. int nDirection = atoi(((string)P0[AttrKey::STITCHDIRECTION]).c_str());
  980. int nStitchType = atoi(((string)P0[AttrKey::STITCHTYPE]).c_str());
  981. mLog::Debug("Enter SetupStitching:[InitHeight:{$},TotalLength:{$},Overlap:{$},SID:{$},TID:{$},PID:{$},Direction:{$},StitchType:{$}]",
  982. nInitHeight, nTargetLength, nOverlap, nSID, nTID, nPID, nDirection, nStitchType);
  983. m_MECHUnit.m_TID->Update(nPID);
  984. m_MECHUnit.m_SID->Update(nSID);
  985. m_MECHUnit.m_StitchHeight->Update(nInitHeight);
  986. m_MECHUnit.m_StitchOverLap->Update(nOverlap);
  987. m_MECHUnit.m_StitchDirection->Update(nDirection);
  988. m_MECHUnit.m_StitchType->Update(nStitchType);
  989. m_MECHUnit.m_CurrentImageNumber->Update(0);
  990. //总长度校验
  991. int maxLength = m_MECHUnit.m_TubeHeight->GetLimitMax() - m_MECHUnit.m_TubeHeight->GetLimitMin();
  992. if (nTargetLength > (maxLength - 5))
  993. {
  994. mLog::Warn("StitchingLengh[{$}] too big [height min:{$},height max:{$}],need change to[{$}]",
  995. nTargetLength, m_MECHUnit.m_TubeHeight->GetLimitMax(), m_MECHUnit.m_TubeHeight->GetLimitMin(), maxLength - 5);
  996. nTargetLength = maxLength - 5;
  997. }
  998. m_MECHUnit.m_StitchLength->Update(nTargetLength);
  999. //总张数计算
  1000. int totalImage = std::ceil(((float)nTargetLength - (float)nOverlap) / ((float)m_nFPDLengh - (float)nOverlap));
  1001. {
  1002. m_MECHUnit.m_TotalImageCount->Update(totalImage);
  1003. FireNotify(AttrKey::TOTALIMAGECOUNT, totalImage);
  1004. }//计算步长
  1005. float tempStepLength = (float)nTargetLength / (float)totalImage;
  1006. m_MECHUnit.m_StitchStepLength->Update(tempStepLength);
  1007. mLog::Debug("compute TransStitching [total ImageCount:{$},StepLength:{$}]",
  1008. totalImage, tempStepLength);
  1009. char temp[50] = { 0 };
  1010. sprintf_s(temp, "USP%02d,%02d,%01d,%02d,%03d", totalImage, nDirection, nStitchType, nOverlap, nInitHeight);
  1011. HWSendWaittimeCMD(temp, strlen(temp));
  1012. return RET_STATUS::RET_SUCCEED;
  1013. }
  1014. RET_STATUS nsMech::HanQing3DDevice::AcceptStitchingImage()
  1015. {
  1016. mLog::Debug("Enter AcceptStitchingImage");
  1017. char temp[50] = { 0 };
  1018. sprintf_s(temp, "USA");
  1019. HWSendWaittimeCMD(temp, strlen(temp));
  1020. if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_Accept_Image))
  1021. {
  1022. FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_Accept_Image);
  1023. m_MECHUnit.m_strInfo = "Stitch Accept Current image";
  1024. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  1025. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  1026. }
  1027. return RET_STATUS::RET_SUCCEED;
  1028. }
  1029. RET_STATUS nsMech::HanQing3DDevice::RejectStitchingImage()
  1030. {
  1031. mLog::Debug("Enter RejectStitchingImage");
  1032. char temp[50] = { 0 };
  1033. sprintf_s(temp, "USJ");
  1034. return HWSendWaittimeCMD(temp, strlen(temp));
  1035. if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_Reject_Image))
  1036. {
  1037. FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_Reject_Image);
  1038. m_MECHUnit.m_strInfo = "Stitch Reject Current Image";
  1039. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  1040. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  1041. }
  1042. return RET_STATUS::RET_SUCCEED;
  1043. }
  1044. RET_STATUS nsMech::HanQing3DDevice::CancelStitching()
  1045. {
  1046. if (m_MECHUnit.m_MechState->Update(AttrKey::MACHINE_STATUS_STANDBY))
  1047. FireNotify(AttrKey::MACHSTATE, m_MECHUnit.m_MechState->Get());
  1048. mLog::Debug("Enter CancelStitching");
  1049. char temp[50] = { 0 };
  1050. sprintf_s(temp, "UCS");
  1051. HWSendWaittimeCMD(temp, strlen(temp));
  1052. if (m_MECHUnit.m_StitchingState->Update(AttrKey::STITCHSTATE_CANCEL))
  1053. {
  1054. FireNotify(AttrKey::STITCHINGSTATE, AttrKey::STITCHSTATE_CANCEL);
  1055. m_MECHUnit.m_strInfo = "Stitch Cancel";
  1056. mLog::Debug("stitching:[{$}]", m_MECHUnit.m_strInfo.c_str());
  1057. FireNotify(AttrKey::UNITINFO, m_MECHUnit.m_strInfo);
  1058. }
  1059. return RET_STATUS::RET_SUCCEED;
  1060. }
  1061. RET_STATUS nsMech::HanQing3DDevice::CompleteStitching()
  1062. {
  1063. if (m_MECHUnit.m_MechState->Update(AttrKey::MACHINE_STATUS_STANDBY))
  1064. FireNotify(AttrKey::MACHSTATE, m_MECHUnit.m_MechState->Get());
  1065. mLog::Debug("Enter CompleteStitching");
  1066. char temp[50] = { 0 };
  1067. sprintf_s(temp, "USE");
  1068. return HWSendWaittimeCMD(temp, strlen(temp));
  1069. }
  1070. RET_STATUS nsMech::HanQing3DDevice::NewExtraView()
  1071. {
  1072. mLog::Debug("Enter NewExtraView");
  1073. return RET_STATUS::RET_SUCCEED;
  1074. }
  1075. RET_STATUS nsMech::HanQing3DDevice::RepeatStitching()
  1076. {
  1077. mLog::Debug("Enter RepeatStitching");
  1078. return RET_STATUS::RET_SUCCEED;
  1079. }
  1080. RET_STATUS nsMech::HanQing3DDevice::SetAutoPosiitonNo(int nPN)
  1081. {
  1082. mLog::Debug("Enter SetAutoPosiitonNo:[{$}]", nPN);
  1083. return RET_STATUS::RET_SUCCEED;
  1084. }
  1085. //牛头
  1086. RET_STATUS nsMech::HanQing3DDevice::SetExpEnable(bool nExpEnabled)
  1087. {
  1088. return RET_STATUS::RET_SUCCEED;
  1089. }
  1090. RET_STATUS nsMech::HanQing3DDevice::SetWS(int nWS)
  1091. {
  1092. mLog::Debug("Enter SetWS:[{$}]", nWS);
  1093. if (m_nWS == nWS)return RET_STATUS::RET_SUCCEED;
  1094. m_nWS = nWS;
  1095. return RET_STATUS::RET_SUCCEED;
  1096. }
  1097. RET_STATUS nsMech::HanQing3DDevice::SetKV(int nKV)
  1098. {
  1099. mLog::Debug("Enter SetKV:[{$}]", nKV);
  1100. if (m_nKV == nKV)return RET_STATUS::RET_SUCCEED;
  1101. m_nKV = nKV;
  1102. return RET_STATUS::RET_SUCCEED;
  1103. }
  1104. RET_STATUS nsMech::HanQing3DDevice::SetMA(float fMA)
  1105. {
  1106. mLog::Debug("Enter SetMA:[{$}]", fMA);
  1107. if (m_fMA == fMA)return RET_STATUS::RET_SUCCEED;
  1108. m_fMA = fMA;
  1109. return RET_STATUS::RET_SUCCEED;
  1110. }
  1111. RET_STATUS nsMech::HanQing3DDevice::SetMS(float fMS)
  1112. {
  1113. mLog::Debug("Enter SetMS:[{$}]", fMS);
  1114. if (m_fMS == fMS)return RET_STATUS::RET_SUCCEED;
  1115. m_fMS = fMS;
  1116. return RET_STATUS::RET_SUCCEED;
  1117. }
  1118. RET_STATUS nsMech::HanQing3DDevice::SetMAS(float fMAS)
  1119. {
  1120. mLog::Debug("Enter SetMAS:[{$}]", fMAS);
  1121. if (m_fMAS == fMAS)return RET_STATUS::RET_SUCCEED;
  1122. m_fMAS = fMAS;
  1123. return RET_STATUS::RET_SUCCEED;
  1124. }
  1125. RET_STATUS nsMech::HanQing3DDevice::SetFO(int nFO)
  1126. {
  1127. mLog::Debug("Enter SetFO:[{$}]", nFO);
  1128. if (m_nFocus == nFO)return RET_STATUS::RET_SUCCEED;
  1129. m_nFocus = nFO;
  1130. return RET_STATUS::RET_SUCCEED;
  1131. }
  1132. RET_STATUS nsMech::HanQing3DDevice::SetTechMode(int nET)
  1133. {
  1134. mLog::Debug("Enter SetTechMode:[{$}]", nET);
  1135. if (m_nET == nET)return RET_STATUS::RET_SUCCEED;
  1136. m_nET = nET;
  1137. return RET_STATUS::RET_SUCCEED;
  1138. }
  1139. RET_STATUS nsMech::HanQing3DDevice::SetAECField(int nAECFieldSel)
  1140. {
  1141. mLog::Debug("Enter SetAECField:[{$}]", nAECFieldSel);
  1142. if (m_nAECField == nAECFieldSel)return RET_STATUS::RET_SUCCEED;
  1143. m_nAECField = nAECFieldSel;
  1144. return RET_STATUS::RET_SUCCEED;
  1145. }
  1146. RET_STATUS nsMech::HanQing3DDevice::SetDensity(float nAECDensity)
  1147. {
  1148. mLog::Debug("Enter SetDensity:[{$}]", nAECDensity);
  1149. if (m_nAECDensity == nAECDensity)return RET_STATUS::RET_SUCCEED;
  1150. m_nAECDensity = nAECDensity;
  1151. return RET_STATUS::RET_SUCCEED;
  1152. }
  1153. RET_STATUS nsMech::HanQing3DDevice::SetBodySize(string strSize)
  1154. {
  1155. mLog::Debug("Enter SetBodySize:[{$}]", strSize.c_str());
  1156. char temp[50] = { 0 };
  1157. int nPS = 2;
  1158. if (strSize == "Small")
  1159. {
  1160. nPS = 1;
  1161. }
  1162. else if (strSize == "Middle")
  1163. {
  1164. nPS = 2;
  1165. }
  1166. else if (strSize == "Large")
  1167. {
  1168. nPS = 3;
  1169. }
  1170. else if (strSize == "Child")
  1171. {
  1172. nPS = 4;
  1173. }
  1174. if (m_nBodySize == nPS)return RET_STATUS::RET_SUCCEED;
  1175. m_nBodySize = nPS;
  1176. return RET_STATUS::RET_SUCCEED;
  1177. }
  1178. //悬吊特有
  1179. RET_STATUS nsMech::HanQing3DDevice::StopAllAxis()
  1180. {
  1181. mLog::Debug("Enter StopAllAxis");
  1182. ResetEvent(m_hMoveBeginEvent);
  1183. char temp[50] = { 0 };
  1184. sprintf_s(temp, "STP99");
  1185. return HWSendWaittimeCMD(temp, strlen(temp));
  1186. }
  1187. RET_STATUS nsMech::HanQing3DDevice::MoveToRelativeDistance(int axisNum, int distance)
  1188. {
  1189. mLog::Debug("Enter MoveToHome:[{$},{$}]", axisNum, distance);
  1190. if (axisNum > 0 && axisNum < 100)
  1191. {
  1192. char temp[50] = { 0 };
  1193. sprintf_s(temp, "MOV%02d%+07d", axisNum, distance);
  1194. return HWSendWaittimeCMD(temp, strlen(temp));
  1195. }
  1196. return RET_STATUS::RET_FAILED;
  1197. }
  1198. RET_STATUS nsMech::HanQing3DDevice::MoveToPositionNumber(int axisNum, int PosNum)
  1199. {
  1200. mLog::Debug("Enter MoveToPositionNomber:[{$},{$}]", axisNum, PosNum);
  1201. if (axisNum > 0 && axisNum < 100)
  1202. {
  1203. char temp[50] = { 0 };
  1204. sprintf_s(temp, "MVT%02d%+07d", axisNum, PosNum);
  1205. return HWSendWaittimeCMD(temp, strlen(temp));
  1206. }
  1207. return RET_STATUS::RET_FAILED;
  1208. }
  1209. RET_STATUS nsMech::HanQing3DDevice::GetAllAxisPosition()
  1210. {
  1211. mLog::Debug("Enter GetAllAxisPosition");
  1212. char temp[50] = { 0 };
  1213. sprintf_s(temp, "POS99");
  1214. return HWSendWaittimeCMD(temp, strlen(temp));
  1215. }
  1216. RET_STATUS nsMech::HanQing3DDevice::SetAxisPositionNumber(int axisNum, int xPos, int yPos, int zPos, int rPos)
  1217. {
  1218. mLog::Debug("Enter SetPositionNomber:[{$},{$},{$},{$},{$}]", axisNum, xPos, yPos, zPos, rPos);
  1219. if (axisNum > 0 && axisNum < 100)
  1220. {
  1221. char temp[50] = { 0 };
  1222. sprintf_s(temp, "HMX%02d%+07d,%+07d,%+07d,%+07d", axisNum, xPos, yPos, zPos, rPos);
  1223. return HWSendWaittimeCMD(temp, strlen(temp));
  1224. }
  1225. return RET_STATUS::RET_FAILED;
  1226. }
  1227. //-----------------------------------------------------------------------------
  1228. // HanQing3DDriver
  1229. //-----------------------------------------------------------------------------
  1230. nsMech::HanQing3DDriver::HanQing3DDriver()
  1231. {
  1232. m_pDriGenDev = nullptr;
  1233. m_pAttribute.reset(new ResDataObject());
  1234. m_pDescription.reset(new ResDataObject());
  1235. }
  1236. nsMech::HanQing3DDriver::~HanQing3DDriver()
  1237. {
  1238. }
  1239. auto nsMech::HanQing3DDriver::CreateDevice(int index) -> std::unique_ptr <IODevice>
  1240. {
  1241. mLog::Debug("Enter CreateDevice[{$}]", index);
  1242. ////mLog::Info("Enter CreateDevice, index={$}", index);
  1243. if (index == 0)
  1244. {
  1245. m_pDriGenDev = new HanQing3DDevice(EventCenter, m_SCF, m_ConfigFileName);
  1246. auto dev = std::unique_ptr<IODevice>(new IODevice(m_pDriGenDev));
  1247. return dev;
  1248. }
  1249. //mLog::Error("unknown index");
  1250. unique_ptr <IODevice> dev;
  1251. return dev;
  1252. }
  1253. void nsMech::HanQing3DDriver::FireNotify(int code, std::string key, std::string content)
  1254. {
  1255. mLog::Debug("Enter FireNotify");
  1256. EventCenter->OnNotify(code, key, content);
  1257. }
  1258. void nsMech::HanQing3DDriver::Prepare()
  1259. {
  1260. string strLogPath = GetProcessDirectory() + R"(\OEMDrivers\Mechanical\Conf\Log4CPP.Config.MECH.xml)";
  1261. //Log4CPP::ThreadContext::Map::Set("LogFileName", "Mech.HanQing3D");
  1262. Log4CPP::GlobalContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "Mech.HanQing3D");
  1263. auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str());
  1264. mLog::gLogger = Log4CPP::LogManager::GetLogger("Mech.HanQing3D");
  1265. m_SCFDllName = GetConnectDLL(m_ConfigFileName);
  1266. super::Prepare();
  1267. }
  1268. bool nsMech::HanQing3DDriver::Connect()
  1269. {
  1270. mLog::Debug("Enter Connect");
  1271. ResDataObject Connection = GetConnectParam(m_ConfigFileName);
  1272. mLog::Info("connections:{$} \n", Connection.encode());
  1273. auto erCode = m_SCF.Connect(Connection.encode(), &nsMech::HanQing3DDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000);
  1274. if (erCode != SCF_ERR::SCF_SUCCEED)
  1275. return false; //return erCode;
  1276. auto rc = super::Connect();
  1277. if (!rc)
  1278. return false; //return 0;
  1279. return true; //return SCF_ERR::SCF_SUCCEED;
  1280. }
  1281. void nsMech::HanQing3DDriver::Disconnect()
  1282. {
  1283. mLog::Debug("Enter Disconnect");
  1284. super::Disconnect();
  1285. m_SCF.Disconnect();
  1286. }
  1287. bool nsMech::HanQing3DDriver::isConnected() const
  1288. {
  1289. return super::isConnected();
  1290. }
  1291. std::string nsMech::HanQing3DDriver::DriverProbe()
  1292. {
  1293. printf("line= %d,%s %s\n", __LINE__, __FUNCTION__, m_ConfigFileName.c_str());
  1294. ResDataObject r_config, HardwareInfo;
  1295. if (r_config.loadFile(m_ConfigFileName.c_str()))
  1296. {
  1297. HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
  1298. HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
  1299. HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
  1300. HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
  1301. HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
  1302. }
  1303. else
  1304. {
  1305. HardwareInfo.add("MajorID", "Machine");
  1306. HardwareInfo.add("MinorID", "Dr");
  1307. HardwareInfo.add("VendorID", "ECOM");
  1308. HardwareInfo.add("ProductID", "HF");
  1309. HardwareInfo.add("SerialID", "1234");
  1310. }
  1311. string ret = HardwareInfo.encode();
  1312. printf("line= %d,%s %s\n", __LINE__, __FUNCTION__, m_ConfigFileName.c_str());
  1313. return ret;
  1314. }
  1315. bool nsMech::HanQing3DDriver::GetDeviceConfig(std::string& Cfg)
  1316. {
  1317. Cfg = m_DeviceConfigSend.encode();
  1318. printf("GetDeviceConfig over , %s", Cfg.c_str());
  1319. return true;
  1320. }
  1321. bool nsMech::HanQing3DDriver::SetDeviceConfig(std::string Cfg)
  1322. {
  1323. printf("\n--Func-- SetDeviceConfig %s\n", Cfg.c_str());
  1324. ResDataObject DeviceConfig;
  1325. DeviceConfig.decode(Cfg.c_str());
  1326. ResDataObject DescriptionTempEx;
  1327. DescriptionTempEx = DeviceConfig["DeviceConfig"]["Attribute"];
  1328. //mLog::Debug("Attribute:{$}", DescriptionTempEx.encode());
  1329. bool bSaveFile = false; //true:重新保存配置文件
  1330. string strAccess = "";
  1331. for (int i = 0; i < DescriptionTempEx.size(); i++)
  1332. {
  1333. string strKey = DescriptionTempEx.GetKey(i);
  1334. ////mLog::Info("{$}", strKey.c_str());
  1335. printf("%s\n", strKey.c_str());
  1336. try
  1337. {
  1338. if (m_pAttribute->GetFirstOf(strKey.c_str()) >= 0)
  1339. {
  1340. strAccess = (string)(*m_pDescription)[strKey.c_str()]["Access"];
  1341. if ("RW" == strAccess)
  1342. {
  1343. //修改对应配置,在其他单元的配置项要同时调用其修改函数修改真实值
  1344. //1. 修改内存中的值,用于给上层发消息
  1345. (*m_pAttribute)[strKey.c_str()] = DescriptionTempEx[i];
  1346. //2. 拿到Innerkey
  1347. int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  1348. ////mLog::Info("nConfigInfoCount {$}", nConfigInfoCount);
  1349. string strTemp = ""; //存储AttributeKey
  1350. for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  1351. {
  1352. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  1353. if (strTemp == strKey)
  1354. {
  1355. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  1356. break;
  1357. }
  1358. }
  1359. //3. 修改配置文件中的值
  1360. if (SetDeviceConfigValue(m_Configurations, strTemp.c_str(), 1, DescriptionTempEx[i]))
  1361. {
  1362. //mLog::Debug("SetDeviceConfigValue over");
  1363. bSaveFile = true;
  1364. }
  1365. }
  1366. else
  1367. {
  1368. ////mLog::Info("{$} is not a RW configuration item", strKey.c_str());
  1369. }
  1370. }
  1371. else
  1372. {
  1373. ////mLog::Info("without this attribute {$}", strKey.c_str());
  1374. }
  1375. }
  1376. catch (ResDataObjectExption& e)
  1377. {
  1378. printf("\nSetDriverConfig crashed: %s\n", e.what());
  1379. //mLog::Error("SetDriverConfig crashed: {$}", e.what());
  1380. return false;
  1381. }
  1382. }
  1383. if (bSaveFile)
  1384. {
  1385. //3. 重新保存配置文件
  1386. SaveConfigFile(true);
  1387. }
  1388. return true;
  1389. }
  1390. bool nsMech::HanQing3DDriver::SaveConfigFile(bool bSendNotify)
  1391. {
  1392. m_ConfigAll["CONFIGURATION"] = m_Configurations;
  1393. bool bRt = m_ConfigAll.SaveFile(m_ConfigFileName.c_str());
  1394. ////mLog::Info("SaveConfigFile over {$}", bRt);
  1395. return true;
  1396. }
  1397. bool nsMech::HanQing3DDriver::GetDeviceConfigValue(ResDataObject config, const char* pInnerKey, int nPathID, string& strValue)
  1398. {
  1399. printf("line= %d,%s,pInnerKey=%s,nPathID=%d\n", __LINE__, __FUNCTION__, pInnerKey, nPathID);
  1400. strValue = "";
  1401. string strTemp = pInnerKey;
  1402. if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取
  1403. {
  1404. int pos = 0;
  1405. ResDataObject resTemp = config;
  1406. while ((pos = strTemp.find_first_of(',')) != string::npos)
  1407. {
  1408. string Key = strTemp.substr(0, pos);
  1409. string TempValue = resTemp[Key.c_str()].encode();
  1410. //printf("-TempValue=== %s\n", TempValue.c_str());
  1411. resTemp.clear();
  1412. resTemp.decode(TempValue.c_str());
  1413. strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1);
  1414. //printf("-************--%s\n", strTemp.c_str());
  1415. }
  1416. if (strTemp != "")
  1417. {
  1418. strValue = (string)resTemp[strTemp.c_str()];
  1419. }
  1420. else
  1421. {
  1422. strValue = (string)resTemp;
  1423. }
  1424. }
  1425. //printf("------------%s\n", strValue.c_str());
  1426. return true;
  1427. }
  1428. bool nsMech::HanQing3DDriver::SetDeviceConfigValue(ResDataObject& config, const char* pInnerKey, int nPathID, const char* szValue)
  1429. {
  1430. string strTemp = pInnerKey;
  1431. //mLog::Debug("Begin to change {$} item value to {$}", pInnerKey, szValue);
  1432. printf("\n Begin to change {%s} item value to {%s}\n", pInnerKey, szValue);
  1433. if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取
  1434. {
  1435. try {
  1436. int pos = 0;
  1437. ResDataObject* resTemp = &config;
  1438. while ((pos = strTemp.find_first_of(',')) != string::npos)
  1439. {
  1440. string Key = strTemp.substr(0, pos);
  1441. resTemp = &(*resTemp)[Key.c_str()];
  1442. strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1);
  1443. }
  1444. if (strTemp != "")
  1445. {
  1446. (*resTemp)[strTemp.c_str()] = szValue;
  1447. }
  1448. else
  1449. {
  1450. *resTemp = szValue;
  1451. }
  1452. }
  1453. catch (ResDataObjectExption& e)
  1454. {
  1455. //mLog::Error("SetDriverConfigvalue crashed: {$}", e.what());
  1456. return false;
  1457. }
  1458. }
  1459. return true;
  1460. }
  1461. std::string nsMech::HanQing3DDriver::GetResource()
  1462. {
  1463. ResDataObject r_config, temp;
  1464. if (!temp.loadFile(m_ConfigFileName.c_str()))
  1465. {
  1466. return "";
  1467. }
  1468. m_ConfigAll = temp;
  1469. r_config = temp["CONFIGURATION"];
  1470. m_Configurations = r_config;
  1471. ResDataObject DescriptionTemp;
  1472. ResDataObject DescriptionSend;
  1473. ResDataObject m_DescriptionSend;
  1474. ResDataObject ListTemp;
  1475. string strTemp = ""; //用于读取字符串配置信息
  1476. string strIndex = ""; //用于读取配置信息中的List项
  1477. int nTemp = -1; //用于读取整型配置信息
  1478. char sstream[10] = { 0 }; //用于转换值
  1479. string strValue = ""; //用于存储配置的值
  1480. string strType = ""; //用于存储配置的类型 int/float/string...
  1481. try
  1482. {
  1483. //便利ConfigToolInfo 中 所有的AttributeInfo 属性段
  1484. int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
  1485. m_pAttribute->clear();
  1486. m_pDescription->clear();
  1487. for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++)
  1488. {
  1489. DescriptionTemp.clear();
  1490. DescriptionSend.clear();
  1491. ListTemp.clear();
  1492. //AttributeType
  1493. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Type"];
  1494. DescriptionTemp.add(ConfKey::DiosType, strTemp.c_str());//DiosGeneratorAttribute
  1495. DescriptionSend.add(ConfKey::DiosType, strTemp.c_str());//DiosGeneratorAttribute
  1496. strType = strTemp; //记录配置项的类型
  1497. //AttributeKey
  1498. //1. 根据AttributeType,内部key和配置路径,拿到当前的真实值
  1499. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
  1500. nTemp = (int)m_Configurations["ConfigToolInfo"][nInfoIndex]["PathID"];
  1501. GetDeviceConfigValue(r_config, strTemp.c_str(), nTemp, strValue); //得到strValue的值
  1502. //2. 赋值
  1503. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  1504. if ("int" == strType)
  1505. {
  1506. (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str()));
  1507. }
  1508. else if ("float" == strType)
  1509. {
  1510. (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str()));
  1511. }
  1512. else //其它先按string类型处理
  1513. {
  1514. (*m_pAttribute).add(strTemp.c_str(), strValue.c_str());
  1515. }
  1516. //AttributeAccess
  1517. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Access"];
  1518. DescriptionTemp.add(ConfKey::DiosAccess, strTemp.c_str());
  1519. DescriptionSend.add(ConfKey::DiosAccess, strTemp.c_str());
  1520. //AttributeRangeMin
  1521. //strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMin"];
  1522. //if (strTemp != "") //不需要的配置项为空
  1523. //{
  1524. // DescriptionTemp.add(ConfKey::DiosRangeMin, strTemp.c_str());
  1525. //}
  1526. ////AttributeRangeMax
  1527. //strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMax"];
  1528. //if (strTemp != "") //不需要的配置项为空
  1529. //{
  1530. // DescriptionTemp.add(ConfKey::DiosRangeMax, strTemp.c_str());
  1531. //}
  1532. //AttributeList
  1533. nTemp = m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListNum"];
  1534. if (nTemp > 0) //ListNum不大于0时说明不需要list配置
  1535. {
  1536. for (int nListIndex = 0; nListIndex < nTemp; nListIndex++)
  1537. {
  1538. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListInfo"][nListIndex];
  1539. auto temKey = std::to_string(nListIndex);
  1540. ListTemp.add(temKey.c_str(), strTemp.c_str());
  1541. }
  1542. DescriptionTemp.add(ConfKey::DiosList, ListTemp);
  1543. DescriptionSend.add(ConfKey::DiosList, ListTemp.encode());
  1544. }
  1545. //AttributeRequired
  1546. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Required"];
  1547. DescriptionTemp.add(ConfKey::DiosRequired, strTemp.c_str());
  1548. DescriptionSend.add(ConfKey::DiosRequired, strTemp.c_str());
  1549. //AttributeDefaultValue
  1550. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["DefaultValue"];
  1551. if (strTemp != "") //不需要的配置项为空
  1552. {
  1553. DescriptionTemp.add(ConfKey::DiosDefaultValue, strTemp.c_str());
  1554. DescriptionSend.add(ConfKey::DiosDefaultValue, strTemp.c_str());
  1555. }
  1556. strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"];
  1557. (*m_pDescription).add(strTemp.c_str(), DescriptionTemp);
  1558. m_DescriptionSend.add(strTemp.c_str(), DescriptionSend.encode());
  1559. }
  1560. }
  1561. catch (ResDataObjectExption& e)
  1562. {
  1563. //mLog::Error("Get config error: {$}", e.what());
  1564. return "";
  1565. }
  1566. ResDataObject resDeviceResource;
  1567. resDeviceResource.add(ConfKey::DiosAttribute, (*m_pAttribute));
  1568. resDeviceResource.add(ConfKey::DiosDescription, (*m_pDescription));
  1569. ResDataObject DescriptionTempEx;
  1570. DescriptionTempEx.add(ConfKey::DiosConfig, resDeviceResource);
  1571. m_DeviceConfig.clear();
  1572. m_DeviceConfig = DescriptionTempEx;
  1573. //mLog::Debug("local ************* get resource over {$}", DescriptionTempEx.encode());
  1574. printf("local ************* get resource over %s \n", DescriptionTempEx.encode());
  1575. resDeviceResource.clear();
  1576. resDeviceResource.add(ConfKey::DiosAttribute, (*m_pAttribute));
  1577. resDeviceResource.add(ConfKey::DiosDescription, m_DescriptionSend);
  1578. DescriptionTempEx.clear();
  1579. DescriptionTempEx.add(ConfKey::DiosConfig, resDeviceResource);
  1580. m_DeviceConfigSend.clear();
  1581. m_DeviceConfigSend = DescriptionTempEx;
  1582. string res = m_DeviceConfigSend.encode();
  1583. //mLog::Debug("get resource over {$}", DescriptionTempEx.encode());
  1584. printf("************* get resource over %s \n", DescriptionTempEx.encode());
  1585. return res;
  1586. }
  1587. std::string nsMech::HanQing3DDriver::DeviceProbe()
  1588. {
  1589. ResDataObject r_config, HardwareInfo;
  1590. if (r_config.loadFile(m_ConfigFileName.c_str()))
  1591. {
  1592. HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
  1593. HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
  1594. HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
  1595. HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
  1596. HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
  1597. }
  1598. else
  1599. {
  1600. HardwareInfo.add("MajorID", "Machine");
  1601. HardwareInfo.add("MinorID", "Dr");
  1602. HardwareInfo.add("VendorID", "ECOM");
  1603. HardwareInfo.add("ProductID", "HF");
  1604. HardwareInfo.add("SerialID", "1234");
  1605. }
  1606. string ret = HardwareInfo.encode();
  1607. return ret;
  1608. }
  1609. void nsMech::HanQing3DDriver::Dequeue(const char* Packet, DWORD Length)
  1610. {
  1611. DecodeFrame(Packet, Length);
  1612. }
  1613. static char strRcvTemp[HanQing3D_Com_NormalLen] = { 0 };
  1614. PACKET_RET nsMech::HanQing3DDriver::callbackPackageProcess(const char* RecData, DWORD nLength, DWORD& PacketLength)
  1615. {
  1616. #if 0
  1617. if (nLength > 1)
  1618. {
  1619. mLog::Error("receive data_len[{$}]", nLength);
  1620. for (int i = 0; i < nLength; i++)
  1621. {
  1622. if (i != nLength - 1)
  1623. {
  1624. mLog::Error("receive data[{$}][{$}]", i, RecData[i]);
  1625. }
  1626. else
  1627. {
  1628. mLog::Error("receive data[{$}][{$}]", i, RecData[i]);
  1629. }
  1630. }
  1631. }
  1632. #endif
  1633. bool bHasHead = false;
  1634. if (nLength < 1)
  1635. {
  1636. PacketLength = 0;
  1637. mLog::Error("nLength too small, nLength=={$}", nLength);
  1638. return PACKET_USELESS;
  1639. }
  1640. else if (nLength > HanQing3D_Com_NormalLen-1)
  1641. {
  1642. PacketLength = nLength;
  1643. mLog::Error("nLength too big, nLength=={$}", nLength);
  1644. return PACKET_USELESS;
  1645. }
  1646. memset(strRcvTemp, 0, HanQing3D_Com_NormalLen);
  1647. for (DWORD i = 0; i < nLength; i++)
  1648. {
  1649. //寻找包头
  1650. if (RecData[i] == HanQing3D_STX)
  1651. {
  1652. if (i != 0) //包头之前的数据格式不对,全部扔掉
  1653. {
  1654. PacketLength = i;
  1655. memcpy_s(strRcvTemp, HanQing3D_Com_NormalLen - 1, RecData, PacketLength);
  1656. mLog::Error("==IN unknown format data ==:[{$}],UselessDataLength={$},TotalLength={$}", strRcvTemp, PacketLength, nLength);
  1657. return PACKET_USELESS;
  1658. }
  1659. else
  1660. {
  1661. bHasHead = true;
  1662. }
  1663. }
  1664. //寻找包尾
  1665. if (RecData[i] == HanQing3D_ETX)
  1666. {
  1667. PacketLength = i + 1; //because ETX
  1668. if (bHasHead)
  1669. {
  1670. if (i >= 6) //正常指令:STX(1BYTE) + CMDid(3BYTE) + [AxisNumber(2BYTE)]
  1671. {
  1672. memcpy_s(strRcvTemp, HanQing3D_Com_NormalLen - 1, RecData + 1, i - 1); //起始位置:+1 排除STX,长度:-1 排除ETX
  1673. if (strncmp(strRcvTemp,"FFF",3) != 0)
  1674. mLog::Info("==IN==:[{$}]", strRcvTemp);
  1675. HanQing3DDevice::m_tDelivermodule.CheckReceive(strRcvTemp, 3);
  1676. return PACKET_ISPACKET;
  1677. }
  1678. else //空指令
  1679. {
  1680. memcpy_s(strRcvTemp, HanQing3D_Com_NormalLen - 1, RecData, PacketLength); //空数据,格式正确但无有效命令
  1681. mLog::Error("==IN uselss data==:[{$}]", strRcvTemp);
  1682. return PACKET_USELESS;
  1683. }
  1684. }
  1685. else //有包尾但无包头
  1686. {
  1687. memcpy_s(strRcvTemp, HanQing3D_Com_NormalLen - 1, RecData, PacketLength);
  1688. mLog::Error("==IN no head data ==:[{$}],NoHeadDataLength={$},TotalLength={$}", strRcvTemp, PacketLength, nLength);
  1689. return PACKET_USELESS;
  1690. }
  1691. }
  1692. }
  1693. if (bHasHead)
  1694. {
  1695. PacketLength = 0;
  1696. }
  1697. return PACKET_NOPACKET;
  1698. }
  1699. //模块对外接口
  1700. static nsMech::HanQing3DDriver gIODriver;
  1701. extern "C" DIOS::Dev::IODriver * __cdecl GetIODriver() // 返回静态对象的引用, 调用者不能删除 !
  1702. {
  1703. return &gIODriver;
  1704. }
  1705. extern "C" DIOS::Dev::IODriver * __cdecl CreateIODriver() // 返回新对象, 调用者必须自行删除此对象 !
  1706. {
  1707. return new nsMech::HanQing3DDriver();
  1708. }