main.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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 "parser.hpp"
  9. #include "src/ActiveSocket.h"
  10. #define MAX_PACKET 4096
  11. using namespace std;
  12. std::vector<std::string> split(const std::string& s, const std::string& delimiter) {
  13. std::vector<std::string> tokens;
  14. size_t pos = 0;
  15. std::string temp = s;
  16. std::string token;
  17. while ((pos = temp.find(delimiter)) != std::string::npos) {
  18. token = temp.substr(0, pos);
  19. tokens.push_back(token);
  20. temp.erase(0, pos + delimiter.length());
  21. }
  22. tokens.push_back(temp);
  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(MAX_PACKET, 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(MAX_PACKET, 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(MAX_PACKET, 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(MAX_PACKET, 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(MAX_PACKET, 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 / sizeof(uint32_t)];
  306. memcpy(apoints, rcvbuf + 6, size);
  307. for(uint32_t i = 0; i < size / sizeof(uint32_t); ++i)
  308. {
  309. cout << "apoints[" << i << "] = " << std::dec << apoints[i] << endl;
  310. }
  311. uint32_t* atimes = new uint32_t[size / sizeof(uint32_t)];
  312. memcpy(atimes, rcvbuf + 6 + size, size);
  313. for(uint32_t i = 0; i < size / sizeof(uint32_t); ++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));
  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));
  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(MAX_PACKET, 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(MAX_PACKET, 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(MAX_PACKET, 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(MAX_PACKET, 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(MAX_PACKET, 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(MAX_PACKET, 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. bool end_of_data = false;
  636. while(!end_of_data)
  637. {
  638. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  639. {
  640. cerr << "Failed to receive data from socket" << endl;
  641. return -1;
  642. }
  643. bytes_received = socket->GetBytesReceived();
  644. if(bytes_received == 0)
  645. {
  646. cerr << "Server disconnected" << endl;
  647. return -1;
  648. }
  649. if(bytes_received < 2)
  650. {
  651. cerr << "Invalid buffer size" << endl;
  652. return -1;
  653. }
  654. if(rcvbuf[0] != 0xAA)
  655. {
  656. cerr << "Invalid magic number" << endl;
  657. return -1;
  658. }
  659. if(rcvbuf[1] == 0xFF)
  660. {
  661. if(bytes_received < 7)
  662. {
  663. cerr << "Invalid buffer size" << endl;
  664. return -1;
  665. }
  666. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  667. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  668. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  669. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  670. }
  671. if(rcvbuf[1] == 0xCB)
  672. {
  673. cout << "Measurement completed..." << endl;
  674. end_of_data = true;
  675. }
  676. else if(rcvbuf[1] = 0xCA)
  677. {
  678. cout << "Measurement in progress..." << endl;
  679. }
  680. else
  681. {
  682. cerr << "Invalid command callback" << endl;
  683. return -1;
  684. }
  685. cout << "Measurement in progress..." << endl;
  686. }
  687. cout << "Pico device measurement completed" << endl;
  688. return 0;
  689. }
  690. int pico_stop()
  691. {
  692. sndbuf[0] = 0xAA;
  693. sndbuf[1] = 0x0E;
  694. if(socket->Send(sndbuf, 2) == -1)
  695. {
  696. cerr << "Failed to send data to socket" << endl;
  697. return -1;
  698. }
  699. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  700. {
  701. cerr << "Failed to receive data from socket" << endl;
  702. return -1;
  703. }
  704. uint32_t bytes_received = socket->GetBytesReceived();
  705. if(bytes_received == 0)
  706. {
  707. cerr << "Server disconnected" << endl;
  708. return -1;
  709. }
  710. if(bytes_received < 2)
  711. {
  712. cerr << "Invalid buffer size" << endl;
  713. return -1;
  714. }
  715. if(rcvbuf[0] != 0xAA)
  716. {
  717. cerr << "Invalid magic number" << endl;
  718. return -1;
  719. }
  720. if(rcvbuf[1] == 0xFF)
  721. {
  722. if(bytes_received < 7)
  723. {
  724. cerr << "Invalid buffer size" << endl;
  725. return -1;
  726. }
  727. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  728. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  729. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  730. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  731. }
  732. else if(rcvbuf[1] != 0xCE)
  733. {
  734. cerr << "Invalid command callback" << endl;
  735. return -1;
  736. }
  737. cout << "Pico device measurement stopped" << endl;
  738. return 0;
  739. }
  740. int pico_exit()
  741. {
  742. sndbuf[0] = 0xAA;
  743. sndbuf[1] = 0x0D;
  744. if(socket->Send(sndbuf, 2) == -1)
  745. {
  746. cerr << "Failed to send data to socket" << endl;
  747. return -1;
  748. }
  749. if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
  750. {
  751. cerr << "Failed to receive data from socket" << endl;
  752. return -1;
  753. }
  754. uint32_t bytes_received = socket->GetBytesReceived();
  755. if(bytes_received == 0)
  756. {
  757. cerr << "Server disconnected" << endl;
  758. return -1;
  759. }
  760. if(bytes_received < 2)
  761. {
  762. cerr << "Invalid buffer size" << endl;
  763. return -1;
  764. }
  765. if(rcvbuf[0] != 0xAA)
  766. {
  767. cerr << "Invalid magic number" << endl;
  768. return -1;
  769. }
  770. if(rcvbuf[1] == 0xFF)
  771. {
  772. if(bytes_received < 7)
  773. {
  774. cerr << "Invalid buffer size" << endl;
  775. return -1;
  776. }
  777. cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
  778. memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
  779. memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
  780. cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
  781. }
  782. else if(rcvbuf[1] != 0xCD)
  783. {
  784. cerr << "Invalid command callback" << endl;
  785. return -1;
  786. }
  787. cout << "Pico device exited" << endl;
  788. return 0;
  789. }
  790. int get_command()
  791. {
  792. if(true)
  793. {
  794. cout << "cmd$ ";
  795. std::getline(std::cin, line_edit);
  796. cout << endl;
  797. tokens = split(line_edit, " ");
  798. if(tokens.size() < 1)
  799. {
  800. cerr << "Invalid command" << endl;
  801. cout << endl;
  802. return -1;
  803. }
  804. if(tokens[0] == "open")
  805. {
  806. if(pico_open() != 0)
  807. {
  808. cerr << "Failed to open pico device" << endl;
  809. cout << endl;
  810. return -1;
  811. }
  812. }
  813. else if(tokens[0] == "xml_config")
  814. {
  815. if(tokens.size() < 2)
  816. {
  817. cerr << "Invalid command" << endl;
  818. cout << endl;
  819. return -1;
  820. }
  821. if(pico_xml_config(tokens[1]) != 0)
  822. {
  823. cerr << "Failed to configure pico device" << endl;
  824. cout << endl;
  825. return -1;
  826. }
  827. }
  828. else if(tokens[0] == "set_params")
  829. {
  830. if(tokens.size() < 5)
  831. {
  832. cerr << "Invalid command" << endl;
  833. cout << endl;
  834. return -1;
  835. }
  836. std::vector<uint32_t> points;
  837. std::vector<uint32_t> times;
  838. uint32_t sample_rate = std::stoi(tokens[2]);
  839. uint32_t number_channels = std::stoi(tokens[3]);
  840. uint32_t size = std::stoi(tokens[4]);
  841. points = string_to_vector(tokens[5]);
  842. times = string_to_vector(tokens[6]);
  843. if(points.size() != size || times.size() != size)
  844. {
  845. cerr << "Invalid data size" << endl;
  846. cout << endl;
  847. return -1;
  848. }
  849. if(pico_set_params(points.data(), times.data(), sample_rate, number_channels, size) != 0)
  850. {
  851. cerr << "Failed to set pico device parameters" << endl;
  852. cout << endl;
  853. return -1;
  854. }
  855. return 0;
  856. }
  857. else if(tokens[0] == "close")
  858. {
  859. if(pico_close() != 0)
  860. {
  861. cerr << "Failed to close pico device" << endl;
  862. cout << endl;
  863. return -1;
  864. }
  865. }
  866. else if(tokens[0] == "exit")
  867. {
  868. if(pico_exit() != 0)
  869. {
  870. cerr << "Failed to exit pico device" << endl;
  871. cout << endl;
  872. return -1;
  873. }
  874. return 1;
  875. }
  876. else if(tokens[0] == "get_current_params")
  877. {
  878. if(pico_get_current_params() != 0)
  879. {
  880. cerr << "Failed to get pico device parameters" << endl;
  881. cout << endl;
  882. return -1;
  883. }
  884. }
  885. else if(tokens[0] == "probe")
  886. {
  887. if(pico_probe() != 0)
  888. {
  889. cerr << "Failed to probe pico device" << endl;
  890. cout << endl;
  891. return -1;
  892. }
  893. }
  894. else if(tokens[0] == "set_points")
  895. {
  896. if(tokens.size() < 3)
  897. {
  898. cerr << "Invalid command" << endl;
  899. cout << endl;
  900. return -1;
  901. }
  902. uint32_t size = std::stoi(tokens[1]);
  903. std::vector<uint32_t> points = string_to_vector(tokens[2]);
  904. if(points.size() != size)
  905. {
  906. cerr << "Invalid data size" << endl;
  907. cout << endl;
  908. return -1;
  909. }
  910. if(pico_set_points(points.data(), size) != 0)
  911. {
  912. cerr << "Failed to set pico device points" << endl;
  913. cout << endl;
  914. return -1;
  915. }
  916. }
  917. else if(tokens[0] == "set_sample_rate")
  918. {
  919. if(tokens.size() < 2)
  920. {
  921. cerr << "Invalid command" << endl;
  922. cout << endl;
  923. return -1;
  924. }
  925. uint32_t sample_rate = std::stoi(tokens[1]);
  926. if(pico_set_sample_rate(sample_rate) != 0)
  927. {
  928. cerr << "Failed to set pico device sample rate" << endl;
  929. cout << endl;
  930. return -1;
  931. }
  932. }
  933. else if(tokens[0] == "set_times")
  934. {
  935. if(tokens.size() < 3)
  936. {
  937. cerr << "Invalid command" << endl;
  938. cout << endl;
  939. return -1;
  940. }
  941. uint32_t size = std::stoi(tokens[1]);
  942. std::vector<uint32_t> times = string_to_vector(tokens[2]);
  943. if(times.size() != size)
  944. {
  945. cerr << "Invalid data size" << endl;
  946. cout << endl;
  947. return -1;
  948. }
  949. if(pico_set_times(times.data(), size) != 0)
  950. {
  951. cerr << "Failed to set pico device times" << endl;
  952. cout << endl;
  953. return -1;
  954. }
  955. }
  956. else if(tokens[0] == "configure_channels")
  957. {
  958. if(tokens.size() < 6)
  959. {
  960. cerr << "Invalid command" << endl;
  961. cout << endl;
  962. return -1;
  963. }
  964. uint32_t number_channels = std::stoi(tokens[1]); // number of channels (1-8)
  965. uint8_t trigger_channel = std::stoi(tokens[2]); // trigger channel (0-7, 0 = channel A, 1 = channel B, ...)
  966. int32_t direction = std::stoi(tokens[3]); // trigger direction (0 = rising, 1 = falling)
  967. uint16_t threshold = std::stoi(tokens[4]); // trigger threshold (0-65535)
  968. int16_t autoTrigger_ms = std::stoi(tokens[5]); // auto trigger time (0-65535 ms)
  969. if(pico_configure_channels(number_channels, trigger_channel, direction, threshold, autoTrigger_ms) != 0)
  970. {
  971. cerr << "Failed to configure pico device channels" << endl;
  972. cout << endl;
  973. return -1;
  974. }
  975. }
  976. else if(tokens[0] == "begin_measurement")
  977. {
  978. if(pico_begin_measurement() != 0)
  979. {
  980. cerr << "Failed to begin pico device measurement" << endl;
  981. cout << endl;
  982. return -1;
  983. }
  984. }
  985. else if(tokens[0] == "stop")
  986. {
  987. if(pico_stop() != 0)
  988. {
  989. cerr << "Failed to stop pico device measurement" << endl;
  990. cout << endl;
  991. return -1;
  992. }
  993. }
  994. else if(tokens[0] == "help")
  995. {
  996. cout << "Available commands:" << endl;
  997. cout << "open" << endl;
  998. cout << "xml_config <filename>" << endl;
  999. cout << "set_params <points> <times> <sample_rate> <number_channels> <size>" << endl;
  1000. cout << "close" << endl;
  1001. cout << "exit" << endl;
  1002. cout << "get_current_params" << endl;
  1003. cout << "probe" << endl;
  1004. cout << "set_points <points> <size>" << endl;
  1005. cout << "set_sample_rate <sample_rate>" << endl;
  1006. cout << "set_times <times> <size>" << endl;
  1007. cout << "configure_channels <number_channels> <trigger_channel> <direction> <threshold> <autoTrigger_ms>" << endl;
  1008. cout << "begin_measurement" << endl;
  1009. cout << "stop" << endl;
  1010. }
  1011. else if(tokens[0] == "test")
  1012. {
  1013. cout << "Test command received" << endl;
  1014. }
  1015. else
  1016. {
  1017. cerr << "Unknown command" << endl;
  1018. cout << endl;
  1019. return -1;
  1020. }
  1021. }
  1022. cout << endl;
  1023. return 0;
  1024. }
  1025. };
  1026. int main()
  1027. {
  1028. cout << "Open socket" << endl;
  1029. CActiveSocket SocketActive(CSimpleSocket::CSocketType::SocketTypeTcp);
  1030. if (!SocketActive.Initialize())
  1031. {
  1032. cerr << "Socket initialization failed" << endl;
  1033. return -1;
  1034. }
  1035. if(!SocketActive.Open("127.0.0.1", 5003))
  1036. {
  1037. cerr << "Socket opening failed" << endl;
  1038. return -1;
  1039. }
  1040. PicoLocalClient pico_client(&SocketActive);
  1041. while(pico_client.get_command() != 1) {
  1042. // empty
  1043. }
  1044. return 0;
  1045. }