From 02b4247d6e201b73d35122d83d1f551d8819bd2a Mon Sep 17 00:00:00 2001 From: doublegized Date: Tue, 25 Apr 2023 20:05:47 +0200 Subject: [PATCH 1/2] bugfix on pyusb implementation for two or more devices of the same model --- brother_ql/backends/pyusb.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/brother_ql/backends/pyusb.py b/brother_ql/backends/pyusb.py index bce5f88..64c289b 100755 --- a/brother_ql/backends/pyusb.py +++ b/brother_ql/backends/pyusb.py @@ -47,11 +47,21 @@ def __call__(self, device): def identifier(dev): try: - serial = usb.util.get_string(dev, 256, dev.iSerialNumber) - return 'usb://0x{:04x}:0x{:04x}_{}'.format(dev.idVendor, dev.idProduct, serial) + # Based on this information (https://github.com/pyusb/pyusb/blob/master/docs/tutorial.rst#dealing-with-multiple-identical-devices), if there are two or more devices of the same vendor and the same model, you have to deal with "bus" and "address" attributes (you can see below how you can retrieve them), but it's not so clear how to use them to define a BrotherQLBackendPyUSB object. + # Anyway, you can find these attributes in the BrotherQLBackendPyUSB istance. + + # PRINTER ATTRIBUTES (it's not so clear how to retrieve a list of all the possible attributes): + # print("device bus:", dev.bus) + # print("device address:", dev.address) + # print("device port:", dev.port_number) + # print("device speed:", dev.speed) + + # to obtain the serial number, I changed the separator (from _ to /) and swapped two parameters. + serial = usb.util.get_string(dev, dev.iSerialNumber, 256) + return 'usb://0x{:04x}:0x{:04x}/{}'.format(dev.idVendor, dev.idProduct, serial) except: return 'usb://0x{:04x}:0x{:04x}'.format(dev.idVendor, dev.idProduct) - + return [{'identifier': identifier(printer), 'instance': printer} for printer in printers] class BrotherQLBackendPyUSB(BrotherQLBackendGeneric): @@ -78,7 +88,12 @@ def __init__(self, device_specifier): vendor, product = int(vendor, 16), int(product, 16) for result in list_available_devices(): printer = result['instance'] - if printer.idVendor == vendor and printer.idProduct == product or (serial and printer.iSerialNumber == serial): + # quick and dirty approach to always use serial number. If it's not present: device not found + try: + iSerialNumber = usb.util.get_string(printer, printer.iSerialNumber, 256) + except Exception: + raise ValueError('Device not found') + if printer.idVendor == vendor and printer.idProduct == product and iSerialNumber == serial: self.dev = printer break if self.dev is None: From 7882a96bb2a9dd02c3876833dbdcd2bd1563285c Mon Sep 17 00:00:00 2001 From: doublegized Date: Sun, 11 Jun 2023 16:38:59 +0200 Subject: [PATCH 2/2] added more time for printer jobs to complete --- brother_ql/backends/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brother_ql/backends/helpers.py b/brother_ql/backends/helpers.py index 279221c..7c638bf 100755 --- a/brother_ql/backends/helpers.py +++ b/brother_ql/backends/helpers.py @@ -67,7 +67,7 @@ def send(instructions, printer_identifier=None, backend_identifier=None, blockin """ No need to wait for completion. The network backend doesn't support readback. """ return status - while time.time() - start < 10: + while time.time() - start < 60: data = printer.read() if not data: time.sleep(0.005)