OxfordControllerWrapper.cpp 55 KB

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