Skip to content

Commit

Permalink
feat: export data
Browse files Browse the repository at this point in the history
  • Loading branch information
liuwuhao committed Jul 1, 2021
1 parent 6a84d5a commit d84f432
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ install(DIRECTORY src/
FILES_MATCHING PATTERN "*.h"
)

# gadd_subdirectory(test)
#add_subdirectory(test)
Binary file modified docs_and_specs/Keysight_InfiniiVision3000.pdf
Binary file not shown.
8 changes: 6 additions & 2 deletions src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
110 changes: 103 additions & 7 deletions src/kst3000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// Created by liuwuhao on 17.06.21.
//
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstring>
#include "kst3000.h"
#include <unistd.h>

Expand Down Expand Up @@ -145,6 +149,36 @@ int KST3000::set_channel_display(int on, int channel) {
return exec(command);
}

vector<string> split (string s, string delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
string token;
vector<string> 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<string> substrs = split(buffer, ",");
return buffer;
}

/**
* @brief Query the number of waveform points to be transferred
* @return the number of waveform points to be transferred
Expand All @@ -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<string> 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;
}

/**
Expand All @@ -181,4 +277,4 @@ int KST3000::digitize() {
* */
int KST3000::get_system_setup(char *buffer) {
return exec("SYSTem:SETup?", buffer);
}
}
2 changes: 2 additions & 0 deletions src/kst3000.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
21 changes: 13 additions & 8 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down

0 comments on commit d84f432

Please sign in to comment.