首页 > 解决方案 > 使用 pyvisa 从示波器保存波形数据时出现缓冲区错误

问题描述

我正在尝试使用 RPi4 上的 PyVisa 与我的 Rigol DS1104Z 示波器进行通信。我已经能够捕获基本测量结果,但还没有波形数据。我发现了大约 5 个示例,但它们都不再起作用了,因为 PyVisa 删除了函数而没有提供等效的替换。

这是我能找到的最新绘图示例...

import os
import pyvisa as visa
import numpy as np

import time

from matplotlib.ticker import LinearLocator, FormatStrFormatter
from matplotlib import cm
import matplotlib.pyplot as plt

#Get the USB device, e.g. 'USB0::0x1AB1::0x0588::DS1ED141904883'
resources = visa.ResourceManager()
usbDevices = list(filter(lambda x: 'USB' in x, resources.list_resources()))
if len(usbDevices) == 0:
    print("no usb devices found")
    sys.exit(-1)
print(usbDevices[0])
myScope = resources.open_resource(usbDevices[0])
print(myScope.query('*IDN?')) #Return the Rigol’s ID string to tell us it’s there

# Initialize our scope
test = myScope

test.write(":RUN")
time.sleep(1)

test.write(":STOP")
 
# Grab the data from channel 1
test.write(":WAV:POIN:MODE RAW")

# Get the voltage scale
voltscale1 = float(test.query(":CHAN1:SCAL?"))
voltscale2 = float(test.query(":CHAN2:SCAL?"))
 
# And the voltage offset
voltoffset1 = float(test.query(":CHAN1:OFFS?"))
voltoffset2 = float(test.query(":CHAN2:OFFS?"))
 
# Get the timescale
timescale1 = float(test.query(":TIM:SCAL?"))
timescale2 = float(test.query(":TIM:SCAL?"))
 
# Get the timescale offset
timeoffset1 = float(test.query(":TIM:OFFS?"))
timeoffset2 = float(test.query(":TIM:OFFS?"))

finalDataChan1 = []

data1 = np.array(myScope.query_binary_values(":WAV:DATA? CHAN1",datatype='B')[10:])

哪里(程序的其余部分被省略,因为)我已经将问题隔离到最后一行。

我收到以下错误:

USB0::6833::1230::DS1ZD223400795::0::INSTR
/usr/local/lib/python3.7/dist-packages/pyvisa_py/protocols/usbtmc.py:116: UserWarning: Unexpected MsgID format. Consider updating the device's firmware. See https://github.com/pyvisa/pyvisa-py/issues/20
  "Unexpected MsgID format. Consider updating the device's firmware. See https://github.com/pyvisa/pyvisa-py/issues/20"
RIGOL TECHNOLOGIES,DS1104Z Plus,DS1ZD223400795,00.04.04.SP4

Traceback (most recent call last):
  File "/home/pi/Documents/projects/cu/pnaci/rigol/testu2.py", line 57, in <module>
    data1 = np.array(myScope.query_binary_values(":WAV:DATA? CHAN1",datatype='B')[10:])
  File "/usr/local/lib/python3.7/dist-packages/pyvisa/resources/messagebased.py", line 754, in query_binary_values
    chunk_size,
  File "/usr/local/lib/python3.7/dist-packages/pyvisa/resources/messagebased.py", line 602, in read_binary_values
    self.read_bytes(expected_length - len(block), chunk_size=chunk_size)
  File "/usr/local/lib/python3.7/dist-packages/pyvisa/resources/messagebased.py", line 371, in read_bytes
    chunk, status = self.visalib.read(self.session, size)
  File "/usr/local/lib/python3.7/dist-packages/pyvisa_py/highlevel.py", line 512, in read
    data, status_code = self.sessions[session].read(count)
  File "/usr/local/lib/python3.7/dist-packages/pyvisa_py/usb.py", line 156, in read
    USBTimeoutException,
  File "/usr/local/lib/python3.7/dist-packages/pyvisa_py/sessions.py", line 793, in _read
    current = reader()
  File "/usr/local/lib/python3.7/dist-packages/pyvisa_py/usb.py", line 133, in _usb_reader
    return self.interface.read(count)
  File "/usr/local/lib/python3.7/dist-packages/pyvisa_py/protocols/usbtmc.py", line 465, in read
    response = BulkInMessage.from_bytes(resp)
  File "/usr/local/lib/python3.7/dist-packages/pyvisa_py/protocols/usbtmc.py", line 113, in from_bytes
    msgid, btag, btaginverse = struct.unpack_from("BBBx", data)
struct.error: unpack_from requires a buffer of at least 4 bytes

标签: python-3.xmatplotlibplotraspberry-pi4pyvisa

解决方案


推荐阅读