0

I'm sending data in python between two connect machines within the same network (successfully connected).

The publisher code is defined as:

def senderzmq(self, lat, lon):
    sock = zmq.Context().socket(zmq.PUB)
    sock.bind("tcp://*:10000")
    running = True
    while running:
        sock.send_string(f"Lat {lat}, Lon={lon}")

The receiver has the following format:

import zmq
context=zmq.Context()
socket=context.socket(zmq.SUB)
socket.connect("tcp://192.168.1.35:10000")
while True:
         message=socket.recv_string()
         print(message)

The function senderzmq is trigger by:

print(f"Lat {self.lat}, Lon={self.lon}")
self.senderzmq(self.lat, self.lon)

self.lat and self.lon are updated from this static method:

@staticmethod
    def gnss(event):
        self.lat = event.latitude + LAT0
        self.lon = event.longitude + LON0

both LAT0 and LON0 are manually configured.

The above development works, however, the sender always sends the same data, it looks the sender remains within the while True state not listening new data. If I remove the while True, the above doesn't work. Any idea how to exit the while True but transmitting new data at each loop?

Thanks. Raúl

8
  • I don't know anything about zmq, but doesn't your sender code run an infinite loop, continuously sending the exact same data as quickly as it can? (that while running loop never seems to pause or end) Commented Nov 22, 2023 at 12:52
  • Yes, it sends the exact same data and as quickly as it can. If tried to insert a running False at the end of the loop, however, doesn't work. Commented Nov 22, 2023 at 12:58
  • Please update your question with sample code which calls your senderzmq(self ...) method. Commented Nov 22, 2023 at 13:03
  • I've updated the question with a sample code in which there is a print of the original data gathered from some sensors and this data and using them in senderzmq function. Commented Nov 22, 2023 at 13:24
  • 1
    Your static method cannot work by definition. Where is the self coming from where you have: self.lat = event.lat ... ? Commented Nov 22, 2023 at 13:41

1 Answer 1

1

I think the part you're missing in subscriber is the following:

socket.setsockopt(zmq.SUBSCRIBE, b"") # or socket.subscribe(b"")

In ZMQ, you should either subscribe to a wildcard ("") or specify a topic (e.g. socket.subscribe(b"TOPIC_NAME")).

I tested the code with the following snippet: publisher.py

import zmq
import time


def main():
    context = zmq.Context()
    sock = context.socket(zmq.PUB)
    sock.bind("tcp://*:5563")
    running = True
    while running:
        sock.send_string("Lat=42, Lon=42")
        time.sleep(1.0)


if __name__ == "__main__":
    main()

subsriber.py:

import zmq

context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://localhost:5563")
socket.setsockopt(zmq.SUBSCRIBE, b"")

while True:
    message = socket.recv_string()
    print(message)

and it worked.

I would also recommend you to quickly walk through zmq guide (you can always turn to "python" for the code snippets there).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.