OxfordControllerWrapper.cpp 53 KB

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