Python 中的 USBTMC
USBTMC in Python
我正在尝试通过 USB 与我的 Clarke-Hess Phase Meter 6000A 通话。为此,我正在使用 https://github.com/python-ivi/python-usbtmc/tree/master/usbtmc。现在,当我连接 USB 数据线并执行 lsusb
时,它会生成
Bus 001 Device 027: ID 0403:b972 Future Technology Devices International, Ltd
作为我识别的设备,所以我简单地使用了usbtmc
并得到了以下错误:
代码
>>> import usbtmc
>>> inst = usbtmc.Instrument(0x0403,0xb972)
Find USBTMC instrument
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "usbtmc/usbtmc.py", line 229, in __init__
raise UsbtmcException("Device not found", 'init')
usbtmc.usbtmc.UsbtmcException: Device not found [init]
但是当我查看 usbtmc.py 时,我发现在 list_devices() 中,usb.util.find_descriptor() 与参数一起使用,如 USBTMC_bInterfaceClass = 0xFE,并且USBTMC_bInterfaceSubClass = 3,这使得它无法检测到我的设备,其描述符为 bInterfaceClass 和 bInterfaceSubClass = 0xff。
然后我把USBTMC_bInterfaceClass和USBTMC_bInterfaceSubClass改成了0xff,然后得到:
代码
>>> import usbtmc
>>> inst = usbtmc.Instrument(0x0403,0xb972)
Find USBTMC instrument
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "usbtmc/usbtmc.py", line 283, in __init__
self.get_capabilities()
File "usbtmc/usbtmc.py", line 299, in get_capabilities
timeout=self.timeout)
File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 971, in ctrl_transfer
self.__get_timeout(timeout))
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 819, in ctrl_transfer
timeout))
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 552, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 32] Pipe error
至少,在这种情况下它检测到我的设备,但它引发了一些其他错误。
我该如何解决这个问题?手册说该设备与类似 GPIB 的命令完全兼容。事实上,我已经用GPIB测试了设备,它工作正常。
@Olaf,你问的对。它将用作使用 PySerial 的串行设备。以下是它的工作原理、我遇到的问题及其解决方案。
我通过USB线连接了设备,然后lsusb
:
root@pelcon:~# lsusb
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 004: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller
Bus 001 Device 003: ID 0cf3:3004 Atheros Communications, Inc.
Bus 001 Device 006: ID 0403:b972 Future Technology Devices International, Ltd
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 002: ID 174f:114f Syntek
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub`
接下来,我检查了设备检测为:
root@pelcon:~# ls -al /dev/ttyUSB*
ls: cannot access /dev/ttyUSB**: No such file or directory
然后我查看了这件事,在Can't open port /dev/ttyUSB0中找到了"In recent kernels (definitely with 14.04 LTS), the ftdi_sio module no longer accepts the product and vendor options."。
我还检查了 dmesg | tail
,显示检测到 FTDI USB,但已断开连接。
然后我做了两件事:
一个。创建 /etc/udev/rules.d/99-axe027.rules
,内容 ATTR{idProduct}=="b972", ATTR{idVendor}=="0403", RUN+="/sbin/modprobe -q ftdi_sio product=0xb972 vendor=0x0403"
。我重新启动并再次 dmesg | tail
。但它仍然没有被发现。 Can't program with USB adapter except through sudo nautilus ubuntu
中给出了步骤
b。然后我进一步搜索,这次我找到了Attaching USB-Serial device with custom PID to ttyUSB0 on embedded,我在哪里:
拔下设备
modprobe ftdi_sio
echo 0403 b972 >/sys/bus/usb-serial/drivers/ftdi_sio/new_id
已插入设备
5.
ls -al /dev/ttyUSB*
crw-rw---- 1 root usbtmc 188, 0 Jul 23 11:33 /dev/ttyUSB0
6.
dmesg | tail
[ 1162.348082] usb 1-1.2: Product: 6000A Phase Meter
[ 1162.348086] usb 1-1.2: Manufacturer: clarke-hess
[ 1162.348089] usb 1-1.2: SerialNumber: 187
[ 1162.350801] ftdi_sio 1-1.2:1.0: FTDI USB Serial Device converter detected
[ 1162.350837] usb 1-1.2: Detected FT232BM
[ 1162.350840] usb 1-1.2: Number of endpoints 2
[ 1162.350842] usb 1-1.2: Endpoint 1 MaxPacketSize 64
[ 1162.350844] usb 1-1.2: Endpoint 2 MaxPacketSize 64
[ 1162.350846] usb 1-1.2: Setting MaxPacketSize 64
[ 1162.351284] usb 1-1.2: FTDI USB Serial Device converter now attached to ttyUSB0`
因此,通过这种方式检测到我的设备,并且我能够通过 PySerial 与其通信:
import serial, time
se = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
print('Open USB serial connection:')
print '\t', se
print se.portstr # Confirm which port was really used
se.write("*IDN?\n")
data = se.readline()
time.sleep(2)
print data
se.close()
输出为:
打开 USB 串行连接:
Serial<id=0xb329574c, open=True>(port='/dev/ttyUSB0', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=False, rtscts=False, dsrdtr=False)
/dev/ttyUSB0
CLARKE-HESS,6000A,187,1.07
看看
usbtmc.list_devices()
或
usbtmc.find_device()
前者不需要参数。后者定义为here.
我正在尝试通过 USB 与我的 Clarke-Hess Phase Meter 6000A 通话。为此,我正在使用 https://github.com/python-ivi/python-usbtmc/tree/master/usbtmc。现在,当我连接 USB 数据线并执行 lsusb
时,它会生成
Bus 001 Device 027: ID 0403:b972 Future Technology Devices International, Ltd
作为我识别的设备,所以我简单地使用了usbtmc
并得到了以下错误:
代码
>>> import usbtmc
>>> inst = usbtmc.Instrument(0x0403,0xb972)
Find USBTMC instrument
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "usbtmc/usbtmc.py", line 229, in __init__
raise UsbtmcException("Device not found", 'init')
usbtmc.usbtmc.UsbtmcException: Device not found [init]
但是当我查看 usbtmc.py 时,我发现在 list_devices() 中,usb.util.find_descriptor() 与参数一起使用,如 USBTMC_bInterfaceClass = 0xFE,并且USBTMC_bInterfaceSubClass = 3,这使得它无法检测到我的设备,其描述符为 bInterfaceClass 和 bInterfaceSubClass = 0xff。
然后我把USBTMC_bInterfaceClass和USBTMC_bInterfaceSubClass改成了0xff,然后得到:
代码
>>> import usbtmc
>>> inst = usbtmc.Instrument(0x0403,0xb972)
Find USBTMC instrument
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "usbtmc/usbtmc.py", line 283, in __init__
self.get_capabilities()
File "usbtmc/usbtmc.py", line 299, in get_capabilities
timeout=self.timeout)
File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 971, in ctrl_transfer
self.__get_timeout(timeout))
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 819, in ctrl_transfer
timeout))
File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 552, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 32] Pipe error
至少,在这种情况下它检测到我的设备,但它引发了一些其他错误。
我该如何解决这个问题?手册说该设备与类似 GPIB 的命令完全兼容。事实上,我已经用GPIB测试了设备,它工作正常。
@Olaf,你问的对。它将用作使用 PySerial 的串行设备。以下是它的工作原理、我遇到的问题及其解决方案。
我通过USB线连接了设备,然后
lsusb
:root@pelcon:~# lsusb Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 001 Device 004: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller Bus 001 Device 003: ID 0cf3:3004 Atheros Communications, Inc. Bus 001 Device 006: ID 0403:b972 Future Technology Devices International, Ltd Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 003 Device 002: ID 174f:114f Syntek Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub`
接下来,我检查了设备检测为:
root@pelcon:~# ls -al /dev/ttyUSB* ls: cannot access /dev/ttyUSB**: No such file or directory
然后我查看了这件事,在Can't open port /dev/ttyUSB0中找到了"In recent kernels (definitely with 14.04 LTS), the ftdi_sio module no longer accepts the product and vendor options."。
我还检查了
dmesg | tail
,显示检测到 FTDI USB,但已断开连接。然后我做了两件事:
一个。创建
中给出了步骤/etc/udev/rules.d/99-axe027.rules
,内容ATTR{idProduct}=="b972", ATTR{idVendor}=="0403", RUN+="/sbin/modprobe -q ftdi_sio product=0xb972 vendor=0x0403"
。我重新启动并再次dmesg | tail
。但它仍然没有被发现。 Can't program with USB adapter except throughsudo nautilus ubuntu
b。然后我进一步搜索,这次我找到了Attaching USB-Serial device with custom PID to ttyUSB0 on embedded,我在哪里:
拔下设备
modprobe ftdi_sio
echo 0403 b972 >/sys/bus/usb-serial/drivers/ftdi_sio/new_id
已插入设备
5.
ls -al /dev/ttyUSB* crw-rw---- 1 root usbtmc 188, 0 Jul 23 11:33 /dev/ttyUSB0
6.
dmesg | tail [ 1162.348082] usb 1-1.2: Product: 6000A Phase Meter [ 1162.348086] usb 1-1.2: Manufacturer: clarke-hess [ 1162.348089] usb 1-1.2: SerialNumber: 187 [ 1162.350801] ftdi_sio 1-1.2:1.0: FTDI USB Serial Device converter detected [ 1162.350837] usb 1-1.2: Detected FT232BM [ 1162.350840] usb 1-1.2: Number of endpoints 2 [ 1162.350842] usb 1-1.2: Endpoint 1 MaxPacketSize 64 [ 1162.350844] usb 1-1.2: Endpoint 2 MaxPacketSize 64 [ 1162.350846] usb 1-1.2: Setting MaxPacketSize 64 [ 1162.351284] usb 1-1.2: FTDI USB Serial Device converter now attached to ttyUSB0`
因此,通过这种方式检测到我的设备,并且我能够通过 PySerial 与其通信:
import serial, time
se = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
print('Open USB serial connection:')
print '\t', se
print se.portstr # Confirm which port was really used
se.write("*IDN?\n")
data = se.readline()
time.sleep(2)
print data
se.close()
输出为:
打开 USB 串行连接:
Serial<id=0xb329574c, open=True>(port='/dev/ttyUSB0', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=False, rtscts=False, dsrdtr=False)
/dev/ttyUSB0
CLARKE-HESS,6000A,187,1.07
看看
usbtmc.list_devices()
或
usbtmc.find_device()
前者不需要参数。后者定义为here.