Editing ZMQ REQ Message Source

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 25: Line 25:
An external Python program can send messages to a ZMQ REQ Message Source block. An example flowgraph and Python code follow.<br>
An external Python program can send messages to a ZMQ REQ Message Source block. An example flowgraph and Python code follow.<br>
The Python client code looks like this:<br>
# -*- coding: utf-8 -*-
# zmqREP.py
#  The REQest / REPly nomenclature of the GNU Radio message blocks is from
#  the perspective of the flowgraph. So, to send a 'request' to GNU Radio, the message
#  must be sent as a 'reply' from the Python client, Likewise, a 'reply' from GNU Radio
#  must be received as a 'request' to the Python client! Therefore, send on the reply socket
#  and receive on the request socket.
#  The zeromq.org website says:
#  "The REQ-REP socket pair is in lockstep. The client issues zmq_send() and then zmq_recv(),
#  in a loop (or once if that's all it needs). Doing any other sequence (e.g., sending two messages in a row)
#  will result in a return code of -1 from the send or recv call." Likewise, the server "issues zmq_recv()
#  and then zmq_send() in that order, as often as it needs to."
#  To conform to that requirement, a non-standard "kludge" is used (see below).
import datetime
import time
import signal
import pmt
import zmq
_debug = 1          # set to zero to turn off diagnostics
# create socket
_PROTOCOL = "tcp://"
_SERVER = ""          # localhost
_PORT = ":50123"
if (_debug):
    print ("zmqREP connecting to:", _ADDR)
context = zmq.Context()
if (_debug):
    assert (context)
sock = context.socket (zmq.REP)
if (_debug):
    assert (sock)
rc = sock.bind (_ADDR)
if (_debug):
    assert (rc == None)
while True:
    _tim = datetime.datetime.now()
    _out = str (_tim)
        sock.recv()                                # this is the non-standard "kludge"
        sock.send (pmt.serialize_str(pmt.to_pmt(_out)))      # send on the 'reply' socket
    except KeyboardInterrupt:
        if (_debug):
            print (" Interrupt received. shutting down.")
        # clean up
=== GNU Radio as a server ===
=== GNU Radio as a server ===

Please note that all contributions to GNU Radio are considered to be released under the Creative Commons Attribution-ShareAlike (see GNU Radio:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel | Editing help (opens in new window)