OxfordControllerWrapper.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572
  1. #include "stdafx.h"
  2. #include "OxfordControllerWrapper.h"
  3. #include "tinyxml2.h"
  4. #include "XMLSerialization.h"
  5. using namespace System;
  6. using namespace System::Windows;
  7. using namespace System::Collections::Generic;
  8. using namespace System::Collections::ObjectModel;
  9. using namespace System::Collections::Specialized;
  10. using namespace System::ComponentModel;
  11. using namespace System::Windows::Forms;
  12. using namespace System::Threading;
  13. /// <summary>
  14. /// The oxford controller channels.
  15. /// Always collect xray by this channels.
  16. /// </summary>
  17. const int g_nOxfordControllerChannels = 4096; // use 2k to collect xray spectrum
  18. const int g_nOxfordControllerProcessTime = 4;
  19. const int g_nOxfordControllerEnergyRange = 20;
  20. const int g_nOxfordControllerEventWaitTimerInt = 100;
  21. const int g_nOxfordControllerEventSleepTimerInt = 20;
  22. const long g_nMicrocopeConnectTimeOutMilliSeconds = 2000;
  23. // Oxford single point delay time(ms)
  24. const long g_nSinglePointCollectDelay = 3000;
  25. const long g_nSingleFeatureCollectDelay = 5000;
  26. const long g_nXrayControllerConnectTimeOutMilliSeconds = 10000;
  27. const long g_nImageTimeOutMilliSeconds = 30000;
  28. const long g_nStageTimeOutMilliSeconds = 30000;
  29. OxfordControllerWrapper::OxfordControllerWrapper(void)
  30. : m_bIsStageUpdated(false)
  31. , m_bBeamPositionSet(false)
  32. {
  33. //_dataType = ImageDataType::DataByte;
  34. m_startControllerEvent = gcnew AutoResetEvent(false);
  35. m_endControllerEvent = gcnew AutoResetEvent(false);
  36. }
  37. void OxfordControllerWrapper::CloseClient(void)
  38. {
  39. if (_controllerThread && _controllerThread->IsAlive)
  40. {
  41. _oxfordControllerData.m_nCommand = OxfordControllerCommand::EXIT;
  42. m_startControllerEvent->Set();
  43. m_endControllerEvent->WaitOne(g_nOxfordControllerEventWaitTimerInt);
  44. }
  45. _controllerThread->Abort();
  46. }
  47. OxfordControllerWrapper::~OxfordControllerWrapper(void)
  48. {
  49. if (_controllerThread && _controllerThread->IsAlive)
  50. {
  51. _oxfordControllerData.m_nCommand = OxfordControllerCommand::EXIT;
  52. m_startControllerEvent->Set();
  53. m_endControllerEvent->WaitOne(g_nOxfordControllerEventWaitTimerInt);
  54. }
  55. _controllerThread->Abort();
  56. }
  57. OxfordControllerWrapper::!OxfordControllerWrapper(void)
  58. {
  59. if (_controllerThread && _controllerThread->IsAlive)
  60. {
  61. _oxfordControllerData.m_nCommand = OxfordControllerCommand::EXIT;
  62. m_startControllerEvent->Set();
  63. m_endControllerEvent->WaitOne(g_nOxfordControllerEventWaitTimerInt);
  64. }
  65. _controllerThread->Abort();
  66. }
  67. bool OxfordControllerWrapper::Init()
  68. {
  69. if (_controllerThread != nullptr && _controllerThread->IsAlive)
  70. {
  71. return true;
  72. }
  73. _controllerThread = gcnew Thread(gcnew ThreadStart(this, &OxfordControllerWrapper::ControllerThreadFunction));
  74. m_startControllerEvent->Reset();
  75. m_endControllerEvent->Reset();
  76. _controllerThread->Start();
  77. return true;
  78. }
  79. void OxfordControllerWrapper::ControllerThreadFunction()
  80. {
  81. if (_microscopeController == nullptr)
  82. {
  83. //OutputDebugString(_T("Create Microscope controller in ControllerThreadFunction."));
  84. CreateMicroscopeController();
  85. }
  86. if (_edSpectrumController == nullptr)
  87. {
  88. //System::Diagnostics::Trace::WriteLine("Create Spectrum controller in ControllerThreadFunction.");
  89. //OutputDebugString(_T("Create Spectrum controller in ControllerThreadFunction."));
  90. CreateEdsSpectrumController();
  91. }
  92. if (_imageAcqusitionController == nullptr)
  93. {
  94. //OutputDebugString(_T("Create Image controller in ControllerThreadFunction."));
  95. CreateImageAcqusitionController();
  96. }
  97. if (_edsChordListController == nullptr)
  98. {
  99. //OutputDebugString(_T("Create chordlist controller in ControllerThreadFunction."));
  100. CreateChordlistController();
  101. }
  102. while (true)
  103. {
  104. while (true)
  105. {
  106. if (m_startControllerEvent->WaitOne(0, true))
  107. {
  108. m_startControllerEvent->Reset();
  109. break;
  110. }
  111. Application::DoEvents();
  112. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  113. }
  114. switch (_oxfordControllerData.m_nCommand)
  115. {
  116. case OxfordControllerCommand::GET_WORKINGDISTANCE:
  117. {
  118. double dWD = 0;
  119. if (GetWorkingDistanceFromController(dWD))
  120. {
  121. _oxfordControllerData.m_dWorkingDistance = dWD;
  122. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  123. }
  124. else
  125. {
  126. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  127. }
  128. m_endControllerEvent->Set();
  129. }
  130. break;
  131. case OxfordControllerCommand::SET_WORKINGDISTANCE:
  132. {
  133. if (SetWorkingDistanceToController(_oxfordControllerData.m_dWorkingDistance))
  134. {
  135. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  136. }
  137. else
  138. {
  139. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  140. }
  141. m_endControllerEvent->Set();
  142. }
  143. break;
  144. case OxfordControllerCommand::GET_POSITIONXY:
  145. {
  146. double dX, dY;
  147. if (GetPositionXYFromController(dX, dY))
  148. {
  149. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  150. _oxfordControllerData.m_dPositionX = dX;
  151. _oxfordControllerData.m_dPositionY = dY;
  152. }
  153. else
  154. {
  155. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  156. }
  157. m_endControllerEvent->Set();
  158. }
  159. break;
  160. case OxfordControllerCommand::SET_POSITIONXY:
  161. {
  162. if (SetPositionXYToController(_oxfordControllerData.m_dPositionX, _oxfordControllerData.m_dPositionY))
  163. {
  164. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  165. }
  166. else
  167. {
  168. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  169. }
  170. m_endControllerEvent->Set();
  171. }
  172. break;
  173. case OxfordControllerCommand::GET_MAGNIFICATION:
  174. {
  175. double dMag = 0;
  176. if (GetMagnificationFromController(dMag))
  177. {
  178. _oxfordControllerData.m_dMagnification = dMag;
  179. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  180. }
  181. else
  182. {
  183. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  184. }
  185. m_endControllerEvent->Set();
  186. }
  187. break;
  188. case OxfordControllerCommand::SET_MAGNIFICATION:
  189. {
  190. if (SetMagnificationToController(_oxfordControllerData.m_dMagnification))
  191. {
  192. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  193. }
  194. else
  195. {
  196. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  197. }
  198. m_endControllerEvent->Set();
  199. }
  200. break;
  201. case OxfordControllerCommand::GET_HIGHVOTAGE:
  202. {
  203. double dValue = 0;
  204. if (GetHighVoltageFromController(dValue))
  205. {
  206. _oxfordControllerData.m_dHighVotage = dValue;
  207. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  208. }
  209. else
  210. {
  211. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  212. }
  213. m_endControllerEvent->Set();
  214. }
  215. break;
  216. case OxfordControllerCommand::SET_HIGHVOTAGE:
  217. {
  218. if (SetHighVoltageToController(_oxfordControllerData.m_dHighVotage))
  219. {
  220. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  221. }
  222. else
  223. {
  224. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  225. }
  226. m_endControllerEvent->Set();
  227. }
  228. break;
  229. case OxfordControllerCommand::GET_BEAMON:
  230. {
  231. bool bValue;
  232. if (GetBeamOnFromController(bValue))
  233. {
  234. _oxfordControllerData.m_bBeamOn = bValue;
  235. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  236. }
  237. else
  238. {
  239. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  240. }
  241. m_endControllerEvent->Set();
  242. }
  243. break;
  244. case OxfordControllerCommand::SET_BEAMON:
  245. {
  246. if (SetBeamOnToController(_oxfordControllerData.m_bBeamOn))
  247. {
  248. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  249. }
  250. else
  251. {
  252. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  253. }
  254. m_endControllerEvent->Set();
  255. }
  256. break;
  257. case OxfordControllerCommand::GET_BEAMBLANK:
  258. {
  259. bool bValue;
  260. if (GetBeamBlankFromController(bValue))
  261. {
  262. _oxfordControllerData.m_bBeamBlank = bValue;
  263. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  264. }
  265. else
  266. {
  267. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  268. }
  269. m_endControllerEvent->Set();
  270. }
  271. break;
  272. case OxfordControllerCommand::SET_BEAMBLANK:
  273. {
  274. if (SetBeamBlankToController(_oxfordControllerData.m_bBeamBlank))
  275. {
  276. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  277. }
  278. else
  279. {
  280. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  281. }
  282. m_endControllerEvent->Set();
  283. }
  284. break;
  285. case OxfordControllerCommand::GET_EXTERNAL:
  286. {
  287. bool bValue;
  288. if (GetExternalScanFromController(bValue))
  289. {
  290. _oxfordControllerData.m_bExternal = bValue;
  291. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  292. }
  293. else
  294. {
  295. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  296. }
  297. m_endControllerEvent->Set();
  298. }
  299. break;
  300. case OxfordControllerCommand::SET_EXTERNAL:
  301. {
  302. if (SetExternalScanToController(_oxfordControllerData.m_bExternal))
  303. {
  304. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  305. }
  306. else
  307. {
  308. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  309. }
  310. m_endControllerEvent->Set();
  311. }
  312. break;
  313. case OxfordControllerCommand::COLLECT_IMAGE:
  314. {
  315. if (!StartImageCollecting())
  316. {
  317. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  318. m_endControllerEvent->Set();
  319. }
  320. else
  321. {
  322. _oxfordControllerData.m_nState = OxfordControllerState::WORKING;
  323. }
  324. }
  325. break;
  326. case OxfordControllerCommand::COLLECT_XRAYPOINT:
  327. {
  328. auto edsController = CreateEdsSpectrumController();
  329. bool bRet = false;
  330. if (edsController && _oxfordControllerData.m_pSpectrumData)
  331. {
  332. edsController->BeginMultipleAcquisition();
  333. //OutputDebugString(_T("BeginMultipleAcquisition...(single point)"));
  334. if (SetBeamPosition(_oxfordControllerData.m_dBeamPositionX, _oxfordControllerData.m_dBeamPositionY))
  335. {
  336. bRet = StartXrayCollecting(_oxfordControllerData.m_nAcTime);
  337. }
  338. }
  339. if (!bRet)
  340. {
  341. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  342. m_endControllerEvent->Set();
  343. }
  344. else
  345. {
  346. _oxfordControllerData.m_nState = OxfordControllerState::WORKING;
  347. }
  348. }
  349. break;
  350. case OxfordControllerCommand::COLLECT_CURRENTXRAYPOINT:
  351. {
  352. auto edsController = CreateEdsSpectrumController();
  353. bool bRet = false;
  354. if (edsController && _oxfordControllerData.m_pSpectrumData)
  355. {
  356. edsController->BeginMultipleAcquisition();
  357. //OutputDebugString(_T("BeginMultipleAcquisition...(single point)"));
  358. if (m_bBeamPositionSet)
  359. {
  360. m_bBeamPositionSet = false;
  361. bRet = true;
  362. //OutputDebugString(_T("Beam position already set."));
  363. Point pos(_oxfordControllerData.m_dBeamPositionX * _oxfordControllerData.m_dPixelSize, _oxfordControllerData.m_dBeamPositionY * _oxfordControllerData.m_dPixelSize);
  364. auto edsSettings = GetEdsSpectrumSettings();
  365. edsSettings->ScanSettings->AcquisitionRegion->CreatePointRegion(pos);
  366. }
  367. else
  368. {
  369. bRet = SetCurrentBeamPosition();
  370. //OutputDebugString(_T("Set current Beam position to collect xray."));
  371. }
  372. if (bRet)
  373. {
  374. bRet = StartXrayCollecting(_oxfordControllerData.m_nAcTime);
  375. }
  376. }
  377. if (!bRet)
  378. {
  379. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  380. m_endControllerEvent->Set();
  381. }
  382. else
  383. {
  384. _oxfordControllerData.m_nState = OxfordControllerState::WORKING;
  385. }
  386. }
  387. break;
  388. case OxfordControllerCommand::COLLECT_XRAYPOINTS:
  389. {
  390. auto edsController = CreateEdsSpectrumController();
  391. if (!edsController || !_oxfordControllerData.m_pXrayDataList || _oxfordControllerData.m_nXrayDataCount <= 0)
  392. {
  393. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  394. m_endControllerEvent->Set();
  395. }
  396. else
  397. {
  398. auto edsSettings = GetEdsSpectrumSettings();
  399. _oxfordControllerData.m_nCollectedXrayCounts = 0;
  400. _oxfordControllerData.m_nState = OxfordControllerState::WORKING;
  401. edsController->BeginMultipleAcquisition();
  402. for (int i = 0; i < _oxfordControllerData.m_nXrayDataCount; ++i)
  403. {
  404. OxfordXrayData* pXrayData = &_oxfordControllerData.m_pXrayDataList[i];
  405. // <note>Accept pixel position to convert to oxford normalized beam position</note>
  406. Point pos(pXrayData->m_nPosX * _oxfordControllerData.m_dPixelSize, pXrayData->m_nPosY * _oxfordControllerData.m_dPixelSize);
  407. edsSettings->ScanSettings->AcquisitionRegion->CreatePointRegion(pos);
  408. edsSettings->EdSettings->AcquisitionTime = TimeSpan::FromMilliseconds(_oxfordControllerData.m_nAcTime);
  409. edsController->StartAcquisition(edsSettings);
  410. }
  411. }
  412. }
  413. break;
  414. case OxfordControllerCommand::COLLECT_XRAYCHOILDLIST:
  415. {
  416. auto edsController = CreateEdsSpectrumController();
  417. if (!edsController || !_oxfordControllerData.m_pXrayDataList || _oxfordControllerData.m_nXrayDataCount <= 0)
  418. {
  419. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  420. m_endControllerEvent->Set();
  421. }
  422. else
  423. {
  424. auto edsSettings = GetEdsSpectrumSettings();
  425. _oxfordControllerData.m_nCollectedXrayCounts = 0;
  426. _oxfordControllerData.m_nState = OxfordControllerState::WORKING;
  427. edsController->BeginMultipleAcquisition();
  428. _oxfordControllerData.m_nCollectedXrayCounts = 0;
  429. for (int i = 0; i < _oxfordControllerData.m_nXrayDataCount; i++)
  430. {
  431. List< OINA::Extender::Data::Chord^>^ chords = gcnew List<OINA::Extender::Data::Chord^>();
  432. for (int j = 0; j < _oxfordControllerData.m_pXrayDataList[i].m_nChordNum; j++)
  433. {
  434. int X = _oxfordControllerData.m_pXrayDataList[i].m_ChordList[j].m_nX;
  435. int Y = _oxfordControllerData.m_pXrayDataList[i].m_ChordList[j].m_nY;
  436. int XLength = _oxfordControllerData.m_pXrayDataList[i].m_ChordList[j].m_nLength;
  437. OINA::Extender::Data::Chord^ chord = gcnew OINA::Extender::Data::Chord(X, Y, XLength);
  438. chords->Add(chord);
  439. }
  440. OINA::Extender::Data::ChordList^ chordsList = gcnew OINA::Extender::Data::ChordList(chords, _oxfordControllerData.m_dPixelSize);//1/1024.0
  441. edsSettings->ScanSettings->AcquisitionRegion->CreateChordListRegion(chordsList);
  442. edsSettings->EdSettings->AcquisitionTime = TimeSpan::FromMilliseconds(_oxfordControllerData.m_nAcTime);
  443. edsController->StartAcquisition(edsSettings);
  444. }
  445. }
  446. }
  447. break;
  448. case OxfordControllerCommand::COLLECT_QUANTIFYSPECTRUM:
  449. if (!StartImageCollecting())
  450. {
  451. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  452. m_endControllerEvent->Set();
  453. }
  454. else
  455. {
  456. _oxfordControllerData.m_nState = OxfordControllerState::WORKING;
  457. }
  458. break;
  459. case OxfordControllerCommand::STOP_ACQUISITION:
  460. {
  461. auto edsController = CreateEdsSpectrumController();
  462. while (edsController->IsAcquiring)
  463. {
  464. edsController->StopAcquisition();
  465. }
  466. }
  467. break;
  468. case OxfordControllerCommand::END_MULTIPLEACQUISITION:
  469. {
  470. auto edsController = CreateEdsSpectrumController();
  471. while (edsController->IsAcquiring)
  472. {
  473. edsController->StopAcquisition();
  474. }
  475. edsController->EndMultipleAcquisition();
  476. }
  477. break;
  478. case OxfordControllerCommand::EXIT:
  479. m_endControllerEvent->Set();
  480. return;
  481. break;
  482. }
  483. }
  484. }
  485. bool OxfordControllerWrapper::IsConnected()
  486. {
  487. return true;
  488. }
  489. bool OxfordControllerWrapper::IsMicroscopeColumnConnected()
  490. {
  491. auto microscopeController = this->CreateMicroscopeController();
  492. if (microscopeController != nullptr)
  493. {
  494. return microscopeController->ColumnConnectionStatus->IsConnected;
  495. }
  496. return false;
  497. }
  498. bool OxfordControllerWrapper::IsMicroscopeStageConnected()
  499. {
  500. auto microscopeController = this->CreateMicroscopeController();
  501. if (microscopeController != nullptr)
  502. {
  503. return microscopeController->StageConnectionStatus->IsConnected;
  504. }
  505. return false;
  506. }
  507. IStageConditions^ OxfordControllerWrapper::GetStageConditions()
  508. {
  509. if (!IsMicroscopeStageConnected())
  510. {
  511. return nullptr;
  512. }
  513. return _microscopeController->StageConditions;
  514. }
  515. bool OxfordControllerWrapper::GetPositionXY(double& a_dPosX, double& a_dPosY)
  516. {
  517. _oxfordControllerData.m_nCommand = OxfordControllerCommand::GET_POSITIONXY;
  518. m_startControllerEvent->Set();
  519. long nCollectedTime = 0;
  520. while (true)
  521. {
  522. if (m_endControllerEvent->WaitOne(0, true))
  523. {
  524. m_endControllerEvent->Reset();
  525. break;
  526. }
  527. Application::DoEvents();
  528. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  529. long nCollectedTimeLimit = g_nStageTimeOutMilliSeconds;
  530. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  531. if (nCollectedTime > nCollectedTimeLimit)
  532. {
  533. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  534. m_endControllerEvent->Reset();
  535. break;
  536. }
  537. }
  538. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  539. {
  540. a_dPosX = _oxfordControllerData.m_dPositionX;
  541. a_dPosY = _oxfordControllerData.m_dPositionY;
  542. return true;
  543. }
  544. return false;
  545. }
  546. /// <summary>
  547. /// Get position xy from controller
  548. /// </summary>
  549. /// <param name="a_dPosX">The double of position x.</param>
  550. /// <param name="a_dPosY">The double of position y.</param>
  551. /// <returns>
  552. /// Return the position xy from controller
  553. /// </returns>
  554. bool OxfordControllerWrapper::GetPositionXYFromController(double& a_dPosX, double& a_dPosY)
  555. {
  556. if (!IsMicroscopeStageConnected())
  557. {
  558. return false;
  559. }
  560. while(!_microscopeController->StageCapabilities->StageX->CanRead)
  561. {
  562. Sleep(100);
  563. }
  564. a_dPosX = _microscopeController->StageConditions->StageX * 1000.0;
  565. while(!_microscopeController->StageCapabilities->StageY->CanRead)
  566. {
  567. Sleep(100);
  568. }
  569. a_dPosY = _microscopeController->StageConditions->StageY * 1000.0;
  570. return true;
  571. }
  572. /// <summary>
  573. /// Set the position xy
  574. /// </summary>
  575. /// <param name="a_dPosX">The double of position x.</param>
  576. /// <param name="a_dPosY">The double of position y.</param>
  577. /// <returns>TRUE if success, otherwise FALSE</returns>
  578. bool OxfordControllerWrapper::SetPositionXY(const double a_dPosX, const double a_dPosY)
  579. {
  580. _oxfordControllerData.m_nCommand = OxfordControllerCommand::SET_POSITIONXY;
  581. _oxfordControllerData.m_dPositionX = a_dPosX;
  582. _oxfordControllerData.m_dPositionY = a_dPosY;
  583. m_bIsStageUpdated = false;
  584. m_startControllerEvent->Set();
  585. long nCollectedTime = 0;
  586. while (true)
  587. {
  588. if (m_bIsStageUpdated)
  589. {
  590. if (m_endControllerEvent->WaitOne(0, true))
  591. {
  592. m_endControllerEvent->Reset();
  593. break;
  594. }
  595. }
  596. Application::DoEvents();
  597. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  598. long nCollectedTimeLimit = g_nStageTimeOutMilliSeconds;
  599. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  600. if (nCollectedTime > nCollectedTimeLimit)
  601. {
  602. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  603. m_endControllerEvent->Reset();
  604. break;
  605. }
  606. }
  607. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  608. {
  609. return true;
  610. }
  611. else
  612. {
  613. return false;
  614. }
  615. }
  616. bool OxfordControllerWrapper::SetPositionXYToController(const double a_dPosX, const double a_dPosY)
  617. {
  618. if (!IsMicroscopeStageConnected())
  619. {
  620. return false;
  621. }
  622. Dictionary<Stage, double>^ stageDictionary = gcnew Dictionary<Stage, double>;
  623. stageDictionary->Add(Stage::StageX, a_dPosX / 1000.0);
  624. stageDictionary->Add(Stage::StageY, a_dPosY / 1000.0);
  625. _microscopeController->SetStageConditions(stageDictionary);
  626. return true;
  627. }
  628. bool OxfordControllerWrapper::GetWorkingDistance(double& a_dWorkingDistance)
  629. {
  630. _oxfordControllerData.m_nCommand = OxfordControllerCommand::GET_WORKINGDISTANCE;
  631. m_startControllerEvent->Set();
  632. long nCollectedTime = 0;
  633. while (true)
  634. {
  635. if (m_endControllerEvent->WaitOne(0, true))
  636. {
  637. m_endControllerEvent->Reset();
  638. break;
  639. }
  640. Application::DoEvents();
  641. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  642. long nCollectedTimeLimit = g_nImageTimeOutMilliSeconds;
  643. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  644. if (nCollectedTime > nCollectedTimeLimit)
  645. {
  646. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  647. m_endControllerEvent->Reset();
  648. break;
  649. }
  650. }
  651. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  652. {
  653. a_dWorkingDistance = _oxfordControllerData.m_dWorkingDistance;
  654. return true;
  655. }
  656. return false;
  657. }
  658. bool OxfordControllerWrapper::GetWorkingDistanceFromController(double& a_dWorkingDistance)
  659. {
  660. if (!IsMicroscopeColumnConnected())
  661. {
  662. return false;
  663. }
  664. a_dWorkingDistance = _microscopeController->ColumnConditions->WorkingDistance;
  665. return true;
  666. }
  667. bool OxfordControllerWrapper::SetWorkingDistance(double a_dWorkingDistance)
  668. {
  669. _oxfordControllerData.m_nCommand = OxfordControllerCommand::SET_WORKINGDISTANCE;
  670. _oxfordControllerData.m_dWorkingDistance = a_dWorkingDistance;
  671. m_startControllerEvent->Set();
  672. long nCollectedTime = 0;
  673. while (true)
  674. {
  675. if (m_endControllerEvent->WaitOne(0, true))
  676. {
  677. m_endControllerEvent->Reset();
  678. break;
  679. }
  680. Application::DoEvents();
  681. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  682. long nCollectedTimeLimit = g_nImageTimeOutMilliSeconds;
  683. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  684. if (nCollectedTime > nCollectedTimeLimit)
  685. {
  686. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  687. m_endControllerEvent->Reset();
  688. break;
  689. }
  690. }
  691. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  692. {
  693. return true;
  694. }
  695. return false;
  696. }
  697. bool OxfordControllerWrapper::SetWorkingDistanceToController(double a_dWorkingDistance)
  698. {
  699. if (!IsMicroscopeColumnConnected())
  700. {
  701. return false;
  702. }
  703. Dictionary<Column, double>^ columnDictionary = gcnew Dictionary<Column, double>;
  704. columnDictionary->Add(Column::WorkingDistance, a_dWorkingDistance);
  705. _microscopeController->SetColumnConditions(columnDictionary);
  706. return true;
  707. }
  708. bool OxfordControllerWrapper::GetMagnification(double& a_dMagnification)
  709. {
  710. _oxfordControllerData.m_nCommand = OxfordControllerCommand::GET_MAGNIFICATION;
  711. m_startControllerEvent->Set();
  712. long nCollectedTime = 0;
  713. while (true)
  714. {
  715. if (m_endControllerEvent->WaitOne(0, true))
  716. {
  717. m_endControllerEvent->Reset();
  718. break;
  719. }
  720. Application::DoEvents();
  721. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  722. long nCollectedTimeLimit = g_nImageTimeOutMilliSeconds;
  723. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  724. if (nCollectedTime > nCollectedTimeLimit)
  725. {
  726. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  727. m_endControllerEvent->Reset();
  728. break;
  729. }
  730. }
  731. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  732. {
  733. a_dMagnification = _oxfordControllerData.m_dMagnification;
  734. return true;
  735. }
  736. return false;
  737. }
  738. bool OxfordControllerWrapper::GetMagnificationFromController(double& a_dMagnification)
  739. {
  740. if (!IsMicroscopeColumnConnected())
  741. {
  742. return false;
  743. }
  744. a_dMagnification = _microscopeController->ColumnConditions->Magnification;
  745. return true;
  746. }
  747. bool OxfordControllerWrapper::SetMagnification(double a_dMagnification)
  748. {
  749. _oxfordControllerData.m_nCommand = OxfordControllerCommand::SET_MAGNIFICATION;
  750. _oxfordControllerData.m_dMagnification = a_dMagnification;
  751. m_startControllerEvent->Set();
  752. long nCollectedTime = 0;
  753. while (true)
  754. {
  755. if (m_endControllerEvent->WaitOne(0, true))
  756. {
  757. m_endControllerEvent->Reset();
  758. break;
  759. }
  760. Application::DoEvents();
  761. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  762. long nCollectedTimeLimit = g_nImageTimeOutMilliSeconds;
  763. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  764. if (nCollectedTime > nCollectedTimeLimit)
  765. {
  766. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  767. m_endControllerEvent->Reset();
  768. break;
  769. }
  770. }
  771. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  772. {
  773. return true;
  774. }
  775. return false;
  776. }
  777. bool OxfordControllerWrapper::SetMagnificationToController(double a_dMagnification)
  778. {
  779. if (!IsMicroscopeColumnConnected())
  780. {
  781. return false;
  782. }
  783. Dictionary<Column, double>^ columnDictionary = gcnew Dictionary<Column, double>;
  784. columnDictionary->Add(Column::Magnification, a_dMagnification);
  785. _microscopeController->SetColumnConditions(columnDictionary);
  786. return true;
  787. }
  788. bool OxfordControllerWrapper::GetHighVoltage(double& a_dHighVoltage)
  789. {
  790. _oxfordControllerData.m_nCommand = OxfordControllerCommand::GET_HIGHVOTAGE;
  791. m_startControllerEvent->Set();
  792. long nCollectedTime = 0;
  793. while (true)
  794. {
  795. if (m_endControllerEvent->WaitOne(0, true))
  796. {
  797. m_endControllerEvent->Reset();
  798. break;
  799. }
  800. Application::DoEvents();
  801. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  802. long nCollectedTimeLimit = g_nImageTimeOutMilliSeconds;
  803. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  804. if (nCollectedTime > nCollectedTimeLimit)
  805. {
  806. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  807. m_endControllerEvent->Reset();
  808. break;
  809. }
  810. }
  811. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  812. {
  813. a_dHighVoltage = _oxfordControllerData.m_dHighVotage;
  814. return true;
  815. }
  816. return false;
  817. }
  818. bool OxfordControllerWrapper::GetHighVoltageFromController(double& a_dHighVoltage)
  819. {
  820. if (!IsMicroscopeColumnConnected())
  821. {
  822. return false;
  823. }
  824. a_dHighVoltage = _microscopeController->ColumnConditions->HighVoltage;
  825. return true;
  826. }
  827. bool OxfordControllerWrapper::SetHighVoltage(double a_dHighVoltage)
  828. {
  829. _oxfordControllerData.m_nCommand = OxfordControllerCommand::SET_HIGHVOTAGE;
  830. _oxfordControllerData.m_dHighVotage = a_dHighVoltage;
  831. m_startControllerEvent->Set();
  832. long nCollectedTime = 0;
  833. while (true)
  834. {
  835. if (m_endControllerEvent->WaitOne(0, true))
  836. {
  837. m_endControllerEvent->Reset();
  838. break;
  839. }
  840. Application::DoEvents();
  841. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  842. long nCollectedTimeLimit = g_nImageTimeOutMilliSeconds;
  843. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  844. if (nCollectedTime > nCollectedTimeLimit)
  845. {
  846. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  847. m_endControllerEvent->Reset();
  848. break;
  849. }
  850. }
  851. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  852. {
  853. return true;
  854. }
  855. return false;
  856. }
  857. bool OxfordControllerWrapper::SetHighVoltageToController(double a_dHighVoltage)
  858. {
  859. if (!IsMicroscopeColumnConnected())
  860. {
  861. return false;
  862. }
  863. Dictionary<Column, double>^ columnDictionary = gcnew Dictionary<Column, double>;
  864. columnDictionary->Add(Column::HighVoltage, a_dHighVoltage);
  865. _microscopeController->SetColumnConditions(columnDictionary);
  866. return true;
  867. }
  868. bool OxfordControllerWrapper::GetBeamOn(bool& a_bBeamOn)
  869. {
  870. _oxfordControllerData.m_nCommand = OxfordControllerCommand::GET_BEAMON;
  871. m_startControllerEvent->Set();
  872. long nCollectedTime = 0;
  873. while (true)
  874. {
  875. if (m_endControllerEvent->WaitOne(0, true))
  876. {
  877. m_endControllerEvent->Reset();
  878. break;
  879. }
  880. Application::DoEvents();
  881. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  882. long nCollectedTimeLimit = g_nImageTimeOutMilliSeconds;
  883. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  884. if (nCollectedTime > nCollectedTimeLimit)
  885. {
  886. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  887. m_endControllerEvent->Reset();
  888. break;
  889. }
  890. }
  891. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  892. {
  893. a_bBeamOn = _oxfordControllerData.m_bBeamOn;
  894. return true;
  895. }
  896. return false;
  897. }
  898. bool OxfordControllerWrapper::GetBeamOnFromController(bool& a_bBeamOn)
  899. {
  900. if (!IsMicroscopeColumnConnected())
  901. {
  902. return false;
  903. }
  904. double dValue = _microscopeController->ColumnConditions->BeamOn;
  905. // notice: false: 0.0 and true: 1.0
  906. a_bBeamOn = dValue > 0.1;
  907. return true;
  908. }
  909. bool OxfordControllerWrapper::SetBeamOn(bool a_bBeamOn)
  910. {
  911. _oxfordControllerData.m_nCommand = OxfordControllerCommand::SET_BEAMON;
  912. _oxfordControllerData.m_bBeamOn = a_bBeamOn;
  913. m_startControllerEvent->Set();
  914. long nCollectedTime = 0;
  915. while (true)
  916. {
  917. if (m_endControllerEvent->WaitOne(0, true))
  918. {
  919. m_endControllerEvent->Reset();
  920. break;
  921. }
  922. Application::DoEvents();
  923. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  924. long nCollectedTimeLimit = g_nImageTimeOutMilliSeconds;
  925. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  926. if (nCollectedTime > nCollectedTimeLimit)
  927. {
  928. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  929. m_endControllerEvent->Reset();
  930. break;
  931. }
  932. }
  933. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  934. {
  935. return true;
  936. }
  937. return false;
  938. }
  939. bool OxfordControllerWrapper::SetBeamOnToController(bool a_bBeamOn)
  940. {
  941. if (!IsConnected())
  942. {
  943. return false;
  944. }
  945. (void)a_bBeamOn;
  946. //// notice: false: 0.0 and true: 1.0
  947. //double dValue = a_bBeamOn? 1.0: 0.0;
  948. //Dictionary<Column, double>^ columnDictionary = gcnew Dictionary<Column, double>;
  949. //columnDictionary->Add(Column::BeamOn, dValue);
  950. //_microscopeController->SetColumnConditions(columnDictionary);
  951. return true;
  952. }
  953. bool OxfordControllerWrapper::GetBeamBlank(bool& a_bBeamBlank)
  954. {
  955. _oxfordControllerData.m_nCommand = OxfordControllerCommand::GET_BEAMBLANK;
  956. m_startControllerEvent->Set();
  957. long nCollectedTime = 0;
  958. while (true)
  959. {
  960. if (m_endControllerEvent->WaitOne(0, true))
  961. {
  962. m_endControllerEvent->Reset();
  963. break;
  964. }
  965. Application::DoEvents();
  966. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  967. long nCollectedTimeLimit = g_nImageTimeOutMilliSeconds;
  968. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  969. if (nCollectedTime > nCollectedTimeLimit)
  970. {
  971. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  972. m_endControllerEvent->Reset();
  973. break;
  974. }
  975. }
  976. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  977. {
  978. a_bBeamBlank = _oxfordControllerData.m_bBeamBlank;
  979. return true;
  980. }
  981. return false;
  982. }
  983. bool OxfordControllerWrapper::GetBeamBlankFromController(bool& a_bBeamBlank)
  984. {
  985. if (!IsConnected())
  986. {
  987. return false;
  988. }
  989. // todo: get beam blank
  990. a_bBeamBlank = true;
  991. return true;
  992. }
  993. bool OxfordControllerWrapper::SetBeamBlank(bool a_bBeamBlank)
  994. {
  995. _oxfordControllerData.m_nCommand = OxfordControllerCommand::SET_BEAMBLANK;
  996. _oxfordControllerData.m_bBeamBlank = a_bBeamBlank;
  997. m_startControllerEvent->Set();
  998. long nCollectedTime = 0;
  999. while (true)
  1000. {
  1001. if (m_endControllerEvent->WaitOne(0, true))
  1002. {
  1003. m_endControllerEvent->Reset();
  1004. break;
  1005. }
  1006. Application::DoEvents();
  1007. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  1008. long nCollectedTimeLimit = g_nImageTimeOutMilliSeconds;
  1009. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  1010. if (nCollectedTime > nCollectedTimeLimit)
  1011. {
  1012. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  1013. m_endControllerEvent->Reset();
  1014. break;
  1015. }
  1016. }
  1017. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  1018. {
  1019. return true;
  1020. }
  1021. return false;
  1022. }
  1023. bool OxfordControllerWrapper::SetBeamBlankToController(bool a_bBeamBlank)
  1024. {
  1025. if (!IsConnected())
  1026. {
  1027. return false;
  1028. }
  1029. // todo: set beam blank
  1030. (void)a_bBeamBlank;
  1031. //try
  1032. //{
  1033. // _microscopeController->ColumnConditions->BeamBlank = a_bBeamBlank;
  1034. //}
  1035. //catch(...)
  1036. //{
  1037. //}
  1038. return true;
  1039. }
  1040. bool OxfordControllerWrapper::GetExternalScan(bool& a_bExternal)
  1041. {
  1042. _oxfordControllerData.m_nCommand = OxfordControllerCommand::GET_EXTERNAL;
  1043. m_startControllerEvent->Set();
  1044. long nCollectedTime = 0;
  1045. while (true)
  1046. {
  1047. if (m_endControllerEvent->WaitOne(0, true))
  1048. {
  1049. m_endControllerEvent->Reset();
  1050. break;
  1051. }
  1052. Application::DoEvents();
  1053. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  1054. long nCollectedTimeLimit = g_nImageTimeOutMilliSeconds;
  1055. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  1056. if (nCollectedTime > nCollectedTimeLimit)
  1057. {
  1058. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  1059. m_endControllerEvent->Reset();
  1060. break;
  1061. }
  1062. }
  1063. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  1064. {
  1065. a_bExternal = _oxfordControllerData.m_bExternal;
  1066. return true;
  1067. }
  1068. return false;
  1069. }
  1070. bool OxfordControllerWrapper::GetExternalScanFromController(bool& a_bExternal)
  1071. {
  1072. if (!IsConnected())
  1073. {
  1074. return false;
  1075. }
  1076. a_bExternal = _microscopeController->IsInExternalScan;
  1077. return true;
  1078. }
  1079. bool OxfordControllerWrapper::SetExternalScan(bool a_bExternal)
  1080. {
  1081. _oxfordControllerData.m_nCommand = OxfordControllerCommand::GET_EXTERNAL;
  1082. _oxfordControllerData.m_bExternal = a_bExternal;
  1083. m_startControllerEvent->Set();
  1084. long nCollectedTime = 0;
  1085. while (true)
  1086. {
  1087. if (m_endControllerEvent->WaitOne(0, true))
  1088. {
  1089. m_endControllerEvent->Reset();
  1090. break;
  1091. }
  1092. Application::DoEvents();
  1093. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  1094. long nCollectedTimeLimit = g_nImageTimeOutMilliSeconds;
  1095. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  1096. if (nCollectedTime > nCollectedTimeLimit)
  1097. {
  1098. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  1099. m_endControllerEvent->Reset();
  1100. break;
  1101. }
  1102. }
  1103. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  1104. {
  1105. return true;
  1106. }
  1107. return false;
  1108. }
  1109. bool OxfordControllerWrapper::SetExternalScanToController(bool a_bExternal)
  1110. {
  1111. if (!IsConnected())
  1112. {
  1113. return false;
  1114. }
  1115. _microscopeController->SetExternalScan(a_bExternal);
  1116. return true;
  1117. }
  1118. /// <summary>
  1119. /// Set the beam position
  1120. /// </summary>
  1121. /// <param name="a_dPosX">The double of position x.</param>
  1122. /// <param name="a_dPosY">The double of position y.</param>
  1123. /// <note>Accept pixel position to convert to oxford normalized beam position</note>
  1124. /// <returns>TRUE if success, otherwise FALSE</returns>
  1125. bool OxfordControllerWrapper::SetBeamPosition(const double a_dPosX, const double a_dPosY)
  1126. {
  1127. auto edsSettings = GetEdsSpectrumSettings();
  1128. if (!edsSettings)
  1129. {
  1130. return false;
  1131. }
  1132. m_bBeamPositionSet = true;
  1133. _oxfordControllerData.m_dBeamPositionX = a_dPosX;
  1134. _oxfordControllerData.m_dBeamPositionY = a_dPosY;
  1135. return true;
  1136. }
  1137. bool OxfordControllerWrapper::SetCurrentBeamPosition()
  1138. {
  1139. auto edsSettings = GetEdsSpectrumSettings();
  1140. if (!edsSettings)
  1141. {
  1142. return false;
  1143. }
  1144. auto ret = edsSettings->ScanSettings->AcquisitionRegion->CreateMicroscopeRegion();
  1145. return ret != nullptr;
  1146. }
  1147. bool OxfordControllerWrapper::StartXrayCollecting(const long a_nAcTimeMilliseconds)
  1148. {
  1149. auto edsController = CreateEdsSpectrumController();
  1150. if (!edsController)
  1151. {
  1152. return false;
  1153. }
  1154. auto edsSettings = GetEdsSpectrumSettings();
  1155. edsSettings->EdSettings->AcquisitionTime = TimeSpan::FromMilliseconds(a_nAcTimeMilliseconds);
  1156. auto spectrum = edsController->StartAcquisition(edsSettings);
  1157. return spectrum != nullptr;
  1158. }
  1159. bool OxfordControllerWrapper::StopXrayCollecting()
  1160. {
  1161. auto edsController = CreateEdsSpectrumController();
  1162. if (!edsController)
  1163. {
  1164. return false;
  1165. }
  1166. if (edsController->IsAcquiring)
  1167. {
  1168. edsController->StopAcquisition();
  1169. }
  1170. return !edsController->IsAcquiring;
  1171. }
  1172. void OxfordControllerWrapper::OnXrayAcquisitionFinished(Object^ sender, OINA::Extender::Acquisition::AcquisitionFinishedEventArgs<OINA::Extender::Data::Ed::IEdSpectrum^>^ e)
  1173. {
  1174. //Quantify processing
  1175. IEdSpectrumProcessing^ EdSpectrumProcessing = ProcessingFactory::CreateSpectrumProcessing();
  1176. // Use the autoIdSettings to define elements that are known or elements that you want to exclude. They also list elements that cannot be identified
  1177. IAutoIdSettings^ autoIdSettings = ProcessingFactory::CreateAutoIdSettings();
  1178. EdSpectrumProcessing->IdentifyElements(e->Value, autoIdSettings);
  1179. ISEMQuantSettings^ settings = ProcessingFactory::CreateSEMQuantSettings();
  1180. // While it is possible to choose other elements, Oxygen is the only supported element by stoichiometry.
  1181. settings->CombinedElement = 8;
  1182. settings->Normalised = true;
  1183. ISEMQuantStatus^ quantStatus = EdSpectrumProcessing->SEMQuantifySpectrum(e->Value, settings);//(a_nChannelData, OIHelper::SEMQuantSettings);
  1184. IEnumerable<ISEMQuantResult^>^ Results = quantStatus->Results;
  1185. if (_oxfordControllerData.m_nCommand == OxfordControllerCommand::COLLECT_XRAYPOINT
  1186. || _oxfordControllerData.m_nCommand == OxfordControllerCommand::COLLECT_CURRENTXRAYPOINT)
  1187. {
  1188. //Get element result for single point
  1189. auto ie = Results->GetEnumerator();
  1190. String^ Quant = gcnew String("");
  1191. while (ie->MoveNext())
  1192. {
  1193. ISEMQuantResult^ result = ie->Current;
  1194. if (result->WeightPercent != 0)
  1195. {
  1196. Quant += "Quant=";
  1197. Quant += ElementProperties::GetElementSymbol(result->AtomicNumber);
  1198. Quant += ",";
  1199. Quant += result->LineType.ToString();
  1200. Quant += ",";
  1201. Quant += result->WeightPercent.ToString();
  1202. }
  1203. }
  1204. _oxfordControllerData.m_sElementResult = Quant;
  1205. ConvertSpectrumData(e->Value, _oxfordControllerData.m_pSpectrumData, _oxfordControllerData.m_nBufferSize);
  1206. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  1207. auto edsController = CreateEdsSpectrumController();
  1208. if (edsController)
  1209. {
  1210. edsController->EndMultipleAcquisition();
  1211. m_endControllerEvent->Set();
  1212. }
  1213. }
  1214. else
  1215. {
  1216. //Get element result for single point
  1217. auto ie = Results->GetEnumerator();
  1218. String^ Quant = gcnew String("");
  1219. while (ie->MoveNext())
  1220. {
  1221. ISEMQuantResult^ result = ie->Current;
  1222. if (result->WeightPercent != 0)
  1223. {
  1224. Quant += "Quant=";
  1225. Quant += ElementProperties::GetElementSymbol(result->AtomicNumber);
  1226. Quant += ",";
  1227. Quant += result->LineType.ToString();
  1228. Quant += ",";
  1229. Quant += result->WeightPercent.ToString();
  1230. Quant += "\n";
  1231. }
  1232. }
  1233. CString msg = Quant;
  1234. //_oxfordControllerData.m_pXrayDataList[_oxfordControllerData.m_nCollectedXrayCounts].m_strElementResult = msg.GetBuffer();
  1235. unsigned char* dst = _oxfordControllerData.m_pXrayDataList[_oxfordControllerData.m_nCollectedXrayCounts].m_strElementResult;
  1236. for (int i = 0; i < msg.GetLength(); i++)
  1237. {
  1238. dst[i] = (unsigned char) msg.GetAt(i);
  1239. }
  1240. ConvertSpectrumData(e->Value, _oxfordControllerData.m_pXrayDataList[_oxfordControllerData.m_nCollectedXrayCounts].m_pXrayData, _oxfordControllerData.m_nBufferSize);
  1241. /*CString sMessage;
  1242. sMessage.Format(_T("Collected xray %d / %d\n"), _oxfordControllerData.m_nCollectedXrayCounts, _oxfordControllerData.m_nXrayDataCount);
  1243. OutputDebugString(sMessage);*/
  1244. _oxfordControllerData.m_nCollectedXrayCounts++;
  1245. if (_oxfordControllerData.m_nCollectedXrayCounts == _oxfordControllerData.m_nXrayDataCount)
  1246. {
  1247. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  1248. auto edsController = CreateEdsSpectrumController();
  1249. if (edsController)
  1250. {
  1251. edsController->EndMultipleAcquisition();
  1252. m_endControllerEvent->Set();
  1253. }
  1254. }
  1255. }
  1256. }
  1257. void OxfordControllerWrapper::OnXrayChordlistFinished(Object^, OINA::Extender::Acquisition::AcquisitionFinishedEventArgs<OINA::Extender::Data::Ed::IEdMap^>^ e)
  1258. {
  1259. }
  1260. bool OxfordControllerWrapper::CollectXrayPoint(const double a_dPosX, const double a_dPosY, const long a_nAcTimeMilliseconds, long* a_pSpectrumData, DWORD a_nBufferSize)
  1261. {
  1262. _oxfordControllerData.m_nCommand = OxfordControllerCommand::COLLECT_XRAYPOINT;
  1263. _oxfordControllerData.m_dBeamPositionX = a_dPosX;
  1264. _oxfordControllerData.m_dBeamPositionY = a_dPosY;
  1265. _oxfordControllerData.m_nAcTime = a_nAcTimeMilliseconds;
  1266. _oxfordControllerData.m_pSpectrumData = a_pSpectrumData;
  1267. _oxfordControllerData.m_nBufferSize = a_nBufferSize;
  1268. m_startControllerEvent->Set();
  1269. Thread::Sleep(a_nAcTimeMilliseconds);
  1270. long nCollectedTime = a_nAcTimeMilliseconds;
  1271. long nCollectedTimeLimit = nCollectedTime + g_nSinglePointCollectDelay + g_nXrayControllerConnectTimeOutMilliSeconds;
  1272. while (true)
  1273. {
  1274. if (m_endControllerEvent->WaitOne(0, true))
  1275. {
  1276. m_endControllerEvent->Reset();
  1277. break;
  1278. }
  1279. Application::DoEvents();
  1280. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  1281. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  1282. if (nCollectedTime > nCollectedTimeLimit)
  1283. {
  1284. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  1285. _oxfordControllerData.m_nCommand = OxfordControllerCommand::END_MULTIPLEACQUISITION;
  1286. m_startControllerEvent->Set();
  1287. m_endControllerEvent->Reset();
  1288. break;
  1289. }
  1290. }
  1291. _oxfordControllerData.m_pSpectrumData = nullptr;
  1292. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  1293. {
  1294. return true;
  1295. }
  1296. return false;
  1297. }
  1298. bool OxfordControllerWrapper::CollectXrayPoint(const long a_nAcTimeMilliseconds, long* a_pSpectrumData, DWORD a_nBufferSize)
  1299. {
  1300. _oxfordControllerData.m_nCommand = OxfordControllerCommand::COLLECT_CURRENTXRAYPOINT;
  1301. _oxfordControllerData.m_nAcTime = a_nAcTimeMilliseconds;
  1302. _oxfordControllerData.m_pSpectrumData = a_pSpectrumData;
  1303. _oxfordControllerData.m_nBufferSize = a_nBufferSize;
  1304. m_startControllerEvent->Set();
  1305. Thread::Sleep(a_nAcTimeMilliseconds);
  1306. long nCollectedTime = a_nAcTimeMilliseconds;
  1307. long nCollectedTimeLimit = nCollectedTime + g_nSinglePointCollectDelay + g_nXrayControllerConnectTimeOutMilliSeconds;
  1308. while (true)
  1309. {
  1310. if (m_endControllerEvent->WaitOne(0, true))
  1311. {
  1312. m_endControllerEvent->Reset();
  1313. break;
  1314. }
  1315. Application::DoEvents();
  1316. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  1317. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  1318. if (nCollectedTime > nCollectedTimeLimit)
  1319. {
  1320. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  1321. _oxfordControllerData.m_nCommand = OxfordControllerCommand::END_MULTIPLEACQUISITION;
  1322. m_startControllerEvent->Set();
  1323. m_endControllerEvent->Reset();
  1324. break;
  1325. }
  1326. }
  1327. _oxfordControllerData.m_pSpectrumData = nullptr;
  1328. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  1329. {
  1330. return true;
  1331. }
  1332. return false;
  1333. }
  1334. bool OxfordControllerWrapper::CollectXrayPoints(const long a_nAcTimeMilliseconds, OxfordXrayData* a_pXrayDataList, const long a_nXrayDataCount, DWORD a_nBufferSize)
  1335. {
  1336. _oxfordControllerData.m_nCommand = OxfordControllerCommand::COLLECT_XRAYPOINTS;
  1337. _oxfordControllerData.m_nAcTime = a_nAcTimeMilliseconds;
  1338. _oxfordControllerData.m_pXrayDataList = a_pXrayDataList;
  1339. _oxfordControllerData.m_nXrayDataCount = a_nXrayDataCount;
  1340. _oxfordControllerData.m_nBufferSize = a_nBufferSize;
  1341. m_startControllerEvent->Set();
  1342. Thread::Sleep(a_nAcTimeMilliseconds * a_nXrayDataCount);
  1343. long nCollectedTime = a_nAcTimeMilliseconds * a_nXrayDataCount;
  1344. long nCollectedTimeLimit = nCollectedTime + a_nXrayDataCount * g_nSinglePointCollectDelay + g_nXrayControllerConnectTimeOutMilliSeconds;
  1345. while (true)
  1346. {
  1347. if (m_endControllerEvent->WaitOne(0, true))
  1348. {
  1349. m_endControllerEvent->Reset();
  1350. break;
  1351. }
  1352. Application::DoEvents();
  1353. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  1354. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  1355. if (nCollectedTime > nCollectedTimeLimit)
  1356. {
  1357. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  1358. _oxfordControllerData.m_nCommand = OxfordControllerCommand::END_MULTIPLEACQUISITION;
  1359. m_startControllerEvent->Set();
  1360. m_endControllerEvent->Reset();
  1361. break;
  1362. }
  1363. }
  1364. _oxfordControllerData.m_pXrayDataList = nullptr;
  1365. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  1366. {
  1367. return true;
  1368. }
  1369. return false;
  1370. }
  1371. bool OxfordControllerWrapper::CollectXrayArea(const long a_nAcTimeMilliseconds, OxfordXrayData* a_pXrayDataList, const long a_nXrayDataCount, DWORD a_nBufferSize)
  1372. {
  1373. _oxfordControllerData.m_nCommand = OxfordControllerCommand::COLLECT_XRAYCHOILDLIST;
  1374. _oxfordControllerData.m_nAcTime = a_nAcTimeMilliseconds;
  1375. _oxfordControllerData.m_pXrayDataList = a_pXrayDataList;
  1376. _oxfordControllerData.m_nXrayDataCount = a_nXrayDataCount;//要采集的feature个数
  1377. _oxfordControllerData.m_nCollectedXrayCounts = 0;
  1378. _oxfordControllerData.m_nChordsNum = a_pXrayDataList[_oxfordControllerData.m_nCollectedXrayCounts].m_nChordNum;
  1379. _oxfordControllerData.m_nCollectedChordsCount = 0;
  1380. _oxfordControllerData.m_nBufferSize = a_nBufferSize;
  1381. //当前采集的feature包含的像素的个数
  1382. _oxfordControllerData.m_nPixelNum = a_pXrayDataList[_oxfordControllerData.m_nCollectedXrayCounts].m_nPixelNum;
  1383. _oxfordControllerData.m_nCollectedPixelCounts = 0;
  1384. m_startControllerEvent->Set();
  1385. Thread::Sleep(a_nAcTimeMilliseconds * a_nXrayDataCount);
  1386. long nCollectedTime = a_nAcTimeMilliseconds * a_nXrayDataCount;
  1387. long nCollectedTimeLimit = nCollectedTime + a_nXrayDataCount * g_nSingleFeatureCollectDelay + g_nXrayControllerConnectTimeOutMilliSeconds;
  1388. while (true)
  1389. {
  1390. if (m_endControllerEvent->WaitOne(0, true))
  1391. {
  1392. m_endControllerEvent->Reset();
  1393. break;
  1394. }
  1395. Application::DoEvents();
  1396. Thread::Sleep(g_nOxfordControllerEventSleepTimerInt);
  1397. nCollectedTime += g_nOxfordControllerEventSleepTimerInt;
  1398. if (nCollectedTime > nCollectedTimeLimit)
  1399. {
  1400. /*CString sMessage;
  1401. sMessage.Format(_T("Error: Total collected time(%d) over limit(%d)"), nCollectedTime, nCollectedTimeLimit);
  1402. OutputDebugString(sMessage);
  1403. LogMessage(sMessage);
  1404. sMessage.Format(_T("1Total collected xray %d / %d"), _oxfordControllerData.m_nCollectedXrayCounts, _oxfordControllerData.m_nXrayDataCount);
  1405. OutputDebugString(sMessage);
  1406. LogMessage(sMessage);*/
  1407. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  1408. m_endControllerEvent->Reset();
  1409. break;
  1410. }
  1411. }
  1412. _oxfordControllerData.m_pXrayDataList = nullptr;
  1413. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  1414. {
  1415. return true;
  1416. }
  1417. /*CString sMessage;
  1418. sMessage.Format(_T("CollectXrayArea failed."));
  1419. OutputDebugString(sMessage);
  1420. LogMessage(sMessage);*/
  1421. return false;
  1422. }
  1423. bool OxfordControllerWrapper::ConvertSpectrumData(IEdSpectrum^ a_spectrum, long* a_pSpectrumData, int a_nBufferSize)
  1424. {
  1425. if (!a_spectrum || !a_pSpectrumData)
  1426. {
  1427. return false;
  1428. }
  1429. memset(a_pSpectrumData, 0, sizeof(long) * a_nBufferSize);
  1430. array<int>^ xrayData = gcnew array<int>(a_spectrum->NumberOfChannels);
  1431. a_spectrum->GetChannelData(xrayData);
  1432. double dZeroChannelValue = a_spectrum->ZeroChannelValue;
  1433. int nChannelStart = 0;
  1434. if (dZeroChannelValue < 0) // zero channel value should less than zero
  1435. {
  1436. nChannelStart = (int)(-dZeroChannelValue / a_spectrum->ChannelWidth + 0.5);
  1437. }
  1438. int nDataLength = (int)(a_spectrum->EnergyRange * 1000 / a_spectrum->ChannelWidth + 0.5);
  1439. double dStep1 = 1.0 / nDataLength;
  1440. double dStep2 = 1.0 / a_nBufferSize;
  1441. for (int i = 0; i < nDataLength; ++i)
  1442. {
  1443. int nValue = xrayData[i + nChannelStart] > 0 ? xrayData[i + nChannelStart] : 0;
  1444. double dBinPos = i * dStep1;
  1445. long nLeftBin = (long)(dBinPos / dStep2);
  1446. // calculate % into left bin
  1447. double dLeft_Percent = double(nLeftBin + 1) - dBinPos / dStep2; // ((nLeftBin + 1)*dStep2 - dBinPos)/dStep2
  1448. // calculate data into the left bin
  1449. long nValueToLeftBin = (long)((double)nValue * dLeft_Percent + 0.5);
  1450. // put data into bins
  1451. a_pSpectrumData[nLeftBin] += nValueToLeftBin;
  1452. if ((nLeftBin + 1) < (long)a_nBufferSize)
  1453. {
  1454. a_pSpectrumData[nLeftBin + 1] += (nValue - nValueToLeftBin);
  1455. }
  1456. }
  1457. return true;
  1458. }
  1459. bool OxfordControllerWrapper::IsXrayCollecting()
  1460. {
  1461. if (_edSpectrumController)
  1462. {
  1463. return _edSpectrumController->IsAcquiring;
  1464. }
  1465. return false;
  1466. }
  1467. bool OxfordControllerWrapper::IsImageCollecting()
  1468. {
  1469. if (_imageAcqusitionController)
  1470. {
  1471. return _imageAcqusitionController->IsAcquiring;
  1472. }
  1473. return false;
  1474. }
  1475. bool OxfordControllerWrapper::IsQuantifySpectrumCollecting()
  1476. {
  1477. if (_edSpectrumController)
  1478. {
  1479. return _edSpectrumController->IsAcquiring;
  1480. }
  1481. return false;
  1482. }
  1483. bool OxfordControllerWrapper::SetScanSpeed(const long a_nMilliseconds)
  1484. {
  1485. auto imageAcqusitionSettings = GetImageAcqusitionSettings();
  1486. imageAcqusitionSettings->ImageSettings->DwellTimeMicroSeconds = a_nMilliseconds;
  1487. return true;
  1488. }
  1489. bool OxfordControllerWrapper::GetImageSize(long& a_nWidth, long& a_nHeight)
  1490. {
  1491. a_nWidth = _oxfordControllerData.m_nImageWidth;
  1492. a_nHeight = _oxfordControllerData.m_nImageHeight;
  1493. return true;
  1494. }
  1495. bool OxfordControllerWrapper::SetImageSize(const long a_nWidth, const long a_nHeight)
  1496. {
  1497. _oxfordControllerData.m_nImageWidth = a_nWidth;
  1498. _oxfordControllerData.m_nImageHeight = a_nHeight;
  1499. auto imageAcqusitionSettings = GetImageAcqusitionSettings();
  1500. imageAcqusitionSettings->ScanSettings->AcquisitionRegion->CreateFullFieldRegion(1.0 / a_nWidth);
  1501. return true;
  1502. }
  1503. bool OxfordControllerWrapper::CollectImage(int a)
  1504. {
  1505. return true;
  1506. }
  1507. bool OxfordControllerWrapper::QuantifySpectrum(unsigned char* cResult)
  1508. {
  1509. CString result = _oxfordControllerData.m_sElementResult;
  1510. cResult = (unsigned char*)result.GetBuffer();
  1511. return true;
  1512. }
  1513. bool OxfordControllerWrapper::CollectImage(BYTE* a_pImageBits)
  1514. {
  1515. _oxfordControllerData.m_nCommand = OxfordControllerCommand::COLLECT_IMAGE;
  1516. _oxfordControllerData.m_pImageBits = a_pImageBits;
  1517. m_startControllerEvent->Set();
  1518. long nCollectedTime = 0;
  1519. long nCollectedTimeLimit = g_nImageTimeOutMilliSeconds;
  1520. while (true)
  1521. {
  1522. if (m_endControllerEvent->WaitOne(0, true))
  1523. {
  1524. m_endControllerEvent->Reset();
  1525. break;
  1526. }
  1527. Application::DoEvents();
  1528. Thread::Sleep(g_nOxfordControllerEventWaitTimerInt);
  1529. nCollectedTime += g_nOxfordControllerEventWaitTimerInt;
  1530. if (nCollectedTime > nCollectedTimeLimit)
  1531. {
  1532. //this->CreateImageAcqusitionController();
  1533. _oxfordControllerData.m_nCommand = OxfordControllerCommand::STOP_ACQUISITION;
  1534. m_startControllerEvent->Set();
  1535. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  1536. m_endControllerEvent->Reset();
  1537. break;
  1538. }
  1539. }
  1540. _oxfordControllerData.m_pImageBits = nullptr;
  1541. if (_oxfordControllerData.m_nState == OxfordControllerState::SUCCEEDED)
  1542. {
  1543. return true;
  1544. }
  1545. return false;
  1546. }
  1547. bool OxfordControllerWrapper::ReadImageData(IElectronImage^ a_electronImage, BYTE* a_pImageBits)
  1548. {
  1549. if (a_electronImage == nullptr)
  1550. {
  1551. return false;
  1552. }
  1553. _oxfordControllerData.m_nImageWidth = a_electronImage->Width;
  1554. _oxfordControllerData.m_nImageHeight = a_electronImage->Height;
  1555. _oxfordControllerData.m_dPixelSize = a_electronImage->PixelSize;
  1556. int nBytesPerPixel = a_electronImage->BytesPerPixel;
  1557. int nImageSize = _oxfordControllerData.m_nImageWidth * _oxfordControllerData.m_nImageHeight;
  1558. int nBufferSize = _oxfordControllerData.m_nImageWidth * _oxfordControllerData.m_nImageHeight * nBytesPerPixel;
  1559. array<byte>^ imageData = gcnew array<byte>(nBufferSize);
  1560. a_electronImage->GetData(imageData);
  1561. // default, oxford will return short image, we need to convert to byte
  1562. if (nBytesPerPixel == 2)
  1563. {
  1564. int nBSEValue = 0;
  1565. for (int i = 0; i < nImageSize; ++i)
  1566. {
  1567. nBSEValue = imageData[0 + i * nBytesPerPixel] + imageData[1 + i * nBytesPerPixel] * 255;
  1568. a_pImageBits[i] = (BYTE)(nBSEValue / 128.0 + 0.5);
  1569. }
  1570. }
  1571. else
  1572. {
  1573. int nOffset = nBytesPerPixel - 1;
  1574. for (int i = 0; i < nImageSize; ++i)
  1575. {
  1576. #ifdef _DEBUG
  1577. byte nValue = imageData[nBytesPerPixel - 1 + i * nBytesPerPixel];
  1578. a_pImageBits[i] = nValue;
  1579. #else
  1580. a_pImageBits[i] = imageData[nOffset + i * nBytesPerPixel];
  1581. #endif
  1582. }
  1583. }
  1584. return true;
  1585. }
  1586. bool OxfordControllerWrapper::StartImageCollecting()
  1587. {
  1588. auto imageAcqusitionController = CreateImageAcqusitionController();
  1589. auto imageAcqusitionSettings = GetImageAcqusitionSettings();
  1590. if (imageAcqusitionController == nullptr)
  1591. {
  1592. return false;
  1593. }
  1594. if (imageAcqusitionSettings == nullptr)
  1595. {
  1596. return false;
  1597. }
  1598. try
  1599. {
  1600. auto image = imageAcqusitionController->StartAcquisition(imageAcqusitionSettings);
  1601. return image != nullptr;
  1602. }
  1603. catch (Exception^ /*ex*/)
  1604. {
  1605. CString sMessage(_T("StartImageCollecting: Start Acquisition failed"));
  1606. OutputDebugString(sMessage);
  1607. LogMessage(sMessage);
  1608. }
  1609. return false;
  1610. }
  1611. void OxfordControllerWrapper::OnImageAcquisitionFinished(Object^ /*sender*/, OINA::Extender::Acquisition::AcquisitionFinishedEventArgs<array<OINA::Extender::Data::Image::IElectronImage^ >^ >^ e)
  1612. {
  1613. List<IElectronImage^>^ electronImageList = gcnew List<IElectronImage^>();
  1614. for each (auto eleImage in e->Value)
  1615. {
  1616. electronImageList->Add(eleImage);
  1617. }
  1618. if (electronImageList->Count == 0)
  1619. {
  1620. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  1621. }
  1622. else if (!ReadImageData(electronImageList[0], _oxfordControllerData.m_pImageBits))
  1623. {
  1624. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  1625. }
  1626. else
  1627. {
  1628. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  1629. }
  1630. m_endControllerEvent->Set();
  1631. }
  1632. void OxfordControllerWrapper::OnController_PixelProcessed(Object^ /*sender*/, OINA::Extender::EventArgs<OINA::Extender::Processing::Quant::IPixelSEMQuantStatus^ >^ e)
  1633. {
  1634. _oxfordControllerData.m_nCollectedPixelCounts++;
  1635. //更新当前feature的元素成分
  1636. //Get element result for single point
  1637. auto ie = e->Value->QuantStatus->Results->GetEnumerator();
  1638. while (ie->MoveNext())
  1639. {
  1640. ISEMQuantResult^ result = ie->Current;
  1641. if (result->WeightPercent != 0)
  1642. {
  1643. Element^ Quant = gcnew Element();
  1644. Quant->m_nAotomaticNo = result->AtomicNumber;
  1645. Quant->m_dWeight = result->WeightPercent;
  1646. if (_oxfordControllerData.m_listElementResult == nullptr)
  1647. {
  1648. _oxfordControllerData.m_listElementResult = gcnew List<Element^ >();
  1649. _oxfordControllerData.m_listElementResult->Add(Quant);
  1650. }
  1651. else
  1652. {
  1653. bool bFind = false;
  1654. for (int i = 0; i < _oxfordControllerData.m_listElementResult->Count; i++)
  1655. {
  1656. Element^ result = _oxfordControllerData.m_listElementResult[i];
  1657. if (Quant->m_nAotomaticNo == result->m_nAotomaticNo)
  1658. {
  1659. result->m_dWeight += Quant->m_dWeight;
  1660. bFind = true;
  1661. }
  1662. }
  1663. if (!bFind)
  1664. {
  1665. _oxfordControllerData.m_listElementResult->Add(Quant);
  1666. }
  1667. }
  1668. }
  1669. }
  1670. //当前的chord数据中pixel没有采集完整
  1671. if (_oxfordControllerData.m_nCollectedPixelCounts == _oxfordControllerData.m_nPixelNum)
  1672. {
  1673. //形成quant数据
  1674. CString Quant = _T("");
  1675. for (int i = 0; i < _oxfordControllerData.m_listElementResult->Count; i++)
  1676. {
  1677. Element^ result = _oxfordControllerData.m_listElementResult[i];
  1678. Quant += "Quant=";
  1679. CString strAoto(ElementProperties::GetElementSymbol(result->m_nAotomaticNo));
  1680. Quant += strAoto;
  1681. Quant += ",";
  1682. Quant += _T("K-serials");
  1683. Quant += ",";
  1684. CString strWeight = (result->m_dWeight / (double)_oxfordControllerData.m_nPixelNum).ToString();
  1685. Quant += strWeight ;
  1686. Quant += "\n";
  1687. }
  1688. CString msg = Quant;
  1689. unsigned char* dst = _oxfordControllerData.m_pXrayDataList[_oxfordControllerData.m_nCollectedXrayCounts].m_strElementResult;
  1690. for (int i = 0; i < msg.GetLength(); i++)
  1691. {
  1692. dst[i] = (unsigned char)msg.GetAt(i);
  1693. }
  1694. _oxfordControllerData.m_nCollectedXrayCounts++;
  1695. if (_oxfordControllerData.m_nCollectedXrayCounts < _oxfordControllerData.m_nXrayDataCount)
  1696. {
  1697. _oxfordControllerData.m_nPixelNum = _oxfordControllerData.m_pXrayDataList[_oxfordControllerData.m_nCollectedXrayCounts].m_nPixelNum;
  1698. _oxfordControllerData.m_nCollectedPixelCounts = 0;
  1699. _oxfordControllerData.m_listElementResult->Clear();
  1700. }
  1701. }
  1702. }
  1703. void OxfordControllerWrapper::OnController_ExperimentFinished(Object^ /*sender*/, OINA::Extender::Acquisition::AcquisitionFinishedEventArgs^ e)
  1704. {
  1705. if (_oxfordControllerData.m_nCollectedXrayCounts == _oxfordControllerData.m_nXrayDataCount)
  1706. _oxfordControllerData.m_nState = OxfordControllerState::SUCCEEDED;
  1707. else
  1708. _oxfordControllerData.m_nState = OxfordControllerState::FAILED;
  1709. m_endControllerEvent->Set();
  1710. }
  1711. bool OxfordControllerWrapper::StopImageCollecting()
  1712. {
  1713. auto imageAcqusitionController = CreateImageAcqusitionController();;
  1714. try
  1715. {
  1716. if (imageAcqusitionController->IsAcquiring)
  1717. {
  1718. imageAcqusitionController->StopAcquisition();
  1719. }
  1720. }
  1721. catch (Exception^ ex)
  1722. {
  1723. CString sMessage = ex->Message;
  1724. CString sErrorMessage;
  1725. // sErrorMessage.Format(_T("StopImageCollecting: Stop Acquisition caught(%s)"), sMessage);
  1726. OutputDebugString(sErrorMessage);
  1727. LogMessage(sMessage);
  1728. }
  1729. return !imageAcqusitionController->IsAcquiring;
  1730. }
  1731. IMicroscopeController^ OxfordControllerWrapper::CreateMicroscopeController()
  1732. {
  1733. if (_microscopeController != nullptr)
  1734. {
  1735. return _microscopeController;
  1736. }
  1737. try
  1738. {
  1739. _microscopeController = AcquireFactory::CreateMicroscopeControl();
  1740. }
  1741. catch(Exception^ ex)
  1742. {
  1743. CString sErrorMessage = ex->Message;
  1744. AfxMessageBox(_T("当前牛津能谱不能正常工作,若需调整为OffLine模式,请在SysMgeApp中修改!\n错误原因:")+ sErrorMessage);
  1745. OutputDebugString(sErrorMessage);
  1746. LogMessage(sErrorMessage);
  1747. exit(0);
  1748. }
  1749. if (_microscopeController == nullptr)
  1750. {
  1751. return nullptr;
  1752. }
  1753. _microscopeController->ColumnChange += gcnew EventHandler<OINA::Extender::MicroscopeControl::ColumnEventArgs^>(this, &OxfordControllerWrapper::OnMicroscopeColumnUpdated);
  1754. _microscopeController->StageChange += gcnew EventHandler<OINA::Extender::MicroscopeControl::StageEventArgs^>(this, &OxfordControllerWrapper::OnMicroscopeStageUpdated);
  1755. m_bIsStageUpdated = false;
  1756. auto nStart = GetTickCount64();
  1757. auto nEnd = nStart;
  1758. do
  1759. {
  1760. if (IsMicroscopeColumnConnected()
  1761. && IsMicroscopeStageConnected()
  1762. && m_bIsStageUpdated)
  1763. {
  1764. break;
  1765. }
  1766. nEnd = GetTickCount64();
  1767. } while (nEnd >= nStart && nEnd <= (nStart + g_nMicrocopeConnectTimeOutMilliSeconds));
  1768. return _microscopeController;
  1769. }
  1770. IEdSpectrumAcquisitionController^ OxfordControllerWrapper::CreateEdsSpectrumController()
  1771. {
  1772. if (_edSpectrumController == nullptr)
  1773. {
  1774. _edSpectrumController = AcquireFactory::CreateEdSpectrumServer();
  1775. _edSpectrumController->ExperimentFinished += gcnew EventHandler<OINA::Extender::Acquisition::AcquisitionFinishedEventArgs<OINA::Extender::Data::Ed::IEdSpectrum^>^>(this, &OxfordControllerWrapper::OnXrayAcquisitionFinished);
  1776. auto edsSpectrumSettings = GetEdsSpectrumSettings();
  1777. while (true)
  1778. {
  1779. if (_edSpectrumController->IsEdHardwareReady(edsSpectrumSettings))
  1780. {
  1781. if (edsSpectrumSettings->EdCapabilities->HasHardwareConnection)
  1782. {
  1783. break;
  1784. }
  1785. }
  1786. }
  1787. }
  1788. return _edSpectrumController;
  1789. }
  1790. IEdSpectrumSettings^ OxfordControllerWrapper::GetEdsSpectrumSettings()
  1791. {
  1792. if (_edsSpectrumSettings == nullptr)
  1793. {
  1794. _edsSpectrumSettings = AcquireFactory::CreateEdSpectrumSettings();
  1795. if (_edsSpectrumSettings == nullptr)
  1796. {
  1797. CString sMessage(_T("Create EDS Spectrum setting failed."));
  1798. OutputDebugString(sMessage);
  1799. LogMessage(sMessage);
  1800. ASSERT(FALSE);
  1801. }
  1802. else
  1803. {
  1804. _edsSpectrumSettings->EdSettings->AcquisitionMode = EdAcquireMode::LiveTime;
  1805. //_edsSpectrumSettings->EdSettings->AcquisitionTime = TimeSpan::FromSeconds(1);
  1806. _edsSpectrumSettings->EdSettings->NumberOfChannels = g_nOxfordControllerChannels;
  1807. _edsSpectrumSettings->EdSettings->ProcessTime = g_nOxfordControllerProcessTime;
  1808. _edsSpectrumSettings->EdSettings->EnergyRange = g_nOxfordControllerEnergyRange;
  1809. }
  1810. }
  1811. return _edsSpectrumSettings;
  1812. }
  1813. IImageAcquisitionController^ OxfordControllerWrapper::CreateImageAcqusitionController()
  1814. {
  1815. if (_imageAcqusitionController == nullptr)
  1816. {
  1817. try
  1818. {
  1819. _imageAcqusitionController = AcquireFactory::CreateImageServer();
  1820. //_imageAcqusitionController->ExperimentStarted += gcnew EventHandler<OINA::Extender::Acquisition::AcquisitionFinishedEventArgs<array<OINA::Extender::Data::Image::IElectronImage^ >^ >^>(this, &OxfordControllerWrapper::OnImageAcquisitioStarted);
  1821. _imageAcqusitionController->ExperimentFinished += gcnew EventHandler<OINA::Extender::Acquisition::AcquisitionFinishedEventArgs<array<OINA::Extender::Data::Image::IElectronImage^ >^ >^>(this, &OxfordControllerWrapper::OnImageAcquisitionFinished);
  1822. auto imageAcqusitionSettings = GetImageAcqusitionSettings();
  1823. while (true)
  1824. {
  1825. if (_imageAcqusitionController->IsImageHardwareReady(imageAcqusitionSettings))
  1826. {
  1827. if (imageAcqusitionSettings->ImageCapabilities->HasHardwareConnection)
  1828. {
  1829. break;
  1830. }
  1831. }
  1832. }
  1833. }
  1834. catch (Exception^ ex)
  1835. {
  1836. CString sMessage = ex->Message;
  1837. CString sErrorMessage;
  1838. // sErrorMessage.Format(_T("CreateImageAcqusitionController: create controller caught(%s)"), sMessage);
  1839. OutputDebugString(sErrorMessage);
  1840. LogMessage(sErrorMessage);
  1841. }
  1842. }
  1843. return _imageAcqusitionController;
  1844. }
  1845. IImageAcquisitionSettings^ OxfordControllerWrapper::GetImageAcqusitionSettings()
  1846. {
  1847. if (_imageAcqusitionSettings == nullptr)
  1848. {
  1849. _imageAcqusitionSettings = AcquireFactory::CreateImageSettings();
  1850. _imageAcqusitionSettings->ScanSettings->FrameCount = 1;
  1851. IImageSettings^ imageSettings = _imageAcqusitionSettings->ImageSettings;
  1852. //imageSettings->InputSources->ToList()->ForEach(i => imageSettings->EnableInputSource(i.Key, false));
  1853. //auto imageList = Enumerable::ToList(imageSettings->InputSources);
  1854. for each (KeyValuePair<OINA::Extender::Data::Image::ImageInputSources, bool>^ imputSource in imageSettings->InputSources)
  1855. {
  1856. imageSettings->EnableInputSource(imputSource->Key, false);
  1857. }
  1858. CString szXMLFileName = "./Config/ProData/HardwareConfig.xml";
  1859. tinyxml2::XMLDocument doc;
  1860. doc.LoadFile(szXMLFileName);//载入xml文件
  1861. xmls::Slo subClass;
  1862. xmls::xString sImageInputSources;
  1863. subClass.Register("ImageInputSources", &sImageInputSources);
  1864. subClass.Register("SemControllerName", &subClass);
  1865. tinyxml2::XMLElement* rootNode;
  1866. rootNode = doc.FirstChildElement(RootClassName);
  1867. subClass.Serialize(false, &doc, rootNode);
  1868. CString cImageInputSources = sImageInputSources.value().c_str();
  1869. if (cImageInputSources == "BSE")
  1870. {
  1871. imageSettings->EnableInputSource(OINA::Extender::Data::Image::ImageInputSources::Bse, true);
  1872. }
  1873. else if (cImageInputSources == "SE")
  1874. {
  1875. imageSettings->EnableInputSource(OINA::Extender::Data::Image::ImageInputSources::SE, true);
  1876. }
  1877. else
  1878. {
  1879. imageSettings->EnableInputSource(OINA::Extender::Data::Image::ImageInputSources::None, true);
  1880. }
  1881. //Action<>
  1882. //Enumerable::ToList(imageSettings->InputSources)->ForEach(imageSettings->EnableInputSource(i.Key, false));
  1883. _imageAcqusitionSettings->ImageSettings->DwellTimeMicroSeconds = 20;
  1884. _imageAcqusitionSettings->ScanSettings->AcquisitionRegion->CreateFullFieldRegion(1.0 / 1024.0);
  1885. }
  1886. return _imageAcqusitionSettings;
  1887. }
  1888. IEdChordListAcquisitionController^ OxfordControllerWrapper::CreateChordlistController()
  1889. {
  1890. if (_edsChordListController == nullptr)
  1891. {
  1892. _edsChordListController = AcquireFactory::CreateEdChordListServer();
  1893. _edsChordListController->PixelProcessed += gcnew EventHandler<OINA::Extender::EventArgs<OINA::Extender::Processing::Quant::IPixelSEMQuantStatus^>^>(this, &OxfordControllerWrapper::OnController_PixelProcessed);
  1894. _edsChordListController->ExperimentFinished += gcnew EventHandler<OINA::Extender::Acquisition::AcquisitionFinishedEventArgs^>(this, &OxfordControllerWrapper::OnController_ExperimentFinished);
  1895. auto edsChordListSettings = GetChordlistSettings();
  1896. while (true)
  1897. {
  1898. if (_edsChordListController->IsEdHardwareReady(edsChordListSettings))
  1899. {
  1900. if (edsChordListSettings->EdCapabilities->HasHardwareConnection)
  1901. {
  1902. break;
  1903. }
  1904. }
  1905. }
  1906. }
  1907. return _edsChordListController;
  1908. }
  1909. /// <summary>
  1910. /// Get some chordlists to acquire.
  1911. /// </summary>
  1912. /// <returns>The chordlists to acquire.</returns>
  1913. System::Collections::Generic::IReadOnlyList<ChordList^>^ GetChordLists()
  1914. {
  1915. OINA::Extender::Data::Chord^ chord1 = gcnew OINA::Extender::Data::Chord(3, 0, 5);
  1916. OINA::Extender::Data::Chord^ chord2 = gcnew OINA::Extender::Data::Chord(0, 0, 3);
  1917. OINA::Extender::Data::Chord^ chord3 = gcnew OINA::Extender::Data::Chord(10, 0, 20);
  1918. OINA::Extender::Data::Chord^ chord4 = gcnew OINA::Extender::Data::Chord(550, 44, 30);
  1919. OINA::Extender::Data::Chord^ chord5 = gcnew OINA::Extender::Data::Chord(0, 270, 10);
  1920. OINA::Extender::Data::Chord^ chord6 = gcnew OINA::Extender::Data::Chord(898, 634, 15);
  1921. OINA::Extender::Data::Chord^ chord7 = gcnew OINA::Extender::Data::Chord(266, 768, 40);
  1922. List< OINA::Extender::Data::Chord^>^ chords = gcnew List<OINA::Extender::Data::Chord^>();
  1923. chords->Add(chord1);
  1924. chords->Add(chord2);
  1925. chords->Add(chord3);
  1926. chords->Add(chord4);
  1927. chords->Add(chord5);
  1928. chords->Add(chord6);
  1929. chords->Add(chord7);
  1930. OINA::Extender::Data::ChordList^ chordsList = gcnew OINA::Extender::Data::ChordList(chords, 1/1024.0);
  1931. List<ChordList^>^ ListchordsList = gcnew List<ChordList^>();
  1932. ListchordsList->Add(chordsList);
  1933. return ListchordsList;
  1934. }
  1935. IEdChordListSettings^ OxfordControllerWrapper::GetChordlistSettings()
  1936. {
  1937. if (_edsChordListSetting == nullptr)
  1938. {
  1939. _edsChordListSetting = AcquireFactory::CreateEdChordListSettings();
  1940. if (_edsChordListSetting == nullptr)
  1941. {
  1942. CString sMessage(_T("Create EDS chordlist setting failed."));
  1943. /*OutputDebugString(sMessage);
  1944. LogMessage(sMessage);*/
  1945. ASSERT(FALSE);
  1946. }
  1947. else
  1948. {
  1949. _edsChordListSetting->EdSettings->AcquisitionMode = EdAcquireMode::LiveTime;
  1950. //_edsChordListSetting->EdSettings->AcquisitionTime = TimeSpan::FromMilliseconds(100);// :FromSeconds(1);
  1951. _edsChordListSetting->EdSettings->NumberOfChannels =4096;
  1952. _edsChordListSetting->EdSettings->ProcessTime = g_nOxfordControllerProcessTime;
  1953. _edsChordListSetting->EdSettings->EnergyRange = g_nOxfordControllerEnergyRange;
  1954. /*_edsChordListSetting->AutoIdSettings->SetKnownElement(79, true);
  1955. _edsChordListSetting->NumberOfProcessingThreads = 1;*/
  1956. //_edsChordListSetting->ChordLists = GetChordLists();
  1957. }
  1958. }
  1959. return _edsChordListSetting;
  1960. }
  1961. void OxfordControllerWrapper::LogMessage(CString a_sMessage)
  1962. {
  1963. OutputDebugString(a_sMessage);
  1964. /*String^ sPath = Environment::GetFolderPath(Environment::SpecialFolder::CommonApplicationData);
  1965. String^ sLogFile = sPath + "\\OTSOxford.log";
  1966. System::IO::StreamWriter^ writer = System::IO::File::AppendText(sLogFile);
  1967. String^ sMessage = gcnew String(a_sMessage);
  1968. writer->WriteLine(sMessage);
  1969. writer->Flush();*/
  1970. }