GNU无线电ZMQ块代表 - REQ

发布于 2025-01-26 04:06:28 字数 860 浏览 3 评论 0原文

我正在尝试使用GR ZMQ REP / REQ块将GNU收音机连接到Python脚本。 GR在路由器地址上的Raspberry Pi 4上运行192.168.1.25。 Python脚本位于单独的计算机上,我可以成功地ping 192.168.1.25。我正在在55555和55556的单独端口上设置REQ-REP对。

流程图:

import pmt
import zmq

# create a REQ socket
req_address = 'tcp://192.168.1.25:55555'
req_context = zmq.Context()
req_sock = req_context.socket (zmq.REQ)
rc = req_sock.connect (req_address)

# create a REP socket
rep_address = 'tcp://192.168.1.25:55556'
rep_context = zmq.Context()
rep_sock = rep_context.socket (zmq.REP)
rc = rep_sock.connect (rep_address)

while True:
    data = req_sock.recv()
    print(data)

    rep_sock.send (b'1')

运行此代码会导致以下错误: ZMQERROR:操作无法在当前状态下完成

该错误在此行上标记: data = req_sock.recv()

您可以评论错误原因吗?我知道有一个严格的req-rep,req-rep ..关系,但是我找不到我的错误。

I am trying to connect GNU Radio to a python script using the GR ZMQ REP / REQ blocks. GR is running on a Raspberry Pi 4 on router address 192.168.1.25. The python script is on a separate computer, from which I can successfully ping 192.168.1.25. I am setting up the REQ-REP pairs on separate ports, 55555 and 55556.

Flow graph:
enter image description here

import pmt
import zmq

# create a REQ socket
req_address = 'tcp://192.168.1.25:55555'
req_context = zmq.Context()
req_sock = req_context.socket (zmq.REQ)
rc = req_sock.connect (req_address)

# create a REP socket
rep_address = 'tcp://192.168.1.25:55556'
rep_context = zmq.Context()
rep_sock = rep_context.socket (zmq.REP)
rc = rep_sock.connect (rep_address)

while True:
    data = req_sock.recv()
    print(data)

    rep_sock.send (b'1')

Running this code leads to the following error:
ZMQError: Operation cannot be accomplished in current state

The error is flagged at this line:
data = req_sock.recv()

Can you comment on the cause of the error? I know there is a strict REQ-REP, REQ-REP.. relationship, but I cannot find my error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2025-02-02 04:06:28

您当前的代码有两个问题:

  • 您调用req_socket.recv(),但随后您调用rep_sock.send():这不是Req/Rep Pair作品。您只需要创建一个套接字(req套接字);它连接到遥控插座。

  • 创建REQ套接字时,您需要在收到答复之前发送请求。

此外,即使您有多个插座,也只能创建一个ZMQ上下文。

您的代码的功能版本可能是这样的:

import zmq

# create a REQ socket
ctx = zmq.Context()
req_sock = ctx.socket (zmq.REQ)

# connect to a remote REP sink
rep_address = 'tcp://192.168.1.25:55555'
rc = req_sock.connect(rep_address)

while True:
    req_sock.send (b'1')
    data = req_sock.recv()
    print(data)

我针对以下GNU无线电配置测试了上述代码:

options:
  parameters:
    author: ''
    catch_exceptions: 'True'
    category: '[GRC Hier Blocks]'
    cmake_opt: ''
    comment: ''
    copyright: ''
    description: ''
    gen_cmake: 'On'
    gen_linking: dynamic
    generate_options: qt_gui
    hier_block_src_path: '.:'
    id: example
    max_nouts: '0'
    output_language: python
    placement: (0,0)
    qt_qss_theme: ''
    realtime_scheduling: ''
    run: 'True'
    run_command: '{python} -u {filename}'
    run_options: prompt
    sizing_mode: fixed
    thread_safe_setters: ''
    title: Example
    window_size: (1000,1000)
  states:
    bus_sink: false
    bus_source: false
    bus_structure: null
    coordinate: [8, 8]
    rotation: 0
    state: enabled

blocks:
- name: samp_rate
  id: variable
  parameters:
    comment: ''
    value: '32000'
  states:
    bus_sink: false
    bus_source: false
    bus_structure: null
    coordinate: [184, 12]
    rotation: 0
    state: enabled
- name: analog_sig_source_x_0
  id: analog_sig_source_x
  parameters:
    affinity: ''
    alias: ''
    amp: '1'
    comment: ''
    freq: '1000'
    maxoutbuf: '0'
    minoutbuf: '0'
    offset: '0'
    phase: '0'
    samp_rate: samp_rate
    type: complex
    waveform: analog.GR_COS_WAVE
  states:
    bus_sink: false
    bus_source: false
    bus_structure: null
    coordinate: [184, 292.0]
    rotation: 0
    state: true
