gpt4 book ai didi

python - 无法通过具有指定 device_id 的 pylibftdi 与 FTDI 设备通信

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:02:39 24 4
gpt4 key购买 nike

我有一个使用 FTDI 芯片的 USB 设备,我可以在 Linux 中识别它:

user@user:~/src/libftdi/build$ lsusb
Bus 009 Device 008: ID 0403:faf0 Future Technology Devices International, Ltd

或:

user@user:~$ lsusb -v -d 0403:faf0

Bus 009 Device 008: ID 0403:faf0 Future Technology Devices International, Ltd
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 0 (Defined at Interface level)
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 8
idVendor 0x0403 Future Technology Devices International, Ltd
idProduct 0xfaf0
bcdDevice 6.00
iManufacturer 1 Thorlabs
iProduct 2 APT DC Motor Controller
iSerial 3 83836244
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 32
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xc0
Self Powered
MaxPower 0mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 2
bInterfaceClass 255 Vendor Specific Class
bInterfaceSubClass 255 Vendor Specific Subclass
bInterfaceProtocol 255 Vendor Specific Protocol
iInterface 2 APT DC Motor Controller
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x02 EP 2 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 0
Device Status: 0x0001
Self Powered

我已经安装了 pylibftdi 并知道如何列出它们

In [26]: dev.driver.list_devices()
Out[26]:
[('Thorlabs', 'APT DC Motor Controller', '83836244'),
('Thorlabs', 'APT DC Motor Controller', '83838416'),
('Thorlabs', 'APT DC Motor Controller', '83837686'),
('Thorlabs', 'APT DC Motor Controller', '83836852'),
('Thorlabs', 'APT DC Motor Controller', '83837812'),
('Thorlabs', 'APT DC Motor Controller', '83825518'),
('Thorlabs', 'APT DC Motor Controller', '83838377'),
('Thorlabs', 'APT DC Motor Controller', '83838379'),
('Thorlabs', 'APT DC Motor Controller', '83836769'),
('Thorlabs', 'APT DC Motor Controller', '83837688'),
('Thorlabs', 'APT DC Motor Controller', '83836926'),
('Thorlabs', 'APT DC Motor Controller', '83837767'),
('Thorlabs', 'APT DC Motor Controller', '83836887'),
('Thorlabs', 'APT DC Motor Controller', '83836737'),
('Thorlabs', 'APT DC Motor Controller', '83838436'),
('Thorlabs', 'APT DC Motor Controller', '83837639'),
('Thorlabs', 'APT DC Motor Controller', '83836040'),
('Thorlabs', 'APT DC Motor Controller', '83837769'),
('Thorlabs', 'Brushed Motor Controller', '27251492'),
('Thorlabs', 'Brushed Motor Controller', '27251539')]


In [1]: from pylibftdi import USB_PID_LIST, USB_VID_LIST, Device
In [2]: USB_PID_LIST.append(0xFAF0)
In [3]: dev = Device()
In [4]: dev.driver.libftdi_version()
Out[4]: libftdi_version(major=0, minor=0, micro=0, version_str='< 1.0 - no ftdi_get_library_version()', snapshot_str='unknown')

pylibusb 的文档说属性 device_id 可用于指定我尝试连接的设备:

 |  __init__(self, device_id=None, mode='b', encoding='latin1', interface_select=None, device_index=0, **kwargs)
| Device([device_id[, mode, [OPTIONS ...]]) -> Device instance
|
| represents a single FTDI device accessible via the libftdi driver.
| Supports a basic file-like interface (open/close/read/write, context
| manager support).
:param device_id: an optional serial number of the device to open.
| if omitted, this refers to the first device found, which is
| convenient if only one device is attached, but otherwise
| fairly useless.

这是我创建两个实例 dev1 和 dev2 的简单代码。对于 dev2,我没有指定 device_id(因此没有特定的序列号),对于 dev1,我指定了。我可以与 dev2 成功通信,但不能与 dev1 通信:

>>>from pylibftdi import USB_PID_LIST, USB_VID_LIST, Device
>>>from struct import pack, unpack

>>>USB_PID_LIST.append(0xFAF0)

>>>command = pack('BBBBBB',0x05,0x00,0x00,0x00,0x50,0x01)

>>>dev2 = Device();
>>>dev2.baudrate = 115200;
>>>dev2.writelines(command);
>>>dev2.readline()
'\x06\x00T\x00\x81P\xc0z\xf2\x04TDC001\x00\x00\x10\x00\x03\x00\x02\x00TDC001 DC Servo Drive\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00104927Apr\x00\x01\x00\x02\x00\x00\x00\x01\x00'

>>>dev1 = Device(device_id = '83838416');
>>>dev1.baudrate = 115200;
>>>dev1.writelines(command);
>>>dev1.readline()
''

因为我有几十个这样的 USB 设备连接到一台计算机,所以能够创建一个实例并与具有定义序列号的设备通信对我来说至关重要。

我不确定这是一个错误还是我做错了什么。

稍后添加:

它在某种程度上依赖于历史。如果我在重新启动 python 后运行相同的代码,我会得到一个空字符串作为响应。我不知道我现在所做的与以前有什么不同。

In [1]: from pylibftdi import USB_PID_LIST, USB_VID_LIST, Device

In [2]: from struct import pack, unpack

In [3]: USB_PID_LIST.append(0xFAF0)

In [4]: command = pack('BBBBBB',0x05,0x00,0x00,0x00,0x50,0x01)

In [5]: dev2 = Device();dev2.baudrate = 115200;dev2.writelines(command);dev2.readline();

In [6]: dev2.__dict__
Out[6]:
{'_baudrate': 115200,
'_opened': True,
'ctx': <ctypes.c_char_Array_1024 at 0x7ff2008a1b00>,
'decoder': <encodings.latin_1.IncrementalDecoder at 0x7ff2006906d0>,
'device_id': None,
'device_index': 0,
'driver': <pylibftdi.driver.Driver at 0x7ff200690610>,
'encoder': <encodings.latin_1.IncrementalEncoder at 0x7ff200681f90>,
'encoding': 'latin1',
'fdll': <CDLL 'libftdi.so.1', handle 5627c1668650 at 7ff2011d08d0>,
'interface_select': None,
'mode': 'b'}

In [8]:

最佳答案

您是否只对 uart(类似)模式感兴趣?如果是这样,您可能会看看 pyserial。特别是 serial.tools.list_ports.comports() 函数:

import serial
import serial.tools.list_ports

print([(x.device,x.hwid,x.description,x.location,x.serial_number) for x in serial.tools.list_ports.comports()])

这样你就可以获得正确的描述并通过

打开端口
ports = [x.device for x in serial.tools.list_ports.comports() if search_string in x.hwid]
serial.Serial(ports[0], 115200)

关于python - 无法通过具有指定 device_id 的 pylibftdi 与 FTDI 设备通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56530409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com