pyUSB - 延迟发送到设备?
pyUSB - delayed sending to the device?
我有一台 ESC 打印机。
我制作了一个简单的脚本来将数据发送到设备,但在更改脚本的内容后 - 打印 1x 以前版本的字幕,然后在再次调用时打印当前版本。
好像来自 USB 的数据缓存在某处。
我怎样才能做一些 FLUSH?
test.py
usb_= Connector(showUsbDevices=False)
usb_.send(b'I LOVE YOU')
连接器:
class Connector:
def __init__(self, idVendor=0x0123, idProduct=0x1234, showUsbDevices=False):
self.idVendor = idVendor
self.idProduct = idProduct
if showUsbDevices:
pass
self.device = self.FindAndConnect()
if self.device is not None:
#if self.device.is_kernel_driver_active(0):
#self.device.detach_kernel_driver(0)
#self.device.detach_kernel_driver(1)
self.device.reset()
self.device.set_configuration()
self.cfg = self.device.get_active_configuration()
data_itfs = list(usb.util.find_descriptor(self.cfg, find_all=True,custom_match=lambda e: (e.bInterfaceClass == 0xA)))
intf = data_itfs[0]
self.device.set_interface_altsetting(intf)
itf_num = intf.bInterfaceNumber
print ("inf descriptor:===============", intf)
print("numer:===============",itf_num)
self.messageOut = usb.util.find_descriptor(intf, custom_match=lambda e: not (e.bEndpointAddress & 0x80))
self.messageIn = usb.util.find_descriptor(intf, custom_match=lambda e: (e.bEndpointAddress & 0x80))
#print(">>>>>>>>>>>>>>>>>>>Message Out",self.messageOut)
#print(">>>>>>>>>>>>>>>>>>>Message In",self.messageIn)
#print(repr(self.cfg))
def __del__(self):
if self.device is not None:
usb.util.dispose_resources(self.device)
def send(self, data):
#print ("endpoint_out",self.messageOut)
if self.device is not None:
print(data.decode("IBM852"))
self.messageOut.write(data)
#self.device.write(1,data,100)
#dane = self.messageIn.read(300)
#print("IN|->",dane)
def FindAndConnect(self):
device=usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
if device is None:
raise ValueError('Not found idVendor 0x%04x i idProduct 0x%04x' % (self.idVendor,self.idProduct))
print('Printer found idVendor 0x%04x i idProduct 0x%04x.... ' %(self.idVendor,self.idProduct))
return device
所以当我 运行 一个测试脚本说
我爱你
我明白了
我爱你
当我把铭文改成
我恨你和 运行 剧本
打印了另一份 I LOVE YOU
只有下一个启动才会给出:我讨厌你
这是什么?错误在哪里?
我找到了!它的:
self.device.reset()
在终结器处:
def __del__(self):
if self.device is not None:
self.device.reset() ############### <<<< THIS IS THE KEY>>>> (added to all code)
usb.util.dispose_resources(self.device)
我有一台 ESC 打印机。 我制作了一个简单的脚本来将数据发送到设备,但在更改脚本的内容后 - 打印 1x 以前版本的字幕,然后在再次调用时打印当前版本。 好像来自 USB 的数据缓存在某处。 我怎样才能做一些 FLUSH?
test.py
usb_= Connector(showUsbDevices=False)
usb_.send(b'I LOVE YOU')
连接器:
class Connector:
def __init__(self, idVendor=0x0123, idProduct=0x1234, showUsbDevices=False):
self.idVendor = idVendor
self.idProduct = idProduct
if showUsbDevices:
pass
self.device = self.FindAndConnect()
if self.device is not None:
#if self.device.is_kernel_driver_active(0):
#self.device.detach_kernel_driver(0)
#self.device.detach_kernel_driver(1)
self.device.reset()
self.device.set_configuration()
self.cfg = self.device.get_active_configuration()
data_itfs = list(usb.util.find_descriptor(self.cfg, find_all=True,custom_match=lambda e: (e.bInterfaceClass == 0xA)))
intf = data_itfs[0]
self.device.set_interface_altsetting(intf)
itf_num = intf.bInterfaceNumber
print ("inf descriptor:===============", intf)
print("numer:===============",itf_num)
self.messageOut = usb.util.find_descriptor(intf, custom_match=lambda e: not (e.bEndpointAddress & 0x80))
self.messageIn = usb.util.find_descriptor(intf, custom_match=lambda e: (e.bEndpointAddress & 0x80))
#print(">>>>>>>>>>>>>>>>>>>Message Out",self.messageOut)
#print(">>>>>>>>>>>>>>>>>>>Message In",self.messageIn)
#print(repr(self.cfg))
def __del__(self):
if self.device is not None:
usb.util.dispose_resources(self.device)
def send(self, data):
#print ("endpoint_out",self.messageOut)
if self.device is not None:
print(data.decode("IBM852"))
self.messageOut.write(data)
#self.device.write(1,data,100)
#dane = self.messageIn.read(300)
#print("IN|->",dane)
def FindAndConnect(self):
device=usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
if device is None:
raise ValueError('Not found idVendor 0x%04x i idProduct 0x%04x' % (self.idVendor,self.idProduct))
print('Printer found idVendor 0x%04x i idProduct 0x%04x.... ' %(self.idVendor,self.idProduct))
return device
所以当我 运行 一个测试脚本说 我爱你
我明白了 我爱你
当我把铭文改成 我恨你和 运行 剧本 打印了另一份 I LOVE YOU 只有下一个启动才会给出:我讨厌你
这是什么?错误在哪里?
我找到了!它的:
self.device.reset()
在终结器处:
def __del__(self):
if self.device is not None:
self.device.reset() ############### <<<< THIS IS THE KEY>>>> (added to all code)
usb.util.dispose_resources(self.device)