如何修复 PyVISA 找不到任何 USB 仪器
问题:
你正在尝试使用 PyVISA 和 pyvisa-py 连接到 USB 仪器,但 PyVISA 资源管理器找不到任何仪器:
pyvisa_list_resources.py
#!/usr/bin/env python3
import visa
rm = visa.ResourceManager()
print(rm.list_resources()) # 打印 "()" => 未找到仪器!解决方案
为了让 pyvisa-py 能够连接到 USB 仪器,你需要安装 Python usb 库!
在 Debian 或 Ubuntu 上,使用以下命令安装
install_python3_usb.sh
sudo apt-get -y install python3-usb或者,如果你仍在使用 Python 2.x
install_python_usb.sh
sudo apt-get -y install python-usb现在,重新运行脚本 - 你应该看到类似这样的输出
pyvisa_example_output.py
('USB0::6833::3601::DL3A204800938::0::INSTR',)如果你仍然看不到输出,运行 python3 -m visa info 或 python -m visa info(对于 Python 2.x)。
它应该显示类似这样的输出:
visa_info_output.txt
Machine Details:
Platform ID: Linux-4.19.0-5-686-i686-with-debian-10.0
Processor:
Python:
Implementation: CPython
Executable: /usr/bin/python3
Version: 3.7.3
Compiler: GCC 8.3.0
Bits: 32bit
Build: Apr 3 2019 05:39:12 (#default)
Unicode: UCS4
PyVISA Version: 1.9.1
Backends:
ni:
Version: 1.9.1 (bundled with PyVISA)
Binary library: Not found
py:
Version: 0.3.1
ASRL INSTR: Available via PySerial (3.4)
USB INSTR: Available via PyUSB (1.0.2). Backend: libusb1
USB RAW: Available via PyUSB (1.0.2). Backend: libusb1
TCPIP INSTR: Available
TCPIP SOCKET: Available
GPIB INSTR:
Please install linux-gpib to use this resource type.
No module named 'gpib'检查 Backends -> py -> USB INSTR:如果它不是 Available via PyUSB,检查信息消息以获取可能问题的提示。例如,如果它说
pyvisa_no_usb_module_error.txt
USB INSTR:
Please install PyUSB to use this resource type.
No module named 'usb'这意味着 Python USB 库未正确安装。
如果 USB 是 Available via PyUSB 但 PyVISA 仍然找不到仪器,使用以下命令检查它是否正确连接
lsusb.sh
lsusb它应该显示与你的仪器制造商相关的一行,例如
lsusb_rigol_output.txt
Bus 001 Device 002: ID 1ab1:0e11 Rigol Technologies还要拔下并重新插入你的仪器,以便 Linux 尝试重新连接到 USB 设备,并检查 sudo dmesg 输出的末尾,它可能列出例如
dmesg_usb_output.txt
[19427.230120] usb 1-2: new high-speed USB device number 2 using ehci-pci
[19427.425464] usb 1-2: config 1 interface 0 altsetting 0 bulk endpoint 0x82 has invalid maxpacket 64
[19427.425469] usb 1-2: config 1 interface 0 altsetting 0 bulk endpoint 0x3 has invalid maxpacket 64
[19427.425947] usb 1-2: New USB device found, idVendor=1ab1, idProduct=0e11, bcdDevice= 0.02
[19427.425950] usb 1-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[19427.425953] usb 1-2: Product: DL3000 Serials
[19427.425955] usb 1-2: Manufacturer: Rigol Technologies.
[19427.425957] usb 1-2: SerialNumber: DL3A204800938
[19429.525745] usbcore: registered new interface driver usbtmc最后一行中的 usbtmc 意味着 USB 设备已被识别为USB 测试与测量类设备,因此你应该能够使用 PyVISA 作为 USB INSTR 连接到它。
Check out similar posts by category:
Electronics, Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow