OxfordControllerWrapper.cpp 54 KB

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