Print a sales receipt using a thermal printer with C# for POS Application

If you have ever tried to build a POS (Point Of Sales or Point of Services) application, you might have come across the requirement to print a sales receipt like the following. Not just retail stores, but these receipts are produced by banks, hospitality and healthcare too.

Most of the time, such a receipt can be printed using a POS printer, which looks like the one below. The application developer needs to send ESC/POS commands to such a printer  (eg: EPSON) to print the actual receipt. Depending on the manufacturer the capabilities can be vary. However, most of them can handle most of the print commands and print a nicely formatted receipt.

The main advantage of using raw ESC/POS commands for printing is that the printing performance will be faster but this depends on the printer itself.

In this post, I will be using a windows application to send commands to the printer. If you want to do this through a ASP.NET website, the process is a bit more complex and I will try to create another post for it, whenever I get some free time.

As usual, a quick google search landed me on some Delphi code and most of the code were for those expensive printers. Mostly, Epson. Then I found a link to this page sending-a-bit-image-to-an-epson-tm-t88iii-receipt-printer-using-c-and-escpos which is a dead end now. Luckily, I searched using a piece of code from SFO and it landed me here. printing-utf-8-encoded-text-in-epson-tm-t88iv-thermal-printer

But still, I didn’t quite understand how to send POS commands to the printer. Then, after a while I found this page (Epson FX Printer Codes). Which is for EPSON FX printers but some of these codes worked for my printer too as it supported ESC/POS commands. I also found this PDF which has some details about the codes.

As an example, Say, I want to print large text, then I have to send 27 87 48 as separate one byte commands in sequence. Then, whatever I print next, will be printed as large text, until I send the command to reset to normal text. 

After a lot of trial and error (and wasted paper), I managed to print an Okay-looking receipt using that cheap printer. You need the code in the following gist.

 If you have a cheap printer like I have, which is USB powered, then chances are it is already detected but you have to setup a printer in the Devices & Printers control panel section. Until you do this, you won’t be able to connect it using the code. (I tried)
So follow these steps to setup the printer, 

  1. Go to Devices  & Printers (Control Panel\All Control Panel Items\Devices and Printers)
  2. On the top menu, there is “Add a Printer”. (Windows 8.1)
  3. Select “The printer I want isn’t listed” => Next
  4. “Add a local printer with manual settings” => Next
  5. “Use an Existing Port” and select “USB001 (Virtual Printer Port For USB)” => Next
  6. Select “Generic” from Manufacturer, “Generic / Text Only” from Printers => Next
  7. If it asked, which version you want, keep recommended option and  => Next
  8. Give a Printer Name, E.g.: “POS58” => Next
  9. “Do not share” but if you want, share it.
  10. Turn on your printer and click on “Print a test page”. It should print a long text.
  11. Click Finish and you are ready to use the printer in your code.
Just add the class files to your code, paste the following code in appropriate places and print the receipt. (NOTE! You need your own Invoice data structure)
I don't even understand most of this code and I don't really care. If it works, why break it? I highly doubt that an interviewer will ask how to send EPOS/POS commands to a thermal printer. but if they do, Refer this Sending a bit image to an Epson TM-T88III receipt printer using C# and ESC/POS by Nicholas Piasecki on December 9th, 2009

Popular posts from this blog

Automatic redirect upon session timeout using ASP.NET MVC and Javascript

Could not create SSL/TLS secure channel - Fixed