OxfordControllerWrapper.cpp 59 KB

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