Feb-22-2019, 06:43 PM
Hi nilamo,
I'm so happy today, you can't believe.
Everything is fixed !
Thank you !
When reading your message, this did remind me a previous message of Larz60+ in this thread https://python-forum.io/Thread-Control-a-dot-matrix-printer. And was showing the use of chr().
So I converted all my hex values into decimal values in the json.
And I changed a bit the lib to use chr()...
And that's all !
Here is the result
[Image: printer2.jpg]
I'm so glad it works !
Now the GUI is left to do !
Thanks again,
Clément.
I'm so happy today, you can't believe.
Everything is fixed !
Thank you !
When reading your message, this did remind me a previous message of Larz60+ in this thread https://python-forum.io/Thread-Control-a-dot-matrix-printer. And was showing the use of chr().
So I converted all my hex values into decimal values in the json.
And I changed a bit the lib to use chr()...
And that's all !
Here is the result
#NAME = commands.json { "lineFeed":{"command":"10", "text":"Line Feed"}, "carriageReturn":{"command":"13", "text":"Carriage Return"}, "formFeed":{"command":"12", "text":"Form Feed"}, "newLine":{"command":"10 13", "text":"New Line"}, "setLetterQuality":{"command":"27 120 49", "text":"Set Letter Quality"}, "setUtility":{"command":"27 120 48", "text":"Set Utility"}, "fontRoman":{"command":"27 107 48"}, "fontHelvette":{"command":"27 107 49"}, "fontCourrier":{"command":"27 107 50"}, "fontPrestige":{"command":"27 107 51"}, "fontGothic":{"command":"27 107 54"}, "fontBold":{"command":"27 107 55"}, "styleNormal":{"command":"27 113 0"}, "styleOutline":{"command":"27 113 1"}, "styleShadow":{"command":"27 113 2"}, "styleOutlineShadow":{"command":"27 113 3"}, "10Cpi":{"command":"27 80 18"}, "12Cpi":{"command":"27 77 18"}, "15Cpi":{"command":"27 103 18"}, "17Cpi":{"command":"27 80 15"}, "20Cpi":{"command":"27 77 15"}, "propOn":{"command":"27 112 1"}, "propOff":{"command":"27 112 0"}, "doubleOn":{"command":"27 119 49 27 87 49"}, "doubleOff":{"command":"27 119 48 27 87 48"} }
#NAME = printer.py import platform #Lib to check os import json class Printer(): #Main class def __init__(self, port, file): #"port" is the parralel port where the printer is pluged in, "file" is the json file containg the custom functions and command to send to the printer self.os = platform.system() #Returning os in a variable self.data = json.load(open(file)) #Opening user selected file if type(port) != int or port <= 0: #Checks if the port is an int raise Exception(f"Invalid port : {port}") #Returns error if self.os == "Linux" : #If on Linux self.port = open("/dev/lp"+str(port-1), "w") #Open as /dev/lpx elif self.os == "Windows" : #If on Winows self.port = open("LPT"+str(port), "w") #Open as LPTx else: #If on oter system raise Exception(f"Invalid OS : {self.os}") #No supported os error def printText(self, text): self.port.write(str(text)) self.port.flush() def command(self, key): #custom command generator if key in self.data: #If the key is in the json commands = self.data[key]["command"] #Extract the command string from the json commands = commands.split(" ") #Split each dec number individually for n in range(len(commands)): #For each dec number self.printText(chr(int(commands[n]))) #Send the command to the printer else: #If the key isn't in the json raise Exception(f"Invalid key: {key}") #Wrong key error def sendAscii(decimals): decimals = decimals.split(" ") for n in range(len(decimals)): printText(chr(decimals[n]))
#NAME = test.py from printer import * ML390 = Printer(1, "commands.json") ML390.command("cleanPaperBin") ML390.command("setLetterQuality") ML390.command("10Cpi") ML390.command("doubleOn") ML390.command("underlineOn") ML390.command("boldOn") ML390.command("styleOutlineShadow") ML390.command("fontGothic") ML390.command("alignCenter") ML390.command("newLine") ML390.printText("****************************************\n") ML390.printText("Thank you nilamo and Larz60+ !\n\n") ML390.printText("You solved all my problems...\n\n") ML390.command("newLine") ML390.command("newLine") ML390.command("alignLeft") ML390.command("20Cpi") ML390.printText("...at least for now ;-p...\n\n") ML390.command("10Cpi") ML390.command("alignCenter") ML390.printText("****************************************\n\n") ML390.command("formFeed") ML390.command("reset")[Image: printer1.jpg]
[Image: printer2.jpg]
I'm so glad it works !
Now the GUI is left to do !
Thanks again,
Clément.