OxfordControllerWrapper.cpp 59 KB

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