main.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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 "simplelogger.hpp"
  9. #include "parser.hpp"
  10. #include "src/PassiveSocket.h"
  11. #define MAX_PACKET 4096
  12. using namespace std;
  13. extern ostream out(cout.rdbuf());
  14. extern SimpleLogger logger(out, "pico", "picologs");
  15. class PicoLocalService
  16. {
  17. private:
  18. CActiveSocket* SocketActive;
  19. const static uint8_t magic_number = 0xAA;
  20. int16_t handle = 0;
  21. uint8_t last_command = 0x00;
  22. uint32_t error_code = 0x00000000;
  23. int32_t points_value = 0;
  24. uint32_t sample_rate = 0;
  25. std::vector<uint32_t> times;
  26. uint32_t number_channels = 0;
  27. std::vector<PS4000A_CHANNEL> channels;
  28. std::vector<uint32_t> points_vector;
  29. PS4000A_CONDITION* conditions;
  30. PS4000A_CHANNEL trig_channel = PS4000A_CHANNEL_A;
  31. PS4000A_THRESHOLD_DIRECTION th_direction = PS4000A_ABOVE;
  32. int16_t threshold = 0;
  33. int16_t trig_delay = 0;
  34. int16_t trig_autoTrigger_ms = 0;
  35. uint32_t nbytes_sent = 0;
  36. std::vector<std::vector<int16_t*>> data_buffer_vec;
  37. uint8_t rcbuf[MAX_PACKET]{0};
  38. uint8_t sndbuf[MAX_PACKET]{0};
  39. public:
  40. PicoLocalService(CActiveSocket* socket) : SocketActive(socket) {}
  41. ~PicoLocalService() {}
  42. int get_request() {
  43. // Check if buffer has at least 2 bytes for magic number and command
  44. // Receive data from socket
  45. int bytes_recieved = SocketActive->Receive(MAX_PACKET, rcbuf);
  46. if(bytes_recieved == 0)
  47. {
  48. logger << LogPref::Flag(ERROR) << "Client disconnected" << endl;
  49. pico_stop();
  50. pico_close();
  51. return 0;
  52. }
  53. if(bytes_recieved < 2)
  54. {
  55. logger << LogPref::Flag(ERROR) << "Invalid buffer size" << endl;
  56. sndbuf[0] = magic_number;
  57. sndbuf[1] = 0xFF; // Error flag
  58. sndbuf[2] = 0x01; // Error type
  59. this->error_code = 0x00000000; // No pico error
  60. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  61. if(SocketActive->Send(sndbuf, 7) == -1) {
  62. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  63. }
  64. return -1;
  65. }
  66. // Extract magic number from buffer
  67. uint8_t magic;
  68. memcpy(&magic, rcbuf, sizeof(uint8_t));
  69. // Check if magic number is correct
  70. if (magic != this->magic_number) {
  71. logger << LogPref::Flag(ERROR) << "Invalid magic number: " << std::hex << magic << endl;
  72. sndbuf[0] = magic_number;
  73. sndbuf[1] = 0xFF;
  74. sndbuf[2] = 0x03;
  75. this->error_code = 0x00000000;
  76. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  77. if(SocketActive->Send(sndbuf, 7) == -1) {
  78. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  79. }
  80. return -1;
  81. }
  82. // Extract command from buffer
  83. uint8_t cmd;
  84. memcpy(&cmd, rcbuf + 1, sizeof(uint8_t));
  85. // Check if command is correct
  86. switch(cmd)
  87. {
  88. case 0x01:
  89. {
  90. this->last_command = 0x01;
  91. logger << LogPref::Flag(INFO) << "Command 0x01 received" << endl;
  92. pico_open();
  93. logger << LogPref::Flag(INFO) << "Pico device opened" << endl;
  94. break;
  95. }
  96. case 0x02:
  97. {
  98. this->last_command = 0x02;
  99. logger << LogPref::Flag(INFO) << "Command 0x02 received" << endl;
  100. if(bytes_recieved < 7) {
  101. logger << LogPref::Flag(ERROR) << "Invalid buffer size" << endl;
  102. sndbuf[0] = magic_number;
  103. sndbuf[1] = 0xFF;
  104. sndbuf[2] = 0x01;
  105. this->error_code = 0x00000000;
  106. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  107. if(SocketActive->Send(sndbuf, 7) == -1) {
  108. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  109. }
  110. return -1;
  111. }
  112. uint32_t size;
  113. memcpy(&size, rcbuf + 2, sizeof(uint32_t));
  114. if(size < 1) {
  115. logger << LogPref::Flag(ERROR) << "Invalid data size" << endl;
  116. sndbuf[0] = magic_number;
  117. sndbuf[1] = 0xFF;
  118. sndbuf[2] = 0x02;
  119. this->error_code = 0x00000000;
  120. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  121. if(SocketActive->Send(sndbuf, 7) == -1) {
  122. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  123. }
  124. return -1;
  125. }
  126. char* file_name = new char[size + 1];
  127. memcpy(file_name, rcbuf + 6, size * sizeof(char));
  128. file_name[size] = '\0'; // Null-terminate the string
  129. std::string file_name_str(file_name);
  130. logger << LogPref::Flag(INFO) << "File name: " << file_name_str << endl;
  131. pico_xml_config(file_name_str);
  132. logger << LogPref::Flag(INFO) << "Pico device configured" << endl;
  133. delete[] file_name;
  134. break;
  135. }
  136. case 0x0C:
  137. {
  138. this->last_command = 0x0C;
  139. logger << LogPref::Flag(INFO) << "Command 0x0C received" << endl;
  140. if(bytes_recieved < 22) {
  141. logger << LogPref::Flag(ERROR) << "Invalid buffer size" << endl;
  142. return -1;
  143. sndbuf[0] = magic_number;
  144. sndbuf[1] = 0xFF;
  145. sndbuf[2] = 0x01;
  146. this->error_code = 0x00000000;
  147. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  148. if(SocketActive->Send(sndbuf, 7) == -1) {
  149. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  150. }
  151. return -1;
  152. }
  153. uint32_t size;
  154. memcpy(&size, rcbuf + 2, sizeof(uint32_t));
  155. if(size < 1) {
  156. logger << LogPref::Flag(ERROR) << "Invalid data size" << endl;
  157. sndbuf[0] = magic_number;
  158. sndbuf[1] = 0xFF;
  159. sndbuf[2] = 0x02;
  160. this->error_code = 0x00000000;
  161. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  162. if(SocketActive->Send(sndbuf, 7) == -1) {
  163. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  164. }
  165. return -1;
  166. }
  167. uint32_t* apoints = new uint32_t[size];
  168. memcpy(apoints, rcbuf + 6, size * sizeof(uint32_t));
  169. uint32_t* atimes = new uint32_t[size];
  170. memcpy(atimes, rcbuf + 6 + size * sizeof(uint32_t), size * sizeof(uint32_t));
  171. uint32_t sample_rate = 0;
  172. memcpy(&sample_rate, rcbuf + 6 + 2 * size * sizeof(uint32_t), sizeof(uint32_t));
  173. uint32_t number_channels = 0;
  174. memcpy(&number_channels, rcbuf + 10 + 2 * size * sizeof(uint32_t), sizeof(uint32_t));
  175. pico_set_params(apoints, atimes, sample_rate, number_channels, size);
  176. logger << LogPref::Flag(INFO) << "Pico device parameters set" << endl;
  177. delete[] apoints;
  178. delete[] atimes;
  179. break;
  180. }
  181. case 0x03:
  182. {
  183. this->last_command = 0x03;
  184. logger << LogPref::Flag(INFO) << "Command 0x03 received" << endl;
  185. pico_close();
  186. logger << LogPref::Flag(INFO) << "Pico device closed" << endl;
  187. break;
  188. }
  189. case 0x04:
  190. {
  191. // Check if buffer has enough data for version numbers
  192. this->last_command = 0x04;
  193. logger << LogPref::Flag(INFO) << "Command 0x04 received" << endl;
  194. pico_get_current_params();
  195. logger << LogPref::Flag(INFO) << "Pico device parameters retrieved" << endl;
  196. break;
  197. }
  198. case 0x05:
  199. {
  200. this->last_command = 0x05;
  201. logger << LogPref::Flag(INFO) << "Command 0x05 received" << endl;
  202. pico_probe();
  203. logger << LogPref::Flag(INFO) << "Pico device probed" << endl;
  204. break;
  205. }
  206. case 0x06:
  207. {
  208. this->last_command = 0x06;
  209. logger << LogPref::Flag(INFO) << "Command 0x06 received" << endl;
  210. if(bytes_recieved < 10) {
  211. logger << LogPref::Flag(ERROR) << "Invalid buffer size" << endl;
  212. sndbuf[0] = magic_number;
  213. sndbuf[1] = 0xFF;
  214. sndbuf[2] = 0x01;
  215. this->error_code = 0x00000000;
  216. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  217. if(SocketActive->Send(sndbuf, 7) == -1) {
  218. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  219. }
  220. return -1;
  221. }
  222. uint32_t size;
  223. memcpy(&size, rcbuf + 2, sizeof(uint32_t));
  224. if(size < 1) {
  225. logger << LogPref::Flag(ERROR) << "Invalid data size" << endl;
  226. sndbuf[0] = magic_number;
  227. sndbuf[1] = 0xFF;
  228. sndbuf[2] = 0x02;
  229. this->error_code = 0x00000000;
  230. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  231. if(SocketActive->Send(sndbuf, 7) == -1) {
  232. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  233. }
  234. return -1;
  235. }
  236. uint32_t* apoints = new uint32_t[size];
  237. memcpy(apoints, rcbuf + 6, size * sizeof(uint32_t));
  238. pico_set_points(apoints, size);
  239. logger << LogPref::Flag(INFO) << "Pico device points set" << endl;
  240. delete[] apoints;
  241. break;
  242. }
  243. case 0x07:
  244. {
  245. this->last_command = 0x07;
  246. logger << LogPref::Flag(INFO) << "Command 0x07 received" << endl;
  247. if(bytes_recieved < 6) {
  248. logger << LogPref::Flag(ERROR) << "Invalid buffer size" << endl;
  249. sndbuf[0] = magic_number;
  250. sndbuf[1] = 0xFF;
  251. sndbuf[2] = 0x01;
  252. this->error_code = 0x00000000;
  253. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  254. if(SocketActive->Send(sndbuf, 7) == -1) {
  255. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  256. }
  257. return -1;
  258. }
  259. uint32_t sample_rate = 0;
  260. memcpy(&sample_rate, rcbuf + 2, sizeof(uint32_t));
  261. pico_set_sample_rate(sample_rate);
  262. logger << LogPref::Flag(INFO) << "Pico device sample rate set" << endl;
  263. break;
  264. }
  265. case 0x08:
  266. {
  267. this->last_command = 0x08;
  268. logger << LogPref::Flag(INFO) << "Command 0x08 received" << endl;
  269. if(bytes_recieved < 10) {
  270. logger << LogPref::Flag(ERROR) << "Invalid buffer size" << endl;
  271. sndbuf[0] = magic_number;
  272. sndbuf[1] = 0xFF;
  273. sndbuf[2] = 0x01;
  274. this->error_code = 0x00000000;
  275. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  276. if(SocketActive->Send(sndbuf, 7) == -1) {
  277. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  278. }
  279. return -1;
  280. }
  281. uint32_t size;
  282. memcpy(&size, rcbuf + 2, sizeof(uint32_t));
  283. if(size < 1) {
  284. logger << LogPref::Flag(ERROR) << "Invalid data size" << endl;
  285. sndbuf[0] = magic_number;
  286. sndbuf[1] = 0xFF;
  287. sndbuf[2] = 0x02;
  288. this->error_code = 0x00000000;
  289. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  290. if(SocketActive->Send(sndbuf, 7) == -1) {
  291. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  292. }
  293. return -1;
  294. }
  295. uint32_t* atimes = new uint32_t[size];
  296. memcpy(atimes, rcbuf + 6, size * sizeof(uint32_t));
  297. pico_set_times(atimes, size);
  298. logger << LogPref::Flag(INFO) << "Pico device times set" << endl;
  299. delete[] atimes;
  300. break;
  301. }
  302. case 0x09:
  303. {
  304. this->last_command = 0x09;
  305. logger << LogPref::Flag(INFO) << "Command 0x09 received" << endl;
  306. if(bytes_recieved < 15) {
  307. logger << LogPref::Flag(ERROR) << "Invalid buffer size" << endl;
  308. sndbuf[0] = magic_number;
  309. sndbuf[1] = 0xFF;
  310. sndbuf[2] = 0x01;
  311. this->error_code = 0x00000000;
  312. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  313. if(SocketActive->Send(sndbuf, 7) == -1) {
  314. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  315. }
  316. return -1;
  317. }
  318. uint32_t channels = 0;
  319. memcpy(&channels, rcbuf + 2, sizeof(uint32_t));
  320. uint8_t trigger_channel = 0;
  321. memcpy(&trigger_channel, rcbuf + 6, sizeof(uint8_t));
  322. int32_t direction = 0;
  323. memcpy(&direction, rcbuf + 7, sizeof(int32_t));
  324. uint16_t threshold = 0;
  325. memcpy(&threshold, rcbuf + 11, sizeof(uint16_t));
  326. int16_t autoTrigger_ms = 0;
  327. memcpy(&autoTrigger_ms, rcbuf + 13, sizeof(int16_t));
  328. pico_configure_channels(channels, trigger_channel, direction, threshold, autoTrigger_ms);
  329. logger << LogPref::Flag(INFO) << "Pico device channels configured" << endl;
  330. break;
  331. }
  332. case 0x0A:
  333. {
  334. this->last_command = 0x0A;
  335. logger << LogPref::Flag(INFO) << "Command 0x0A received" << endl;
  336. pico_begin_measurement();
  337. logger << LogPref::Flag(INFO) << "Pico device measurement started" << endl;
  338. break;
  339. }
  340. case 0x0D:
  341. {
  342. this->last_command = 0x0D;
  343. logger << LogPref::Flag(INFO) << "Command 0x0B received" << endl;
  344. int ret = pico_exit();
  345. if(ret == 0) {
  346. logger << LogPref::Flag(INFO) << "Pico device exited successfully" << endl;
  347. return 0;
  348. } else {
  349. logger << LogPref::Flag(ERROR) << "Failed to exit Pico device" << endl;
  350. }
  351. break;
  352. }
  353. case 0x0E:
  354. {
  355. this->last_command = 0x0E;
  356. logger << LogPref::Flag(INFO) << "Command 0x0E received" << endl;
  357. pico_stop();
  358. logger << LogPref::Flag(INFO) << "Pico device stopped" << endl;
  359. break;
  360. }
  361. default:
  362. {
  363. this->last_command = 0xFF;
  364. logger << LogPref::Flag(ERROR) << "Invalid command: " << std::hex << cmd << endl;
  365. sndbuf[0] = magic_number;
  366. sndbuf[1] = 0xFF;
  367. sndbuf[2] = 0x00;
  368. this->error_code = 0x00000000;
  369. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  370. if(SocketActive->Send(sndbuf, 7) == -1) {
  371. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  372. }
  373. return -1;
  374. }
  375. }
  376. return 1;
  377. }
  378. int pico_open() {
  379. // Open socket and initialize Pico device
  380. auto retval = ps4000aOpenUnit(&handle, NULL);
  381. logger << LogPref::Flag(INFO) << "Pico device opened with handle: " << handle << endl;
  382. if (retval != 0) {
  383. logger << LogPref::Flag(ERROR) << "Failed to open Pico device. Code: " << retval << endl;
  384. sndbuf[0] = magic_number;
  385. sndbuf[1] = 0xFF;
  386. sndbuf[2] = 0x03;
  387. this->error_code = retval;
  388. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  389. if(SocketActive->Send(sndbuf, 7) == -1) {
  390. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  391. }
  392. return -1;
  393. }
  394. int16_t start{10};
  395. retval = ps4000aFlashLed(handle, start);
  396. logger << LogPref::Flag(INFO) << "Pico device LED flashed." << endl;
  397. if (retval != 0) {
  398. logger << LogPref::Flag(ERROR) << "Failed to flash Pico device LED. Code: " << retval << endl;
  399. sndbuf[0] = magic_number;
  400. sndbuf[1] = 0xFF;
  401. sndbuf[2] = 0x04;
  402. this->error_code = retval;
  403. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  404. if(SocketActive->Send(sndbuf, 7) == -1) {
  405. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  406. }
  407. return -1;
  408. }
  409. logger << LogPref::Flag(INFO) << "Pico device LED flashed." << endl;
  410. sndbuf[0] = magic_number;
  411. sndbuf[1] = 0xC1;
  412. if(SocketActive->Send(sndbuf, 2) == -1) {
  413. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  414. return -1;
  415. }
  416. logger << LogPref::Flag(INFO) << "Data sent to socket" << endl;
  417. return 0;
  418. }
  419. int pico_stop()
  420. {
  421. // Stop measurement and close Pico device
  422. auto retval = ps4000aStop(handle);
  423. if (retval != 0) {
  424. logger << LogPref::Flag(ERROR) << "Failed to stop Pico device. Code: " << retval << endl;
  425. sndbuf[0] = magic_number;
  426. sndbuf[1] = 0xFF;
  427. sndbuf[2] = 0x06;
  428. this->error_code = retval;
  429. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  430. if(SocketActive->Send(sndbuf, 7) == -1) {
  431. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  432. }
  433. return -1;
  434. }
  435. logger << LogPref::Flag(INFO) << "Pico device stopped" << endl;
  436. sndbuf[0] = magic_number;
  437. sndbuf[1] = 0xCE;
  438. if(SocketActive->Send(sndbuf, 2) == -1) {
  439. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  440. return -1;
  441. }
  442. logger << LogPref::Flag(INFO) << "Data sent to socket" << endl;
  443. return 0;
  444. }
  445. int pico_xml_config(const string& file_name) {
  446. // Load configuration from XML file
  447. auto data_set = parse_xml_function(file_name.c_str());
  448. points_vector = string_to_vector(std::get<0>(data_set));
  449. times = string_to_vector(std::get<2>(data_set));
  450. points_value = *(std::max_element(points_vector.begin(), points_vector.end()));
  451. number_channels = std::get<1>(data_set);
  452. sample_rate = std::get<3>(data_set);
  453. sndbuf[0] = magic_number;
  454. sndbuf[1] = 0xC2;
  455. if(SocketActive->Send(sndbuf, 2) == -1) {
  456. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  457. return -1;
  458. }
  459. logger << LogPref::Flag(INFO) << "Data sent to socket" << endl;
  460. return 0;
  461. }
  462. int pico_set_params(const uint32_t* points, const uint32_t* times, const uint32_t sample_rate, const uint32_t number_channels, const uint32_t size) {
  463. // Set parameters for measurement
  464. this->points_vector.assign(points, points + size);
  465. this->times.assign(times, times + size);
  466. this->sample_rate = sample_rate;
  467. this->number_channels = number_channels;
  468. points_value = *(std::max_element(points_vector.begin(), points_vector.end()));
  469. logger << LogPref::Flag(INFO) << "Parameters set" << endl;
  470. sndbuf[0] = magic_number;
  471. sndbuf[1] = 0xCC;
  472. if(SocketActive->Send(sndbuf, 2) == -1) {
  473. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  474. return -1;
  475. }
  476. return 0;
  477. }
  478. int pico_close() {
  479. // Close Pico device and socket
  480. auto retval = ps4000aCloseUnit(handle);
  481. if (retval != 0) {
  482. logger << LogPref::Flag(ERROR) << "Failed to close Pico device. Code: " << retval << endl;
  483. sndbuf[0] = magic_number;
  484. sndbuf[1] = 0xFF;
  485. sndbuf[2] = 0x05;
  486. this->error_code = retval;
  487. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  488. if(SocketActive->Send(sndbuf, 7) == -1) {
  489. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  490. }
  491. return -1;
  492. }
  493. logger << LogPref::Flag(INFO) << "Pico device closed" << endl;
  494. sndbuf[0] = magic_number;
  495. sndbuf[1] = 0xC3;
  496. if(SocketActive->Send(sndbuf, 2) == -1) {
  497. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  498. return -1;
  499. }
  500. logger << LogPref::Flag(INFO) << "Data sent to socket" << endl;
  501. return 0;
  502. }
  503. int pico_get_current_params() {
  504. // Get current parameters from Pico device
  505. logger << LogPref::Flag(INFO) << "Current parameters:" << endl;
  506. logger << "Points: " << points_value << endl;
  507. logger << "Times size: " << times.size() << endl;
  508. logger << "Sample rate: " << sample_rate << endl;
  509. logger << "Number of channels: " << number_channels << endl;
  510. logger << "Trigger channel: " << trig_channel << endl;
  511. logger << "Trigger direction: " << th_direction << endl;
  512. sndbuf[0] = magic_number;
  513. sndbuf[1] = 0xC4;
  514. uint32_t bsize = points_vector.size() * sizeof(uint32_t);
  515. uint32_t test = 0;
  516. memcpy(sndbuf + 2, &bsize, sizeof(uint32_t));
  517. memcpy(sndbuf + 6, points_vector.data(), bsize);
  518. memcpy(&test, points_vector.data(), bsize);
  519. logger << LogPref::Flag(INFO) << test << endl;
  520. memcpy(sndbuf + 6 + bsize, times.data(), bsize);
  521. logger << LogPref::Flag(INFO) << times.data()[0] << endl;
  522. memcpy(sndbuf + 6 + 2 * bsize, &sample_rate, sizeof(sample_rate));
  523. memcpy(sndbuf + 10 + 2 * bsize, &number_channels, sizeof(number_channels));
  524. if(SocketActive->Send(sndbuf, 10 + sizeof(points_vector.data()) + sizeof(times.data())) == -1) {
  525. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  526. return -1;
  527. }
  528. return 0;
  529. }
  530. int pico_probe() {
  531. // Probe signal from Pico device
  532. sndbuf[0] = magic_number;
  533. sndbuf[1] = 0xC5;
  534. if(SocketActive->Send(sndbuf, 2) == -1) {
  535. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  536. return -1;
  537. }
  538. logger << LogPref::Flag(INFO) << "Data sent to socket" << endl;
  539. return 0;
  540. }
  541. int pico_set_points(const uint32_t* points, const uint32_t size) {
  542. // Set points for measurement
  543. this->points_vector.assign(points, points + size);
  544. points_value = *(std::max_element(points_vector.begin(), points_vector.end()));
  545. logger << LogPref::Flag(INFO) << "Points set" << endl;
  546. sndbuf[0] = magic_number;
  547. sndbuf[1] = 0xC6;
  548. if(SocketActive->Send(sndbuf, 2) == -1) {
  549. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  550. return -1;
  551. }
  552. return 0;
  553. }
  554. int pico_set_sample_rate(const uint32_t sample_rate) {
  555. // Set sample rate for measurement
  556. this->sample_rate = sample_rate;
  557. logger << LogPref::Flag(INFO) << "Sample rate set to: " << sample_rate << endl;
  558. sndbuf[0] = magic_number;
  559. sndbuf[1] = 0xC7;
  560. if(SocketActive->Send(sndbuf, 2) == -1) {
  561. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  562. return -1;
  563. }
  564. return 0;
  565. }
  566. int pico_set_times(const uint32_t* times, const uint32_t size) {
  567. // Set times for measurement
  568. this->times.assign(times, times + size);
  569. logger << LogPref::Flag(INFO) << "Times set" << endl;
  570. sndbuf[0] = magic_number;
  571. sndbuf[1] = 0xC8;
  572. if(SocketActive->Send(sndbuf, 2) == -1) {
  573. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  574. return -1;
  575. }
  576. return 0;
  577. }
  578. int pico_configure_channels(const uint32_t number_channels, uint8_t trigger_channel, int32_t direction, uint16_t threshold, int16_t autoTrigger_ms) {
  579. // Set number of channels for measurement
  580. this->number_channels = number_channels;
  581. logger << LogPref::Flag(INFO) << "Number of channels set to: " << number_channels << endl;
  582. this->channels = create_channel(number_channels);
  583. conditions = new PS4000A_CONDITION[number_channels];
  584. for (uint32_t i = 0; i < number_channels; ++i) {
  585. conditions[i].source = channels[i];
  586. conditions[i].condition = PS4000A_CONDITION_FALSE;
  587. }
  588. if(auto retval = ps4000aSetChannel(handle, channels[trigger_channel], true, PS4000A_DC, PICO_X1_PROBE_5V, 0) != 0) {
  589. logger << LogPref::Flag(ERROR) << "Failed to set trigger channel. Code: " << retval << endl;
  590. sndbuf[0] = magic_number;
  591. sndbuf[1] = 0xFF;
  592. sndbuf[2] = 0x06;
  593. this->error_code = retval;
  594. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  595. if(SocketActive->Send(sndbuf, 7) == -1) {
  596. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  597. }
  598. return -1;
  599. }
  600. logger << LogPref::Flag(INFO) << "Trigger channel set" << endl;
  601. this->trig_channel = channels[trigger_channel];
  602. this->th_direction = static_cast<PS4000A_THRESHOLD_DIRECTION>(direction);
  603. this->threshold = threshold;
  604. this->trig_delay = 10;
  605. this->trig_autoTrigger_ms = autoTrigger_ms;
  606. for(uint8_t i = 0; i < number_channels; ++i) {
  607. if(i == trigger_channel) continue;
  608. else {
  609. auto retval = ps4000aSetChannel(handle, channels[i], true, PS4000A_AC, PICO_X1_PROBE_5V, 0);
  610. if (retval != 0) {
  611. logger << LogPref::Flag(ERROR) << "Failed to set channel " << i << ". Code: " << retval << endl;
  612. sndbuf[0] = magic_number;
  613. sndbuf[1] = 0xFF;
  614. sndbuf[2] = 0x07 + i;
  615. this->error_code = retval;
  616. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  617. if(SocketActive->Send(sndbuf, 7) == -1) {
  618. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  619. }
  620. return -1;
  621. }
  622. logger << LogPref::Flag(INFO) << "Channel " << i << " set" << endl;
  623. }
  624. }
  625. auto retval = ps4000aSetSimpleTrigger
  626. (
  627. handle,
  628. true,
  629. conditions[trigger_channel].source,
  630. threshold,
  631. th_direction,
  632. trig_delay,
  633. trig_autoTrigger_ms
  634. );
  635. if (retval != 0) {
  636. logger << LogPref::Flag(ERROR) << "Failed to set trigger. Code: " << retval << endl;
  637. sndbuf[0] = magic_number;
  638. sndbuf[1] = 0xFF;
  639. sndbuf[2] = 0x12;
  640. this->error_code = retval;
  641. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  642. if(SocketActive->Send(sndbuf, 7) == -1) {
  643. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  644. }
  645. return -1;
  646. }
  647. sndbuf[0] = magic_number;
  648. sndbuf[1] = 0xC9;
  649. if(SocketActive->Send(sndbuf, 2) == -1) {
  650. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  651. return -1;
  652. }
  653. return 0;
  654. }
  655. int pico_begin_measurement() {
  656. // Begin measurement on Pico device
  657. data_buffer_vec.assign(times.size(), std::vector<int16_t*>(number_channels, nullptr));
  658. for(size_t i = 0; i < times.size(); ++i) {
  659. logger << LogPref::Flag(INFO) << "Set data buffer:" << endl;
  660. for(size_t j = 0; j < number_channels; ++j) {
  661. data_buffer_vec[i][j] = new int16_t[points_vector[i]];
  662. }
  663. logger << LogPref::Flag(INFO) << "Get timebase:" << endl;
  664. uint32_t timebase = timebase_choice(sample_rate);
  665. logger << "Timebase = " << timebase << endl;
  666. int32_t timeIntervalNanoseconds = 0;
  667. int32_t maxSamples = 0;
  668. int32_t segmentIndex = 0;
  669. int32_t retval = ps4000aGetTimebase(handle, timebase, points_vector[i], &timeIntervalNanoseconds,
  670. &maxSamples, segmentIndex);
  671. logger << "retval: " << retval << endl;
  672. if(retval != 0) {
  673. logger << LogPref::Flag(ERROR) << "Failed to get timebase. Code: " << retval << endl;
  674. sndbuf[0] = magic_number;
  675. sndbuf[1] = 0xFF;
  676. sndbuf[2] = 0x13;
  677. this->error_code = retval;
  678. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  679. if(SocketActive->Send(sndbuf, 7) == -1) {
  680. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  681. }
  682. return -1;
  683. }
  684. logger << LogPref::Flag(INFO) << "Run block:" << endl;
  685. int32_t noOfPreTriggerSamples = 0;
  686. int32_t noOfPostTriggerSamples = points_value;
  687. retval = ps4000aRunBlock(handle, noOfPreTriggerSamples, noOfPostTriggerSamples, timebase,
  688. nullptr, segmentIndex, nullptr, nullptr);
  689. logger << "retval: " << retval << endl;
  690. if(retval != 0) {
  691. logger << LogPref::Flag(ERROR) << "Failed to run block. Code: " << retval << endl;
  692. sndbuf[0] = magic_number;
  693. sndbuf[1] = 0xFF;
  694. sndbuf[2] = 0x14;
  695. this->error_code = retval;
  696. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  697. if(SocketActive->Send(sndbuf, 7) == -1) {
  698. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  699. }
  700. return -1;
  701. }
  702. logger << LogPref::Flag(INFO) << "Run block done" << endl;
  703. int16_t ready = 0;
  704. while (ready == 0) {
  705. retval = ps4000aIsReady(handle, &ready);
  706. }
  707. for(uint8_t j = 0; j < number_channels; ++j) {
  708. logger << LogPref::Flag(INFO) << "Set data buffer for channel " << j << endl;
  709. PS4000A_RATIO_MODE mode = PS4000A_RATIO_MODE_NONE;
  710. retval = ps4000aSetDataBuffer(handle, channels[j], data_buffer_vec[i][j], points_vector[i],
  711. segmentIndex, mode);
  712. logger << "retval: " << retval << endl;
  713. if(retval != 0) {
  714. logger << LogPref::Flag(ERROR) << "Failed to set data buffer for channel " << j << ". Code: " << retval << endl;
  715. sndbuf[0] = magic_number;
  716. sndbuf[1] = 0xFF;
  717. sndbuf[2] = 0x15 + j;
  718. this->error_code = retval;
  719. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  720. if(SocketActive->Send(sndbuf, 7) == -1) {
  721. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  722. }
  723. return -1;
  724. }
  725. }
  726. logger << LogPref::Flag(INFO) << "Set data buffer done" << endl;
  727. logger << LogPref::Flag(INFO) << "Data collection start" << endl;
  728. uint32_t StartIndex = 0;
  729. uint32_t noOfSamples = static_cast<uint32_t>(points_vector[i]);
  730. int16_t overflow = 0;
  731. uint32_t DownSampleRatio = 1;
  732. retval = ps4000aGetValues(handle, StartIndex, &noOfSamples, DownSampleRatio, PS4000A_RATIO_MODE_NONE,
  733. segmentIndex, &overflow);
  734. logger << "retval: " << retval << endl;
  735. if(retval != 0) {
  736. logger << LogPref::Flag(ERROR) << "Failed to get values. Code: " << retval << endl;
  737. sndbuf[0] = magic_number;
  738. sndbuf[1] = 0xFF;
  739. sndbuf[2] = 0x21;
  740. this->error_code = retval;
  741. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  742. if(SocketActive->Send(sndbuf, 7) == -1) {
  743. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  744. }
  745. return -1;
  746. }
  747. logger << LogPref::Flag(INFO) << "Data collection done" << endl;
  748. logger << LogPref::Flag(INFO) << "Confirm data to socket" << endl;
  749. sndbuf[0] = magic_number;
  750. sndbuf[1] = 0xCA;
  751. if(SocketActive->Send(sndbuf, 2) == -1) {
  752. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  753. return -1;
  754. }
  755. logger << LogPref::Flag(INFO) << "Data sent to socket" << endl;
  756. logger << LogPref::Flag(INFO) << "Data confirmed" << endl;
  757. logger << LogPref::Flag(INFO) << "Sleeping for " << times[i+1] - times[i] << " ms" << endl;
  758. if(i < times.size() - 1) {
  759. std::chrono::duration<int64, std::milli> duration(times[i+1] - times[i]);
  760. std::this_thread::sleep_for(duration);
  761. }
  762. }
  763. for(int i = 0; i < data_buffer_vec.size(); ++i) {
  764. logger << LogPref::Flag(INFO) << "Writing data " << i << " to file" << endl;
  765. std::string filename = "data" + std::to_string(i) + ".csv";
  766. writing_data_fixed_name(filename, data_buffer_vec[i], points_vector[i], number_channels);
  767. logger << LogPref::Flag(INFO) << "Data collection done" << endl;
  768. logger << LogPref::Flag(INFO) << "Free buffers? Why?" << endl;
  769. free_buffers(data_buffer_vec[i]);
  770. logger << LogPref::Flag(INFO) << "Buffers freed" << endl;
  771. }
  772. logger << LogPref::Flag(INFO) << "Measurement done" << endl;
  773. sndbuf[0] = magic_number;
  774. sndbuf[1] = 0xCB;
  775. if (SocketActive->Send(sndbuf, 2) == -1) {
  776. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  777. return -1;
  778. }
  779. logger << LogPref::Flag(INFO) << "Data sent to socket" << endl;
  780. return 0;
  781. }
  782. int pico_exit() {
  783. // Exit Pico device and free resources
  784. if (handle != NULL) {
  785. auto ret = ps4000aCloseUnit(handle);
  786. if (ret != 0) {
  787. logger << LogPref::Flag(ERROR) << "Failed to close Pico device. Code: " << ret << endl;
  788. sndbuf[0] = magic_number;
  789. sndbuf[1] = 0xFF;
  790. sndbuf[2] = 0x23;
  791. this->error_code = ret;
  792. memcpy(sndbuf + 3, &this->error_code, sizeof(uint32_t));
  793. if(SocketActive->Send(sndbuf, 7) == -1) {
  794. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  795. }
  796. return -1;
  797. }
  798. sndbuf[0] = magic_number;
  799. sndbuf[1] = 0xCD;
  800. if(SocketActive->Send(sndbuf, 2) == -1) {
  801. logger << LogPref::Flag(ERROR) << "Failed to send data to socket" << endl;
  802. return -1;
  803. }
  804. logger << LogPref::Flag(INFO) << "Pico device closed" << endl;
  805. handle = NULL;
  806. }
  807. return 0;
  808. }
  809. };
  810. int main()
  811. {
  812. logger.enableConsoleOutput(true);
  813. logger << "Open socket" << endl;
  814. CPassiveSocket SocketPassive(CSimpleSocket::CSocketType::SocketTypeTcp);
  815. if (!SocketPassive.Initialize())
  816. {
  817. logger << LogPref::Flag(ERROR) << "Socket initialization failed" << endl;
  818. return -1;
  819. }
  820. if(!SocketPassive.Listen("127.0.0.1", 5003))
  821. {
  822. logger << LogPref::Flag(ERROR) << "Socket listening failed" << endl;
  823. return -1;
  824. }
  825. logger << "Socket initialized!" << endl;
  826. CActiveSocket* ClientSocket;
  827. PicoLocalService* pico_service;
  828. while(true)
  829. {
  830. logger << LogPref::Flag(INFO) << "Wait for connection..." << endl;
  831. logger.closeLogger();
  832. if ((ClientSocket = SocketPassive.Accept()) == nullptr)
  833. {
  834. logger.closeLogger();
  835. logger.initializeLogger("picologs", "pico");
  836. logger.enableConsoleOutput(true);
  837. logger << LogPref::Flag(ERROR) << "Socket accept failed" << endl;
  838. logger.closeLogger();
  839. return -1;
  840. }
  841. logger.initializeLogger("picologs", "pico");
  842. logger.enableConsoleOutput(true);
  843. logger << LogPref::Flag(INFO) << "Client connected" << endl;
  844. pico_service = new PicoLocalService(ClientSocket);
  845. while(pico_service->get_request() != 0)
  846. {
  847. logger << LogPref::Flag(INFO) << "Request received" << endl;
  848. }
  849. logger << LogPref::Flag(INFO) << "Request processing finished" << endl;
  850. delete pico_service;
  851. pico_service = nullptr;
  852. delete ClientSocket;
  853. ClientSocket = nullptr;
  854. }
  855. return 0;
  856. }