OxfordControllerWrapper.cpp 59 KB

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