- name: blocks_throttle_0
  id: blocks_throttle
  parameters:
    affinity: ''
    alias: ''
    comment: ''
    ignoretag: 'True'
    maxoutbuf: '0'
    minoutbuf: '0'
    samples_per_second: samp_rate
    type: complex
    vlen: '1'
  states:
    bus_sink: false
    bus_source: false
    bus_structure: null
    coordinate: [344, 140.0]
    rotation: 0
    state: true
- name: zeromq_rep_sink_0
  id: zeromq_rep_sink
  parameters:
    address: tcp://0.0.0.0:55555
    affinity: ''
    alias: ''
    comment: ''
    hwm: '-1'
    pass_tags: 'False'
    timeout: '100'
    type: complex
    vlen: '1'
  states:
    bus_sink: false
    bus_source: false
    bus_structure: null
    coordinate: [504, 216.0]
    rotation: 0
    state: true

connections:
- [analog_sig_source_x_0, '0', blocks_throttle_0, '0']
- [blocks_throttle_0, '0', zeromq_rep_sink_0, '0']

metadata:
  file_format: 1

Your current code has two problems:

  • You call req_socket.recv(), but then you call rep_sock.send(): that's not how a REQ/REP pair works. You only need to create one socket (the REQ socket); it connects to a remote REP socket.

  • When you create a REQ socket, you need to send a REQuest before you receive a REPly.

Additionally, you should only create a single ZMQ context, even if you have multiple sockets.

A functional version of your code might look like this:

import zmq

# create a REQ socket
ctx = zmq.Context()
req_sock = ctx.socket (zmq.REQ)

# connect to a remote REP sink
rep_address = 'tcp://192.168.1.25:55555'
rc = req_sock.connect(rep_address)

while True:
    req_sock.send (b'1')
    data = req_sock.recv()
    print(data)

I tested the above code against the following GNU Radio config:

options:
  parameters:
    author: ''
    catch_exceptions: 'True'
    category: '[GRC Hier Blocks]'
    cmake_opt: ''
    comment: ''
    copyright: ''
    description: ''
    gen_cmake: 'On'
    gen_linking: dynamic
    generate_options: qt_gui
    hier_block_src_path: '.:'
    id: example
    max_nouts: '0'
    output_language: python
    placement: (0,0)
    qt_qss_theme: ''
    realtime_scheduling: ''
    run: 'True'
    run_command: '{python} -u {filename}'
    run_options: prompt
    sizing_mode: fixed
    thread_safe_setters: ''
    title: Example
    window_size: (1000,1000)
  states:
    bus_sink: false
    bus_source: false
    bus_structure: null
    coordinate: [8, 8]
    rotation: 0
    state: enabled

blocks:
- name: samp_rate
  id: variable
  parameters:
    comment: ''
    value: '32000'
  states:
    bus_sink: false
    bus_source: false
    bus_structure: null
    coordinate: [184, 12]
    rotation: 0
    state: enabled
- name: analog_sig_source_x_0
  id: analog_sig_source_x
  parameters:
    affinity: ''
    alias: ''
    amp: '1'
    comment: ''
    freq: '1000'
    maxoutbuf: '0'
    minoutbuf: '0'
    offset: '0'
    phase: '0'
    samp_rate: samp_rate
    type: complex
    waveform: analog.GR_COS_WAVE
  states:
    bus_sink: false
    bus_source: false
    bus_structure: null
    coordinate: [184, 292.0]
    rotation: 0
    state: true
- name: blocks_throttle_0
  id: blocks_throttle
  parameters:
    affinity: ''
    alias: ''
    comment: ''
    ignoretag: 'True'
    maxoutbuf: '0'
    minoutbuf: '0'
    samples_per_second: samp_rate
    type: complex
    vlen: '1'
  states:
    bus_sink: false
    bus_source: false
    bus_structure: null
    coordinate: [344, 140.0]
    rotation: 0
    state: true
- name: zeromq_rep_sink_0
  id: zeromq_rep_sink
  parameters:
    address: tcp://0.0.0.0:55555
    affinity: ''
    alias: ''
    comment: ''
    hwm: '-1'
    pass_tags: 'False'
    timeout: '100'
    type: complex
    vlen: '1'
  states:
    bus_sink: false
    bus_source: false
    bus_structure: null
    coordinate: [504, 216.0]
    rotation: 0
    state: true

connections:
- [analog_sig_source_x_0, '0', blocks_throttle_0, '0']
- [blocks_throttle_0, '0', zeromq_rep_sink_0, '0']

metadata:
  file_format: 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文