OxfordControllerWrapper.cpp 54 KB

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