diff --git a/CMakeLists.txt b/CMakeLists.txt index c0dc14ba..3ce7eb77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,4 +25,4 @@ install(DIRECTORY src/ FILES_MATCHING PATTERN "*.h" ) -# gadd_subdirectory(test) +#add_subdirectory(test) diff --git a/docs_and_specs/Keysight_InfiniiVision3000.pdf b/docs_and_specs/Keysight_InfiniiVision3000.pdf index 0fca8fc8..3aecb705 100644 Binary files a/docs_and_specs/Keysight_InfiniiVision3000.pdf and b/docs_and_specs/Keysight_InfiniiVision3000.pdf differ diff --git a/src/device.cpp b/src/device.cpp index a62b23cb..0c2f7c0a 100644 --- a/src/device.cpp +++ b/src/device.cpp @@ -63,7 +63,7 @@ int Device::connect() { * KST3000 k.exec("RSTater?", buffer); * @endcode * */ -int Device::exec(string message, char* result, bool br) { +int Device::exec(string message, char* result, bool br, int size) { if (br) { message += '\n'; } @@ -73,7 +73,7 @@ int Device::exec(string message, char* result, bool br) { // testcase: KST3000 k.exec("STATus? CHANnel2", buffer); send(sockfd, command, strlen(command), 0); if (result) { - read(sockfd, result, 1024); + read(sockfd, result, size); } cout << "Executed Successfully.\n"; return 0; @@ -105,6 +105,10 @@ void Device::cli() { cout << "Input a command: "; string commands = ""; getline(cin, commands); + if (commands.compare("f") == 0) { + cout << "CLI finished.\n"; + break; + } bool is_query = commands.find("?") != string::npos; if (is_query) { memset(buffer, 0, sizeof(buffer)); diff --git a/src/device.h b/src/device.h index 38f0ac50..eae03c95 100644 --- a/src/device.h +++ b/src/device.h @@ -18,7 +18,7 @@ class Device { int test(); int connect(); - int exec(string message, char *result = nullptr, bool = true); + int exec(string message, char *result = nullptr, bool = true, int size = 1024); int exec_commands(string commands); void what_am_i(); void cli(); diff --git a/src/kst3000.cpp b/src/kst3000.cpp index eca39caf..db602ba3 100644 --- a/src/kst3000.cpp +++ b/src/kst3000.cpp @@ -2,6 +2,10 @@ // Created by liuwuhao on 17.06.21. // #include +#include +#include +#include +#include #include "kst3000.h" #include @@ -145,6 +149,36 @@ int KST3000::set_channel_display(int on, int channel) { return exec(command); } +vector split (string s, string delimiter) { + size_t pos_start = 0, pos_end, delim_len = delimiter.length(); + string token; + vector res; + + while ((pos_end = s.find (delimiter, pos_start)) != string::npos) { + token = s.substr (pos_start, pos_end - pos_start); + pos_start = pos_end + delim_len; + res.push_back (token); + } + + res.push_back (s.substr (pos_start)); + return res; +} + +/** + * @brief Query the preamble of waveform data + * @return a array containing preamble + * [Waveform format, Acquire type, Waveform points desired, Waveform average count, + * Waveform X increment, Waveform X origin, Waveform X reference, + * Waveform Y increment, Waveform Y origin, Waveform Y reference] + * */ +char* KST3000::get_waveform_preamble() { + string command = "WAVeform:PREamble?"; + char buffer[1024] = {0}; + exec(command, buffer); + // vector substrs = split(buffer, ","); + return buffer; +} + /** * @brief Query the number of waveform points to be transferred * @return the number of waveform points to be transferred @@ -156,17 +190,79 @@ int KST3000::get_waveform_points() { return stoi(buffer); } +int write_to_file(string data, string file_path) { + string buffer(data); + ofstream file; + file.open(file_path); + file << buffer; + file.close(); + return 0; +} + char* KST3000::get_waveform_data() { string command = "WAVeform:DATA?"; int num = get_waveform_points(); - char buffer[num]; - memset(buffer, 0, num); - exec(command, buffer); - cout << sizeof(buffer) << endl; - cout << 1123 << endl; + char buffer[num + 10]; + memset(buffer, 1, num); + cout << strlen(buffer) << endl; + exec(command, buffer, true, num); + char data[num]; + memcpy(data, buffer + 10, num); for (int i = 0; i < num; i++) { - cout << hex << (int)buffer[i]; + cout << (int) (unsigned char)data[i]; + cout << " "; } + cout << endl; +// string s_buffer(buffer); +// s_buffer = s_buffer.substr(10); +// cout << buffer; +// write_to_file(buffer, "/tmp/buffer"); + return data; +} + +int KST3000::save_waveform_data(string file_path) { + char *preamble = get_waveform_preamble(); + vector v_preamble = split(preamble, ","); + int points = stoi(v_preamble[2]); + double x_increment = stod(v_preamble[4]); + double x_origin = stod(v_preamble[5]); + double x_reference = stod(v_preamble[6]); + double y_increment = stod(v_preamble[7]); + double y_origin = stod(v_preamble[8]); + double y_reference = stod(v_preamble[9]); + + char *data = get_waveform_data(); + for (int i = 0; i < points; i++) { + cout << (int) (unsigned char)data[i]; + cout << " "; + } + cout << endl; + +// string command = "WAVeform:DATA?"; +// int num = get_waveform_points(); +// char buffer[num + 10]; +// memset(buffer, 1, num); +// cout << strlen(buffer) << endl; +// exec(command, buffer, true, num); +// char data[num]; +// memcpy(data, buffer + 10, num); + + cout << endl; + stringstream stream; + for (int i = 0; i < points; i++) { + double time = ((i - x_reference) * x_increment) + x_origin; + int voltage_data = (int) (unsigned char) data[i]; + if (voltage_data == 0) { + continue; + } + double voltage = ((voltage_data - y_reference) * y_increment) + y_origin; + cout << voltage_data; + cout << " " ; + stream << time << "," << voltage << endl; + } + + write_to_file(stream.str(), file_path); + return 0; } /** @@ -181,4 +277,4 @@ int KST3000::digitize() { * */ int KST3000::get_system_setup(char *buffer) { return exec("SYSTem:SETup?", buffer); -} \ No newline at end of file +} diff --git a/src/kst3000.h b/src/kst3000.h index 56d7bd75..8defa700 100644 --- a/src/kst3000.h +++ b/src/kst3000.h @@ -24,8 +24,10 @@ class KST3000 : public Device { int set_channel_range(double, int channel = 1, bool is_v = true); int set_channel_offset(double offset, int channel = 1); int set_channel_display(int on, int channel = 1); + char *get_waveform_preamble(); int get_waveform_points(); char *get_waveform_data(); + int save_waveform_data(string file_path = "./buffer"); int digitize(); int get_system_setup(char *buffer); }; diff --git a/test/main.cpp b/test/main.cpp index caf6b1fe..ee72d43e 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -10,34 +10,39 @@ using namespace std; -string cap_commands = "ACQuire:TYPE AVERage\n" +string cap_commands = "ACQuire:TYPE NORMal\n" "ACQuire:COMPlete 100\n" "ACQuire:COUNt 8\n" - "DIGitize CHANnel1\n" +// "DIGitize CHANnel1\n"; "WAVeform:SOURce CHANnel1\n" "WAVeform:FORMat BYTE\n" - "WAVeform:POINts 1000\n"; + "WAVeform:POINts 1000"; int run_kst3000() { KST3000 k = KST3000("132.231.14.172"); k.connect(); -// k.set_time_range(1); +// k.set_time_range(0.05); // k.set_channel_scale(3.3); // k.set_channel_range(40); // k.set_channel_offset(-2); // k.set_channel_display(1); // k.set_time_delay(0.003); // k.set_trigger_edge("NEG"); + // k.set_trigger_source(); -// k.single(); - k.cli(); +// k.run(); // char buffer[2048]; // k.get_system_setup(buffer); // cout << buffer; -// k.exec_commands(cap_commands); + k.exec_commands(cap_commands); +// k.single(); +// k.get_waveform_preamble(); // int num = k.get_waveform_points(); // cout << num; // k.get_waveform_data(); +// k.cli(); + k.save_waveform_data("/tmp/buffer"); + k.cli(); // cout << k.get_waveform_points(); // char buffer[1024] = {0}; // k.exec("RUN"); @@ -49,7 +54,7 @@ int run_kst33500() { k.connect(); k.what_am_i(); // k.display_connection(); - k.cli(); +// k.cli(); // k.function("SIN"); // k.frequency("+1.0E+05"); // k.voltage("+2.0", "HIGH");