Windows pyusb的使用

1 USB驅動安裝工具Zadig
Zadig - USB driver installation made easy
https://zadig.akeo.ie/

其中ie表示Ireland愛爾蘭。Zadig基於libwdi(Windows Driver Installer library)開發。

2 libusb for Windows
2.1 注意點
測試pyusb與adb驅動通信時,需要運行adb kill-server,纔可以使用pyusb;否則調用pyusb的API會返回沒有權限錯誤

2.2 查找是否安裝過libusb
import ctypes
from ctypes import util, cdll
import os
import sys

if sys.platform == 'win32':
    libname = ctypes.util.find_library('libusb-1.0')
else:
    libname = ctypes.util.find_library('usb-1.0')
if libname is None:
    raise OSError('USB library could not be found')

print (libname)

2.3 pyusb的安裝及使用
1)安裝Python 3.8
安裝時選擇安裝pip工具

2)pyusb
https://github.com/pyusb/pyusb

python -m pip install pyusb
或者
cd PATH_TO\PyUSB
python setup.py install

3)libusb
https://libusb.info/

打開壓縮包,選擇MS64\dll\libusb-1.0.dll,複製到C:\Windows\System32
選擇同目錄下的MS64\dll\libusb-1.0.lib,複製到C:\Python38\Lib

4)例子
import os
os.environ['PYUSB_DEBUG'] = 'debug'
import usb.util
import usb.core
import sys

all_devs = usb.core.find(find_all=True)
for d in all_devs:
    if (d.idVendor == 0x8087) & (d.idProduct == 0x09ef):
        print(d)
        bmRequestType = usb.util.build_request_type(
            usb.util.CTRL_OUT, # 如果無數據傳輸,必須是CTRL_OUT,否則ctrl_transfer()不會返回     
            usb.util.CTRL_TYPE_STANDARD,
            usb.util.CTRL_RECIPIENT_INTERFACE)
        #d.ctrl_transfer(bmRequestType, 0x40, 0, 0, [])
        d.ctrl_transfer(bmRequestType, 0x40, 0, 0, "test")
        exit(0)

3 python hidapi
1)下載hidapi whl文件
hidapi 0.7.99.post21
https://pypi.org/project/hidapi/#files

2)安裝hidapi whl
pip install PATH_TO\filename.whl

升級安裝:
pip install -U PATH_TO\filename.whl

4 libusbK
libusbK:基於KMDF框架開發,與MS WinUSB同樣的作用
libusb:Linux上基於usbfs;Windows上基於WinUSB或者libusbK

5 Abbreviations
ARC:Argonant RISC Core
AT91SAM9260:SAM means Smart ARM-based Microcontroller
ATMEL SAMBA:ATMEL Smart ARM-based Microcontroller Boot Assistant
CC2530:TI ChipCon2530
DWC2:Design Ware Controller 2,Apple的嵌入式設備,包括iPad和iPhone都是使用的DWC2
ISP1161:Philips' Integrated host Solution Pairs 1161,“Firms introduce USB host controllers”,https://www.eetimes.com/document.asp?doc_id=1290054
Quirks:the attributes of a device that are considered to be noncompliant with expected operation
SL811HS:Cypress/ScanLogic 811 Host/Slave,性能上與ISP1161(Integrated host Solution Pairs 1161)相當
TDI:TransDimension Inc.,該公司首先發明瞭將TT集成到EHCI RootHub中的方法,這樣對於嵌入式系統來說,就省去了OHCI/UHCI的硬件,同時降低了成本,作爲對該公司的紀念,Linux內核定義了宏ehci_is_TDI(ehci);產品UHC124表示USB Host Controller;收購了ARC USB技術;現已被chipidea收購,chipidea又被mips收購
TLV:TI Low Value,高性價比
TPS:TI Performance Solution
TT:Transaction Translator(事務轉換器,將USB2.0的包轉換成USB1.1的包)
USB BH reset:Bigger Hammer or Brad Hosler,表示warm reset;you may be confused why the USB 3.0 spec calls the same type of reset "warm reset" in some places and "BH reset" in other places. "BH" reset is supposed to stand for "Big Hammer" reset, but it also stands for "Brad Hosler". Brad died shortly after the USB 3.0 bus specification was started, and they decided to name the reset after him. The suggestion was made shortly before the spec was finalized, so the wording is a bit inconsistent
WHL:Python Wheel Package
Zadig:an Automated Driver Installer GUI application for WinUSB, libusb-win32 and libusbK

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章