| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154 |
- #include <iostream>
- #include <memory>
- #include <windows.h>
- #include <tuple>
- #include "picofunctions.h"
- #include "pugiconfig.hpp"
- #include "pugixml.hpp"
- #include "parser.hpp"
- #include "src/ActiveSocket.h"
- #define MAX_PACKET 4096
- using namespace std;
- std::vector<std::string> split(const std::string& s, const std::string& delimiter) {
- std::vector<std::string> tokens;
- size_t pos = 0;
- std::string temp = s;
- std::string token;
- while ((pos = temp.find(delimiter)) != std::string::npos) {
- token = temp.substr(0, pos);
- tokens.push_back(token);
- temp.erase(0, pos + delimiter.length());
- }
- tokens.push_back(temp);
- return tokens;
- }
- class PicoLocalClient
- {
- private:
- CActiveSocket *socket;
- uint8_t sndbuf[MAX_PACKET]{0};
- uint8_t rcvbuf[MAX_PACKET]{0};
- std::string line_edit;
- std::vector<std::string> tokens;
- uint8_t error_state = 0x00;
- uint32_t error_code = 0x00000000;
- public:
- PicoLocalClient(CActiveSocket *socket) : socket(socket) {}
- ~PicoLocalClient() { delete socket; }
- int pico_open()
- {
- sndbuf[0] = 0xAA;
- sndbuf[1] = 0x01;
- if(socket->Send(sndbuf, 2) == 0)
- {
- cerr << "Failed to send data to socket" << endl;
- return -1;
- }
- if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
- {
- cerr << "Failed to receive data from socket" << endl;
- return -1;
- }
- uint32_t bytes_received = socket->GetBytesReceived();
- if(bytes_received == 0)
- {
- cerr << "Server disconnected" << endl;
- return -1;
- }
- if(bytes_received < 2)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- if(rcvbuf[0] != 0xAA)
- {
- cerr << "Invalid magic number" << endl;
- return -1;
- }
- if(rcvbuf[1] == 0xFF)
- {
- if(bytes_received < 7)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
- memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
- memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
- cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
- }
- else if(rcvbuf[1] != 0xC1)
- {
- cerr << "Invalid command callback" << endl;
- return -1;
- }
- cout << "Pico device opened" << endl;
- return 0;
- }
- int pico_xml_config(const std::string& file_name)
- {
- sndbuf[0] = 0xAA;
- sndbuf[1] = 0x02;
- uint32_t size = file_name.size();
- memcpy(sndbuf + 2, &size, sizeof(uint32_t));
- memcpy(sndbuf + 6, file_name.c_str(), size);
- if(socket->Send(sndbuf, size + 6) == -1)
- {
- cerr << "Failed to send data to socket" << endl;
- return -1;
- }
- if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
- {
- cerr << "Failed to receive data from socket" << endl;
- return -1;
- }
- uint32_t bytes_received = socket->GetBytesReceived();
- if(bytes_received == 0)
- {
- cerr << "Server disconnected" << endl;
- return -1;
- }
- if(bytes_received < 2)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- if(rcvbuf[0] != 0xAA)
- {
- cerr << "Invalid magic number" << endl;
- return -1;
- }
- if(rcvbuf[1] == 0xFF)
- {
- if(bytes_received < 7)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
- memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
- memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
- cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
- }
- else if(rcvbuf[1] != 0xC2)
- {
- cerr << "Invalid command callback" << endl;
- return -1;
- }
- cout << "Pico device configured" << endl;
- return 0;
- }
- int pico_set_params(uint32_t* apoints, uint32_t* times, uint32_t sample_rate, uint32_t number_channels, uint32_t size)
- {
- sndbuf[0] = 0xAA;
- sndbuf[1] = 0x0C;
- memcpy(sndbuf + 2, &size, sizeof(uint32_t));
- memcpy(sndbuf + 6, apoints, size * sizeof(uint32_t));
- memcpy(sndbuf + 6 + size * sizeof(uint32_t), times, size * sizeof(uint32_t));
- memcpy(sndbuf + 6 + 2 * size * sizeof(uint32_t), &sample_rate, sizeof(uint32_t));
- memcpy(sndbuf + 10 + 2 * size * sizeof(uint32_t), &number_channels, sizeof(uint32_t));
- if(socket->Send(sndbuf, size * 2 * sizeof(uint32_t) + 14) == -1)
- {
- cerr << "Failed to send data to socket" << endl;
- return -1;
- }
- if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
- {
- cerr << "Failed to receive data from socket" << endl;
- return -1;
- }
- uint32_t bytes_received = socket->GetBytesReceived();
- if(bytes_received == 0)
- {
- cerr << "Server disconnected" << endl;
- return -1;
- }
- if(bytes_received < 2)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- if(rcvbuf[0] != 0xAA)
- {
- cerr << "Invalid magic number" << endl;
- return -1;
- }
- if(rcvbuf[1] == 0xFF)
- {
- if(bytes_received < 7)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
- memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
- memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
- cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
- }
- else if(rcvbuf[1] != 0xCC)
- {
- cerr << "Invalid command callback" << endl;
- return -1;
- }
- cout << "Pico device parameters set" << endl;
- return 0;
- }
- int pico_close()
- {
- sndbuf[0] = 0xAA;
- sndbuf[1] = 0x03;
- if(socket->Send(sndbuf, 2) == -1)
- {
- cerr << "Failed to send data to socket" << endl;
- return -1;
- }
- if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
- {
- cerr << "Failed to receive data from socket" << endl;
- return -1;
- }
- uint32_t bytes_received = socket->GetBytesReceived();
- if(bytes_received == 0)
- {
- cerr << "Server disconnected" << endl;
- return -1;
- }
- if(bytes_received < 2)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- if(rcvbuf[0] != 0xAA)
- {
- cerr << "Invalid magic number" << endl;
- return -1;
- }
- if(rcvbuf[1] == 0xFF)
- {
- if(bytes_received < 7)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
- memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
- memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
- cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
- }
- else if(rcvbuf[1] != 0xC3)
- {
- cerr << "Invalid command callback" << endl;
- return -1;
- }
- cout << "Pico device closed" << endl;
- return 0;
- }
- int pico_get_current_params()
- {
- sndbuf[0] = 0xAA;
- sndbuf[1] = 0x04;
- if(socket->Send(sndbuf, 2) == -1)
- {
- cerr << "Failed to send data to socket" << endl;
- return -1;
- }
- if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
- {
- cerr << "Failed to receive data from socket" << endl;
- return -1;
- }
- uint32_t bytes_received = socket->GetBytesReceived();
- if(bytes_received == 0)
- {
- cerr << "Server disconnected" << endl;
- return -1;
- }
- if(bytes_received < 2)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- if(rcvbuf[0] != 0xAA)
- {
- cerr << "Invalid magic number" << endl;
- return -1;
- }
- if(rcvbuf[1] == 0xFF)
- {
- if(bytes_received < 7)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
- memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
- memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
- cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
- }
- else if(rcvbuf[1] != 0xC4)
- {
- cerr << "Invalid command callback" << endl;
- return -1;
- }
- if(bytes_received < 18)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- uint32_t size;
- memcpy(&size, rcvbuf + 2, sizeof(uint32_t));
- if(size < 1)
- {
- cerr << "Invalid data size" << endl;
- return -1;
- }
- uint32_t* apoints = new uint32_t[size / sizeof(uint32_t)];
- memcpy(apoints, rcvbuf + 6, size);
- for(uint32_t i = 0; i < size / sizeof(uint32_t); ++i)
- {
- cout << "apoints[" << i << "] = " << std::dec << apoints[i] << endl;
- }
- uint32_t* atimes = new uint32_t[size / sizeof(uint32_t)];
- memcpy(atimes, rcvbuf + 6 + size, size);
- for(uint32_t i = 0; i < size / sizeof(uint32_t); ++i)
- {
- cout << "atimes[" << i << "] = " << std::dec << atimes[i] << endl;
- }
- uint32_t sample_rate = 0;
- memcpy(&sample_rate, rcvbuf + 6 + 2 * size, sizeof(uint32_t));
- cout << "sample_rate = " << std::dec << sample_rate << endl;
- uint32_t number_channels = 0;
- memcpy(&number_channels, rcvbuf + 10 + 2 * size, sizeof(uint32_t));
- cout << "number_channels = " << std::dec << number_channels << endl;
- cout << "Pico device parameters retrieved" << endl;
- return 0;
- }
- int pico_probe()
- {
- sndbuf[0] = 0xAA;
- sndbuf[1] = 0x05;
- if(socket->Send(sndbuf, 2) == -1)
- {
- cerr << "Failed to send data to socket" << endl;
- return -1;
- }
- if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
- {
- cerr << "Failed to receive data from socket" << endl;
- return -1;
- }
- uint32_t bytes_received = socket->GetBytesReceived();
- if(bytes_received == 0)
- {
- cerr << "Server disconnected" << endl;
- return -1;
- }
- if(bytes_received < 2)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- if(rcvbuf[0] != 0xAA)
- {
- cerr << "Invalid magic number" << endl;
- return -1;
- }
- if(rcvbuf[1] == 0xFF)
- {
- if(bytes_received < 7)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
- memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
- memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
- cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
- }
- else if(rcvbuf[1] != 0xC5)
- {
- cerr << "Invalid command callback" << endl;
- return -1;
- }
- cout << "Pico device probe completed" << endl;
- return 0;
- }
- int pico_set_points(uint32_t* points, uint32_t size)
- {
- sndbuf[0] = 0xAA;
- sndbuf[1] = 0x06;
- memcpy(sndbuf + 2, &size, sizeof(uint32_t));
- memcpy(sndbuf + 6, points, size * sizeof(uint32_t));
- if(socket->Send(sndbuf, size * sizeof(uint32_t) + 6) == -1)
- {
- cerr << "Failed to send data to socket" << endl;
- return -1;
- }
- if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
- {
- cerr << "Failed to receive data from socket" << endl;
- return -1;
- }
- uint32_t bytes_received = socket->GetBytesReceived();
- if(bytes_received == 0)
- {
- cerr << "Server disconnected" << endl;
- return -1;
- }
- if(bytes_received < 2)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- if(rcvbuf[0] != 0xAA)
- {
- cerr << "Invalid magic number" << endl;
- return -1;
- }
- if(rcvbuf[1] == 0xFF)
- {
- if(bytes_received < 7)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
- memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
- memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
- cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
- }
- else if(rcvbuf[1] != 0xC6)
- {
- cerr << "Invalid command callback" << endl;
- return -1;
- }
- cout << "Pico device points set" << endl;
- return 0;
- }
- int pico_set_sample_rate(uint32_t sample_rate)
- {
- sndbuf[0] = 0xAA;
- sndbuf[1] = 0x07;
- memcpy(sndbuf + 2, &sample_rate, sizeof(uint32_t));
- if(socket->Send(sndbuf, sizeof(uint32_t) + 6) == -1)
- {
- cerr << "Failed to send data to socket" << endl;
- return -1;
- }
- if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
- {
- cerr << "Failed to receive data from socket" << endl;
- return -1;
- }
- uint32_t bytes_received = socket->GetBytesReceived();
- if(bytes_received == 0)
- {
- cerr << "Server disconnected" << endl;
- return -1;
- }
- if(bytes_received < 2)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- if(rcvbuf[0] != 0xAA)
- {
- cerr << "Invalid magic number" << endl;
- return -1;
- }
- if(rcvbuf[1] == 0xFF)
- {
- if(bytes_received < 7)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
- memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
- memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
- cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
- return 0;
- }
- else if(rcvbuf[1] != 0xC7)
- {
- cerr << "Invalid command callback" << endl;
- return -1;
- }
- cout << "Pico device sample rate set" << endl;
- return 0;
- }
- int pico_set_times(uint32_t* times, uint32_t size)
- {
- sndbuf[0] = 0xAA;
- sndbuf[1] = 0x08;
- memcpy(sndbuf + 2, &size, sizeof(uint32_t));
- memcpy(sndbuf + 6, times, size * sizeof(uint32_t));
- if(socket->Send(sndbuf, size * sizeof(uint32_t) + 6) == -1)
- {
- cerr << "Failed to send data to socket" << endl;
- return -1;
- }
- if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
- {
- cerr << "Failed to receive data from socket" << endl;
- return -1;
- }
- uint32_t bytes_received = socket->GetBytesReceived();
- if(bytes_received == 0)
- {
- cerr << "Server disconnected" << endl;
- return -1;
- }
- if(bytes_received < 2)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- if(rcvbuf[0] != 0xAA)
- {
- cerr << "Invalid magic number" << endl;
- return -1;
- }
- if(rcvbuf[1] == 0xFF)
- {
- if(bytes_received < 7)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
- memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
- memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
- cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
- }
- else if(rcvbuf[1] != 0xC8)
- {
- cerr << "Invalid command callback" << endl;
- return -1;
- }
- cout << "Pico device times set" << endl;
- return 0;
- }
- int pico_configure_channels(const uint32_t number_channels, uint8_t trigger_channel, int32_t direction, uint16_t threshold, int16_t autoTrigger_ms)
- {
- sndbuf[0] = 0xAA;
- sndbuf[1] = 0x09;
- memcpy(sndbuf + 2, &number_channels, sizeof(uint32_t));
- memcpy(sndbuf + 6, &trigger_channel, sizeof(uint8_t));
- memcpy(sndbuf + 7, &direction, sizeof(int32_t));
- memcpy(sndbuf + 11, &threshold, sizeof(uint16_t));
- memcpy(sndbuf + 13, &autoTrigger_ms, sizeof(int16_t));
- if(socket->Send(sndbuf, 15) == -1)
- {
- cerr << "Failed to send data to socket" << endl;
- return -1;
- }
- if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
- {
- cerr << "Failed to receive data from socket" << endl;
- return -1;
- }
- uint32_t bytes_received = socket->GetBytesReceived();
- if(bytes_received == 0)
- {
- cerr << "Server disconnected" << endl;
- return -1;
- }
- if(bytes_received < 2)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- if(rcvbuf[0] != 0xAA)
- {
- cerr << "Invalid magic number" << endl;
- return -1;
- }
- if(rcvbuf[1] == 0xFF)
- {
- if(bytes_received < 7)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
- memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
- memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
- cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
- }
- else if(rcvbuf[1] != 0xC9)
- {
- cerr << "Invalid command callback" << endl;
- return -1;
- }
- cout << "Pico device channels configured" << endl;
- return 0;
- }
- int pico_begin_measurement()
- {
- sndbuf[0] = 0xAA;
- sndbuf[1] = 0x0A;
- if(socket->Send(sndbuf, 2) == -1)
- {
- cerr << "Failed to send data to socket" << endl;
- return -1;
- }
- if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
- {
- cerr << "Failed to receive data from socket" << endl;
- return -1;
- }
- uint32_t bytes_received = socket->GetBytesReceived();
- if(bytes_received == 0)
- {
- cerr << "Server disconnected" << endl;
- return -1;
- }
- if(bytes_received < 2)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- if(rcvbuf[0] != 0xAA)
- {
- cerr << "Invalid magic number" << endl;
- return -1;
- }
- if(rcvbuf[1] == 0xFF)
- {
- if(bytes_received < 7)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
- memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
- memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
- cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
- }
- else if(rcvbuf[1] != 0xCA)
- {
- cout << "Invalid command callback" << endl;
- }
- cout << "Pico device measurement started" << endl;
- cout << "Measurement in progress..." << endl;
- bool end_of_data = false;
- while(!end_of_data)
- {
- if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
- {
- cerr << "Failed to receive data from socket" << endl;
- return -1;
- }
- bytes_received = socket->GetBytesReceived();
- if(bytes_received == 0)
- {
- cerr << "Server disconnected" << endl;
- return -1;
- }
- if(bytes_received < 2)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- if(rcvbuf[0] != 0xAA)
- {
- cerr << "Invalid magic number" << endl;
- return -1;
- }
- if(rcvbuf[1] == 0xFF)
- {
- if(bytes_received < 7)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
- memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
- memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
- cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
- }
- if(rcvbuf[1] == 0xCB)
- {
- cout << "Measurement completed..." << endl;
- end_of_data = true;
- }
- else if(rcvbuf[1] = 0xCA)
- {
- cout << "Measurement in progress..." << endl;
- }
- else
- {
- cerr << "Invalid command callback" << endl;
- return -1;
- }
- cout << "Measurement in progress..." << endl;
- }
- cout << "Pico device measurement completed" << endl;
- return 0;
- }
- int pico_stop()
- {
- sndbuf[0] = 0xAA;
- sndbuf[1] = 0x0E;
- if(socket->Send(sndbuf, 2) == -1)
- {
- cerr << "Failed to send data to socket" << endl;
- return -1;
- }
- if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
- {
- cerr << "Failed to receive data from socket" << endl;
- return -1;
- }
- uint32_t bytes_received = socket->GetBytesReceived();
- if(bytes_received == 0)
- {
- cerr << "Server disconnected" << endl;
- return -1;
- }
- if(bytes_received < 2)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- if(rcvbuf[0] != 0xAA)
- {
- cerr << "Invalid magic number" << endl;
- return -1;
- }
- if(rcvbuf[1] == 0xFF)
- {
- if(bytes_received < 7)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
- memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
- memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
- cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
- }
- else if(rcvbuf[1] != 0xCE)
- {
- cerr << "Invalid command callback" << endl;
- return -1;
- }
- cout << "Pico device measurement stopped" << endl;
- return 0;
- }
- int pico_exit()
- {
- sndbuf[0] = 0xAA;
- sndbuf[1] = 0x0D;
- if(socket->Send(sndbuf, 2) == -1)
- {
- cerr << "Failed to send data to socket" << endl;
- return -1;
- }
- if(socket->Receive(MAX_PACKET, rcvbuf) == -1)
- {
- cerr << "Failed to receive data from socket" << endl;
- return -1;
- }
- uint32_t bytes_received = socket->GetBytesReceived();
- if(bytes_received == 0)
- {
- cerr << "Server disconnected" << endl;
- return -1;
- }
- if(bytes_received < 2)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- if(rcvbuf[0] != 0xAA)
- {
- cerr << "Invalid magic number" << endl;
- return -1;
- }
- if(rcvbuf[1] == 0xFF)
- {
- if(bytes_received < 7)
- {
- cerr << "Invalid buffer size" << endl;
- return -1;
- }
- cerr << "Error at ADC: " << std::hex << rcvbuf[2] << endl;
- memcpy(&error_state, rcvbuf + 2, sizeof(uint8_t));
- memcpy(&error_code, rcvbuf + 3, sizeof(uint32_t));
- cerr << "Error code: " << std::hex << error_code << " " << return_fun(error_code) << endl;
- }
- else if(rcvbuf[1] != 0xCD)
- {
- cerr << "Invalid command callback" << endl;
- return -1;
- }
- cout << "Pico device exited" << endl;
- return 0;
- }
- int get_command()
- {
- if(true)
- {
- cout << "cmd$ ";
- std::getline(std::cin, line_edit);
- cout << endl;
- tokens = split(line_edit, " ");
- if(tokens.size() < 1)
- {
- cerr << "Invalid command" << endl;
- cout << endl;
- return -1;
- }
- if(tokens[0] == "open")
- {
- if(pico_open() != 0)
- {
- cerr << "Failed to open pico device" << endl;
- cout << endl;
- return -1;
- }
- }
- else if(tokens[0] == "xml_config")
- {
- if(tokens.size() < 2)
- {
- cerr << "Invalid command" << endl;
- cout << endl;
- return -1;
- }
- if(pico_xml_config(tokens[1]) != 0)
- {
- cerr << "Failed to configure pico device" << endl;
- cout << endl;
- return -1;
- }
- }
- else if(tokens[0] == "set_params")
- {
- if(tokens.size() < 5)
- {
- cerr << "Invalid command" << endl;
- cout << endl;
- return -1;
- }
- std::vector<uint32_t> points;
- std::vector<uint32_t> times;
- points = string_to_vector(tokens[1]);
- times = string_to_vector(tokens[2]);
- uint32_t sample_rate = std::stoi(tokens[3]);
- uint32_t number_channels = std::stoi(tokens[4]);
- uint32_t size = std::stoi(tokens[5]);
- if(points.size() != size || times.size() != size)
- {
- cerr << "Invalid data size" << endl;
- cout << endl;
- return -1;
- }
- if(pico_set_params(points.data(), times.data(), sample_rate, number_channels, size) != 0)
- {
- cerr << "Failed to set pico device parameters" << endl;
- cout << endl;
- return -1;
- }
- return 0;
- }
- else if(tokens[0] == "close")
- {
- if(pico_close() != 0)
- {
- cerr << "Failed to close pico device" << endl;
- cout << endl;
- return -1;
- }
- }
- else if(tokens[0] == "exit")
- {
- if(pico_exit() != 0)
- {
- cerr << "Failed to exit pico device" << endl;
- cout << endl;
- return -1;
- }
- return 1;
- }
- else if(tokens[0] == "get_current_params")
- {
- if(pico_get_current_params() != 0)
- {
- cerr << "Failed to get pico device parameters" << endl;
- cout << endl;
- return -1;
- }
- }
- else if(tokens[0] == "probe")
- {
- if(pico_probe() != 0)
- {
- cerr << "Failed to probe pico device" << endl;
- cout << endl;
- return -1;
- }
- }
- else if(tokens[0] == "set_points")
- {
- if(tokens.size() < 3)
- {
- cerr << "Invalid command" << endl;
- cout << endl;
- return -1;
- }
- uint32_t size = std::stoi(tokens[1]);
- std::vector<uint32_t> points = string_to_vector(tokens[2]);
- if(points.size() != size)
- {
- cerr << "Invalid data size" << endl;
- cout << endl;
- return -1;
- }
- if(pico_set_points(points.data(), size) != 0)
- {
- cerr << "Failed to set pico device points" << endl;
- cout << endl;
- return -1;
- }
- }
- else if(tokens[0] == "set_sample_rate")
- {
- if(tokens.size() < 2)
- {
- cerr << "Invalid command" << endl;
- cout << endl;
- return -1;
- }
- uint32_t sample_rate = std::stoi(tokens[1]);
- if(pico_set_sample_rate(sample_rate) != 0)
- {
- cerr << "Failed to set pico device sample rate" << endl;
- cout << endl;
- return -1;
- }
- }
- else if(tokens[0] == "set_times")
- {
- if(tokens.size() < 3)
- {
- cerr << "Invalid command" << endl;
- cout << endl;
- return -1;
- }
- uint32_t size = std::stoi(tokens[1]);
- std::vector<uint32_t> times = string_to_vector(tokens[2]);
- if(times.size() != size)
- {
- cerr << "Invalid data size" << endl;
- cout << endl;
- return -1;
- }
- if(pico_set_times(times.data(), size) != 0)
- {
- cerr << "Failed to set pico device times" << endl;
- cout << endl;
- return -1;
- }
- }
- else if(tokens[0] == "configure_channels")
- {
- if(tokens.size() < 6)
- {
- cerr << "Invalid command" << endl;
- cout << endl;
- return -1;
- }
- uint32_t number_channels = std::stoi(tokens[1]); // number of channels (1-8)
- uint8_t trigger_channel = std::stoi(tokens[2]); // trigger channel (0-7, 0 = channel A, 1 = channel B, ...)
- int32_t direction = std::stoi(tokens[3]); // trigger direction (0 = rising, 1 = falling)
- uint16_t threshold = std::stoi(tokens[4]); // trigger threshold (0-65535)
- int16_t autoTrigger_ms = std::stoi(tokens[5]); // auto trigger time (0-65535 ms)
- if(pico_configure_channels(number_channels, trigger_channel, direction, threshold, autoTrigger_ms) != 0)
- {
- cerr << "Failed to configure pico device channels" << endl;
- cout << endl;
- return -1;
- }
- }
- else if(tokens[0] == "begin_measurement")
- {
- if(pico_begin_measurement() != 0)
- {
- cerr << "Failed to begin pico device measurement" << endl;
- cout << endl;
- return -1;
- }
- }
- else if(tokens[0] == "stop")
- {
- if(pico_stop() != 0)
- {
- cerr << "Failed to stop pico device measurement" << endl;
- cout << endl;
- return -1;
- }
- }
- else if(tokens[0] == "help")
- {
- cout << "Available commands:" << endl;
- cout << "open" << endl;
- cout << "xml_config <filename>" << endl;
- cout << "set_params <points> <times> <sample_rate> <number_channels> <size>" << endl;
- cout << "close" << endl;
- cout << "exit" << endl;
- cout << "get_current_params" << endl;
- cout << "probe" << endl;
- cout << "set_points <points> <size>" << endl;
- cout << "set_sample_rate <sample_rate>" << endl;
- cout << "set_times <times> <size>" << endl;
- cout << "configure_channels <number_channels> <trigger_channel> <direction> <threshold> <autoTrigger_ms>" << endl;
- cout << "begin_measurement" << endl;
- cout << "stop" << endl;
- }
- else if(tokens[0] == "test")
- {
- cout << "Test command received" << endl;
- }
- else
- {
- cerr << "Unknown command" << endl;
- cout << endl;
- return -1;
- }
- }
- cout << endl;
- return 0;
- }
- };
- int main()
- {
- cout << "Open socket" << endl;
- CActiveSocket SocketActive(CSimpleSocket::CSocketType::SocketTypeTcp);
- if (!SocketActive.Initialize())
- {
- cerr << "Socket initialization failed" << endl;
- return -1;
- }
- if(!SocketActive.Open("127.0.0.1", 5003))
- {
- cerr << "Socket opening failed" << endl;
- return -1;
- }
- PicoLocalClient pico_client(&SocketActive);
- while(pico_client.get_command() != 1) {
- // empty
- }
- return 0;
- }
|