用于通过WinUsb驱动程序与windows上的USB/CDC设备通信的Python包

WinUsbCDC的Python项目详细描述


WinUsbCDC公司

用于与WinCDC/USB设备通信的python软件包 通过内置的 WinUsb 司机。在

它用于与USB CDC(虚拟com端口)设备通信 不使用内置USB系列系统尽管是司机 相当不稳定,在Win 10上有数据丢失问题- https://wiki.segger.com/CDC#CDC-ACM_issues_on_Windows_10- https://community.nxp.com/thread/458640- https://social.msdn.microsoft.com/Forums/sqlserver/en-US/671638cd-0aed-4cae-80f2-860c1c551347/usb-cdc-acm-data-lost?forum=wdk

标准的使用方法是通过 winusbcdc.ComPort(name, vid, pid)类,它与 pyserial.Serial类api。在

本模块直接基于 WinUsbPy也是如此 公开相同的winusbapi:-基于WinUSB的1:1包装,允许 直接调用来自相关DLL的C++函数。-高级api 这简化了大量的C++/WLLL/Cype杂乱的交互提供 只是一些简单的方法。在

安装WinUsbCDC

pip install winusbcdc
^{pr2}$

USB CDC设备

确保您的设备配置了WinUSB驱动程序。这是可以做到的 带Zadiglibusbk InfWizard 或者类似的。在

fromwinusbcdcimportComPortp=ComPort("My USB Device")# friendly name as shown in device manager# orp=ComPort(vid=0xF055,pid=0x9800)p.open()p.write(b'foo')print(p.read())p.close()

低级WinUsbPy Api

低级api提供了三种方法来调用来自三个 不同的DLL。在

#args: arguments of the C++ function calleddefexec_function_winusb(self,function_name,*args):defexec_function_kernel32(self,function_name,*args):defexec_function_setupapi(self,function_name,*args):

如果我们需要打电话 SetupDiGetClassDevs 它展示了这个原型:

c++ HDEVINFO SetupDiGetClassDevs(_In_opt_ const GUID *ClassGuid,_In_opt_ PCTSTR Enumerator,_In_opt_ HWND hwndParent,_In_ DWORD Flags);

fromwinusbpyimport*fromctypesimport*fromctypes.wintypesimport*fromwinusbclassesimportDIGCF_DEVICE_INTERFACE,DIGCF_PRESENTapi=WinUSBApi()byte_array=c_byte*8guid=GUID(0xA5DCBF10L,0x6530,0x11D2,byte_array(0x90,0x1F,0x00,0xC0,0x4F,0xB9,0x51,0xED))flags=DWORD(DIGCF_DEVICE_INTERFACE|DIGCF_PRESENT)hdev_info=api.exec_function_setupapi("SetupDiGetClassDevs",byref(guid),None,None,flags)

Good resources of WinUsb if you develop using this low level layer

高级WinUsbPy Api

构建在底层包装器之上的是一个更易于执行的api 常见的USB操作。这里是定义函数的列表:

# Possible keyword arguments: default, present, allclasses, profile, deviceinterface (Boolean), Usually called as follows list_usb_devices(deviceinterface=True, present=True)deflist_usb_devices(self,**kwargs):# vid and pid must be str, returns True if device was correctly initialized and False otherwisedefinit_winusb_device(self,vid,pid):# Returns True if device was correctly closed and False otherwise.defclose_winusb_device(self):# Returns last error code. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspxdefget_last_error_code(self):# Returns information for a open device (0x03:High Speed, 0x01:full-speed or lower), query=1 in order to get USB speed.defquery_device_info(self,query=1):# Returns a UsbInterfaceDescriptor object with information about a specified interfacedefquery_interface_settings(self,index):# Change current interface, Winusb opens first interface (0 index) when a device is initializeddefchange_interface(self,index):# Returns a PipeInfo object with information of a specified pipe within current interfacedefquery_pipe(self,pipe_index):# Send a control requesto to open device, setup_packet is a UsbSetupPacket object.# buff = None implies no data is going to be transferred besides setup packet# buff = [0] create a buffer of length 1. Buffer could be IN or OUT, direction is defined in setup packet# it returns a dict with the response and with the buffer under the keywords 'result' and 'buffer'defcontrol_transfer(self,setup_packet,buff=None):#Send Bulk data to the Usb device, write_buffer must be a str bufferdefwrite(self,pipe_id,write_buffer):#Read Bulk data from the Usb device, Returns of a buffer not greater than length_buffer lengthdefread(self,pipe_id,length_buffer):

让我们向我们的设备问好:

fromwinusbpyimport*vid="vid_device"# for example: VID:067b PID:2303pid="pid_device"api=WinUsbPy()result=api.list_usb_devices(deviceinterface=True,present=True)ifresult:ifapi.init_winusb_device(pl2303_vid,pl2303_pid):api.write(0x02,"hello")

实例

如果您将winusbcdc作为一个模块运行,那么它的行为应该类似于一个基本的串行 终端,请参阅__main__.py,以了解所使用的代码。在

python -m winusbcdc --name "My USB Device"

在“Examples”文件夹中有两个配置PL2303的WinUsbPy示例 串行usb设备,列出特性并发送数据。在

Using WinUsbPy Low Level Api

Using WinUsbPy High Level Api

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Hibernate:jpamodelgen生成java而不是类   java如何在第二次活动结束后显示广告?   javascript如何在linux或windows上将java导出到jar   java One ArrayList添加了2个不同的选项   AmazonWeb服务如何在java中使用AWS Textract检索pdf中存在的表   java为什么RecycleView中的水平项在单击时不起作用?   java计算如果存在映射   java在捕获的组上应用正则表达式   如何使用Java在MySQL的同一个表中插入来自不同类的值   java字符串中最常见的字母(大写和小写)   Spring SessionBean实例正在由java中的多个用户共享   使用Spring Boot将@WebInitParam中的值外部化   java一创建线程就开始执行(多线程)   java是安卓所需的系统权限。意图行动给你打电话?