Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions brother_ql/backends/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ 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)
time.sleep(0.05)
continue
try:
result = interpret_response(data)
Expand Down
25 changes: 20 additions & 5 deletions brother_ql/backends/pyusb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -67,7 +77,7 @@ def __init__(self, device_specifier):

self.dev = None
self.read_timeout = 10. # ms
self.write_timeout = 15000. # ms
self.write_timeout = 60000. # ms
# strategy : try_twice or select
self.strategy = 'try_twice'
if isinstance(device_specifier, str):
Expand All @@ -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:
Expand Down