Hello everybody,

I made a C++ code to request and receive spectra, via KATCP protocol, from 
a sideband separator spectrometer model in the RFSoX 4x2. The code records 
the round-trip time it takes for the PC to request the spectrum and the 
spectrum to be sent from the RFSoC. The problem is that, after the first 
minute or so, there is an important delay in the time it takes for the 
spectrum to be received by the PC. Any idea what could be causing this 
problem? I've included a plot of the result of the experiment. Thank you 
very much.

Best Regards,

Maximiliano
[image: tiempo rfsoc request3.png]
P.S: Here is the code:
#include <iostream>
#include <time.h>
#include <fstream>
#include <chrono>
#include <string>
#include <vector>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h> // For inet_pton and sockaddr_in
#include <sys/socket.h> // For socket(), send(), recv()
#include <csignal> // For signal handling
#include <sstream> // For stringstream

#define SERVER_IP "169.254.67.120" // IP address of the KATCP server
#define SERVER_PORT 7147 // Port number for KATCP connection
#define BUFFER_SIZE 8192 // Size of the buffer for receiving spectrum

// Global variables to store delay statistics
long long total_time = 0;
long long min_time = 1000000;
long long max_time = 0;
int num_iterations = 0;

// CSV file stream
std::ofstream csv_file;
// Start time for calculating elapsed time
std::chrono::steady_clock::time_point start_time;

void handle_sigint(int sig) {
std::cout << "\nReceived SIGINT (Ctrl+C). Calculating delay statistics...\n"
;
if (num_iterations > 0) {
std::cout << "Total Time (us): " << total_time << std::endl;
std::cout << "Average Time (us): " << total_time / num_iterations << std::
endl;
std::cout << "Minimum Time (us): " << min_time << std::endl;
std::cout << "Maximum Time (us): " << max_time << std::endl;
} else {
std::cout << "No iterations were completed.\n";
}

// Close the CSV file before exiting
if (csv_file.is_open()) {
csv_file.close();
}

exit(0);
}

// Function to create and send a message via KATCP
void send_message(int sock, const std::string& message) {
ssize_t bytes_sent = send(sock, message.c_str(), message.size(), 0);
if (bytes_sent == -1) {
perror("send");
close(sock);
exit(EXIT_FAILURE);
}
//std::cout << "Sent message: " << message << std::endl;
}

// Function to receive and process a spectrum from the KATCP server
void receive_spectrum(int sock) {
std::vector<char> buffer(BUFFER_SIZE);
ssize_t bytes_received = recv(sock, buffer.data(), buffer.size() - 1, 0);
if (bytes_received == -1) {
perror("recv");
close(sock);
exit(EXIT_FAILURE);
}

buffer[bytes_received] = '\0'; // Null-terminate the received data

// Print out the raw data (spectrum)
//std::cout << "Received spectrum data:\n" << buffer.data() << std::endl;

// Process the spectrum data if necessary. You can parse it here if it's in 
a specific format.
}

std::string get_current_date_string() {
auto now = std::chrono::system_clock::now();
auto time_t_now = std::chrono::system_clock::to_time_t(now);
struct tm *time_info = localtime(&time_t_now);

// Create a stringstream to format the date into YYYYMMDD_HHMMSS
std::stringstream date_stream;
date_stream << (1900 + time_info->tm_year) 
<< (1 + time_info->tm_mon) 
<< time_info->tm_mday
<< "_"
<< time_info->tm_hour 
<< time_info->tm_min 
<< time_info->tm_sec;
return date_stream.str();
}


int main() {
int sock;
struct sockaddr_in server_address;
// Register signal handler for SIGINT (Ctrl+C)
signal(SIGINT, handle_sigint);
std::string message = "?read synth0_0 2048 2048 \n"; // Replace with the 
actual KATCP message

// Create a socket for the KATCP connection
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("socket");
exit(EXIT_FAILURE);
}

// Set up the server address structure
std::memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(SERVER_PORT);
if (inet_pton(AF_INET, SERVER_IP, &server_address.sin_addr) <= 0) {
perror("inet_pton");
close(sock);
exit(EXIT_FAILURE);
}

// Connect to the KATCP server
if (connect(sock, reinterpret_cast<struct sockaddr*>(&server_address), 
sizeof(server_address)) == -1) {
perror("connect");
close(sock);
exit(EXIT_FAILURE);
}

std::cout << "Connected to KATCP server at " << SERVER_IP << ":" << 
SERVER_PORT << std::endl;

// Open CSV file to store time differences
std::string filename = "time_differences_" + get_current_date_string() + 
".csv";

csv_file.open(filename);
if (!csv_file.is_open()) {
std::cerr << "Failed to open CSV file for writing!" << std::endl;
exit(EXIT_FAILURE);
}

// Write the CSV header
csv_file << "Iteration,Time Difference (us),Elapsed Time (us)\n";

// Record the start time
start_time = std::chrono::steady_clock::now();

// Start the loop to measure delay statistics
while (true){
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now
();
// Send the KATCP message
send_message(sock, message);
// Receive the spectrum data
receive_spectrum(sock);
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now
();
// Calculate round-trip time in microseconds
long long round_trip_time = std::chrono::duration_cast<std::chrono::
microseconds>(end - begin).count();
num_iterations++;

// Calculate elapsed time since the start of the program
long long elapsed_time = std::chrono::duration_cast<std::chrono::
microseconds>(end - start_time).count();

// Update delay statistics
total_time += round_trip_time;
if (round_trip_time < min_time) min_time = round_trip_time;
if (round_trip_time > max_time) max_time = round_trip_time;
// Write the time difference and elapsed time to the CSV file
csv_file << num_iterations << "," << round_trip_time << "," << elapsed_time 
<< "\n";

// Print the individual delay for this iteration (optional, can be 
commented out to reduce output)
std::cout << "Time difference = " << round_trip_time << "[us]" << std::endl;
}

close(sock);
return 0;
}

-- 
You received this message because you are subscribed to the Google Groups 
"casper@lists.berkeley.edu" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to casper+unsubscr...@lists.berkeley.edu.
To view this discussion visit 
https://groups.google.com/a/lists.berkeley.edu/d/msgid/casper/9d65cf79-ba85-46e0-b594-6b1295754fb0n%40lists.berkeley.edu.

Reply via email to