MACHINE_DPC.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. #include "stdafx.h"
  2. #include <map>
  3. #include <list>
  4. #include <functional>
  5. #include "All.h"
  6. #include "MACHINE_DPC.h"
  7. #include "MACHINE_Logical.h"
  8. #include "PacketAnalizer.h"
  9. #ifdef CMD_ANSYNC
  10. #include "RingBuffer.h"
  11. #endif
  12. #ifdef _WIN64
  13. #ifdef _DEBUG
  14. const string SCFFile = "SerialSCFX64D.dll";
  15. #else
  16. const string SCFFile = "SerialSCFX64.dll";
  17. #endif
  18. #else
  19. #ifdef _DEBUG
  20. const string SCFFile = "SerialSCFD.dll";
  21. #else
  22. const string SCFFile = "SerialSCF.dll";
  23. #endif
  24. #endif
  25. #define MACHINERY_TID "DetectorToTableDistance"
  26. #define MACHINERY_SID "DistanceSourcetoDetector"
  27. #define MACHINERY_ENTRANCE "DistanceSourcetoEntrance"
  28. #define MACHINERY_PROJECTIONNUM "ProjectionNum"
  29. #define MACHINERY_MAXANGLE "ProjectionMaxAngle"
  30. #define MACHINERY_DIRECTION "ProjectionDirection"
  31. /////////////////////////////////////////////////////////////////////////
  32. #define FUNNAME_CREATE_SCF TEXT("GetSCF")
  33. #define FUNNAME_RELEASE_SCF TEXT("ReleseSCF")
  34. #define OFFSET_CLASSNAME_DOSE 14
  35. #define OFFSET_CLASSNAME_DOSE_SP 22
  36. #define OFFSET_CLASSNAME_LOGIC 17
  37. #define OFFSET_CLASSNAME_LOGIC_SP 25
  38. #define CMD_LEN_MAX 128
  39. #define TIMEOUTVALUE 1000
  40. const int STX = 0x02;
  41. const int ETX = 0x03;
  42. CMACHINE_DPC::CMACHINE_DPC()
  43. {
  44. m_pWorkpath = new std::string();
  45. m_SCFDllFileHandle = LoadLibrary(SCFFile.c_str());
  46. if (m_SCFDllFileHandle)
  47. {
  48. m_fpGetSCF = (GetSCF)GetProcAddress(m_SCFDllFileHandle, "GetSCF");
  49. m_pSCFServer = m_fpGetSCF();
  50. m_pSCFServer->SetPassiveDisconnectEvt(GetPassiveDisConnectEvtHandle());
  51. }
  52. else
  53. {
  54. DWORD errNo = GetLastError();
  55. }
  56. m_DetectorToTableDistance.add(MACHINERY_TID, (FLOAT)0.0);
  57. m_DistanceSourcetoDetector.add(MACHINERY_SID, (FLOAT)1.0);
  58. m_DistanceSourcetoEntrance.add(MACHINERY_ENTRANCE, (FLOAT)15.0);
  59. m_ProjectionNum.add(MACHINERY_PROJECTIONNUM, (INT)15);
  60. m_ProjectionMaxAngle.add(MACHINERY_MAXANGLE, (FLOAT)15.0);
  61. m_ProjectionDirection.add(MACHINERY_DIRECTION, (INT)-1);
  62. m_pSendPacket = (SCFPacket *)MallocSCFPacket();
  63. m_pReceivePacket = (SCFPacket *)MallocSCFPacket();
  64. }
  65. CMACHINE_DPC::~CMACHINE_DPC()
  66. {
  67. ReleaseSCFPacket(m_pSendPacket);
  68. ReleaseSCFPacket(m_pReceivePacket);
  69. delete m_pWorkpath;
  70. }
  71. //bool CMACHINE_DPC::DpcEntry(ResDataObject &Configuration)
  72. //{
  73. // try
  74. // {
  75. // m_DeviceConfig = Configuration;
  76. // }
  77. // catch (...)
  78. // {
  79. // PRINTA_INFO(m_pLog, "Can't get the Configuration for Device");
  80. // }
  81. // PRINTA_INFO(m_pLog, "DpcEntry Configuration");
  82. //
  83. // return true;
  84. //}
  85. //
  86. //ResDataObject CMACHINE_DPC::GetConnectionType()
  87. //{
  88. // return ResDataObject();
  89. //}
  90. //
  91. //bool CMACHINE_DPC::Connect(ResDataObject &Connection)
  92. //{
  93. // m_pSCFServer = m_fpGetSCF();
  94. //
  95. // int Result = m_pSCFServer->Connect(Connection, &CMACHINE_DPC::PackageProcess, SCF_PACKET_TRANSFER);
  96. // if (Result != SCF_SUCCEED)
  97. // {
  98. // return false;
  99. // }
  100. //
  101. // return true;
  102. //}
  103. //
  104. //bool CMACHINE_DPC::Probe(ResDataObject &HardwareInfo)
  105. //{
  106. //#ifdef ENV_DIOS
  107. // HardwareInfo.add("MajorID", "Generator");
  108. // HardwareInfo.add("MinorID", "DR");
  109. // HardwareInfo.add("VendorID", "CPI");
  110. // HardwareInfo.add("ProductID", "SHF");
  111. // HardwareInfo.add("SerialID", "1234");
  112. //#endif
  113. // return true;
  114. //}
  115. //
  116. //bool CMACHINE_DPC::SetWorkPath(const char *pWorkPath)
  117. //{
  118. // m_strWorkPath = pWorkPath;
  119. // return true;
  120. //}
  121. //
  122. //PVOID CMACHINE_DPC::LoadLogicDevices()
  123. //{
  124. // DevTree *pTree = new DevTree();
  125. // LogicDevice * pLD = NULL;
  126. //
  127. // //Load DOSE Logic unit
  128. // m_pGenDose = new CMACHINE_Dose();
  129. // m_pGenDose->DPCPoint(this);
  130. // pLD = (LogicDevice *)m_pGenDose;
  131. // pTree->Add((PVOID)pLD, TYPE_DEVICE);
  132. //
  133. // //Load SyncGen
  134. // m_pGenLogical = new CMACHINE_Logical();
  135. // m_pGenLogical->DPCPoint(this);
  136. // pLD = (LogicDevice *)m_pGenLogical;
  137. // pTree->Add((PVOID)pLD, TYPE_DEVICE);
  138. //
  139. //#if 0
  140. // try{
  141. // m_pGenDose->m_DeviceConfig.nTable = (int)(m_DeviceConfig["Table"]);
  142. // m_pGenDose->m_DeviceConfig.nWall = (int)(m_DeviceConfig["Wall"]);
  143. // m_pGenDose->m_DeviceConfig.nFree = (int)(m_DeviceConfig["Free"]);
  144. // m_pGenDose->m_DeviceConfig.nTomo = (int)(m_DeviceConfig["Tomo"]);
  145. // m_pGenDose->m_DeviceConfig.nConventional = (int)(m_DeviceConfig["Conventional"]);
  146. // }
  147. // catch (...)
  148. // {
  149. // printf("Get Gen config item Crash.\n");
  150. // }
  151. //#endif
  152. // return (PVOID)pTree;
  153. //}
  154. //
  155. //void CMACHINE_DPC::UnloadLogicDevices(/*LogicDevice*/PVOID p)
  156. //{
  157. //#ifdef ENV_DIOS
  158. // DevTree *pTree = (DevTree*)p;
  159. // size_t Size = pTree->size();
  160. // for (size_t i = 0; i < Size; i++)
  161. // {
  162. // DevTreeNode node = (*pTree)[i];
  163. // if (node.m_NodeType == TYPE_DEVICE)
  164. // {
  165. // LogicDevice *pdev = (LogicDevice*)node.m_pObject;
  166. // delete pdev;
  167. // }
  168. // else
  169. // {
  170. // //tree dev
  171. // //do not copy this!!!!
  172. // UnloadLogicDevices(node.m_pObject);
  173. // }
  174. // }
  175. //
  176. // delete pTree;
  177. //#endif
  178. //}
  179. //
  180. //bool CMACHINE_DPC::DisConnect()
  181. //{
  182. // m_pSCFServer->Disconnect();
  183. // m_fpReleaseSCF(m_pSCFServer);
  184. // return true;
  185. //}
  186. //
  187. //bool CMACHINE_DPC::OnNotify(HANDLE ExitNotify)
  188. //{
  189. // Work();
  190. // return true;
  191. //}
  192. bool CMACHINE_DPC::DriverEntry(ResDataObject &Configuration)
  193. {
  194. try
  195. {
  196. m_DeviceConfig = Configuration;
  197. }
  198. catch (...)
  199. {
  200. PRINTA_INFO(m_pLog, "Can't get the Configuration for Device");
  201. }
  202. PRINTA_INFO(m_pLog, "DpcEntry Configuration");
  203. return LogicDriver::DriverEntry(Configuration);
  204. }
  205. bool CMACHINE_DPC::Driver_Probe(ResDataObject &HardwareInfo)
  206. {
  207. HardwareInfo.add("MajorID", "Machine");
  208. HardwareInfo.add("MinorID", "DR");
  209. HardwareInfo.add("VendorID", "SiemensTOMO");
  210. HardwareInfo.add("ProductID", "TOMO");
  211. HardwareInfo.add("SerialID", "DRVOBJ");
  212. return true;
  213. }
  214. bool SYSTEM_CALL CMACHINE_DPC::SetDriverWorkPath(const char * PARAM_IN pWorkPath)
  215. {
  216. return true;
  217. }
  218. PVOID SYSTEM_CALL CMACHINE_DPC::LoadDriver()
  219. {
  220. return NULL;
  221. }
  222. void SYSTEM_CALL CMACHINE_DPC::UnloadDriver()
  223. {
  224. }
  225. bool CMACHINE_DPC::Connect()
  226. {
  227. ResDataObject Connection = GetConnectionParam();
  228. PRINTA_INFO(m_pLog, "Connection[0]= %s", Connection[0].encode());
  229. printf("Connection[0]= %s", Connection[0].encode());
  230. int Result = m_pSCFServer->Connect(Connection[0], &CMACHINE_DPC::PackageProcess, SCF_PACKET_TRANSFER);
  231. if (Result != SCF_SUCCEED)
  232. {
  233. LogicDriver::DisConnect();
  234. return false;
  235. }
  236. if (!OnHeartBeat())
  237. {
  238. LogicDriver::DisConnect();
  239. return false;
  240. }
  241. //Load Command Send Thread
  242. LogicDriver::Connect();//make sure it's connected
  243. return true;
  244. }
  245. bool CMACHINE_DPC::Device_Probe(ResDataObject &HardwareInfo)
  246. {
  247. HardwareInfo.add("MajorID", "Machine");
  248. HardwareInfo.add("MinorID", "DR");
  249. HardwareInfo.add("VendorID", "SiemensTOMO");
  250. HardwareInfo.add("ProductID", "TOMO");
  251. HardwareInfo.add("SerialID", "1234");
  252. return true;
  253. }
  254. bool CMACHINE_DPC::SetDeviceWorkPath(const char *pWorkPath)
  255. {
  256. (*m_pWorkpath) = pWorkPath;
  257. //要把读取单一硬件属性的过程放到此处
  258. return true;
  259. }
  260. PVOID CMACHINE_DPC::LoadLogicDevices()
  261. {
  262. DevTree *pTree = (DevTree *)GetDeviceTree();
  263. m_pGenLogical = new CMACHINE_Logical();
  264. m_pGenLogical->DPCPoint(this);
  265. LogicDevice *pret = (LogicDevice *)m_pGenLogical;
  266. pTree->Add((PVOID)pret, TYPE_DEVICE);
  267. m_bStatusFlag = true;
  268. return (PVOID)pTree;
  269. }
  270. void CMACHINE_DPC::UnloadLogicDevices()
  271. {
  272. m_bStatusFlag = false;
  273. DevTree *pTree = (DevTree *)GetDeviceTree();
  274. size_t Size = pTree->size();
  275. for (size_t i = 0; i < Size; i++)
  276. {
  277. DevTreeNode node = (*pTree)[i];
  278. if (node.m_NodeType == TYPE_DEVICE)
  279. {
  280. LogicDevice *pdev = (LogicDevice*)node.m_pObject;
  281. delete pdev;
  282. }
  283. else
  284. {
  285. //tree dev
  286. //do not copy this!!!!
  287. //UnloadLogicDevices(node.m_pObject);
  288. }
  289. }
  290. pTree->clear();
  291. PRINTA_INFO(m_pLog, "UnLoad Over");
  292. }
  293. void CMACHINE_DPC::DisConnect()
  294. {
  295. m_pSCFServer->Disconnect();
  296. //unload dpc
  297. ReleaseSCF dpcSCFfunc = (ReleaseSCF)GetProcAddress(m_SCFDllFileHandle, "ReleseSCF");
  298. if (dpcSCFfunc)
  299. {
  300. dpcSCFfunc(m_pSCFServer);
  301. }
  302. LogicDriver::DisConnect();
  303. }
  304. bool SYSTEM_CALL CMACHINE_DPC::OnHeartBeat()
  305. {
  306. return true;
  307. }
  308. RET_STATUS SYSTEM_CALL CMACHINE_DPC::GetDeviceResource(ResDataObject PARAM_OUT *pDeviceResource)
  309. {
  310. RET_STATUS rs = LogicDriver::GetDeviceResource(pDeviceResource);
  311. bool ret = true;
  312. ResDataObject val = (*pDeviceResource)["Attribute"];
  313. //val.add(MACHINERY_READY, m_MachineryReady[MACHINERY_READY]);
  314. val.add(MACHINERY_TID, m_DetectorToTableDistance[MACHINERY_TID]);
  315. val.add(MACHINERY_SID, m_DistanceSourcetoDetector[MACHINERY_SID]);
  316. val.add(MACHINERY_ENTRANCE, m_DistanceSourcetoEntrance[MACHINERY_ENTRANCE]);
  317. val.add(MACHINERY_PROJECTIONNUM, m_ProjectionNum[MACHINERY_PROJECTIONNUM]);
  318. val.add(MACHINERY_MAXANGLE, m_ProjectionMaxAngle[MACHINERY_MAXANGLE]);
  319. val.add(MACHINERY_DIRECTION, m_ProjectionDirection[MACHINERY_DIRECTION]);
  320. ret &= pDeviceResource->update("Attribute", val);
  321. if (ret)
  322. {
  323. return RET_SUCCEED;
  324. }
  325. return RET_FAILED;
  326. }
  327. DWORD SYSTEM_CALL CMACHINE_DPC::OnNotify(HANDLE evtList[], DWORD count)
  328. {
  329. char pPacket[MAX_STRING] = { 0 };
  330. char pTempPacket[MAX_STRING] = { 0 };
  331. int nLength = MAX_STRING;
  332. int nTimeout = 1000; int nPacketLen = 0;
  333. string strTempCmd;
  334. HANDLE hSCFHandle = m_pSCFServer->GetScfNotifyHandle();
  335. HANDLE pHandlArray[MAXIMUM_WAIT_OBJECTS] = { 0 };
  336. DWORD HandleCount = 0;
  337. for (HandleCount = 0; HandleCount < count; HandleCount++)
  338. {
  339. pHandlArray[HandleCount] = evtList[HandleCount];
  340. }
  341. pHandlArray[HandleCount++] = hSCFHandle;
  342. DWORD dwResult = WaitForMultipleObjects(HandleCount, pHandlArray, FALSE, INFINITE);
  343. if ((dwResult >= WAIT_OBJECT_0) && (dwResult < WAIT_OBJECT_0 + HandleCount - 1))
  344. {
  345. PRINTA_INFO(m_pLog, "OnNotify Platform");
  346. return dwResult - WAIT_OBJECT_0;
  347. }
  348. else
  349. {
  350. if (LogicDriver::GetConnectionStatus() && true == m_bStatusFlag)
  351. {
  352. char Command[VALUELENGTH_MAX];
  353. int PacketLength = VALUELENGTH_MAX;
  354. memset(Command, 0, VALUELENGTH_MAX);
  355. while (m_pSCFServer && (PacketLength = m_pSCFServer->DeQueNotifyPacket(Command, VALUELENGTH_MAX, TIMEOUTVALUE))>0)
  356. {
  357. OnCallback(Command, PacketLength);
  358. }
  359. }
  360. }
  361. return 1;
  362. }
  363. PACKET_RET CMACHINE_DPC::PackageProcess(const char * RecData, DWORD nLength, DWORD& PacketLength)
  364. {
  365. //判断是否是整包
  366. /*
  367. 这个是回调函数,我收到的数据会需要这个回调函数帮我判断是否是整个的包;
  368. 如果有整个的包,返回值为true,在PacketLength处返回给我整个包的长度,我再截取后放入缓存供上层使用;
  369. 如果缓存中的数据没有整个的数据包,那么返回false
  370. */
  371. if (nLength < 2)
  372. {
  373. return PACKET_NOPACKET;
  374. }
  375. unsigned char chsend, chend;
  376. chsend = 0x0d;
  377. chend = 0x0a;
  378. for (size_t i = 0; i < nLength; i++)
  379. {
  380. if ((RecData[i] == chsend) && (RecData[i + 1] == chend))
  381. {
  382. PacketLength = i + 2;
  383. /*char szContext[40] = { 0 };
  384. memset(szContext, 0, 40);
  385. memcpy(szContext, RecData, PacketLength);
  386. printf("Got Packet:%s\n", szContext);*/
  387. return PACKET_ISPACKET;
  388. }
  389. }
  390. return PACKET_NOPACKET;
  391. }
  392. bool CMACHINE_DPC::SetupStitching(int nImageCount, int nDirection, int nInitialHeight, int nStitchLength, int nStepLength, int nSID, int nStitchType)
  393. {
  394. m_nImageCount = nStitchLength / nStepLength;
  395. m_nCurrentImage = 1;
  396. //code begin 20101125
  397. if (m_nImageCount < 2)
  398. {
  399. m_nImageCount = 2;
  400. TPRINTA_WARN("The image count is not correct");
  401. }
  402. else if (m_nImageCount > 4)
  403. {
  404. m_nImageCount = 4;
  405. TPRINTA_WARN("The image count is not correct");
  406. }
  407. //code end 20101125
  408. if (nDirection)//up down, now just support two direction
  409. {
  410. nDirection = 1;
  411. }
  412. else
  413. {
  414. nDirection = 2;//down up
  415. }
  416. if (nStitchType == 0)
  417. {
  418. nStitchType = 0;//line
  419. }
  420. else
  421. {
  422. nStitchType = 1;// Angle
  423. }
  424. TPRINTA_INFO("Image index = %d, total images %d", m_nCurrentImage, m_nImageCount);
  425. char strcommand[100]{""};
  426. sprintf_s(strcommand, "USP%02d%02d%d%02d%03d%03d%03d", m_nImageCount, nDirection, nStitchType, nStepLength, nInitialHeight, nSID, nStitchLength);
  427. SendCommandWithoutAck(strcommand);
  428. return true;
  429. }
  430. int CMACHINE_DPC::SendCommandWithoutAck(char* strCommand, int nTimeOut)
  431. {
  432. int len = strlen(strCommand);
  433. if (len <= 0)
  434. {
  435. return -1;
  436. }
  437. int sum = 0;
  438. for (int i = 0; i < len; i++)
  439. {
  440. sum += strCommand[i];
  441. }
  442. sum = sum + (0x02) + (0x03);
  443. char xorResult = sum & 0xFF;
  444. char strSendCommand[100] = { 0 };
  445. strSendCommand[0] = STX;
  446. for (int i = 0; i < len; i++)
  447. {
  448. strSendCommand[i + 1] = strCommand[i];
  449. }
  450. strSendCommand[len + 1] = ETX;
  451. strSendCommand[len + 2] = xorResult;
  452. //增加SCF发送机制
  453. //SCFPacket* SCFCommand;
  454. //SCFCommand = new SCFPacket();
  455. m_pSendPacket->SetPacket(strSendCommand, strlen(strSendCommand));
  456. int bResult;
  457. PRINTA_TRACE(m_pLog, "== OUT ==: Begin: %s, Time: %d\n", strCommand, GetTickCount());
  458. if (m_pSCFServer)
  459. {
  460. m_pSCFServer->SCFLock(TIMEOUTVALUE);
  461. m_strCurrentCommand = strCommand;
  462. bResult = m_pSCFServer->SendPacket(m_pSendPacket, nTimeOut);
  463. m_pSCFServer->SCFUnLock();
  464. Sleep(50);
  465. PRINTA_TRACE(m_pLog, "== OUT ==: End: %s, Time: %d\n", strCommand, GetTickCount());
  466. }
  467. return bResult;
  468. }
  469. /////////////////////////////////////////////////////////////////////////
  470. RET_STATUS SYSTEM_CALL CMACHINE_DPC::GetDriverDictionary(ResDataObject& PARAM_OUT DriverInfo)
  471. {
  472. DriverInfo.add("DriverType", "PositionMachine");
  473. DriverInfo.add("DeviceName", "SiemensTOMO");
  474. DriverInfo.add("DateOfManufacture", "2019.4.12");
  475. DriverInfo.add("SoftwareVersion", "Alpha1.0");
  476. DriverInfo.add("HardwareVersion", "Alpha1.0");
  477. DriverInfo.add("Description", "None");
  478. DriverInfo.add("Properties", "None");
  479. return RET_SUCCEED;
  480. }
  481. struct tFrameMapping
  482. {
  483. static const int MaxLen = 5; // 前缀不能超超过 5 个字符 !
  484. using cbFun = std::function <void(const char *, int)>;
  485. char strHead[MaxLen];
  486. int NbOfCharOfHead;
  487. cbFun fun;
  488. tFrameMapping(char * str, int len, cbFun f)
  489. {
  490. assert(len < MaxLen);
  491. for (int i = 0; i < len; i++)
  492. strHead[i] = str[i];
  493. NbOfCharOfHead = len;
  494. fun = f;
  495. }
  496. };
  497. static std::list <tFrameMapping> arFrame;
  498. static bool DecodeFrame(const char* strFrame, int length);
  499. size_t CMACHINE_DPC::OnCallback(const char * strPackage, size_t nLength)
  500. {
  501. auto HWNotProcess = [](const char * value, int length) -> void
  502. {
  503. TPRINTA_WARN("This commands didn't need to process!");
  504. };
  505. auto HWExitStitch = [this](const char * value, int length) -> void
  506. {
  507. int ret = atoi(value);
  508. if (ret)
  509. {
  510. m_bStitchingInProgress = false;
  511. TPRINTA_INFO("Exit stitching state!");
  512. }
  513. else
  514. {
  515. TPRINTA_ERROR("Exit stitching failed!");
  516. }
  517. };
  518. auto HWERROR = [this](const char* value, int length)
  519. {
  520. assert(value);
  521. int nValue = atoi(value);
  522. if (nValue != 0)
  523. {
  524. char ErrorCode[10]{ "" };
  525. sprintf_s(ErrorCode, "OTC_ERROR_MSG_%03d", nValue);
  526. int level = 1;
  527. AddErrorMessage(ErrorCode, level, "");
  528. }
  529. else
  530. {
  531. int level = 1;
  532. char ErrorCode[10]{ "" };
  533. DelErrorMessage(ErrorCode, level, "");
  534. }
  535. };
  536. auto HWWARN = [this](const char* value, int length)
  537. {
  538. assert(value);
  539. int nValue = atoi(value);
  540. if (nValue != 0)
  541. {
  542. char ErrorCode[10]{ "" };
  543. sprintf_s(ErrorCode, "OTC_WARN_MSG_%03d", nValue);
  544. int level = 1;
  545. AddErrorMessage(ErrorCode, level, "",1);
  546. }
  547. else
  548. {
  549. int level = 1;
  550. char ErrorCode[10]{ "" };
  551. DelErrorMessage(ErrorCode, level, "",1);
  552. }
  553. };
  554. auto HWExtraImage = [this](const char * value, int length) -> void
  555. {
  556. int ret = atoi(value);
  557. if (ret)
  558. {
  559. m_pGenLogical->MechStitch::NewExtraView();
  560. TPRINTA_INFO("Extra image number!");
  561. }
  562. else
  563. {
  564. TPRINTA_ERROR("Extra image number failed!");
  565. }
  566. };
  567. auto HWAcceptImage = [this](const char * value, int length) -> void
  568. {
  569. m_pGenLogical->MechStitch::AcceptStitchingImage();
  570. };
  571. auto HWRejectImage = [this](const char * value, int length) -> void
  572. {
  573. m_pGenLogical->MechStitch::RejectStitchingImage();
  574. };
  575. auto HWCurrentImage = [this](const char * value, int length) -> void
  576. {
  577. m_nCurrentImage = atoi(value);
  578. m_pGenLogical->MechStitch::UpdateCurrentImage(m_nCurrentImage);
  579. };
  580. auto HWImageCount = [this](const char * value, int length) -> void
  581. {
  582. m_nImageCount = atoi(value);
  583. m_pGenLogical->MechStitch::UpdateImageCount(m_nImageCount);
  584. };
  585. auto HWDriction = [this](const char * value, int length) -> void
  586. {
  587. int nStitchDirection = atoi(value);
  588. if (1 == nStitchDirection)
  589. {
  590. nStitchDirection = UP_TO_DOWN;
  591. }
  592. else if (2 == nStitchDirection)
  593. {
  594. nStitchDirection = DOWN_TO_UP;
  595. }
  596. else
  597. {
  598. //default
  599. nStitchDirection = UP_TO_DOWN;
  600. }
  601. m_pGenLogical->MechStitch::UpdateStitchDirection(nStitchDirection);
  602. };
  603. auto HWStartStitch = [this](const char * value, int length) -> void
  604. {
  605. string strvalue = value;
  606. m_nImageCount = atoi(strvalue.substr(0,2).c_str());
  607. m_nCurrentImage = 1;
  608. m_bStitchingInProgress = true;
  609. m_pGenLogical->MechStitch::UpdateImageCount(m_nImageCount);
  610. m_pGenLogical->MechStitch::UpdateCurrentImage(m_nCurrentImage);
  611. };
  612. auto HWStitchState = [this](const char * value, int length) -> void
  613. {
  614. int nTemp = atoi(value);
  615. switch (nTemp)
  616. {
  617. case 1:
  618. {
  619. if (m_bStitchingInProgress == false)
  620. {
  621. m_bStitchingInProgress = true;
  622. //g_pLogFile->WriteLog("First image, SETUP_AVAIABLE;",LOG_INFORMATION,LOG_DEBUG,true);
  623. //::PostMessage(m_hWnd,MSG_STITCHING_PARAM,STITCH_PARAM_SETUP_AVAIABLE,NULL);
  624. }
  625. TPRINTA_INFO("First image, SETUP_AVAIABLE;");
  626. break;
  627. }
  628. case 2:
  629. {
  630. if (m_bStitchingInProgress)
  631. {
  632. TPRINTA_INFO("Need move to the position !");
  633. }
  634. break;
  635. }
  636. case 3:
  637. {
  638. if (m_bStitchingInProgress)
  639. {
  640. //::PostMessage(m_hWnd, MSG_STITCHING_PARAM, STITCH_PARAM_HW_STATUS, HW_NOT_READY);
  641. if (m_nCurrentImage == 1)
  642. {
  643. TPRINTA_INFO("First image, SETUP_MOVING;");
  644. //::PostMessage(m_hWnd, MSG_STITCHING_PARAM, STITCH_PARAM_SETUP_MOVING, NULL);
  645. }
  646. else
  647. {
  648. TPRINTA_INFO("Moving to next position ;");
  649. //::PostMessage(m_hWnd, MSG_STITCHING_PARAM, STITCH_PARAM_MOVING_STEP, NULL);
  650. }
  651. }
  652. break;
  653. }
  654. case 4:
  655. {
  656. if (m_bStitchingInProgress)
  657. {
  658. //::PostMessage(m_hWnd, MSG_STITCHING_PARAM, STITCH_PARAM_HW_STATUS, HW_READY);
  659. TPRINTA_INFO("Ready for exposure ;");
  660. //::PostMessage(m_hWnd, MSG_STITCHING_PARAM, STITCH_PARAM_EXP_READY, NULL);
  661. }
  662. break;
  663. }
  664. case 5:
  665. {
  666. if (m_bStitchingInProgress)
  667. {
  668. m_bStitchingInProgress = false;
  669. }
  670. break;
  671. }
  672. case 6:
  673. {
  674. if (m_bStitchingInProgress == false)
  675. {
  676. m_bStitchingInProgress = true;
  677. //g_pLogFile->WriteLog("First image, SETUP_AVAIABLE;",LOG_INFORMATION,LOG_DEBUG,true);
  678. //::PostMessage(m_hWnd,MSG_STITCHING_PARAM,STITCH_PARAM_SETUP_AVAIABLE,NULL);
  679. }
  680. //::PostMessage(m_hWnd, MSG_STITCHING_PARAM, STITCH_PARAM_SETUP_RECEIVED, NULL);
  681. break;
  682. }
  683. case 7:
  684. {
  685. if (m_bStitchingInProgress == false)
  686. {
  687. m_bStitchingInProgress = true;
  688. //g_pLogFile->WriteLog("First image, SETUP_AVAIABLE;",LOG_INFORMATION,LOG_DEBUG,true);
  689. //::PostMessage(m_hWnd,MSG_STITCHING_PARAM,STITCH_PARAM_SETUP_AVAIABLE,NULL);
  690. }
  691. TPRINTA_INFO("First image, SETUP_AVAIABLE;");
  692. //::PostMessage(m_hWnd, MSG_STITCHING_PARAM, STITCH_PARAM_SETUP_AVAIABLE, NULL);
  693. break;
  694. }
  695. case 8:
  696. {
  697. break;
  698. }
  699. case 9:
  700. {
  701. if (m_bStitchingInProgress)
  702. {
  703. //::PostMessage(m_hWnd, MSG_STITCHING_PARAM, STITCH_PARAM_HW_STATUS, HW_READY);
  704. TPRINTA_INFO("Setup position reached ,Send SS to start produce;");
  705. //::PostMessage(m_hWnd, MSG_STITCHING_PARAM, STITCH_PARAM_SETUP_REACHED, NULL);
  706. }
  707. break;
  708. }
  709. case 10:
  710. {
  711. if (m_bStitchingInProgress)
  712. {
  713. TPRINTA_INFO(" Position reached;");
  714. //::PostMessage(m_hWnd, MSG_STITCHING_PARAM, STITCH_PARAM_POSITION_REACHED, NULL);
  715. }
  716. break;
  717. }
  718. case 11:
  719. {
  720. if (m_bStitchingInProgress)
  721. {
  722. TPRINTA_INFO(" Extra image reached;");
  723. //::PostMessage(m_hWnd, MSG_STITCHING_PARAM, STITCH_PARAM_EXTRA_IMAGE_REACHED, NULL);
  724. }
  725. break;
  726. }
  727. default:
  728. break;
  729. }
  730. };
  731. //
  732. arFrame.clear();
  733. arFrame.push_back(tFrameMapping("UCS", 3, HWExitStitch));
  734. arFrame.push_back(tFrameMapping("USE", 3, HWExitStitch));
  735. arFrame.push_back(tFrameMapping("UER", 3, HWERROR));
  736. arFrame.push_back(tFrameMapping("UEX", 3, HWExtraImage));
  737. arFrame.push_back(tFrameMapping("USA", 3, HWAcceptImage));
  738. arFrame.push_back(tFrameMapping("USC", 3, HWCurrentImage));
  739. arFrame.push_back(tFrameMapping("USD", 3, HWDriction));
  740. arFrame.push_back(tFrameMapping("USJ", 3, HWRejectImage));
  741. arFrame.push_back(tFrameMapping("USR", 3, HWNotProcess));
  742. arFrame.push_back(tFrameMapping("USM", 3, HWNotProcess));
  743. arFrame.push_back(tFrameMapping("USN", 3, HWImageCount));
  744. arFrame.push_back(tFrameMapping("USP", 3, HWStartStitch));
  745. arFrame.push_back(tFrameMapping("USS", 3, HWNotProcess));
  746. arFrame.push_back(tFrameMapping("UST", 3, HWStitchState));
  747. arFrame.push_back(tFrameMapping("UWN", 3, HWWARN));
  748. return nLength;
  749. }
  750. //-----------------------------------------------------------------------------
  751. // DecodeFrame
  752. //-----------------------------------------------------------------------------
  753. static bool DecodeFrame(const char* strFrame, int length)
  754. {
  755. auto pr = [strFrame, length](const tFrameMapping& Item)
  756. {
  757. for (int i = 0; i < Item.NbOfCharOfHead; i++)
  758. {
  759. if (strFrame[i] != Item.strHead[i])
  760. {
  761. return false;
  762. }
  763. }
  764. return true;
  765. };
  766. auto found = std::find_if(arFrame.begin(), arFrame.end(), pr);
  767. if (found == arFrame.end())
  768. {
  769. return false;
  770. }
  771. const auto& Item = *found;
  772. auto pc = strFrame;
  773. pc += Item.NbOfCharOfHead;
  774. Item.fun(pc, length - Item.NbOfCharOfHead);
  775. return true;
  776. }