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 93: Line 93:
 
=== GNU Radio as a server ===
 
=== GNU Radio as a server ===
  
If the GNU Radio flowgraph(s) is configured as a server, the REQ message is processed by the flowgraph and a message is sent back in a REP message as the response. An example flowgraph and Python code follow. Note that two different ports are used.<br>
+
If the GNU Radio flowgraph(s) is configured as a server, the REQ message is processed by the flowgraph and a message is sent back in a REP message as the response. An example flowgraph and Python code follow.<br>
  
 
[[File:Server_demo_fg.png]]
 
[[File:Server_demo_fg.png]]
Line 102: Line 102:
 
from gnuradio import gr
 
from gnuradio import gr
 
import pmt
 
import pmt
 +
 +
inputText = ""
  
 
class my_sync_block (gr.sync_block):
 
class my_sync_block (gr.sync_block):
Line 117: Line 119:
  
 
     def handle_msg(self, msg):
 
     def handle_msg(self, msg):
 +
        global inputText
 
         inputText = pmt.symbol_to_string (msg)
 
         inputText = pmt.symbol_to_string (msg)
 
         # print (inputText)
 
         # print (inputText)
Line 129: Line 132:
 
         # with no data ports, there is nothing to do
 
         # with no data ports, there is nothing to do
 
         return (0)
 
         return (0)
</pre>
 
 
The Python client code looks like this:<br>
 
 
<pre>
 
#!/usr/bin/python3
 
# -*- coding: utf-8 -*-
 
 
# zmq_REQ_client.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 a REQ socket
 
_PROTOCOL = "tcp://"
 
_SERVER = "127.0.0.1"          # localhost
 
_REQ_PORT = ":49202"
 
_REQ_ADDR = _PROTOCOL + _SERVER + _REQ_PORT
 
if (_debug):
 
    print ("'zmq_REQ_client' connecting to:", _REQ_ADDR)
 
req_context = zmq.Context()
 
if (_debug):
 
    assert (req_context)
 
req_sock = req_context.socket (zmq.REQ)
 
if (_debug):
 
    assert (req_sock)
 
rc = req_sock.connect (_REQ_ADDR)
 
if (_debug):
 
    assert (rc == None)
 
 
# create a REP socket
 
_PROTOCOL = "tcp://"
 
_SERVER = "127.0.0.1"          # localhost
 
_REP_PORT = ":49201"
 
_REP_ADDR = _PROTOCOL + _SERVER + _REP_PORT
 
if (_debug):
 
    print ("'zmq_REQ_client' binding to:", _REP_ADDR)
 
rep_context = zmq.Context()
 
if (_debug):
 
    assert (rep_context)
 
rep_sock = rep_context.socket (zmq.REP)
 
if (_debug):
 
    assert (rep_sock)
 
rc = rep_sock.bind (_REP_ADDR)
 
if (_debug):
 
    assert (rc == None)
 
 
while True:
 
    # generate an outgoing message
 
    _tim = datetime.datetime.now()
 
    _out = ("Local time is " + str (_tim))
 
    try:
 
        print(_out)
 
        rep_sock.recv()                                        # this is the non-standard "kludge"
 
        rep_sock.send (pmt.serialize_str(pmt.to_pmt(_out)))    # send on the 'reply' socket
 
        time.sleep(1)
 
        req_sock.send_string("\x01\x00\x00\x00")        # this is the non-standard "kludge"
 
        msg = req_sock.recv()                          # receive on the 'request' socket
 
        print (pmt.to_python(pmt.deserialize_str(msg)))
 
        time.sleep(4)
 
    except KeyboardInterrupt:
 
        if (_debug):
 
            print (" Interrupt received. shutting down.")
 
        # clean up
 
        req_sock.close()
 
        req_context.term()
 
        rep_sock.close()
 
        rep_context.term()
 
        exit()
 
 
</pre>
 
</pre>
  

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)