OxfordControllerWrapper.cpp 59 KB

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