Feb-21-2019, 05:55 PM
Hi nilamo and Larz60+,
Thank you for your help !
So nilmao, you're totally right. I don't know why I haven't done that before, I'm stupid. So i worked on it.
Larz60+, I saw your code, that's not exactly what I was looking for, but nilmao found what I was looking for. Anyway, I keep it in mind, it could be useful to another project.
Anyway, following your idea nilmao, here is what I ended up with :
), python tells me that I have to put the hex value after. And if I replace the string by "\\x" or r"\x", when running the command, I the printer litearally prints "\x0A" or "\x0B", or ... (not the quotes).
Should I create an other thread or can you help me now ?
Thanks again (I never say it enough
),
Clément.
Thank you for your help !
So nilmao, you're totally right. I don't know why I haven't done that before, I'm stupid. So i worked on it.
Larz60+, I saw your code, that's not exactly what I was looking for, but nilmao found what I was looking for. Anyway, I keep it in mind, it could be useful to another project.
Anyway, following your idea nilmao, here is what I ended up with :
#NAME = test.py from printer import * ML390 = Printer(1, "commands.json") ML390.command("setUtility")
#NAME = commands.py { "lineFeed":{"command":"0A", "text":"Line Feed"}, "carriageReturn":{"command":"0D", "text":"Carriage Return"}, "formFeed":{"command":"0C", "text":"formFeed"}, "setLetterQuality":{"command":"1B 78 32", "text":"Set Letter Quality"}, "setUtility":{"command":"1B 78 31", "text":"Set Utility"} }
#NAME = printer.py import platform #Lib to check os import json class Printer(): #Main class def __init__(self, port, file): #"port" is the parallel port where the printer is plugged in, "file" is the json file containing 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 print(commands) #TEMPORARY commands = commands.split(" ") #Split each hex number individually for n in range(len(commands)): #For each hex number commands[n] = f"\x{commands[n]}" #add "\x" before commands = "".join(commands) #Put everything back into a single string self.printText(commands) #Send the command to the printer print(self.data[key]["text"]) #TEMPORARY print(commands) #TEMPORARY else: #If the key isn't in the json raise Exception(f"Invalid key: {key}") #Wrong key errorThe new problem now is that in line 31, (you already know what will happen
![Wink Wink](https://python-forum.io/images/smilies/wink.png)
Should I create an other thread or can you help me now ?
Thanks again (I never say it enough
![Big Grin Big Grin](https://python-forum.io/images/smilies/biggrin.png)
Clément.