0

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.

shirazn
  • 11
  • _It seems to get stuck at the `client.recv(4096)`_ There is no such line of code anywhere in your post... – John Gordon Sep 03 '23 at 00:48
  • 1
    @JohnGordon sorry about that. I meant chunk = client.recv(1048). Its in the while loop under the "Receive acquired data". Sorry its my first time posting here. – shirazn Sep 03 '23 at 00:57
  • What exactly do you mean it gets "stuck"? Do you mean the loop keeps going longer than you expected, and you see the output "everything ok" printed again and again and again, forever? – John Gordon Sep 03 '23 at 01:14
  • Likely not your problem, but `send` isn't guaranteed to send everything you give it. Use `sendall` instead. – tdelaney Sep 03 '23 at 01:15
  • Are you sure that your python code connects to the red pitaya ? – Juliano Negri Sep 03 '23 at 01:18
  • a recv that hangs forever means that the server stopped sending stuff but didn't shutdown the socket. The problem could be on the other side. Does this code work for multiple send/recv cycles before stopping? Could it be that your calculation of how much data to expect is wrong? Is it 2 bytes to receive or 2 characters, which could potentially be encoded in multiple bytes? – tdelaney Sep 03 '23 at 01:22
  • @JohnGordon Actually the 'everything ok" gets printed once. I just added it to check if everything checks out to that point. No other thing gets printed after the chunk=client.recv line. Thanks. – shirazn Sep 03 '23 at 01:23
  • Another aside, when doing network communications, be specific about encoding. `.encode()` uses the default for the system, which could be a bug on a system with a different encoding. – tdelaney Sep 03 '23 at 01:23
  • You could do `print("everything ok", received_samples, num_samples_per_iteration)` before the receive and `print(data)` after data.extend, then you'd have a better look at the flow. – tdelaney Sep 03 '23 at 01:27
  • @tdelaney Its supposed receive voltage amplitude over time in bytes. The code doesn't even run one cycle. Like the plot window doesn't even open up. Thanks. – shirazn Sep 03 '23 at 01:37
  • @tdelaney I get "everything ok 0 1000000" for 'print("everything ok", received_samples, num_samples_per_iteration)' but the 'print(data)' does not print anything. I found that any code after the 'chunk = client.recv' doesn't run. Not even print. Thanks very much for your help so far though. – shirazn Sep 03 '23 at 01:45
  • @JulianoNegri yes. the connection works because one i switch of the redpitaya whiles the code is running, I get an error message saying a connection has been interrupted.Thanks – shirazn Sep 03 '23 at 01:46
  • So it seems pitaya is not sending data back... take a look at the doc to check if you are using the right commands maybe? I found this sample https://github.com/RedPitaya/RedPitaya/blob/master/Examples/python/redpitaya_scpi.py – Juliano Negri Sep 03 '23 at 01:59

1 Answers1

0

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

Juliano Negri
  • 299
  • 2
  • 9