From 8e8b7925d233743ad007fa7daf0b1ddb940cdb3a Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Wed, 16 Nov 2016 14:52:33 +0100 Subject: [PATCH] [FIX] hw_escpos: auto-detect endpoint address Instead of hardcoding the IN & OUT endpoint addresses for the ESC/POS printers, we now attempt to auto-detect them. This should increase compatibility with many compatible thermal printers that are simply using a different address, such as the - STMicroelectronics POS58 Printer USB - HU HAI SUNCSW Receipt Printer Co.,Ltd. Gprinter USB Printer Fixes #12890 --- addons/hw_escpos/controllers/main.py | 8 ++++++-- addons/hw_escpos/escpos/printer.py | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/addons/hw_escpos/controllers/main.py b/addons/hw_escpos/controllers/main.py index 0b91dc0d37e..23672e12385 100644 --- a/addons/hw_escpos/controllers/main.py +++ b/addons/hw_escpos/controllers/main.py @@ -104,8 +104,12 @@ class EscposDriver(Thread): printers = self.connected_usb_devices() if len(printers) > 0: - self.set_status('connected','Connected to '+printers[0]['name']) - return Usb(printers[0]['vendor'], printers[0]['product']) + print_dev = Usb(printers[0]['vendor'], printers[0]['product']) + self.set_status( + 'connected', + "Connected to %s (in=0x%02x,out=0x%02x)" % (printers[0]['name'], print_dev.in_ep, print_dev.out_ep) + ) + return print_dev else: self.set_status('disconnected','Printer Not Found') return None diff --git a/addons/hw_escpos/escpos/printer.py b/addons/hw_escpos/escpos/printer.py index 1f32875a5cb..d52668b50f9 100644 --- a/addons/hw_escpos/escpos/printer.py +++ b/addons/hw_escpos/escpos/printer.py @@ -13,7 +13,7 @@ from time import sleep class Usb(Escpos): """ Define USB printer """ - def __init__(self, idVendor, idProduct, interface=0, in_ep=0x82, out_ep=0x01): + def __init__(self, idVendor, idProduct, interface=0, in_ep=None, out_ep=None): """ @param idVendor : Vendor ID @param idProduct : Product ID @@ -42,6 +42,23 @@ class Usb(Escpos): self.device.detach_kernel_driver(self.interface) self.device.set_configuration() usb.util.claim_interface(self.device, self.interface) + + cfg = self.device.get_active_configuration() + intf = cfg[(0,0)] # first interface + if self.in_ep is None: + # Attempt to detect IN/OUT endpoint addresses + try: + is_IN = lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN + is_OUT = lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT + endpoint_in = usb.util.find_descriptor(intf, custom_match=is_IN) + endpoint_out = usb.util.find_descriptor(intf, custom_match=is_OUT) + self.in_ep = endpoint_in.bEndpointAddress + self.out_ep = endpoint_out.bEndpointAddress + except usb.core.USBError: + # default values for officially supported printers + self.in_ep = 0x82 + self.out_ep = 0x01 + except usb.core.USBError as e: raise HandleDeviceError(e)