main.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. points = string_to_vector(tokens[1]);
  839. times = string_to_vector(tokens[2]);
  840. uint32_t sample_rate = std::stoi(tokens[3]);
  841. uint32_t number_channels = std::stoi(tokens[4]);
  842. uint32_t size = std::stoi(tokens[5]);
  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. }