OxfordControllerWrapper.cpp 54 KB

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