main.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. #include <iostream>
  2. #include <memory>
  3. #include <windows.h>
  4. #include <tuple>
  5. #include "picofunctions.h"
  6. #include "pugiconfig.hpp"
  7. #include "pugixml.hpp"
  8. <<<<<<< HEAD
  9. #include "simplecout.hpp"
  10. =======
  11. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  12. #include "parser.hpp"
  13. #include "src/ActiveSocket.h"
  14. #define MAX_PACKET 4096
  15. using namespace std;
  16. std::vector<std::string> split(const std::string& s, const std::string& delimiter) {
  17. std::vector<std::string> tokens;
  18. size_t pos = 0;
  19. <<<<<<< HEAD
  20. std::string token;
  21. while ((pos = s.find(delimiter)) != std::string::npos) {
  22. token = s.substr(0, pos);
  23. tokens.push_back(token);
  24. s.erase(0, pos + delimiter.length());
  25. }
  26. tokens.push_back(s);
  27. =======
  28. std::string temp = s;
  29. std::string token;
  30. while ((pos = temp.find(delimiter)) != std::string::npos) {
  31. token = temp.substr(0, pos);
  32. tokens.push_back(token);
  33. temp.erase(0, pos + delimiter.length());
  34. }
  35. tokens.push_back(temp);
  36. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  37. return tokens;
  38. }
  39. class PicoLocalClient
  40. {
  41. private:
  42. CActiveSocket *socket;
  43. uint8_t sndbuf[MAX_PACKET]{0};
  44. uint8_t rcvbuf[MAX_PACKET]{0};
  45. std::string line_edit;
  46. std::vector<std::string> tokens;
  47. uint8_t error_state = 0x00;
  48. uint32_t error_code = 0x00000000;
  49. public:
  50. PicoLocalClient(CActiveSocket *socket) : socket(socket) {}
  51. ~PicoLocalClient() { delete socket; }
  52. int pico_open()
  53. {
  54. sndbuf[0] = 0xAA;
  55. sndbuf[1] = 0x01;
  56. if(socket->Send(sndbuf, 2) == 0)
  57. {
  58. cerr << "Failed to send data to socket" << endl;
  59. return -1;
  60. }
  61. <<<<<<< HEAD
  62. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  63. =======
  64. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  65. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  66. {
  67. cerr << "Failed to receive data from socket" << endl;
  68. return -1;
  69. }
  70. uint32_t bytes_received = socket->GetBytesReceived();
  71. if(bytes_received == 0)
  72. <<<<<<< HEAD
  73. {
  74. =======
  75. {
  76. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  77. cerr << "Server disconnected" << endl;
  78. return -1;
  79. }
  80. if(bytes_received < 2)
  81. {
  82. cerr << "Invalid buffer size" << endl;
  83. return -1;
  84. }
  85. if(rcvbuf[0] != 0xAA)
  86. {
  87. cerr << "Invalid magic number" << endl;
  88. return -1;
  89. }
  90. if(rcvbuf[1] == 0xFF)
  91. {
  92. if(bytes_received < 7)
  93. {
  94. cerr << "Invalid buffer size" << endl;
  95. return -1;
  96. }
  97. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  98. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  99. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  100. <<<<<<< HEAD
  101. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  102. =======
  103. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  104. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  105. }
  106. else if(rcvbuf[1] != 0xC1)
  107. {
  108. cerr << "Invalid command callback" << endl;
  109. return -1;
  110. }
  111. cout << "Pico device opened" << endl;
  112. return 0;
  113. }
  114. int pico_xml_config(const std::string& file_name)
  115. {
  116. sndbuf[0] = 0xAA;
  117. sndbuf[1] = 0x02;
  118. uint32_t size = file_name.size();
  119. memcpy(sndbuf + 2, &size, sizeof(uint32_t));
  120. memcpy(sndbuf + 6, file_name.c_str(), size);
  121. if(socket->Send(sndbuf, size + 6) == -1)
  122. {
  123. cerr << "Failed to send data to socket" << endl;
  124. return -1;
  125. }
  126. <<<<<<< HEAD
  127. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  128. =======
  129. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  130. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  131. {
  132. cerr << "Failed to receive data from socket" << endl;
  133. return -1;
  134. }
  135. uint32_t bytes_received = socket->GetBytesReceived();
  136. if(bytes_received == 0)
  137. <<<<<<< HEAD
  138. {
  139. =======
  140. {
  141. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  142. cerr << "Server disconnected" << endl;
  143. return -1;
  144. }
  145. if(bytes_received < 2)
  146. {
  147. cerr << "Invalid buffer size" << endl;
  148. return -1;
  149. }
  150. if(rcvbuf[0] != 0xAA)
  151. {
  152. cerr << "Invalid magic number" << endl;
  153. return -1;
  154. }
  155. if(rcvbuf[1] == 0xFF)
  156. {
  157. if(bytes_received < 7)
  158. {
  159. cerr << "Invalid buffer size" << endl;
  160. return -1;
  161. }
  162. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  163. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  164. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  165. <<<<<<< HEAD
  166. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  167. =======
  168. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  169. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  170. }
  171. else if(rcvbuf[1] != 0xC2)
  172. {
  173. cerr << "Invalid command callback" << endl;
  174. return -1;
  175. }
  176. cout << "Pico device configured" << endl;
  177. <<<<<<< HEAD
  178. return 0;
  179. =======
  180. return 0;
  181. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  182. }
  183. int pico_set_params(uint32_t* apoints, uint32_t* times, uint32_t sample_rate, uint32_t number_channels, uint32_t size)
  184. {
  185. sndbuf[0] = 0xAA;
  186. sndbuf[1] = 0x0C;
  187. memcpy(sndbuf + 2, &size, sizeof(uint32_t));
  188. memcpy(sndbuf + 6, apoints, size * sizeof(uint32_t));
  189. memcpy(sndbuf + 6 + size * sizeof(uint32_t), times, size * sizeof(uint32_t));
  190. memcpy(sndbuf + 6 + 2 * size * sizeof(uint32_t), &sample_rate, sizeof(uint32_t));
  191. memcpy(sndbuf + 10 + 2 * size * sizeof(uint32_t), &number_channels, sizeof(uint32_t));
  192. if(socket->Send(sndbuf, size * 2 * sizeof(uint32_t) + 14) == -1)
  193. {
  194. cerr << "Failed to send data to socket" << endl;
  195. return -1;
  196. }
  197. <<<<<<< HEAD
  198. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  199. =======
  200. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  201. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  202. {
  203. cerr << "Failed to receive data from socket" << endl;
  204. return -1;
  205. }
  206. <<<<<<< HEAD
  207. uint32_t bytes_received = socket->GetBytesReceived();
  208. if(bytes_received == 0)
  209. {
  210. =======
  211. uint32_t bytes_received = socket->GetBytesReceived();
  212. if(bytes_received == 0)
  213. {
  214. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  215. cerr << "Server disconnected" << endl;
  216. return -1;
  217. }
  218. if(bytes_received < 2)
  219. {
  220. cerr << "Invalid buffer size" << endl;
  221. return -1;
  222. }
  223. if(rcvbuf[0] != 0xAA)
  224. {
  225. cerr << "Invalid magic number" << endl;
  226. return -1;
  227. }
  228. if(rcvbuf[1] == 0xFF)
  229. {
  230. if(bytes_received < 7)
  231. {
  232. cerr << "Invalid buffer size" << endl;
  233. return -1;
  234. }
  235. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  236. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  237. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  238. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  239. }
  240. else if(rcvbuf[1] != 0xCC)
  241. {
  242. cerr << "Invalid command callback" << endl;
  243. return -1;
  244. }
  245. cout << "Pico device parameters set" << endl;
  246. return 0;
  247. }
  248. int pico_close()
  249. {
  250. sndbuf[0] = 0xAA;
  251. sndbuf[1] = 0x03;
  252. if(socket->Send(sndbuf, 2) == -1)
  253. {
  254. cerr << "Failed to send data to socket" << endl;
  255. return -1;
  256. }
  257. <<<<<<< HEAD
  258. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  259. =======
  260. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  261. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  262. {
  263. cerr << "Failed to receive data from socket" << endl;
  264. return -1;
  265. }
  266. uint32_t bytes_received = socket->GetBytesReceived();
  267. if(bytes_received == 0)
  268. <<<<<<< HEAD
  269. {
  270. cerr << "Server disconnected" << endl;
  271. return -1;
  272. }
  273. if(bytes_received < 2)
  274. {
  275. cerr << "Invalid buffer size" << endl;
  276. return -1;
  277. }
  278. if(rcvbuf[0] != 0xAA)
  279. {
  280. cerr << "Invalid magic number" << endl;
  281. return -1;
  282. }
  283. if(rcvbuf[1] == 0xFF)
  284. {
  285. if(bytes_received < 7)
  286. {
  287. cerr << "Invalid buffer size" << endl;
  288. return -1;
  289. }
  290. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  291. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  292. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  293. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  294. }
  295. else if(rcvbuf[1] != 0xC3)
  296. {
  297. cerr << "Invalid command callback" << endl;
  298. return -1;
  299. }
  300. cout << "Pico device closed" << endl;
  301. return 0;
  302. }
  303. int pico_get_current_params()
  304. {
  305. sndbuf[0] = 0xAA;
  306. sndbuf[1] = 0x04;
  307. if(socket->Send(sndbuf, 2) == -1)
  308. {
  309. cerr << "Failed to send data to socket" << endl;
  310. return -1;
  311. }
  312. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  313. {
  314. cerr << "Failed to receive data from socket" << endl;
  315. return -1;
  316. }
  317. uint32_t bytes_received = socket->GetBytesReceived();
  318. if(bytes_received == 0)
  319. {
  320. cerr << "Server disconnected" << endl;
  321. return -1;
  322. }
  323. if(bytes_received < 2)
  324. {
  325. cerr << "Invalid buffer size" << endl;
  326. return -1;
  327. }
  328. if(rcvbuf[0] != 0xAA)
  329. {
  330. cerr << "Invalid magic number" << endl;
  331. return -1;
  332. }
  333. if(rcvbuf[1] == 0xFF)
  334. {
  335. if(bytes_received < 7)
  336. {
  337. cerr << "Invalid buffer size" << endl;
  338. return -1;
  339. }
  340. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  341. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  342. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  343. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  344. }
  345. else if(rcvbuf[1] != 0xC4)
  346. {
  347. cerr << "Invalid command callback" << endl;
  348. return -1;
  349. }
  350. if(bytes_received < 18)
  351. {
  352. cerr << "Invalid buffer size" << endl;
  353. return -1;
  354. }
  355. uint32_t size;
  356. memcpy(&size, rcvbuf + 2, sizeof(uint32_t));
  357. if(size < 1)
  358. {
  359. cerr << "Invalid data size" << endl;
  360. return -1;
  361. }
  362. uint32_t* apoints = new uint32_t[size];
  363. memcpy(apoints, rcvbuf + 6, size * sizeof(uint32_t));
  364. for(uint32_t i = 0; i < size; ++i)
  365. {
  366. cout << "apoints[" << i << "] = " << std::dec << apoints[i] << endl;
  367. }
  368. uint32_t* atimes = new uint32_t[size];
  369. memcpy(atimes, rcvbuf + 6 + size * sizeof(uint32_t), size * sizeof(uint32_t));
  370. for(uint32_t i = 0; i < size; ++i)
  371. {
  372. cout << "atimes[" << i << "] = " << std::dec << atimes[i] << endl;
  373. }
  374. uint32_t sample_rate = 0;
  375. memcpy(&sample_rate, rcvbuf + 6 + 2 * size * sizeof(uint32_t), sizeof(uint32_t));
  376. cout << "sample_rate = " << std::dec << sample_rate << endl;
  377. uint32_t number_channels = 0;
  378. memcpy(&number_channels, rcvbuf + 10 + 2 * size * sizeof(uint32_t), sizeof(uint32_t));
  379. cout << "number_channels = " << std::dec << number_channels << endl;
  380. cout << "Pico device parameters retrieved" << endl;
  381. return 0;
  382. }
  383. int pico_probe()
  384. {
  385. sndbuf[0] = 0xAA;
  386. sndbuf[1] = 0x05;
  387. if(socket->Send(sndbuf, 2) == -1)
  388. {
  389. cerr << "Failed to send data to socket" << endl;
  390. return -1;
  391. }
  392. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  393. {
  394. cerr << "Failed to receive data from socket" << endl;
  395. return -1;
  396. }
  397. uint32_t bytes_received = socket->GetBytesReceived();
  398. if(bytes_received == 0)
  399. {
  400. cerr << "Server disconnected" << endl;
  401. return -1;
  402. }
  403. if(bytes_received < 2)
  404. {
  405. cerr << "Invalid buffer size" << endl;
  406. return -1;
  407. }
  408. if(rcvbuf[0] != 0xAA)
  409. {
  410. cerr << "Invalid magic number" << endl;
  411. return -1;
  412. }
  413. if(rcvbuf[1] == 0xFF)
  414. {
  415. if(bytes_received < 7)
  416. {
  417. cerr << "Invalid buffer size" << endl;
  418. return -1;
  419. }
  420. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  421. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  422. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  423. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  424. }
  425. else if(rcvbuf[1] != 0xC5)
  426. {
  427. cerr << "Invalid command callback" << endl;
  428. return -1;
  429. }
  430. cout << "Pico device probe completed" << endl;
  431. return 0;
  432. }
  433. int pico_set_points(uint32_t* points, uint32_t size)
  434. {
  435. sndbuf[0] = 0xAA;
  436. sndbuf[1] = 0x06;
  437. memcpy(sndbuf + 2, &size, sizeof(uint32_t));
  438. memcpy(sndbuf + 6, points, size * sizeof(uint32_t));
  439. if(socket->Send(sndbuf, size * sizeof(uint32_t) + 6) == -1)
  440. {
  441. cerr << "Failed to send data to socket" << endl;
  442. return -1;
  443. }
  444. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  445. {
  446. cerr << "Failed to receive data from socket" << endl;
  447. return -1;
  448. }
  449. uint32_t bytes_received = socket->GetBytesReceived();
  450. if(bytes_received == 0)
  451. {
  452. cerr << "Server disconnected" << endl;
  453. return -1;
  454. }
  455. if(bytes_received < 2)
  456. {
  457. cerr << "Invalid buffer size" << endl;
  458. return -1;
  459. }
  460. if(rcvbuf[0] != 0xAA)
  461. {
  462. cerr << "Invalid magic number" << endl;
  463. return -1;
  464. }
  465. if(rcvbuf[1] == 0xFF)
  466. {
  467. if(bytes_received < 7)
  468. {
  469. cerr << "Invalid buffer size" << endl;
  470. return -1;
  471. }
  472. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  473. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  474. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  475. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  476. }
  477. else if(rcvbuf[1] != 0xC6)
  478. {
  479. cerr << "Invalid command callback" << endl;
  480. return -1;
  481. }
  482. cout << "Pico device points set" << endl;
  483. return 0;
  484. }
  485. int pico_set_sample_rate(uint32_t sample_rate)
  486. {
  487. sndbuf[0] = 0xAA;
  488. sndbuf[1] = 0x07;
  489. memcpy(sndbuf + 2, &sample_rate, sizeof(uint32_t));
  490. if(socket->Send(sndbuf, sizeof(uint32_t) + 6) == -1)
  491. {
  492. cerr << "Failed to send data to socket" << endl;
  493. return -1;
  494. }
  495. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  496. {
  497. cerr << "Failed to receive data from socket" << endl;
  498. return -1;
  499. }
  500. uint32_t bytes_received = socket->GetBytesReceived();
  501. if(bytes_received == 0)
  502. {
  503. =======
  504. {
  505. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  506. cerr << "Server disconnected" << endl;
  507. return -1;
  508. }
  509. if(bytes_received < 2)
  510. {
  511. cerr << "Invalid buffer size" << endl;
  512. return -1;
  513. }
  514. if(rcvbuf[0] != 0xAA)
  515. {
  516. cerr << "Invalid magic number" << endl;
  517. return -1;
  518. }
  519. if(rcvbuf[1] == 0xFF)
  520. {
  521. if(bytes_received < 7)
  522. {
  523. cerr << "Invalid buffer size" << endl;
  524. return -1;
  525. }
  526. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  527. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  528. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  529. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  530. <<<<<<< HEAD
  531. return 0;
  532. }
  533. else if(rcvbuf[1] != 0xC7)
  534. =======
  535. }
  536. else if(rcvbuf[1] != 0xC3)
  537. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  538. {
  539. cerr << "Invalid command callback" << endl;
  540. return -1;
  541. }
  542. <<<<<<< HEAD
  543. cout << "Pico device sample rate set" << endl;
  544. return 0;
  545. }
  546. int pico_set_times(uint32_t* times, uint32_t size)
  547. {
  548. sndbuf[0] = 0xAA;
  549. sndbuf[1] = 0x08;
  550. memcpy(sndbuf + 2, &size, sizeof(uint32_t));
  551. memcpy(sndbuf + 6, times, size * sizeof(uint32_t));
  552. if(socket->Send(sndbuf, size * sizeof(uint32_t) + 6) == -1)
  553. =======
  554. cout << "Pico device closed" << endl;
  555. return 0;
  556. }
  557. int pico_get_current_params()
  558. {
  559. sndbuf[0] = 0xAA;
  560. sndbuf[1] = 0x04;
  561. if(socket->Send(sndbuf, 2) == -1)
  562. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  563. {
  564. cerr << "Failed to send data to socket" << endl;
  565. return -1;
  566. }
  567. <<<<<<< HEAD
  568. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  569. =======
  570. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  571. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  572. {
  573. cerr << "Failed to receive data from socket" << endl;
  574. return -1;
  575. }
  576. uint32_t bytes_received = socket->GetBytesReceived();
  577. if(bytes_received == 0)
  578. <<<<<<< HEAD
  579. {
  580. =======
  581. {
  582. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  583. cerr << "Server disconnected" << endl;
  584. return -1;
  585. }
  586. if(bytes_received < 2)
  587. {
  588. cerr << "Invalid buffer size" << endl;
  589. return -1;
  590. }
  591. if(rcvbuf[0] != 0xAA)
  592. {
  593. cerr << "Invalid magic number" << endl;
  594. return -1;
  595. }
  596. if(rcvbuf[1] == 0xFF)
  597. {
  598. if(bytes_received < 7)
  599. {
  600. cerr << "Invalid buffer size" << endl;
  601. return -1;
  602. }
  603. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  604. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  605. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  606. <<<<<<< HEAD
  607. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  608. =======
  609. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  610. }
  611. else if(rcvbuf[1] != 0xC4)
  612. {
  613. cerr << "Invalid command callback" << endl;
  614. return -1;
  615. }
  616. if(bytes_received < 18)
  617. {
  618. cerr << "Invalid buffer size" << endl;
  619. return -1;
  620. }
  621. uint32_t size;
  622. memcpy(&size, rcvbuf + 2, sizeof(uint32_t));
  623. if(size < 1)
  624. {
  625. cerr << "Invalid data size" << endl;
  626. return -1;
  627. }
  628. uint32_t* apoints = new uint32_t[size / sizeof(uint32_t)];
  629. memcpy(apoints, rcvbuf + 6, size);
  630. for(uint32_t i = 0; i < size / sizeof(uint32_t); ++i)
  631. {
  632. cout << "apoints[" << i << "] = " << std::dec << apoints[i] << endl;
  633. }
  634. uint32_t* atimes = new uint32_t[size / sizeof(uint32_t)];
  635. memcpy(atimes, rcvbuf + 6 + size, size);
  636. for(uint32_t i = 0; i < size / sizeof(uint32_t); ++i)
  637. {
  638. cout << "atimes[" << i << "] = " << std::dec << atimes[i] << endl;
  639. }
  640. uint32_t sample_rate = 0;
  641. memcpy(&sample_rate, rcvbuf + 6 + 2 * size, sizeof(uint32_t));
  642. cout << "sample_rate = " << std::dec << sample_rate << endl;
  643. uint32_t number_channels = 0;
  644. memcpy(&number_channels, rcvbuf + 10 + 2 * size, sizeof(uint32_t));
  645. cout << "number_channels = " << std::dec << number_channels << endl;
  646. cout << "Pico device parameters retrieved" << endl;
  647. return 0;
  648. }
  649. int pico_probe()
  650. {
  651. sndbuf[0] = 0xAA;
  652. sndbuf[1] = 0x05;
  653. if(socket->Send(sndbuf, 2) == -1)
  654. {
  655. cerr << "Failed to send data to socket" << endl;
  656. return -1;
  657. }
  658. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  659. {
  660. cerr << "Failed to receive data from socket" << endl;
  661. return -1;
  662. }
  663. uint32_t bytes_received = socket->GetBytesReceived();
  664. if(bytes_received == 0)
  665. {
  666. cerr << "Server disconnected" << endl;
  667. return -1;
  668. }
  669. if(bytes_received < 2)
  670. {
  671. cerr << "Invalid buffer size" << endl;
  672. return -1;
  673. }
  674. if(rcvbuf[0] != 0xAA)
  675. {
  676. cerr << "Invalid magic number" << endl;
  677. return -1;
  678. }
  679. if(rcvbuf[1] == 0xFF)
  680. {
  681. if(bytes_received < 7)
  682. {
  683. cerr << "Invalid buffer size" << endl;
  684. return -1;
  685. }
  686. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  687. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  688. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  689. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  690. }
  691. else if(rcvbuf[1] != 0xC5)
  692. {
  693. cerr << "Invalid command callback" << endl;
  694. return -1;
  695. }
  696. cout << "Pico device probe completed" << endl;
  697. return 0;
  698. }
  699. int pico_set_points(uint32_t* points, uint32_t size)
  700. {
  701. sndbuf[0] = 0xAA;
  702. sndbuf[1] = 0x06;
  703. memcpy(sndbuf + 2, &size, sizeof(uint32_t));
  704. memcpy(sndbuf + 6, points, size * sizeof(uint32_t));
  705. if(socket->Send(sndbuf, size * sizeof(uint32_t) + 6) == -1)
  706. {
  707. cerr << "Failed to send data to socket" << endl;
  708. return -1;
  709. }
  710. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  711. {
  712. cerr << "Failed to receive data from socket" << endl;
  713. return -1;
  714. }
  715. uint32_t bytes_received = socket->GetBytesReceived();
  716. if(bytes_received == 0)
  717. {
  718. cerr << "Server disconnected" << endl;
  719. return -1;
  720. }
  721. if(bytes_received < 2)
  722. {
  723. cerr << "Invalid buffer size" << endl;
  724. return -1;
  725. }
  726. if(rcvbuf[0] != 0xAA)
  727. {
  728. cerr << "Invalid magic number" << endl;
  729. return -1;
  730. }
  731. if(rcvbuf[1] == 0xFF)
  732. {
  733. if(bytes_received < 7)
  734. {
  735. cerr << "Invalid buffer size" << endl;
  736. return -1;
  737. }
  738. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  739. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  740. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  741. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  742. }
  743. else if(rcvbuf[1] != 0xC6)
  744. {
  745. cerr << "Invalid command callback" << endl;
  746. return -1;
  747. }
  748. cout << "Pico device points set" << endl;
  749. return 0;
  750. }
  751. int pico_set_sample_rate(uint32_t sample_rate)
  752. {
  753. sndbuf[0] = 0xAA;
  754. sndbuf[1] = 0x07;
  755. memcpy(sndbuf + 2, &sample_rate, sizeof(uint32_t));
  756. if(socket->Send(sndbuf, sizeof(uint32_t) + 6) == -1)
  757. {
  758. cerr << "Failed to send data to socket" << endl;
  759. return -1;
  760. }
  761. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  762. {
  763. cerr << "Failed to receive data from socket" << endl;
  764. return -1;
  765. }
  766. uint32_t bytes_received = socket->GetBytesReceived();
  767. if(bytes_received == 0)
  768. {
  769. cerr << "Server disconnected" << endl;
  770. return -1;
  771. }
  772. if(bytes_received < 2)
  773. {
  774. cerr << "Invalid buffer size" << endl;
  775. return -1;
  776. }
  777. if(rcvbuf[0] != 0xAA)
  778. {
  779. cerr << "Invalid magic number" << endl;
  780. return -1;
  781. }
  782. if(rcvbuf[1] == 0xFF)
  783. {
  784. if(bytes_received < 7)
  785. {
  786. cerr << "Invalid buffer size" << endl;
  787. return -1;
  788. }
  789. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  790. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  791. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  792. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  793. return 0;
  794. }
  795. else if(rcvbuf[1] != 0xC7)
  796. {
  797. cerr << "Invalid command callback" << endl;
  798. return -1;
  799. }
  800. cout << "Pico device sample rate set" << endl;
  801. return 0;
  802. }
  803. int pico_set_times(uint32_t* times, uint32_t size)
  804. {
  805. sndbuf[0] = 0xAA;
  806. sndbuf[1] = 0x08;
  807. memcpy(sndbuf + 2, &size, sizeof(uint32_t));
  808. memcpy(sndbuf + 6, times, size * sizeof(uint32_t));
  809. if(socket->Send(sndbuf, size * sizeof(uint32_t) + 6) == -1)
  810. {
  811. cerr << "Failed to send data to socket" << endl;
  812. return -1;
  813. }
  814. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  815. {
  816. cerr << "Failed to receive data from socket" << endl;
  817. return -1;
  818. }
  819. uint32_t bytes_received = socket->GetBytesReceived();
  820. if(bytes_received == 0)
  821. {
  822. cerr << "Server disconnected" << endl;
  823. return -1;
  824. }
  825. if(bytes_received < 2)
  826. {
  827. cerr << "Invalid buffer size" << endl;
  828. return -1;
  829. }
  830. if(rcvbuf[0] != 0xAA)
  831. {
  832. cerr << "Invalid magic number" << endl;
  833. return -1;
  834. }
  835. if(rcvbuf[1] == 0xFF)
  836. {
  837. if(bytes_received < 7)
  838. {
  839. cerr << "Invalid buffer size" << endl;
  840. return -1;
  841. }
  842. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  843. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  844. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  845. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  846. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  847. }
  848. else if(rcvbuf[1] != 0xC8)
  849. {
  850. cerr << "Invalid command callback" << endl;
  851. return -1;
  852. }
  853. cout << "Pico device times set" << endl;
  854. <<<<<<< HEAD
  855. return 0;
  856. =======
  857. return 0;
  858. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  859. }
  860. int pico_configure_channels(const uint32_t number_channels, uint8_t trigger_channel, int32_t direction, uint16_t threshold, int16_t autoTrigger_ms)
  861. {
  862. sndbuf[0] = 0xAA;
  863. sndbuf[1] = 0x09;
  864. memcpy(sndbuf + 2, &number_channels, sizeof(uint32_t));
  865. memcpy(sndbuf + 6, &trigger_channel, sizeof(uint8_t));
  866. memcpy(sndbuf + 7, &direction, sizeof(int32_t));
  867. memcpy(sndbuf + 11, &threshold, sizeof(uint16_t));
  868. memcpy(sndbuf + 13, &autoTrigger_ms, sizeof(int16_t));
  869. if(socket->Send(sndbuf, 15) == -1)
  870. {
  871. cerr << "Failed to send data to socket" << endl;
  872. return -1;
  873. }
  874. <<<<<<< HEAD
  875. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  876. =======
  877. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  878. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  879. {
  880. cerr << "Failed to receive data from socket" << endl;
  881. return -1;
  882. }
  883. uint32_t bytes_received = socket->GetBytesReceived();
  884. if(bytes_received == 0)
  885. <<<<<<< HEAD
  886. {
  887. =======
  888. {
  889. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  890. cerr << "Server disconnected" << endl;
  891. return -1;
  892. }
  893. if(bytes_received < 2)
  894. {
  895. cerr << "Invalid buffer size" << endl;
  896. return -1;
  897. }
  898. if(rcvbuf[0] != 0xAA)
  899. {
  900. cerr << "Invalid magic number" << endl;
  901. return -1;
  902. }
  903. if(rcvbuf[1] == 0xFF)
  904. {
  905. if(bytes_received < 7)
  906. {
  907. cerr << "Invalid buffer size" << endl;
  908. return -1;
  909. }
  910. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  911. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  912. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  913. <<<<<<< HEAD
  914. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  915. =======
  916. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  917. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  918. }
  919. else if(rcvbuf[1] != 0xC9)
  920. {
  921. cerr << "Invalid command callback" << endl;
  922. return -1;
  923. }
  924. cout << "Pico device channels configured" << endl;
  925. <<<<<<< HEAD
  926. return 0;
  927. =======
  928. return 0;
  929. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  930. }
  931. int pico_begin_measurement()
  932. {
  933. sndbuf[0] = 0xAA;
  934. sndbuf[1] = 0x0A;
  935. if(socket->Send(sndbuf, 2) == -1)
  936. {
  937. cerr << "Failed to send data to socket" << endl;
  938. return -1;
  939. }
  940. <<<<<<< HEAD
  941. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  942. =======
  943. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  944. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  945. {
  946. cerr << "Failed to receive data from socket" << endl;
  947. return -1;
  948. }
  949. <<<<<<< HEAD
  950. uint32_t bytes_received = socket->GetBytesReceived();
  951. if(bytes_received == 0)
  952. {
  953. =======
  954. uint32_t bytes_received = socket->GetBytesReceived();
  955. if(bytes_received == 0)
  956. {
  957. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  958. cerr << "Server disconnected" << endl;
  959. return -1;
  960. }
  961. if(bytes_received < 2)
  962. {
  963. cerr << "Invalid buffer size" << endl;
  964. return -1;
  965. }
  966. if(rcvbuf[0] != 0xAA)
  967. {
  968. cerr << "Invalid magic number" << endl;
  969. return -1;
  970. }
  971. if(rcvbuf[1] == 0xFF)
  972. {
  973. if(bytes_received < 7)
  974. {
  975. cerr << "Invalid buffer size" << endl;
  976. return -1;
  977. }
  978. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  979. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  980. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  981. <<<<<<< HEAD
  982. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  983. =======
  984. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  985. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  986. }
  987. else if(rcvbuf[1] != 0xCA)
  988. {
  989. cout << "Invalid command callback" << endl;
  990. }
  991. cout << "Pico device measurement started" << endl;
  992. cout << "Measurement in progress..." << endl;
  993. <<<<<<< HEAD
  994. while(rcvbuf[1] != 0xCB)
  995. {
  996. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  997. =======
  998. bool end_of_data = false;
  999. while(!end_of_data)
  1000. {
  1001. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  1002. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1003. {
  1004. cerr << "Failed to receive data from socket" << endl;
  1005. return -1;
  1006. }
  1007. bytes_received = socket->GetBytesReceived();
  1008. if(bytes_received == 0)
  1009. <<<<<<< HEAD
  1010. {
  1011. =======
  1012. {
  1013. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1014. cerr << "Server disconnected" << endl;
  1015. return -1;
  1016. }
  1017. if(bytes_received < 2)
  1018. {
  1019. cerr << "Invalid buffer size" << endl;
  1020. return -1;
  1021. }
  1022. if(rcvbuf[0] != 0xAA)
  1023. {
  1024. cerr << "Invalid magic number" << endl;
  1025. return -1;
  1026. }
  1027. if(rcvbuf[1] == 0xFF)
  1028. {
  1029. if(bytes_received < 7)
  1030. {
  1031. cerr << "Invalid buffer size" << endl;
  1032. return -1;
  1033. }
  1034. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  1035. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  1036. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  1037. <<<<<<< HEAD
  1038. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  1039. }
  1040. else if(rcvbuf[1] != 0xCA)
  1041. =======
  1042. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  1043. }
  1044. if(rcvbuf[1] == 0xCB)
  1045. {
  1046. cout << "Measurement completed..." << endl;
  1047. end_of_data = true;
  1048. }
  1049. else if(rcvbuf[1] = 0xCA)
  1050. {
  1051. cout << "Measurement in progress..." << endl;
  1052. }
  1053. else
  1054. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1055. {
  1056. cerr << "Invalid command callback" << endl;
  1057. return -1;
  1058. }
  1059. cout << "Measurement in progress..." << endl;
  1060. }
  1061. cout << "Pico device measurement completed" << endl;
  1062. return 0;
  1063. }
  1064. int pico_stop()
  1065. {
  1066. sndbuf[0] = 0xAA;
  1067. sndbuf[1] = 0x0E;
  1068. if(socket->Send(sndbuf, 2) == -1)
  1069. {
  1070. cerr << "Failed to send data to socket" << endl;
  1071. return -1;
  1072. }
  1073. <<<<<<< HEAD
  1074. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  1075. =======
  1076. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  1077. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1078. {
  1079. cerr << "Failed to receive data from socket" << endl;
  1080. return -1;
  1081. }
  1082. uint32_t bytes_received = socket->GetBytesReceived();
  1083. if(bytes_received == 0)
  1084. <<<<<<< HEAD
  1085. {
  1086. =======
  1087. {
  1088. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1089. cerr << "Server disconnected" << endl;
  1090. return -1;
  1091. }
  1092. if(bytes_received < 2)
  1093. {
  1094. cerr << "Invalid buffer size" << endl;
  1095. return -1;
  1096. }
  1097. if(rcvbuf[0] != 0xAA)
  1098. {
  1099. cerr << "Invalid magic number" << endl;
  1100. return -1;
  1101. }
  1102. <<<<<<< HEAD
  1103. =======
  1104. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1105. if(rcvbuf[1] == 0xFF)
  1106. {
  1107. if(bytes_received < 7)
  1108. {
  1109. cerr << "Invalid buffer size" << endl;
  1110. return -1;
  1111. }
  1112. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  1113. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  1114. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  1115. <<<<<<< HEAD
  1116. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  1117. =======
  1118. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  1119. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1120. }
  1121. else if(rcvbuf[1] != 0xCE)
  1122. {
  1123. cerr << "Invalid command callback" << endl;
  1124. return -1;
  1125. }
  1126. cout << "Pico device measurement stopped" << endl;
  1127. return 0;
  1128. }
  1129. int pico_exit()
  1130. {
  1131. sndbuf[0] = 0xAA;
  1132. sndbuf[1] = 0x0D;
  1133. if(socket->Send(sndbuf, 2) == -1)
  1134. {
  1135. cerr << "Failed to send data to socket" << endl;
  1136. return -1;
  1137. }
  1138. <<<<<<< HEAD
  1139. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  1140. =======
  1141. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  1142. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1143. {
  1144. cerr << "Failed to receive data from socket" << endl;
  1145. return -1;
  1146. }
  1147. uint32_t bytes_received = socket->GetBytesReceived();
  1148. if(bytes_received == 0)
  1149. <<<<<<< HEAD
  1150. {
  1151. =======
  1152. {
  1153. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1154. cerr << "Server disconnected" << endl;
  1155. return -1;
  1156. }
  1157. if(bytes_received < 2)
  1158. {
  1159. cerr << "Invalid buffer size" << endl;
  1160. return -1;
  1161. }
  1162. if(rcvbuf[0] != 0xAA)
  1163. {
  1164. cerr << "Invalid magic number" << endl;
  1165. return -1;
  1166. }
  1167. if(rcvbuf[1] == 0xFF)
  1168. {
  1169. if(bytes_received < 7)
  1170. {
  1171. cerr << "Invalid buffer size" << endl;
  1172. return -1;
  1173. }
  1174. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  1175. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  1176. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  1177. <<<<<<< HEAD
  1178. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  1179. =======
  1180. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  1181. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1182. }
  1183. else if(rcvbuf[1] != 0xCD)
  1184. {
  1185. cerr << "Invalid command callback" << endl;
  1186. return -1;
  1187. }
  1188. cout << "Pico device exited" << endl;
  1189. return 0;
  1190. }
  1191. int get_command()
  1192. {
  1193. <<<<<<< HEAD
  1194. if(socket->Receive(sndbuf, sizeof(sndbuf)) != 0)
  1195. {
  1196. std::getline(std::cin, line_edit);
  1197. =======
  1198. if(true)
  1199. {
  1200. cout << "cmd$ ";
  1201. std::getline(std::cin, line_edit);
  1202. cout << endl;
  1203. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1204. tokens = split(line_edit, " ");
  1205. if(tokens.size() < 1)
  1206. {
  1207. cerr << "Invalid command" << endl;
  1208. <<<<<<< HEAD
  1209. return -1;
  1210. }
  1211. if(tokens[0] = "open")
  1212. =======
  1213. cout << endl;
  1214. return -1;
  1215. }
  1216. if(tokens[0] == "open")
  1217. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1218. {
  1219. if(pico_open() != 0)
  1220. {
  1221. cerr << "Failed to open pico device" << endl;
  1222. <<<<<<< HEAD
  1223. =======
  1224. cout << endl;
  1225. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1226. return -1;
  1227. }
  1228. }
  1229. else if(tokens[0] == "xml_config")
  1230. {
  1231. if(tokens.size() < 2)
  1232. {
  1233. cerr << "Invalid command" << endl;
  1234. <<<<<<< HEAD
  1235. =======
  1236. cout << endl;
  1237. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1238. return -1;
  1239. }
  1240. if(pico_xml_config(tokens[1]) != 0)
  1241. {
  1242. cerr << "Failed to configure pico device" << endl;
  1243. <<<<<<< HEAD
  1244. =======
  1245. cout << endl;
  1246. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1247. return -1;
  1248. }
  1249. }
  1250. else if(tokens[0] == "set_params")
  1251. {
  1252. if(tokens.size() < 5)
  1253. {
  1254. cerr << "Invalid command" << endl;
  1255. <<<<<<< HEAD
  1256. =======
  1257. cout << endl;
  1258. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1259. return -1;
  1260. }
  1261. std::vector<uint32_t> points;
  1262. std::vector<uint32_t> times;
  1263. <<<<<<< HEAD
  1264. uint32_t sample_rate = std::stoi(tokens[2]);
  1265. uint32_t number_channels = std::stoi(tokens[3]);
  1266. uint32_t size = std::stoi(tokens[4]);
  1267. points = string_to_vector(points);
  1268. times = string_to_vector(times);
  1269. if(points.size() != size || times.size() != size)
  1270. {
  1271. cerr << "Invalid data size" << endl;
  1272. =======
  1273. points = string_to_vector(tokens[1]);
  1274. times = string_to_vector(tokens[2]);
  1275. uint32_t sample_rate = std::stoi(tokens[3]);
  1276. uint32_t number_channels = std::stoi(tokens[4]);
  1277. uint32_t size = std::stoi(tokens[5]);
  1278. if(points.size() != size || times.size() != size)
  1279. {
  1280. cerr << "Invalid data size" << endl;
  1281. cout << endl;
  1282. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1283. return -1;
  1284. }
  1285. if(pico_set_params(points.data(), times.data(), sample_rate, number_channels, size) != 0)
  1286. {
  1287. cerr << "Failed to set pico device parameters" << endl;
  1288. <<<<<<< HEAD
  1289. =======
  1290. cout << endl;
  1291. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1292. return -1;
  1293. }
  1294. return 0;
  1295. }
  1296. else if(tokens[0] == "close")
  1297. {
  1298. if(pico_close() != 0)
  1299. {
  1300. cerr << "Failed to close pico device" << endl;
  1301. <<<<<<< HEAD
  1302. =======
  1303. cout << endl;
  1304. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1305. return -1;
  1306. }
  1307. }
  1308. else if(tokens[0] == "exit")
  1309. {
  1310. if(pico_exit() != 0)
  1311. {
  1312. cerr << "Failed to exit pico device" << endl;
  1313. <<<<<<< HEAD
  1314. =======
  1315. cout << endl;
  1316. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1317. return -1;
  1318. }
  1319. return 1;
  1320. }
  1321. else if(tokens[0] == "get_current_params")
  1322. {
  1323. if(pico_get_current_params() != 0)
  1324. {
  1325. cerr << "Failed to get pico device parameters" << endl;
  1326. <<<<<<< HEAD
  1327. =======
  1328. cout << endl;
  1329. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1330. return -1;
  1331. }
  1332. }
  1333. else if(tokens[0] == "probe")
  1334. {
  1335. if(pico_probe() != 0)
  1336. {
  1337. cerr << "Failed to probe pico device" << endl;
  1338. <<<<<<< HEAD
  1339. =======
  1340. cout << endl;
  1341. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1342. return -1;
  1343. }
  1344. }
  1345. else if(tokens[0] == "set_points")
  1346. {
  1347. if(tokens.size() < 3)
  1348. {
  1349. cerr << "Invalid command" << endl;
  1350. <<<<<<< HEAD
  1351. return -1;
  1352. }
  1353. uint32_t size = std::stoi(tokens[2]);
  1354. std::vector<uint32_t> points = string_to_vector(points);
  1355. if(points.size() != size)
  1356. {
  1357. cerr << "Invalid data size" << endl;
  1358. =======
  1359. cout << endl;
  1360. return -1;
  1361. }
  1362. uint32_t size = std::stoi(tokens[1]);
  1363. std::vector<uint32_t> points = string_to_vector(tokens[2]);
  1364. if(points.size() != size)
  1365. {
  1366. cerr << "Invalid data size" << endl;
  1367. cout << endl;
  1368. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1369. return -1;
  1370. }
  1371. if(pico_set_points(points.data(), size) != 0)
  1372. {
  1373. cerr << "Failed to set pico device points" << endl;
  1374. <<<<<<< HEAD
  1375. =======
  1376. cout << endl;
  1377. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1378. return -1;
  1379. }
  1380. }
  1381. else if(tokens[0] == "set_sample_rate")
  1382. {
  1383. if(tokens.size() < 2)
  1384. {
  1385. cerr << "Invalid command" << endl;
  1386. <<<<<<< HEAD
  1387. =======
  1388. cout << endl;
  1389. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1390. return -1;
  1391. }
  1392. uint32_t sample_rate = std::stoi(tokens[1]);
  1393. if(pico_set_sample_rate(sample_rate) != 0)
  1394. {
  1395. cerr << "Failed to set pico device sample rate" << endl;
  1396. <<<<<<< HEAD
  1397. =======
  1398. cout << endl;
  1399. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1400. return -1;
  1401. }
  1402. }
  1403. else if(tokens[0] == "set_times")
  1404. {
  1405. if(tokens.size() < 3)
  1406. {
  1407. cerr << "Invalid command" << endl;
  1408. <<<<<<< HEAD
  1409. return -1;
  1410. }
  1411. uint32_t size = std::stoi(tokens[2]);
  1412. std::vector<uint32_t> times = string_to_vector(times);
  1413. if(times.size() != size)
  1414. {
  1415. cerr << "Invalid data size" << endl;
  1416. =======
  1417. cout << endl;
  1418. return -1;
  1419. }
  1420. uint32_t size = std::stoi(tokens[1]);
  1421. std::vector<uint32_t> times = string_to_vector(tokens[2]);
  1422. if(times.size() != size)
  1423. {
  1424. cerr << "Invalid data size" << endl;
  1425. cout << endl;
  1426. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1427. return -1;
  1428. }
  1429. if(pico_set_times(times.data(), size) != 0)
  1430. {
  1431. cerr << "Failed to set pico device times" << endl;
  1432. <<<<<<< HEAD
  1433. =======
  1434. cout << endl;
  1435. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1436. return -1;
  1437. }
  1438. }
  1439. else if(tokens[0] == "configure_channels")
  1440. {
  1441. if(tokens.size() < 6)
  1442. {
  1443. cerr << "Invalid command" << endl;
  1444. <<<<<<< HEAD
  1445. =======
  1446. cout << endl;
  1447. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1448. return -1;
  1449. }
  1450. uint32_t number_channels = std::stoi(tokens[1]); // number of channels (1-8)
  1451. uint8_t trigger_channel = std::stoi(tokens[2]); // trigger channel (0-7, 0 = channel A, 1 = channel B, ...)
  1452. int32_t direction = std::stoi(tokens[3]); // trigger direction (0 = rising, 1 = falling)
  1453. uint16_t threshold = std::stoi(tokens[4]); // trigger threshold (0-65535)
  1454. int16_t autoTrigger_ms = std::stoi(tokens[5]); // auto trigger time (0-65535 ms)
  1455. if(pico_configure_channels(number_channels, trigger_channel, direction, threshold, autoTrigger_ms) != 0)
  1456. {
  1457. cerr << "Failed to configure pico device channels" << endl;
  1458. <<<<<<< HEAD
  1459. =======
  1460. cout << endl;
  1461. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1462. return -1;
  1463. }
  1464. }
  1465. else if(tokens[0] == "begin_measurement")
  1466. {
  1467. if(pico_begin_measurement() != 0)
  1468. {
  1469. cerr << "Failed to begin pico device measurement" << endl;
  1470. <<<<<<< HEAD
  1471. =======
  1472. cout << endl;
  1473. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1474. return -1;
  1475. }
  1476. }
  1477. else if(tokens[0] == "stop")
  1478. {
  1479. if(pico_stop() != 0)
  1480. {
  1481. cerr << "Failed to stop pico device measurement" << endl;
  1482. <<<<<<< HEAD
  1483. =======
  1484. cout << endl;
  1485. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1486. return -1;
  1487. }
  1488. }
  1489. else if(tokens[0] == "help")
  1490. {
  1491. cout << "Available commands:" << endl;
  1492. cout << "open" << endl;
  1493. cout << "xml_config <filename>" << endl;
  1494. cout << "set_params <points> <times> <sample_rate> <number_channels> <size>" << endl;
  1495. cout << "close" << endl;
  1496. cout << "exit" << endl;
  1497. cout << "get_current_params" << endl;
  1498. cout << "probe" << endl;
  1499. cout << "set_points <points> <size>" << endl;
  1500. cout << "set_sample_rate <sample_rate>" << endl;
  1501. cout << "set_times <times> <size>" << endl;
  1502. cout << "configure_channels <number_channels> <trigger_channel> <direction> <threshold> <autoTrigger_ms>" << endl;
  1503. cout << "begin_measurement" << endl;
  1504. cout << "stop" << endl;
  1505. }
  1506. else if(tokens[0] == "test")
  1507. {
  1508. cout << "Test command received" << endl;
  1509. }
  1510. else
  1511. {
  1512. cerr << "Unknown command" << endl;
  1513. <<<<<<< HEAD
  1514. =======
  1515. cout << endl;
  1516. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1517. return -1;
  1518. }
  1519. }
  1520. <<<<<<< HEAD
  1521. return 0;
  1522. }
  1523. }
  1524. =======
  1525. cout << endl;
  1526. return 0;
  1527. }
  1528. };
  1529. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1530. int main()
  1531. {
  1532. cout << "Open socket" << endl;
  1533. <<<<<<< HEAD
  1534. CPassiveSocket SocketPassive(CSimpleSocket::CSocketType::SocketTypeTcp);
  1535. if (!SocketPassive.Initialize())
  1536. =======
  1537. CActiveSocket SocketActive(CSimpleSocket::CSocketType::SocketTypeTcp);
  1538. if (!SocketActive.Initialize())
  1539. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1540. {
  1541. cerr << "Socket initialization failed" << endl;
  1542. return -1;
  1543. }
  1544. <<<<<<< HEAD
  1545. if(!SocketPassive.Listen("localhost", 5002))
  1546. {
  1547. cerr << "Socket listening failed" << endl;
  1548. return -1;
  1549. }
  1550. CActiveSocket* ClientSocket;
  1551. while(true)
  1552. {
  1553. if ((ClientSocket = SocketPassive.Accept()) != nullptr)
  1554. {
  1555. cerr << "Socket accept failed" << endl;
  1556. return -1;
  1557. }
  1558. cout << LogPref::Flag(INFO) << "Client connected" << endl;
  1559. pico_service = new PicoLocalService(ClientSocket);
  1560. while(pico_service->get_request() != 0)
  1561. {
  1562. cout << LogPref::Flag(INFO) << "Request received" << endl;
  1563. }
  1564. cout << LogPref::Flag(INFO) << "Request processing finished" << endl;
  1565. delete pico_service;
  1566. pico_service = nullptr;
  1567. delete ClientSocket;
  1568. ClientSocket = nullptr;
  1569. =======
  1570. if(!SocketActive.Open("127.0.0.1", 5003))
  1571. {
  1572. cerr << "Socket opening failed" << endl;
  1573. return -1;
  1574. }
  1575. PicoLocalClient pico_client(&SocketActive);
  1576. while(pico_client.get_command() != 1) {
  1577. // empty
  1578. >>>>>>> 6a0c5e06dc737368566c8c5b45d8073a39148b99
  1579. }
  1580. return 0;
  1581. }