OxfordControllerWrapper.cpp 58 KB

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