Reputation: 452
I am using a Thermal Receipt Printer POS-8330 found HERE
I am writing code that sends a python string to the printer, here is the code that accomplishes this
companyName = "Hilton Steel and Cement"
paytype = self.payment_Mode
companyName = companyName + "\n\nSale Receipt\n\nOpp Golden Line Mall\nP.O BOX 3404-20100\n TEL: 0727441192\nEMAIL: [email protected]"
receiptNo = randint(1, 100000)
finalString = companyName + "\n\nReceipt No:" + str(receiptNo) + "\n\n" + self.getproductList()\
+ "\n______________________________________\n" + "Total Due: " + str(self.totaltoBePaid) + "\n______________________________________\n\n" + "Paid In: " + paytype + "\n\n"+ "Served By: " + str(self.user) +"\n" + "Payment: " + str(self.finalvariables[1])+ "\nBalance: " + str(self.finalvariables[0]) + "\n\n\nWelcome Back"
open(self.filename, "w").write(finalString)
os.startfile(self.filename, "print")
The printer has a programming manual found HERE, but I cannot make out anything off of it or particularly how to apply it. Kindly give me some help with the following
The code I have written gives this result on the POS printer.
On a regular full size printer it gives
which is exactly what I want.
Upvotes: 0
Views: 727
Reputation: 43523
Looking at the manual, this printer uses control characters (the first 32 characters in ASCII) to start commands and separate them from text.
So you can intersperse text and printer commands. Just make sure to open the output file in binary
mode!
For example, looking at page 17 of the programming manual, the command ESC @
resets the printer.
This command consists of two characters (bytes); decimal 27 followed by decimal 64.
You can create that command as follows:
In [7]: bytes([27,64])
Out[7]: b'\x1b@'
You have to set the left margin (command GS L
, page 37) and the printing area width (command GS W
, page 38).
Note that "horizontal motion units" is explained in the GS P
command on page 38. The default horizontal motion unit is 25.4/180 = 0.1411 mm or 1/180 = 0.0055 inches.
So if you want to set the left margin to approximately 5 mm (4.94 mm to be precise), you have to send the following command:
dIn [14]: bytes([29, 76, 35,0])
Out[14]: b'\x1dL#\x00'
The value 35 is calculated like this:
In [13]: round(5/(25.4/180))
Out[13]: 35
If you also want to set the printing width to 60 mm, then the argument for the GS W
command would have to be:
In [15]: round(60/(25.4/180))
Out[15]: 425
This is larger than 255, so it would have to be spread over two bytes;
In [17]: 425-256
Out[17]: 169
The command would be:
In [18]: bytes([29, 87, 169,1])
Out[18]: b'\x1dW\xa9\x01'
You can combine these commands, I think:
In [20]: bytes([29, 76, 35, 0, 29, 87, 169,1])
Out[20]: b'\x1dL#\x00\x1dW\xa9\x01'
Edit:
Adding the print commands to the data is easy:
printdata = b'\x1dL#\x00\x1dW\xa9\x01' + finalString.encode('utf-8')
Note that you do need to encode
the string, since the printer commands are binary.
Also note that you need to write the string to a file in binary mode.
with open(self.filename, "wb") outf:
outf.write(printdata)
os.startfile(self.filename, "print")
Finally, I used with
so that the file is properly closed after writing. The way you are using it leaves the file open.
Upvotes: 1