OxfordControllerWrapper.cpp 59 KB

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