日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python调用usb设备_用Python与USB设备通信

發布時間:2023/12/8 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python调用usb设备_用Python与USB设备通信 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

假設您使用Linux和libusb-1.0作為PyUSB的后端庫。// Detach a kernel driver from an interface.

// If successful, you will then be able to claim the interface and perform I/O.

int libusb_detach_kernel_driver (libusb_device_handle *dev,

int interface_number)

// Re-attach an interface's kernel driver, which was previously

// detached using libusb_detach_kernel_driver().

int libusb_attach_kernel_driver(libusb_device_handle *dev,

int interface_number)

因此,基本上,您需要首先調用detach_kernel_driver來從設備接口分離已經連接的內核驅動程序(如果有的話),這樣您就可以在代碼中與它通信(它要么是您的代碼,要么是某個內核驅動程序在與設備接口通信)。完成后,您可能需要調用attach_kernel_driver重新連接內核驅動程序。

我相信如果您可以確保沒有為給定的設備加載內核驅動程序(或者在運行代碼之前手動卸載它),那么就不需要調用任何這些C函數/Python方法。

編輯:

我剛把這段代碼(基于你的示例)處理好。注意:為了簡單起見,我將0硬編碼為detach_kernel_driver和attach_kernel_driver的接口號-我想您應該使它更智能。import usb

dev = usb.core.find(idVendor=0x0403, idProduct=0x6001)

reattach = False

if dev.is_kernel_driver_active(0):

reattach = True

dev.detach_kernel_driver(0)

dev.set_configuration()

cfg = dev.get_active_configuration()

interface_number = cfg[(0,0)].bInterfaceNumber

alternate_settting = usb.control.get_interface(dev, interface_number)

intf = usb.util.find_descriptor(cfg, bInterfaceNumber = interface_number,

bAlternateSetting = alternate_settting)

ep = usb.util.find_descriptor(intf,custom_match = \

lambda e: \

usb.util.endpoint_direction(e.bEndpointAddress) == \

usb.util.ENDPOINT_OUT)

ep.write("test\n\r")

# This is needed to release interface, otherwise attach_kernel_driver fails

# due to "Resource busy"

usb.util.dispose_resources(dev)

# It may raise USBError if there's e.g. no kernel driver loaded at all

if reattach:

dev.attach_kernel_driver(0)

總結

以上是生活随笔為你收集整理的python调用usb设备_用Python与USB设备通信的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。