From 206bb2933443075b1a555b6fc9b18c25a827eb32 Mon Sep 17 00:00:00 2001 From: Emanuele Date: Mon, 30 Sep 2019 12:59:14 +0200 Subject: [PATCH] =?UTF-8?q?Modifiche=20a=20main=20e=20printer=20per=20rend?= =?UTF-8?q?ere=20possibile=20l'invio=20di=20keepalive=20anche=20quando=20s?= =?UTF-8?q?i=20=C3=A8=20=20in=20attesa=20di=20una=20risposta=20di=20stampa?= =?UTF-8?q?=20avvenuta=20o=20di=20un=20errore:=20sfrutta=20il=20timeout=20?= =?UTF-8?q?del=20socket.=20Non=20robustissimo=20ma=20funziona?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MkpWIFI/mainDime.py | 51 +++++++++++++++++++++++++++++++++++++-------- MkpWIFI/printer.py | 29 ++++++++++++++++---------- 2 files changed, 60 insertions(+), 20 deletions(-) diff --git a/MkpWIFI/mainDime.py b/MkpWIFI/mainDime.py index f88f50d..d5d8d1e 100644 --- a/MkpWIFI/mainDime.py +++ b/MkpWIFI/mainDime.py @@ -7,30 +7,63 @@ import time import sys import printer as p import logging +import socket import protocol def main(): isRunning = True + lastAck = True while isRunning: printer = p.MarkoPrinter('./conf.json', LOGGER) connessa = printer.connetti() + printer.sendCommand(protocol.deleteMemory()) while connessa: - lastTimestamp = int(time.time()*100) - commandString = protocol.callPrintImage(imageName='TM2.00I', - printMode='10', - fieldContent='{0}\r{0}'.format(lastTimestamp)) try: - printer.sendCommand(commandString) - LOGGER.debug('SENT: {}'.format(commandString)) - r=printer.readResponse('callPrintImage') - LOGGER.debug('RECEIVED: {}'.format(r)) - protocol.checkResponse('callPrintImage',r) + if lastAck == True: + lastTimestamp = int(time.time()*100) + commandString = protocol.callPrintImage(imageName='TM2.00I', + printMode='10', + fieldContent='{0}\r{0}'.format(lastTimestamp)) + printer.sendCommand(commandString) + LOGGER.debug('SENT: {}'.format(subChars(commandString))) + lastAck = False + else: + r=printer.readResponse('callPrintImage') + LOGGER.debug('RECEIVED: {}'.format(subChars(r))) + if protocol.checkResponse('callPrintImage',r): + LOGGER.info("Stampa Riuscita: {}".format(lastTimestamp)) + else: + LOGGER.error("Stampa Fallita: {}".format(lastTimestamp)) + lastAck = True + except socket.timeout as t: + LOGGER.debug("Timeout stampa") + lastAck = False + printer.keepalive() + pass except Exception as e: LOGGER.error(str(e)) pass pass pass +def subChars(s): + subDict = {'\x02': " #STX ", + '\x03': " #ETX ", + '\x1B': " #ESC ", + '\r': " #CR ", + '\f': " #LF ", + '\t': " #TAB ", + '\x06': " #ACK ", + '\x15': " #NACK " + } + out = "" + for c in s: + if c in subDict.keys(): + out += subDict[c] + else: + out += c + return out + if __name__ == '__main__': # Setup Logger LOGGER = logging.getLogger(__name__) diff --git a/MkpWIFI/printer.py b/MkpWIFI/printer.py index 85509f4..78eebfb 100644 --- a/MkpWIFI/printer.py +++ b/MkpWIFI/printer.py @@ -2,6 +2,7 @@ import socket import json import time import protocol +import threading import commands as cmd class MarkoPrinter: @@ -33,11 +34,13 @@ class MarkoPrinter: self.connected = True self.printSock.settimeout(0.5) self.checkStatus() + self.LOGGER.debug("Connessione Riuscita") except socket.error as e: self.printSock = None self.connected = False - self.log("Connessione Fallita: {}".format(e)) + self.LOGGER.debug("Connessione Fallita: {}".format(e)) else: + self.keepaliveTimer.cancel() self.printSock.close() self.printSock = None self.connetti() @@ -61,7 +64,7 @@ class MarkoPrinter: while c != b"\r": inData += c if len(inData) > 200: - self.log("Status Message Length Error") + self.LOGGER.error("Status Message Length Error") raise IOError c = self.printSock.recv(1) inData = str(inData, "ascii") @@ -71,10 +74,10 @@ class MarkoPrinter: self.headStatus[key] = int(inData[index]) else: self.headStatus[key] = str(inData[index]) - self.log("Status: {}".format(self.headStatus)) + self.LOGGER.info("Status: {}".format(self.headStatus)) statusUpdated = True except: - self.log("Status retreive error!") + self.LOGGER.error("Status retreive error!") return statusUpdated def upload(self, descr, datastr): @@ -124,11 +127,12 @@ class MarkoPrinter: self.printSock.settimeout(1.0) self.printSock.send(bytearray(c,'ascii')) self.printSock.settimeout(0.5) + self.sending = False return True except socket.error as e: self.LOGGER.error('Socket Error: {}'.format(e)) return False - self.sending = False + self.sending = False pass def readResponse(self, c): @@ -137,17 +141,20 @@ class MarkoPrinter: respTerm = cmd.DEFprefix['end'] if cmd.MKPcommands[c]['defPrefix'] else cmd.MKPcommands[c]['end'] if self.connected: try: - self.printSock.settimeout(1.0) - self.printSock.setblocking(True) + # lettura bloccante del socket in attesa di conferma + self.printSock.settimeout(5) c = self.printSock.recv(1) while c != respTerm: buf += c if str(c, 'ascii') == respTerm: break c = self.printSock.recv(1) + # disattivo lettura bloccante self.printSock.settimeout(0.5) - self.printSock.setblocking(False) return str(buf, 'ascii') + except socket.timeout as t: + self.reading = False + raise t except socket.error as e: self.LOGGER.error('Socket Error: {}'.format(e)) return False @@ -156,12 +163,12 @@ class MarkoPrinter: def keepalive(self): statusUpdated = False - if not self.sending: - self.log("Keepalive") + self.LOGGER.debug("Keepalive") + if not self.sending and not self.reading: if self.connected: try: self.printSock.send(b"\x1B*\r") - self.log(str(self.printSock.recv(5))) + self.LOGGER.debug(str(self.printSock.recv(5))) if self.inkStatCounter <= self.settings["inkstat"]: self.inkStatCounter += 1 else: