Python program gets stuck at client.recv without an error message

65 Views Asked by At

I have the code below to continuously generate a signal from the OUT1 of redpitaya and simultaneously detect on the IN1 of the redpitaya. The problem is that the code just keeps running without displaying any plot or output. It seems to get stuck at the client.recv(4096). What drives me crazy is that it doesn't display any error message. Does anyone know how I can go around this? Thank you in advance.

import socket
import numpy as np
import matplotlib.pyplot as plt
import time

# Red Pitaya configuration
RP_IP = "169.254.79.236"  #  Red Pitaya's IP address
RP_OUT_CHANNEL = "out1"    # Output channel (e.g., out1 for Output 1)
RP_IN_CHANNEL = "in1"      # Input channel (e.g., in1 for Input 1)
RP_SAMPLE_RATE = 1e6       # Sample rate in Hz (adjust as needed)
RP_WAVEFORM_FREQUENCY = 1e3  # Signal frequency in Hz (adjust as needed)
RP_AMPLITUDE = 1           # Signal amplitude (adjust as needed)

# Create a socket client
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
RP_PORT = 5000  # Red Pitaya's default port for data streaming

# Connect to Red Pitaya
client.connect((RP_IP, RP_PORT))

# Send commands to Red Pitaya
client.send(f"ACQ:RST\n".encode())  # Reset acquisition
client.send(f"ACQ:START\n".encode())  # Start acquisition

# Configure the output signal
client.send(f"SOUR:{RP_OUT_CHANNEL}:FUNC SINE\n".encode())
client.send(f"SOUR:{RP_OUT_CHANNEL}:FREQ {RP_WAVEFORM_FREQUENCY}\n".encode())
client.send(f"SOUR:{RP_OUT_CHANNEL}:AMPL {RP_AMPLITUDE}\n".encode())
client.send(f"SOUR:{RP_OUT_CHANNEL}:OUTP ON\n".encode())

# Define the number of samples you want to receive in each iteration
num_samples_per_iteration = int(RP_SAMPLE_RATE * 1)  # Adjust as needed

while True:
    # Acquire data
    client.send(f"ACQ:SOUR {RP_IN_CHANNEL}\n".encode())
    client.send(f"ACQ:TRIG:LEVEL 0.0\n".encode())  # Set trigger level (adjust as needed)
    client.send(f"ACQ:TRIG:DLY 0\n".encode())      # Set trigger delay
    client.send(f"ACQ:AVG OFF\n".encode())          # Disable averaging
    client.send(f"ACQ:START\n".encode())            # Start acquisition

    # Receive acquired data
    data = bytearray()
    received_samples = 0  # Keep track of the number of received samples
    while received_samples < num_samples_per_iteration:
        print("everything ok")
        chunk = client.recv(1048)
        if not chunk:
            break  # Exit the loop if no more data is received
        data.extend(chunk)
        received_samples = len(data) // 2  # Each sample is 2 bytes

    # Convert received data to a numpy array of integers
    acquired_data = np.frombuffer(data, dtype=np.int16)

    # Plot the acquired data
    time_axis = np.arange(0, len(acquired_data)) / RP_SAMPLE_RATE
    plt.plot(time_axis, acquired_data)
    plt.xlabel("Time (s)")
    plt.ylabel("Amplitude")
    plt.title("Acquired Data")
    plt.show()
client.close()

I tried to generate a signal using a redpitaya and acquire at the same time using the same device on python. I expected a continuous display of the signal in a plot window but instead nothing happens. I can confirm that there is a connection between my PC and the redpitaya but nothing displays.

1

There are 1 best solutions below

0
Juliano Negri On

That is by design. Recv only return when data is available or the connection is closed.

That a look at this other answer When does socket.recv(recv_size) return?

Probably the server is not sending data back to your client.

Check this answer for validating that the connection is still open:

Python - How to check if socket is still connected