Python:通过套接字接收数据 - [Errno 11] 资源暂时不可用

3

背景

我需要用Python与Tektronix MSO 4104进行通信。通信是通过LAN使用vxi11以太网协议和Python的socket库实现的。

情况

现在这个方法相当有效;我可以连接到示波器,我可以发送任何想要的命令(例如:<socket object>.send('*IDN?'))。然而,每当一个命令应该发送一个响应(像*IDN?应该做的那样),我尝试使用<socket object>.recv(1024),但我总是收到错误消息"[Errno 11] Resource temporarily unavailable."

我知道连接是良好的,因为我可以通过内置的HTTP接口从相同的'*IDN?'提示接收信息。

代码

以下是scope.py中创建与示波器接口的代码片段。

import socket
import sys
import time

class Tek_scope(object):
    '''
    Open up socket connection for a Tektronix scope given an IP address
    '''
    def __init__(self, IPaddress, PortNumber = 4000):
        self.s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
        self.s.connect((IPaddress, PortNumber))
        self.s.setblocking(False)
        print "Scope opened Successfully"

现在我要获取错误信息,我运行以下命令:
import scope # Imports the above (and other utility functions)

scope1 = scope.Tek_scope("10.1.10.15") #Connects to the scope

scope1.s.send('*IDN?') #Sends the *IDN? command to the scope. 

# I have verified these signals are always recieved as I can 
# see them reading out on the display

scope1.s.recv(1024) 

# This should receive the response... but it always gives the error

系统

  • Fedora 16
  • Python 2.7
  • Tektronix MSO4104

问题

为什么我没有收到任何响应我的提示的数据?是我忘记了某种准备工作吗?数据是否被发送到我没有检查的地方?还是我只是错误地使用了模块?任何帮助将不胜感激!

1个回答

4

使用相同的作用域,这对我有效。

设置setblocking(True),并将\n添加到*IDN?命令中。

import socket
import sys
import time

class Tek_scope(object):

    def __init__(self, IPaddress, PortNumber = 4000):
        self.s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
        self.s.connect((IPaddress, PortNumber))
        self.s.setblocking(True)
        print "Scope opened Successfully"

scope1 = Tek_scope("10.1.10.15") # Connects to the scope

scope1.s.send('*IDN?\n') # Sends the *IDN? command to the scope. 

print scope1.s.recv(1024) 

嘿,谢谢,那确实起作用了!不幸的是,我最终放弃了一切,改用C语言重新编写。但还是感谢你提供的修复方案! - Bacaa14

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接