git.schokokeks.org
Repositories
Help
Report an Issue
python-escpos-printer.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
ff31c22
Branches
Tags
main
v0.1.0
python-escpos-printer.git
src
escpos_printer
usbprinter.py
initial commit
Bernd Wurst
commited
ff31c22
at 2026-11-26 17:47:00
usbprinter.py
Blame
History
Raw
# -* coding: utf-8 *- # (C) 2012 by Bernd Wurst <bernd@schokokeks.org> # This file is part of Bib2011. # # Bib2011 is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Bib2011 is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Bib2011. If not, see <http://www.gnu.org/licenses/>. import subprocess import logging log = logging.getLogger('usbprinter') printer_definitions = [ {'idVendor': 0x04b8, 'idProduct': 0x0e03, 'cp858': 19}, {'idVendor': 0x04b8, 'idProduct': 0x0e20, 'cp858': 14}, ] import usb.core import usb.util class USBPrinter (object): def __init__(self, fix_script=None): self.fix_script = fix_script self.ep_out = None self.ep_in = None self.device = None self.printer_config = {'idVendor': None, 'idProduct': None, 'cp858': None} self.initialized = False self.initialize() def initialize(self, auto_retry=True): try: devs = usb.core.find(find_all=True) for d in devs: for candidate in printer_definitions: if candidate['idVendor'] == d.idVendor and candidate['idProduct'] == d.idProduct: self.printer_config = candidate self.device = d log.info(f'using printer device {d.idVendor:04x}:{d.idProduct:04x}') if not self.device: log.error('Kein Drucker gefunden!') return None cfg = self.device.get_active_configuration() interface_number = cfg[(0,0)].bInterfaceNumber if self.device.is_kernel_driver_active(interface_number): self.device.detach_kernel_driver(interface_number) self.device.set_configuration() intf = usb.util.find_descriptor(cfg, bInterfaceNumber = interface_number) endpoints = list(usb.util.find_descriptor(intf, find_all = True)) ep_out = None for e in endpoints: if (usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT): ep_out = e break ep_in = None for e in endpoints: if (usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN): ep_in = e break assert ep_out is not None and ep_in is not None, "Error getting a USB endpoint address" self.ep_out = ep_out self.ep_in = ep_in self.initialized = True except usb.core.USBError as e: log.error(str(e)) if self.fix_script: log.error('Running permission fixer script') subprocess.call(self.fix_script, shell=True) if self.fix_script and auto_retry: self.initialize(auto_retry=False) def get_endpoints(self): if self.initialized: return self.ep_out, self.ep_in else: log.error('initialization error') return None, None def write(self, data): # Es gab Probleme mit langen Belegen, daher senden wir die Daten jetzt in kleineren Häppchen i = 0 while i < len(data): self.ep_out.write(data[i:i + 1024]) i += 1024 def read(self) -> bytes: data = self.ep_in.read(1024, timeout=200) if data: return data return b'' def __del__(self): self.terminate() def terminate(self): if self.initialized: usb.util.dispose_resources(self.device) self.initialized = False if __name__ == '__main__': prn = USBPrinter() (fd_out, fd_in) = prn.get_endpoints() # RESET fd_out.write('\x1b\x40') # Code page fd_out.write('\x1b\x74\x0e') fd_out.write('Hallo äöü\n\n\n'.encode('cp858')) prn.write('\x1d\x72\x01') status = prn.read() if status: print(f'received {len(status)} bytes!') print(f"{status[0]:08b}")