win7 64 位
python3 pyusb 研究 收集
py372_64_env
D:\PYTHON_ENV\py372_64_env\Scripts\
从 https://sourceforge.net/projects/libusb/
下载下来的
libusb-1.0.20\MS64\dll
libusb-1.0.dll
libusb-1.0.lib
libusb-1.0.pdb
复制其中的 libusb-1.0.dll 和 libusb-1.0.lib
到D:\PYTHON_ENV\py372_64_env\Scripts\ 目录下面
执行如下程序:
import usb.core
print(usb.core.show_devices())
dev = usb.core.find(idVendor=0x8866, idProduct=0x0100)
print(dev)
得到如下:
D:\PYTHON_ENV\py372_64_env\Scripts\python.exe F:/pyqt5/example/PyQt5快速开发与实战_Code/test/printer/usb_printer.py
DEVICE ID 0403:6001 on Bus 001 Address 004, Specified at interface
DEVICE ID 046d:c31c on Bus 001 Address 003, Specified at interface
DEVICE ID 04ca:0061 on Bus 001 Address 002, Specified at interface
DEVICE ID 8866:0100 on Bus 001 Address 006, Specified at interface
DEVICE ID 8086:a12f on Bus 001 Address 001, Specified at interface
DEVICE ID 8866:0100 on Bus 001 Address 006 =================
bLength : 0x12 (18 bytes)
bDescriptorType : 0x1 Device
bcdUSB : 0x110 USB 1.1
bDeviceClass : 0x0 Specified at interface
bDeviceSubClass : 0x0
bDeviceProtocol : 0x0
bMaxPacketSize0 : 0x40 (64 bytes)
idVendor : 0x8866
idProduct : 0x0100
bcdDevice : 0x100 Device 1.0
iManufacturer : 0x1 Error Accessing String
iProduct : 0x2 Error Accessing String
iSerialNumber : 0x3 Error Accessing String
bNumConfigurations : 0x1
CONFIGURATION 1: 100 mA ==================================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x2 Configuration
wTotalLength : 0x20 (32 bytes)
bNumInterfaces : 0x1
bConfigurationValue : 0x1
iConfiguration : 0x0
bmAttributes : 0x80 Bus Powered
bMaxPower : 0x32 (100 mA)
INTERFACE 0: Printer ===================================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x4 Interface
bInterfaceNumber : 0x0
bAlternateSetting : 0x0
bNumEndpoints : 0x2
bInterfaceClass : 0x7 Printer
bInterfaceSubClass : 0x1
bInterfaceProtocol : 0x2
iInterface : 0x0
ENDPOINT 0x81: Bulk IN ===============================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x81 IN
bmAttributes : 0x2 Bulk
wMaxPacketSize : 0x40 (64 bytes)
bInterval : 0xa
ENDPOINT 0x2: Bulk OUT ===============================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x2 OUT
bmAttributes : 0x2 Bulk
wMaxPacketSize : 0x40 (64 bytes)
bInterval : 0xa
Process finished with exit code 0
8866:0100 这个是 佳博热敏小票打印机 GP-L80180I usb 接口
使用如下代码:
from escpos.connections import getUSBPrinter
printer = getUSBPrinter()(idVendor=0x8866,
idProduct=0x0100,
inputEndPoint=0x81,
outputEndPoint=0x02) # Create the printer object with the connection params
printer.text("Hello World")
printer.lf()
如果小票打印机没有上电,那么肯定探测不到这个usb设备
D:\PYTHON_ENV\py372_64_env\Scripts\python.exe F:/pyqt5/example/PyQt5快速开发与实战_Code/test/printer/usb_printer.py
Traceback (most recent call last):
File "F:/pyqt5/example/PyQt5快速开发与实战_Code/test/printer/usb_printer.py", line 8, in <module>
outputEndPoint=0x02) # Create the printer object with the connection params
File "D:\PYTHON_ENV\py372_64_env\lib\site-packages\escpos\connections.py", line 87, in __init__
self.__open()
File "D:\PYTHON_ENV\py372_64_env\lib\site-packages\escpos\connections.py", line 104, in __open
raise RuntimeError("Cable isn't plugged in")
RuntimeError: Cable isn't plugged in
Process finished with exit code 1
上电之后:
D:\PYTHON_ENV\py372_64_env\Scripts\python.exe F:/pyqt5/example/PyQt5快速开发与实战_Code/test/printer/usb_printer.py
Traceback (most recent call last):
File "F:/pyqt5/example/PyQt5快速开发与实战_Code/test/printer/usb_printer.py", line 8, in <module>
outputEndPoint=0x02) # Create the printer object with the connection params
File "D:\PYTHON_ENV\py372_64_env\lib\site-packages\escpos\connections.py", line 87, in __init__
self.__open()
File "D:\PYTHON_ENV\py372_64_env\lib\site-packages\escpos\connections.py", line 106, in __open
if self._device.is_kernel_driver_active(0):
File "D:\PYTHON_ENV\py372_64_env\lib\site-packages\usb\core.py", line 1061, in is_kernel_driver_active
self._ctx.managed_open()
File "D:\PYTHON_ENV\py372_64_env\lib\site-packages\usb\core.py", line 102, in wrapper
return f(self, *args, **kwargs)
File "D:\PYTHON_ENV\py372_64_env\lib\site-packages\usb\core.py", line 120, in managed_open
self.handle = self.backend.open_device(self.dev)
File "D:\PYTHON_ENV\py372_64_env\lib\site-packages\usb\backend\libusb1.py", line 786, in open_device
return _DeviceHandle(dev)
File "D:\PYTHON_ENV\py372_64_env\lib\site-packages\usb\backend\libusb1.py", line 643, in __init__
_check(_lib.libusb_open(self.devid, byref(self.handle)))
File "D:\PYTHON_ENV\py372_64_env\lib\site-packages\usb\backend\libusb1.py", line 593, in _check
raise NotImplementedError(_strerror(ret))
NotImplementedError: Operation not supported or unimplemented on this platform
Process finished with exit code 1
2
libusb-1.0.lib/libusb.h -> WinUSB(v6.1.7600.16385)
pyUSB -> libusb-win32
但是如果不需要考虑这种情况时候,最好的方式就是使用Zadig.exe,直接可以选择操作USB所需要的所有类型的驱动,比如
3
I downloaded latest libusb windows binaries from here: http://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.20/libusb-1.0.20.7z/download then I copied libusb1.dll according to this: when you are on 64-bit Windows, MS64\dll\libusb-1.0.dll must be copied into C:\Windows\System32 and (for running 32-bit applications that use libusb) MS32\dll\libusb-1.0.dll must be copied into C:\Windows\SysWOW64. and then I copied libusb1.lib from binaries to lib dir in python directory and now pyusb works for me. hope it helps.
4
坑1:pyusb需要libusb,如果直接使用
usb.core.find(find_all=True)或者其他,将会报错No backend available
坑2:libusb需要指定寻找路径,否则会去系统目录C:/WINDOWS/SYSTEM32 或者 Python27//Scripts 或者 系统环境变量目录底下找
backend = usb.backend.libusb0.get_backend()
调用函数
所以此处需要一个自定义的find_library()函数作为参数传入,注意,find_library是个函数!返回值是dll的完整路径
坑3:Source Code上下载的bin里面只有libusb0.dll
LibUSB-Win32是一个用于Windows操作系统(Win98SE、WinME、Win2k和WinXP、Vista、Win7等)上的通用USB设备驱动程序。该驱动程序允许使用者在不写任何一行核心驱动程序代码的情况下,可以访问Windows系统上的任意一个USB设备,具有以下特点:
https://sourceforge.net/projects/libusb-win32/files/latest/download
你在市面上买的到的intel 64位 CPU都属于amd64范畴这个架构应该称为 x86_64,因此不管是AMD的64位CPU,还是INTEL的64位CPU,都是属于AMD64范畴.
而IA64指的是Intel安腾系列CPU,不是X86架构的。ia64主要用在服务器上面,而不是我们平常使用的桌面,通常这些cpu很贵,相关的内存以及硬盘同样很贵!
因此你以后你PC的CPU是64位的[有Intel,也有AMD],非安腾的CPU的话,你就下载AMD64的OS进行安装即可.
https://sourceforge.net/p/libusb-win32/wiki/Home/
libusb-win32
libusb-win32 is now in Bug-Fix-Only maintenance mode. It is recommended new project should migrate to libusb-1.0 API and use libusb Windows (http://libusb.info) instead. For Windows only project, you can also look at libusbK below (http://libusbk.sourceforge.net/UsbK3/index.html)
10
I downloaded latest libusb windows binaries from here: http://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.20/libusb-1.0.20.7z/download then I copied libusb1.dll according to this: when you are on 64-bit Windows, MS64\dll\libusb-1.0.dll must be copied into C:\Windows\System32 and (for running 32-bit applications that use libusb) MS32\dll\libusb-1.0.dll must be copied into C:\Windows\SysWOW64. and then I copied libusb1.lib from binaries to lib dir in python directory and now pyusb works for me. hope it helps.
1
https://github.com/walac/pyusb
PyUSB is installed through pip https://pypi.python.org/pypi/pyusb:
pip install pyusb
Remember that you need libusb (1.0 or 0.1) or OpenUSB running on your system. For Windows users, libusb 0.1 is provided through libusb-win32 package. Check the libusb website for updates (http://www.libusb.info).
https://libusb.info/
https://sourceforge.net/p/libusb-win32/wiki/Home/
2
在64系统上运行32位DLL,应将DLL放在C:\Windows\SysWOW64目录下
之前都搞反了。
64位系统安装目录分为两种
64位用:C:\Program Files
32位用:C:\Program Files (x86)
64位系统系统目录分为两种
64位用:C:\Windows\System32
32位用:C:\Windows\SysWOW64
64位系统注册表分为两种
64位用:[HKEY_LOCAL_MACHINE\SOFTWARE]
32位用:[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node]
所以在64位系统中运行32位的DLL,应将DLL放在C:\Windows\SysWOW64目录下,写注册表应该是[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node]下,调用的程序应该在C:\Program Files (x86)下。注册表中的键值应该指向的是C:\Windows\SysWOW64下
from escpos.connections import getUSBPrinter
https://pythonhosted.org/python-printer-escpos/manual/connections/usb.html
https://pypi.org/project/python-printer-escpos/0.0.3/
If you are just starting up with POS RECEIPT Printers, you must first refer to http://pythonhosted.org/python-printer-escpos/ to get started.
python-printer-escpos provides four different types of printer connections to talk to a ESCPOS printer.
http://pythonhosted.org/python-printer-escpos/
Get some prerequisites first for image printing etc.
pip install Pillow
Install python-printer-escpos
pip install python-printer-escpos
Serial Printer
You printer may also be connected over a RS232 serial interface. You must get the correct baudrate for your printer for a serial connection.
Synposis
from escpos.connections import getSerialPrinter
printer = getSerialPrinter()(dev='/dev/ttyS0',
baudrate=9600)
printer.text("Hello World")
printer.lf()