我正在使用escposprinter python库在热敏打印机中打印数据。这取决于 pyusb。该库在 Linux 中运行良好。在Windows 7中,我发现它有一些问题。这是我得到的输出。
File "main.py", line 1, in <module>
from app import app
File "D:freeth-in-erp-60ab8eb96fadapp__init__.py", line 94, in <module>
from .api_routes import *
File "D:freeth-in-erp-60ab8eb96fadappapi_routes.py", line 44, in <module>
from .printer import pos
File "D:freeth-in-erp-60ab8eb96fadappprinterpos.py", line 14, in <module>
Epson = printer.Usb(idVendor=0x0416,idProduct=0x5011)
File "D:freeth-in-erp-60ab8eb96fadvenvlibsite-packagesescposprinterprint
er.py", line 37, in __init__
self.open()
File "D:freeth-in-erp-60ab8eb96fadvenvlibsite-packagesescposprinterprint
er.py", line 46, in open
if self.device.is_kernel_driver_active(0):
File "D:freeth-in-erp-60ab8eb96fadvenvlibsite-packagesusbcore.py", line
1064, in is_kernel_driver_active
interface)
File "D:freeth-in-erp-60ab8eb96fadvenvlibsite-packagesusbbackend__init_
_.py", line 365, in is_kernel_driver_active
_not_implemented(self.is_kernel_driver_active)
File "D:freeth-in-erp-60ab8eb96fadvenvlibsite-packagesusbbackend__init_
_.py", line 81, in _not_implemented
raise NotImplementedError(func.__name__)
NotImplementedError: is_kernel_driver_active
我从 libusb.info 下载libusb-1.20并从MinGW32复制文件libusb-1.0.dll并粘贴"C:\windows\System32"。我得到以下结果。
from app import app
File "D:freeth-in-erp-60ab8eb96fadapp__init__.py", line 94, in <module>
from .api_routes import *
File "D:freeth-in-erp-60ab8eb96fadappapi_routes.py", line 44, in <module>
from .printer import pos
File "D:freeth-in-erp-60ab8eb96fadappprinterpos.py", line 14, in <module>
Epson = printer.Usb(idVendor=0x0416,idProduct=0x5011)
File "D:freeth-in-erp-60ab8eb96fadvenvlibsite-packagesescposprinterprint
er.py", line 37, in __init__
self.open()
File "D:freeth-in-erp-60ab8eb96fadvenvlibsite-packagesescposprinterprint
er.py", line 46, in open
if self.device.is_kernel_driver_active(0):
File "D:freeth-in-erp-60ab8eb96fadvenvlibsite-packagesusbcore.py", line
1064, in is_kernel_driver_active
interface)
File "D:freeth-in-erp-60ab8eb96fadvenvlibsite-packagesusbbackendlibusb1
.py", line 898, in is_kernel_driver_active
intf)))
File "D:freeth-in-erp-60ab8eb96fadvenvlibsite-packagesusbbackendlibusb1
.py", line 593, in _check
raise NotImplementedError(_strerror(ret))
NotImplementedError: Operation not supported or unimplemented on this platform
我的代码是`
from escposprinter import *
from tabulate import tabulate
Epson = printer.Usb(0x0416,0x5011)
来自库 Escposprinter
class Usb(Escpos):
""" Define USB printer """
def __init__(self, idVendor, idProduct, interface=0, in_ep=0x82, out_ep=0x01):
"""
@param idVendor : Vendor ID
@param idProduct : Product ID
@param interface : USB device interface
@param in_ep : Input end point
@param out_ep : Output end point
"""
self.idVendor = idVendor
self.idProduct = idProduct
self.interface = interface
self.in_ep = in_ep
self.out_ep = out_ep
self.open()
def open(self):
""" Search device on USB tree and set is as escpos device """
self.device = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
if self.device is None:
print ("Cable isn't plugged in")
if self.device.is_kernel_driver_active(0):
try:
self.device.detach_kernel_driver(0)
except usb.core.USBError as e:
print ("Could not detatch kernel driver: %s" % str(e))
帮我你的建议`
问题很简单,检查内核驱动程序是否处于活动状态,这在 Windows 上是不可能的。似乎这个库根本不是为在Windows上使用而编写的。
如果您从源代码中删除以 if self.device.is_kernel_driver_active(0):
开头的最后 5 行,或者事先检查代码是否在 Windows 上运行并且不要调用它们,则可以在 Windows 上使用它。
请记住,在Windows上,您需要安装自己的驱动程序。我建议使用 Zadig 来创建和/或安装驱动程序。