Python: Talking to Star Micronics POS printer thrugh serial port (lib)

November 30th, 2008

The computer in some shop that uses my program broke this week. Program was written years ago in Python and uses wxPython for GUI. It needed to communicate to those little squeaky printers that print bills. The whole thing looked kind of cute back then and still does. Python talks to the printer directly via serial port (without printer drivers or something like that) and it’s very simple. I used pyserial module. So in case there is someone somewhere who wants to connect his python to one of those particular printers he can do that with this little library:

download here: posprinter.py

Library has two classes, SerialPrinter is for talking to the printer, and ConsolePrinter is for simulating the same thing in console (for when you don’t have a printer, testing, developing..)

Usage is simple:

s = ConsolePrinter(None, 42)
headers  = [["NAME", "SURNAME", "AGE"]]
data  = [["mojca", "baz", "25"],["jana", "foo", "29"],["janko", "foo", "101"]]
format = ('', '', 'align=right')
tabs = (0, 15, 30, 42)

s.write('LIST OF PEOPLE:')
s.feed()
s.writeTable(headers, tabs, format)
s.line()
s.writeTable(data, tabs, format)
s.line('=')

Will print:

LIST OF PEOPLE

NAME           SURNAME                 AGE
------------------------------------------
mojca          baz                      25
jana           foo                      29
janko          foo                     101
==========================================

The code works on Star Micronics printers that have a serial port. You get a book with them that describe the binary commands that you have to send.

Leave a Reply