Background:
Hello there, I am attempting to issue commands to and recieve data from a remote system via UHF antenna. In order to do this, I planned to have a python program running on one computer send and receive data to and from a GNU Radio program on another computer. In order to make this link, I decided to use ZeroMQ, specifically the PUB and SUB blocks. The link between the remote UHF and GNU Radio works well, but I am having trouble with the link between GNU Radio and my python script.
As far as I can tell, using ZMQ PUB on GNU Radio and SUB in Python works perfectly. However, I am having trouble going from Python to GNU Radio.
The Setup:
Here is my Python test code:
import zmq
import time
import pmt
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.connect("tcp://127.0.0.1:5557")
i=1
while True:
message = str("(Message #"+str(i)+") Hello!\n")
res = bytes(message, 'utf-8')
socket.send(res)
print("Sending "+str(i))
time.sleep(0.01)
i+=1
Here is my GNU Radio flowgraph:
A ZMQ PUB block connected to a file sink block
The Python script sends a numbered message every so often and the GNU Radio program writes all received messages to a text file. For testing purposes this is done over localhost.
I also have a Python script that receives data using ZMQ SUB:
import zmq
import time
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.bind("tcp://127.0.0.1:5556")
socket.subscribe("")
while True:
try:
message = socket.recv(flags=zmq.NOBLOCK)
print("Message rx")
print(str(message))
except zmq.Again as e:
e=1
time.sleep(0.01)
The Problem:
I run the above code by first starting the GNU Radio flowgraph and then quickly starting the Python PUB script. I then wait several seconds for a few hundred messages to send, shut down the Python script, and then shut down the GNU Radio flowgraph. I receive the expected output in the text file (minus a few messages at the beginning or end).
However, when I increase the value in time.sleep()
, the resulting text file will be empty. I have been unable to nail down the exact value this happens at, as it appears to be somewhat inconsistent. I have almost never successfully received the messages at values higher than 0.1 seconds, and I have almost always successfully received the messages at values below 0.02 seconds.
When I attempt to receive the messages via my Python SUB script, it works properly regardless of the time interval (although often the first few messages will be missing but I believe this is expected behavior).
Ideally, I would be able to start this GNU Radio flowgraph and send and receive data to and from it with variable intervals ranging from fractions of a second to several hours.
What I have tried:
I have tried:
- Swapping connect/bind on both ends
- Adjusting the GNU Radio SUB block's timeout value
- Starting the programs in different orders
- Adjusting the flowgraph's sample rate (shouldn't be used but I thought I'd try anyway)
- Formatting outgoing messages with
pmt.serialize_str(pmt.to_pmt())
and various data type - Waiting several seconds after flowgraph startup before sending messages
- Toggling the SUB block's "Pass Tags" value
- Sending message in bursts, with 10 sacrificial messages, once every 0.01 seconds, followed by one content message, followed by a 1 second delay. This sequence is repeated indefinetely.
All of the above either did not fix the issue or prevented message reception all together.
Expected behavior is that when I send a message using the Python script, with any interval, it will show up in the text file output by the GNU Radio flowgraph.
Other information:
Operating system is Windows 10.
I am using Python 3.10.10.
For these tests, GNU Radio is also running on Windows and was installed as part of Radioconda, but will be run on Ubuntu outside of testing.
If this problem cannot be solved, I am open to other methods to intermittently transfer data between GNU Radio and an external program, please let me know if I am trying to use the wrong tool for the job. It does not necessarily have to be over the internet, it can be done on the same machine if necessary. I have also not tried the other two types of ZMQ blocks, if those would be more applicable, please let me know.