main.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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. #include "simplecout.hpp"
  9. #include "parser.hpp"
  10. #include "src/ActiveSocket.h"
  11. #define MAX_PACKET 4096
  12. using namespace std;
  13. std::vector<std::string> split(const std::string& s, const std::string& delimiter) {
  14. std::vector<std::string> tokens;
  15. size_t pos = 0;
  16. std::string token;
  17. while ((pos = s.find(delimiter)) != std::string::npos) {
  18. token = s.substr(0, pos);
  19. tokens.push_back(token);
  20. s.erase(0, pos + delimiter.length());
  21. }
  22. tokens.push_back(s);
  23. return tokens;
  24. }
  25. class PicoLocalClient
  26. {
  27. private:
  28. CActiveSocket *socket;
  29. uint8_t sndbuf[MAX_PACKET]{0};
  30. uint8_t rcvbuf[MAX_PACKET]{0};
  31. std::string line_edit;
  32. std::vector<std::string> tokens;
  33. uint8_t error_state = 0x00;
  34. uint32_t error_code = 0x00000000;
  35. public:
  36. PicoLocalClient(CActiveSocket *socket) : socket(socket) {}
  37. ~PicoLocalClient() { delete socket; }
  38. int pico_open()
  39. {
  40. sndbuf[0] = 0xAA;
  41. sndbuf[1] = 0x01;
  42. if(socket->Send(sndbuf, 2) == 0)
  43. {
  44. cerr << "Failed to send data to socket" << endl;
  45. return -1;
  46. }
  47. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  48. {
  49. cerr << "Failed to receive data from socket" << endl;
  50. return -1;
  51. }
  52. uint32_t bytes_received = socket->GetBytesReceived();
  53. if(bytes_received == 0)
  54. {
  55. cerr << "Server disconnected" << endl;
  56. return -1;
  57. }
  58. if(bytes_received < 2)
  59. {
  60. cerr << "Invalid buffer size" << endl;
  61. return -1;
  62. }
  63. if(rcvbuf[0] != 0xAA)
  64. {
  65. cerr << "Invalid magic number" << endl;
  66. return -1;
  67. }
  68. if(rcvbuf[1] == 0xFF)
  69. {
  70. if(bytes_received < 7)
  71. {
  72. cerr << "Invalid buffer size" << endl;
  73. return -1;
  74. }
  75. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  76. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  77. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  78. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  79. }
  80. else if(rcvbuf[1] != 0xC1)
  81. {
  82. cerr << "Invalid command callback" << endl;
  83. return -1;
  84. }
  85. cout << "Pico device opened" << endl;
  86. return 0;
  87. }
  88. int pico_xml_config(const std::string& file_name)
  89. {
  90. sndbuf[0] = 0xAA;
  91. sndbuf[1] = 0x02;
  92. uint32_t size = file_name.size();
  93. memcpy(sndbuf + 2, &size, sizeof(uint32_t));
  94. memcpy(sndbuf + 6, file_name.c_str(), size);
  95. if(socket->Send(sndbuf, size + 6) == -1)
  96. {
  97. cerr << "Failed to send data to socket" << endl;
  98. return -1;
  99. }
  100. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  101. {
  102. cerr << "Failed to receive data from socket" << endl;
  103. return -1;
  104. }
  105. uint32_t bytes_received = socket->GetBytesReceived();
  106. if(bytes_received == 0)
  107. {
  108. cerr << "Server disconnected" << endl;
  109. return -1;
  110. }
  111. if(bytes_received < 2)
  112. {
  113. cerr << "Invalid buffer size" << endl;
  114. return -1;
  115. }
  116. if(rcvbuf[0] != 0xAA)
  117. {
  118. cerr << "Invalid magic number" << endl;
  119. return -1;
  120. }
  121. if(rcvbuf[1] == 0xFF)
  122. {
  123. if(bytes_received < 7)
  124. {
  125. cerr << "Invalid buffer size" << endl;
  126. return -1;
  127. }
  128. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  129. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  130. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  131. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  132. }
  133. else if(rcvbuf[1] != 0xC2)
  134. {
  135. cerr << "Invalid command callback" << endl;
  136. return -1;
  137. }
  138. cout << "Pico device configured" << endl;
  139. return 0;
  140. }
  141. int pico_set_params(uint32_t* apoints, uint32_t* times, uint32_t sample_rate, uint32_t number_channels, uint32_t size)
  142. {
  143. sndbuf[0] = 0xAA;
  144. sndbuf[1] = 0x0C;
  145. memcpy(sndbuf + 2, &size, sizeof(uint32_t));
  146. memcpy(sndbuf + 6, apoints, size * sizeof(uint32_t));
  147. memcpy(sndbuf + 6 + size * sizeof(uint32_t), times, size * sizeof(uint32_t));
  148. memcpy(sndbuf + 6 + 2 * size * sizeof(uint32_t), &sample_rate, sizeof(uint32_t));
  149. memcpy(sndbuf + 10 + 2 * size * sizeof(uint32_t), &number_channels, sizeof(uint32_t));
  150. if(socket->Send(sndbuf, size * 2 * sizeof(uint32_t) + 14) == -1)
  151. {
  152. cerr << "Failed to send data to socket" << endl;
  153. return -1;
  154. }
  155. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  156. {
  157. cerr << "Failed to receive data from socket" << endl;
  158. return -1;
  159. }
  160. uint32_t bytes_received = socket->GetBytesReceived();
  161. if(bytes_received == 0)
  162. {
  163. cerr << "Server disconnected" << endl;
  164. return -1;
  165. }
  166. if(bytes_received < 2)
  167. {
  168. cerr << "Invalid buffer size" << endl;
  169. return -1;
  170. }
  171. if(rcvbuf[0] != 0xAA)
  172. {
  173. cerr << "Invalid magic number" << endl;
  174. return -1;
  175. }
  176. if(rcvbuf[1] == 0xFF)
  177. {
  178. if(bytes_received < 7)
  179. {
  180. cerr << "Invalid buffer size" << endl;
  181. return -1;
  182. }
  183. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  184. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  185. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  186. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  187. }
  188. else if(rcvbuf[1] != 0xCC)
  189. {
  190. cerr << "Invalid command callback" << endl;
  191. return -1;
  192. }
  193. cout << "Pico device parameters set" << endl;
  194. return 0;
  195. }
  196. int pico_close()
  197. {
  198. sndbuf[0] = 0xAA;
  199. sndbuf[1] = 0x03;
  200. if(socket->Send(sndbuf, 2) == -1)
  201. {
  202. cerr << "Failed to send data to socket" << endl;
  203. return -1;
  204. }
  205. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  206. {
  207. cerr << "Failed to receive data from socket" << endl;
  208. return -1;
  209. }
  210. uint32_t bytes_received = socket->GetBytesReceived();
  211. if(bytes_received == 0)
  212. {
  213. cerr << "Server disconnected" << endl;
  214. return -1;
  215. }
  216. if(bytes_received < 2)
  217. {
  218. cerr << "Invalid buffer size" << endl;
  219. return -1;
  220. }
  221. if(rcvbuf[0] != 0xAA)
  222. {
  223. cerr << "Invalid magic number" << endl;
  224. return -1;
  225. }
  226. if(rcvbuf[1] == 0xFF)
  227. {
  228. if(bytes_received < 7)
  229. {
  230. cerr << "Invalid buffer size" << endl;
  231. return -1;
  232. }
  233. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  234. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  235. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  236. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  237. }
  238. else if(rcvbuf[1] != 0xC3)
  239. {
  240. cerr << "Invalid command callback" << endl;
  241. return -1;
  242. }
  243. cout << "Pico device closed" << endl;
  244. return 0;
  245. }
  246. int pico_get_current_params()
  247. {
  248. sndbuf[0] = 0xAA;
  249. sndbuf[1] = 0x04;
  250. if(socket->Send(sndbuf, 2) == -1)
  251. {
  252. cerr << "Failed to send data to socket" << endl;
  253. return -1;
  254. }
  255. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  256. {
  257. cerr << "Failed to receive data from socket" << endl;
  258. return -1;
  259. }
  260. uint32_t bytes_received = socket->GetBytesReceived();
  261. if(bytes_received == 0)
  262. {
  263. cerr << "Server disconnected" << endl;
  264. return -1;
  265. }
  266. if(bytes_received < 2)
  267. {
  268. cerr << "Invalid buffer size" << endl;
  269. return -1;
  270. }
  271. if(rcvbuf[0] != 0xAA)
  272. {
  273. cerr << "Invalid magic number" << endl;
  274. return -1;
  275. }
  276. if(rcvbuf[1] == 0xFF)
  277. {
  278. if(bytes_received < 7)
  279. {
  280. cerr << "Invalid buffer size" << endl;
  281. return -1;
  282. }
  283. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  284. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  285. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  286. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  287. }
  288. else if(rcvbuf[1] != 0xC4)
  289. {
  290. cerr << "Invalid command callback" << endl;
  291. return -1;
  292. }
  293. if(bytes_received < 18)
  294. {
  295. cerr << "Invalid buffer size" << endl;
  296. return -1;
  297. }
  298. uint32_t size;
  299. memcpy(&size, rcvbuf + 2, sizeof(uint32_t));
  300. if(size < 1)
  301. {
  302. cerr << "Invalid data size" << endl;
  303. return -1;
  304. }
  305. uint32_t* apoints = new uint32_t[size];
  306. memcpy(apoints, rcvbuf + 6, size * sizeof(uint32_t));
  307. for(uint32_t i = 0; i < size; ++i)
  308. {
  309. cout << "apoints[" << i << "] = " << std::dec << apoints[i] << endl;
  310. }
  311. uint32_t* atimes = new uint32_t[size];
  312. memcpy(atimes, rcvbuf + 6 + size * sizeof(uint32_t), size * sizeof(uint32_t));
  313. for(uint32_t i = 0; i < size; ++i)
  314. {
  315. cout << "atimes[" << i << "] = " << std::dec << atimes[i] << endl;
  316. }
  317. uint32_t sample_rate = 0;
  318. memcpy(&sample_rate, rcvbuf + 6 + 2 * size * sizeof(uint32_t), sizeof(uint32_t));
  319. cout << "sample_rate = " << std::dec << sample_rate << endl;
  320. uint32_t number_channels = 0;
  321. memcpy(&number_channels, rcvbuf + 10 + 2 * size * sizeof(uint32_t), sizeof(uint32_t));
  322. cout << "number_channels = " << std::dec << number_channels << endl;
  323. cout << "Pico device parameters retrieved" << endl;
  324. return 0;
  325. }
  326. int pico_probe()
  327. {
  328. sndbuf[0] = 0xAA;
  329. sndbuf[1] = 0x05;
  330. if(socket->Send(sndbuf, 2) == -1)
  331. {
  332. cerr << "Failed to send data to socket" << endl;
  333. return -1;
  334. }
  335. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  336. {
  337. cerr << "Failed to receive data from socket" << endl;
  338. return -1;
  339. }
  340. uint32_t bytes_received = socket->GetBytesReceived();
  341. if(bytes_received == 0)
  342. {
  343. cerr << "Server disconnected" << endl;
  344. return -1;
  345. }
  346. if(bytes_received < 2)
  347. {
  348. cerr << "Invalid buffer size" << endl;
  349. return -1;
  350. }
  351. if(rcvbuf[0] != 0xAA)
  352. {
  353. cerr << "Invalid magic number" << endl;
  354. return -1;
  355. }
  356. if(rcvbuf[1] == 0xFF)
  357. {
  358. if(bytes_received < 7)
  359. {
  360. cerr << "Invalid buffer size" << endl;
  361. return -1;
  362. }
  363. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  364. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  365. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  366. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  367. }
  368. else if(rcvbuf[1] != 0xC5)
  369. {
  370. cerr << "Invalid command callback" << endl;
  371. return -1;
  372. }
  373. cout << "Pico device probe completed" << endl;
  374. return 0;
  375. }
  376. int pico_set_points(uint32_t* points, uint32_t size)
  377. {
  378. sndbuf[0] = 0xAA;
  379. sndbuf[1] = 0x06;
  380. memcpy(sndbuf + 2, &size, sizeof(uint32_t));
  381. memcpy(sndbuf + 6, points, size * sizeof(uint32_t));
  382. if(socket->Send(sndbuf, size * sizeof(uint32_t) + 6) == -1)
  383. {
  384. cerr << "Failed to send data to socket" << endl;
  385. return -1;
  386. }
  387. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  388. {
  389. cerr << "Failed to receive data from socket" << endl;
  390. return -1;
  391. }
  392. uint32_t bytes_received = socket->GetBytesReceived();
  393. if(bytes_received == 0)
  394. {
  395. cerr << "Server disconnected" << endl;
  396. return -1;
  397. }
  398. if(bytes_received < 2)
  399. {
  400. cerr << "Invalid buffer size" << endl;
  401. return -1;
  402. }
  403. if(rcvbuf[0] != 0xAA)
  404. {
  405. cerr << "Invalid magic number" << endl;
  406. return -1;
  407. }
  408. if(rcvbuf[1] == 0xFF)
  409. {
  410. if(bytes_received < 7)
  411. {
  412. cerr << "Invalid buffer size" << endl;
  413. return -1;
  414. }
  415. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  416. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  417. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  418. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  419. }
  420. else if(rcvbuf[1] != 0xC6)
  421. {
  422. cerr << "Invalid command callback" << endl;
  423. return -1;
  424. }
  425. cout << "Pico device points set" << endl;
  426. return 0;
  427. }
  428. int pico_set_sample_rate(uint32_t sample_rate)
  429. {
  430. sndbuf[0] = 0xAA;
  431. sndbuf[1] = 0x07;
  432. memcpy(sndbuf + 2, &sample_rate, sizeof(uint32_t));
  433. if(socket->Send(sndbuf, sizeof(uint32_t) + 6) == -1)
  434. {
  435. cerr << "Failed to send data to socket" << endl;
  436. return -1;
  437. }
  438. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  439. {
  440. cerr << "Failed to receive data from socket" << endl;
  441. return -1;
  442. }
  443. uint32_t bytes_received = socket->GetBytesReceived();
  444. if(bytes_received == 0)
  445. {
  446. cerr << "Server disconnected" << endl;
  447. return -1;
  448. }
  449. if(bytes_received < 2)
  450. {
  451. cerr << "Invalid buffer size" << endl;
  452. return -1;
  453. }
  454. if(rcvbuf[0] != 0xAA)
  455. {
  456. cerr << "Invalid magic number" << endl;
  457. return -1;
  458. }
  459. if(rcvbuf[1] == 0xFF)
  460. {
  461. if(bytes_received < 7)
  462. {
  463. cerr << "Invalid buffer size" << endl;
  464. return -1;
  465. }
  466. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  467. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  468. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  469. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  470. return 0;
  471. }
  472. else if(rcvbuf[1] != 0xC7)
  473. {
  474. cerr << "Invalid command callback" << endl;
  475. return -1;
  476. }
  477. cout << "Pico device sample rate set" << endl;
  478. return 0;
  479. }
  480. int pico_set_times(uint32_t* times, uint32_t size)
  481. {
  482. sndbuf[0] = 0xAA;
  483. sndbuf[1] = 0x08;
  484. memcpy(sndbuf + 2, &size, sizeof(uint32_t));
  485. memcpy(sndbuf + 6, times, size * sizeof(uint32_t));
  486. if(socket->Send(sndbuf, size * sizeof(uint32_t) + 6) == -1)
  487. {
  488. cerr << "Failed to send data to socket" << endl;
  489. return -1;
  490. }
  491. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  492. {
  493. cerr << "Failed to receive data from socket" << endl;
  494. return -1;
  495. }
  496. uint32_t bytes_received = socket->GetBytesReceived();
  497. if(bytes_received == 0)
  498. {
  499. cerr << "Server disconnected" << endl;
  500. return -1;
  501. }
  502. if(bytes_received < 2)
  503. {
  504. cerr << "Invalid buffer size" << endl;
  505. return -1;
  506. }
  507. if(rcvbuf[0] != 0xAA)
  508. {
  509. cerr << "Invalid magic number" << endl;
  510. return -1;
  511. }
  512. if(rcvbuf[1] == 0xFF)
  513. {
  514. if(bytes_received < 7)
  515. {
  516. cerr << "Invalid buffer size" << endl;
  517. return -1;
  518. }
  519. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  520. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  521. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  522. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  523. }
  524. else if(rcvbuf[1] != 0xC8)
  525. {
  526. cerr << "Invalid command callback" << endl;
  527. return -1;
  528. }
  529. cout << "Pico device times set" << endl;
  530. return 0;
  531. }
  532. int pico_configure_channels(const uint32_t number_channels, uint8_t trigger_channel, int32_t direction, uint16_t threshold, int16_t autoTrigger_ms)
  533. {
  534. sndbuf[0] = 0xAA;
  535. sndbuf[1] = 0x09;
  536. memcpy(sndbuf + 2, &number_channels, sizeof(uint32_t));
  537. memcpy(sndbuf + 6, &trigger_channel, sizeof(uint8_t));
  538. memcpy(sndbuf + 7, &direction, sizeof(int32_t));
  539. memcpy(sndbuf + 11, &threshold, sizeof(uint16_t));
  540. memcpy(sndbuf + 13, &autoTrigger_ms, sizeof(int16_t));
  541. if(socket->Send(sndbuf, 15) == -1)
  542. {
  543. cerr << "Failed to send data to socket" << endl;
  544. return -1;
  545. }
  546. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  547. {
  548. cerr << "Failed to receive data from socket" << endl;
  549. return -1;
  550. }
  551. uint32_t bytes_received = socket->GetBytesReceived();
  552. if(bytes_received == 0)
  553. {
  554. cerr << "Server disconnected" << endl;
  555. return -1;
  556. }
  557. if(bytes_received < 2)
  558. {
  559. cerr << "Invalid buffer size" << endl;
  560. return -1;
  561. }
  562. if(rcvbuf[0] != 0xAA)
  563. {
  564. cerr << "Invalid magic number" << endl;
  565. return -1;
  566. }
  567. if(rcvbuf[1] == 0xFF)
  568. {
  569. if(bytes_received < 7)
  570. {
  571. cerr << "Invalid buffer size" << endl;
  572. return -1;
  573. }
  574. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  575. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  576. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  577. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  578. }
  579. else if(rcvbuf[1] != 0xC9)
  580. {
  581. cerr << "Invalid command callback" << endl;
  582. return -1;
  583. }
  584. cout << "Pico device channels configured" << endl;
  585. return 0;
  586. }
  587. int pico_begin_measurement()
  588. {
  589. sndbuf[0] = 0xAA;
  590. sndbuf[1] = 0x0A;
  591. if(socket->Send(sndbuf, 2) == -1)
  592. {
  593. cerr << "Failed to send data to socket" << endl;
  594. return -1;
  595. }
  596. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  597. {
  598. cerr << "Failed to receive data from socket" << endl;
  599. return -1;
  600. }
  601. uint32_t bytes_received = socket->GetBytesReceived();
  602. if(bytes_received == 0)
  603. {
  604. cerr << "Server disconnected" << endl;
  605. return -1;
  606. }
  607. if(bytes_received < 2)
  608. {
  609. cerr << "Invalid buffer size" << endl;
  610. return -1;
  611. }
  612. if(rcvbuf[0] != 0xAA)
  613. {
  614. cerr << "Invalid magic number" << endl;
  615. return -1;
  616. }
  617. if(rcvbuf[1] == 0xFF)
  618. {
  619. if(bytes_received < 7)
  620. {
  621. cerr << "Invalid buffer size" << endl;
  622. return -1;
  623. }
  624. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  625. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  626. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  627. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  628. }
  629. else if(rcvbuf[1] != 0xCA)
  630. {
  631. cout << "Invalid command callback" << endl;
  632. }
  633. cout << "Pico device measurement started" << endl;
  634. cout << "Measurement in progress..." << endl;
  635. while(rcvbuf[1] != 0xCB)
  636. {
  637. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  638. {
  639. cerr << "Failed to receive data from socket" << endl;
  640. return -1;
  641. }
  642. bytes_received = socket->GetBytesReceived();
  643. if(bytes_received == 0)
  644. {
  645. cerr << "Server disconnected" << endl;
  646. return -1;
  647. }
  648. if(bytes_received < 2)
  649. {
  650. cerr << "Invalid buffer size" << endl;
  651. return -1;
  652. }
  653. if(rcvbuf[0] != 0xAA)
  654. {
  655. cerr << "Invalid magic number" << endl;
  656. return -1;
  657. }
  658. if(rcvbuf[1] == 0xFF)
  659. {
  660. if(bytes_received < 7)
  661. {
  662. cerr << "Invalid buffer size" << endl;
  663. return -1;
  664. }
  665. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  666. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  667. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  668. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  669. }
  670. else if(rcvbuf[1] != 0xCA)
  671. {
  672. cerr << "Invalid command callback" << endl;
  673. return -1;
  674. }
  675. cout << "Measurement in progress..." << endl;
  676. }
  677. cout << "Pico device measurement completed" << endl;
  678. return 0;
  679. }
  680. int pico_stop()
  681. {
  682. sndbuf[0] = 0xAA;
  683. sndbuf[1] = 0x0E;
  684. if(socket->Send(sndbuf, 2) == -1)
  685. {
  686. cerr << "Failed to send data to socket" << endl;
  687. return -1;
  688. }
  689. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  690. {
  691. cerr << "Failed to receive data from socket" << endl;
  692. return -1;
  693. }
  694. uint32_t bytes_received = socket->GetBytesReceived();
  695. if(bytes_received == 0)
  696. {
  697. cerr << "Server disconnected" << endl;
  698. return -1;
  699. }
  700. if(bytes_received < 2)
  701. {
  702. cerr << "Invalid buffer size" << endl;
  703. return -1;
  704. }
  705. if(rcvbuf[0] != 0xAA)
  706. {
  707. cerr << "Invalid magic number" << endl;
  708. return -1;
  709. }
  710. if(rcvbuf[1] == 0xFF)
  711. {
  712. if(bytes_received < 7)
  713. {
  714. cerr << "Invalid buffer size" << endl;
  715. return -1;
  716. }
  717. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  718. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  719. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  720. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  721. }
  722. else if(rcvbuf[1] != 0xCE)
  723. {
  724. cerr << "Invalid command callback" << endl;
  725. return -1;
  726. }
  727. cout << "Pico device measurement stopped" << endl;
  728. return 0;
  729. }
  730. int pico_exit()
  731. {
  732. sndbuf[0] = 0xAA;
  733. sndbuf[1] = 0x0D;
  734. if(socket->Send(sndbuf, 2) == -1)
  735. {
  736. cerr << "Failed to send data to socket" << endl;
  737. return -1;
  738. }
  739. if(socket->Receive(rcvbuf, sizeof(rcvbuf)) == -1)
  740. {
  741. cerr << "Failed to receive data from socket" << endl;
  742. return -1;
  743. }
  744. uint32_t bytes_received = socket->GetBytesReceived();
  745. if(bytes_received == 0)
  746. {
  747. cerr << "Server disconnected" << endl;
  748. return -1;
  749. }
  750. if(bytes_received < 2)
  751. {
  752. cerr << "Invalid buffer size" << endl;
  753. return -1;
  754. }
  755. if(rcvbuf[0] != 0xAA)
  756. {
  757. cerr << "Invalid magic number" << endl;
  758. return -1;
  759. }
  760. if(rcvbuf[1] == 0xFF)
  761. {
  762. if(bytes_received < 7)
  763. {
  764. cerr << "Invalid buffer size" << endl;
  765. return -1;
  766. }
  767. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  768. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  769. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  770. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  771. }
  772. else if(rcvbuf[1] != 0xCD)
  773. {
  774. cerr << "Invalid command callback" << endl;
  775. return -1;
  776. }
  777. cout << "Pico device exited" << endl;
  778. return 0;
  779. }
  780. int get_command()
  781. {
  782. if(socket->Receive(sndbuf, sizeof(sndbuf)) != 0)
  783. {
  784. std::getline(std::cin, line_edit);
  785. tokens = split(line_edit, " ");
  786. if(tokens.size() < 1)
  787. {
  788. cerr << "Invalid command" << endl;
  789. return -1;
  790. }
  791. if(tokens[0] = "open")
  792. {
  793. if(pico_open() != 0)
  794. {
  795. cerr << "Failed to open pico device" << endl;
  796. return -1;
  797. }
  798. }
  799. else if(tokens[0] == "xml_config")
  800. {
  801. if(tokens.size() < 2)
  802. {
  803. cerr << "Invalid command" << endl;
  804. return -1;
  805. }
  806. if(pico_xml_config(tokens[1]) != 0)
  807. {
  808. cerr << "Failed to configure pico device" << endl;
  809. return -1;
  810. }
  811. }
  812. else if(tokens[0] == "set_params")
  813. {
  814. if(tokens.size() < 5)
  815. {
  816. cerr << "Invalid command" << endl;
  817. return -1;
  818. }
  819. std::vector<uint32_t> points;
  820. std::vector<uint32_t> times;
  821. uint32_t sample_rate = std::stoi(tokens[2]);
  822. uint32_t number_channels = std::stoi(tokens[3]);
  823. uint32_t size = std::stoi(tokens[4]);
  824. points = string_to_vector(points);
  825. times = string_to_vector(times);
  826. if(points.size() != size || times.size() != size)
  827. {
  828. cerr << "Invalid data size" << endl;
  829. return -1;
  830. }
  831. if(pico_set_params(points.data(), times.data(), sample_rate, number_channels, size) != 0)
  832. {
  833. cerr << "Failed to set pico device parameters" << endl;
  834. return -1;
  835. }
  836. return 0;
  837. }
  838. else if(tokens[0] == "close")
  839. {
  840. if(pico_close() != 0)
  841. {
  842. cerr << "Failed to close pico device" << endl;
  843. return -1;
  844. }
  845. }
  846. else if(tokens[0] == "exit")
  847. {
  848. if(pico_exit() != 0)
  849. {
  850. cerr << "Failed to exit pico device" << endl;
  851. return -1;
  852. }
  853. return 1;
  854. }
  855. else if(tokens[0] == "get_current_params")
  856. {
  857. if(pico_get_current_params() != 0)
  858. {
  859. cerr << "Failed to get pico device parameters" << endl;
  860. return -1;
  861. }
  862. }
  863. else if(tokens[0] == "probe")
  864. {
  865. if(pico_probe() != 0)
  866. {
  867. cerr << "Failed to probe pico device" << endl;
  868. return -1;
  869. }
  870. }
  871. else if(tokens[0] == "set_points")
  872. {
  873. if(tokens.size() < 3)
  874. {
  875. cerr << "Invalid command" << endl;
  876. return -1;
  877. }
  878. uint32_t size = std::stoi(tokens[2]);
  879. std::vector<uint32_t> points = string_to_vector(points);
  880. if(points.size() != size)
  881. {
  882. cerr << "Invalid data size" << endl;
  883. return -1;
  884. }
  885. if(pico_set_points(points.data(), size) != 0)
  886. {
  887. cerr << "Failed to set pico device points" << endl;
  888. return -1;
  889. }
  890. }
  891. else if(tokens[0] == "set_sample_rate")
  892. {
  893. if(tokens.size() < 2)
  894. {
  895. cerr << "Invalid command" << endl;
  896. return -1;
  897. }
  898. uint32_t sample_rate = std::stoi(tokens[1]);
  899. if(pico_set_sample_rate(sample_rate) != 0)
  900. {
  901. cerr << "Failed to set pico device sample rate" << endl;
  902. return -1;
  903. }
  904. }
  905. else if(tokens[0] == "set_times")
  906. {
  907. if(tokens.size() < 3)
  908. {
  909. cerr << "Invalid command" << endl;
  910. return -1;
  911. }
  912. uint32_t size = std::stoi(tokens[2]);
  913. std::vector<uint32_t> times = string_to_vector(times);
  914. if(times.size() != size)
  915. {
  916. cerr << "Invalid data size" << endl;
  917. return -1;
  918. }
  919. if(pico_set_times(times.data(), size) != 0)
  920. {
  921. cerr << "Failed to set pico device times" << endl;
  922. return -1;
  923. }
  924. }
  925. else if(tokens[0] == "configure_channels")
  926. {
  927. if(tokens.size() < 6)
  928. {
  929. cerr << "Invalid command" << endl;
  930. return -1;
  931. }
  932. uint32_t number_channels = std::stoi(tokens[1]); // number of channels (1-8)
  933. uint8_t trigger_channel = std::stoi(tokens[2]); // trigger channel (0-7, 0 = channel A, 1 = channel B, ...)
  934. int32_t direction = std::stoi(tokens[3]); // trigger direction (0 = rising, 1 = falling)
  935. uint16_t threshold = std::stoi(tokens[4]); // trigger threshold (0-65535)
  936. int16_t autoTrigger_ms = std::stoi(tokens[5]); // auto trigger time (0-65535 ms)
  937. if(pico_configure_channels(number_channels, trigger_channel, direction, threshold, autoTrigger_ms) != 0)
  938. {
  939. cerr << "Failed to configure pico device channels" << endl;
  940. return -1;
  941. }
  942. }
  943. else if(tokens[0] == "begin_measurement")
  944. {
  945. if(pico_begin_measurement() != 0)
  946. {
  947. cerr << "Failed to begin pico device measurement" << endl;
  948. return -1;
  949. }
  950. }
  951. else if(tokens[0] == "stop")
  952. {
  953. if(pico_stop() != 0)
  954. {
  955. cerr << "Failed to stop pico device measurement" << endl;
  956. return -1;
  957. }
  958. }
  959. else if(tokens[0] == "help")
  960. {
  961. cout << "Available commands:" << endl;
  962. cout << "open" << endl;
  963. cout << "xml_config <filename>" << endl;
  964. cout << "set_params <points> <times> <sample_rate> <number_channels> <size>" << endl;
  965. cout << "close" << endl;
  966. cout << "exit" << endl;
  967. cout << "get_current_params" << endl;
  968. cout << "probe" << endl;
  969. cout << "set_points <points> <size>" << endl;
  970. cout << "set_sample_rate <sample_rate>" << endl;
  971. cout << "set_times <times> <size>" << endl;
  972. cout << "configure_channels <number_channels> <trigger_channel> <direction> <threshold> <autoTrigger_ms>" << endl;
  973. cout << "begin_measurement" << endl;
  974. cout << "stop" << endl;
  975. }
  976. else if(tokens[0] == "test")
  977. {
  978. cout << "Test command received" << endl;
  979. }
  980. else
  981. {
  982. cerr << "Unknown command" << endl;
  983. return -1;
  984. }
  985. }
  986. return 0;
  987. }
  988. }
  989. int main()
  990. {
  991. cout << "Open socket" << endl;
  992. CPassiveSocket SocketPassive(CSimpleSocket::CSocketType::SocketTypeTcp);
  993. if (!SocketPassive.Initialize())
  994. {
  995. cerr << "Socket initialization failed" << endl;
  996. return -1;
  997. }
  998. if(!SocketPassive.Listen("localhost", 5002))
  999. {
  1000. cerr << "Socket listening failed" << endl;
  1001. return -1;
  1002. }
  1003. CActiveSocket* ClientSocket;
  1004. while(true)
  1005. {
  1006. if ((ClientSocket = SocketPassive.Accept()) != nullptr)
  1007. {
  1008. cerr << "Socket accept failed" << endl;
  1009. return -1;
  1010. }
  1011. cout << LogPref::Flag(INFO) << "Client connected" << endl;
  1012. pico_service = new PicoLocalService(ClientSocket);
  1013. while(pico_service->get_request() != 0)
  1014. {
  1015. cout << LogPref::Flag(INFO) << "Request received" << endl;
  1016. }
  1017. cout << LogPref::Flag(INFO) << "Request processing finished" << endl;
  1018. delete pico_service;
  1019. pico_service = nullptr;
  1020. delete ClientSocket;
  1021. ClientSocket = nullptr;
  1022. }
  1023. return 0;
  1024. }