Python Forum

Full Version: Control a dot matrix printer
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I've recently got two of these amazing dot matrix printers. They support text data and ASCII commands control them.
So, I'd like to make a GUI to control it using... eh... Python of course Big Grin !
The point is that I know how to send text data to the printer, using :
variableOfYourChoice = open("LPT1", "w")
variableOfYourChoice.write("Your text")
variableOfYourChoice.close
but now I'd like to send ASCII or decimal commands to them so I can change the print quality, text size, font, text format, control paper, etc... For you to understand what I'm explaining, here is the list of the compatible commands (I have the ML320E and ML390E printers), they are in the tables starting at page 12 : OKI Microline printers compatible commands list
How am I supposed to do that ?

Thank you in advance for your replies,
Clément.
use chr(): https://docs.python.org/2/library/functions.html#chr
and ord(): https://docs.python.org/2/library/functions.html#ord
or encoded string (this one in hex for 'ABC') "\x41\x42\x43"
I'm a bit confused,
I want for example to send 27 120 0 to set it in Utility mode.
When I do :
printer = open("LPT1", "w")
printer.write("\x27\x120\x0")
printer.close
It says I can't use more that two numbers:
Error:
File "C:\Documents and Settings\Administrateur\Bureau\test.py", line 2 printer.write("\x27\x120\x0") ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 9-11: truncated \xXX escape
And when I do :
printer = open("LPT1", "w")
printer.write(chr(27), chr(120), chr(0))
printer.close
It obviously tells me that printer.write takes only one argument and that I gave 3 of them.
Quote:I want for example to send 27 120 0 to set it in Utility mode.
I assume 27, 120, and 0 are decimal, if so:
printer.write("\x27\x120\x0")
is wrong because the x indicates hex.
you need:
printer.write("\x1b\x78\x00")
witch is the hex equivalent.

download an ASCII conversion chart from google images
THANK YOU !
It works !!
I'm so happy, thank you !
I'll finally be able to do what I wanted to do !
Thank you ! Clap