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
networkprinter.py
initial commit
Bernd Wurst
commited
ff31c22
at 2026-11-26 17:47:00
networkprinter.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 socket import logging log = logging.getLogger('networkprinter') class NetworkPrinter (object): def __init__(self, address, port, cp858=14): self.connection = None self.address = address self.port = port self.printer_config = {'idVendor': None, 'idProduct': None, 'cp858': cp858} self.initialized = False self.connect() def initialize(self): self.connect() def connect(self): try: self.connection = socket.create_connection((self.address, self.port), timeout=5) self.initialized = True except OSError as e: log.error(str(e)) pass def write(self, data): if type(data) == str: data = data.encode('cp858') # Es gab Probleme mit langen Belegen, daher senden wir die Daten jetzt in kleineren Häppchen i = 0 while i < len(data): self.send(data[i:i + 1024]) i += 1024 def read(self): return self.recv() def recv(self): data = self.connection.recv(1024) if len(data) > 0: return data return None def send(self, data): num = 0 try: num = self.connection.send(data) except Exception: pass if num < len(data): self.connect() num = self.connection.send(data) if num < len(data): raise RuntimeError('printer not reachable') def __del__(self): self.terminate() def terminate(self): if self.initialized: self.connection.close() self.initialized = False if __name__ == '__main__': prn = NetworkPrinter('10.0.11.98', 9100) # RESET prn.write('\x1b\x40') # Code page prn.write('\x1b\x74\x0e') #prn.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}")