Monday, January 11, 2010

Python and RS323 (ESC/POS) Receipt Printer

Currently, I am working a bit with RS323 (ESC/POS) Receipt Printer. The most interesting part working with it is I do not need to have a driver. This is very helpful because I am planning to use it with Linux OS.
I decided to use Python, so anytime I need to switch to Windows OS, nothing need to be changed in the code part.

Since the printer is using RS323 connection, I need to find the simplest way of communicating with that device in Python. With no effort I found pySerial. Taken from the website itself: This module encapsulates the access for the serial port. It provides backends for Python running on Windows, Linux, BSD (possibly any POSIX compliant system), Jython and IronPython (.NET and Mono). The module named "serial" automatically selects the appropriate backend.

After installing pySerial, I started to play around with it. It is really simple and straight-forward.
It took me sometimes to understand ESC/POS commands, since I was totally not familiar with it.

Just to give some idea on how simple it is, here is an example:

import serial
ser = serial.Serial(port='/dev/ttyS0', baudrate=19200)
ser.portstr #check which serial port
ser.write('Hello World!\n')
ser.close()


I spent sometimes trying to figure out how to work with ESC/POS. One of the issue is the baud rate setting. This must be configured properly otherwise the printer will not print the data properly.

Another thing is that the printer will not print anything if you don't tell it to print data in it's buffer. One way to do this is terminate any string/data you want to print with '\n' (or ASCII decimal 10).

Next step is to know what ESC/POS commands available for your type of printer. I downloaded some docs from the vendor's website, but unfortunately there are only minimum info about ESC/POS programming.
Fortunately, ESC/POS commands is pretty much a standard, so I found some other documents from different vendor, by googling it, and got more informations.

Another interesting thing is that when I tried to print several lines of data in Linux, somehow only partial string in certain line that was printed. Got me confused for a bit, then I realized that this is printer buffer issue. The buffer in the printer is too small for the amount and speed of the data I sent to it. In this case adjustment need to be done, so the printer buffer won't get overloaded and discarded the incoming data.

I don't really like current receipt printer that I work on. Minimum documentations, vendor specific power supply cable, and some other stuff.
I haven't search much about other receipt printer from other vendors, but I am pretty sure that there are some that are more portable to work with.

No comments:

Post a Comment