Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage two or more printers of the same model #144

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion brother_ql/backends/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 19 additions & 4 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 @@ -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