Tuesday, December 29, 2015

Print a receipt using a Thermal Printer with C#.NET

I’ve been asked to integrate receipt printing for a software I’ve been developing and the thermal printer my client uses is a very cheap one ordered from eBay for $36.31. There are more developer friendly feature rich printers available but they are very expensive. Who wants to use a expensive printer when the customers will throw away the receipt anyway?

UPDATE: After seeing the feature set of the more expensive Epson printer, I do think that anyone who wishes to have receipts printed, should use a feature rich printer. Some even have color printing capabilities. 

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. Below is the code.  You also need the code in the following gist. https://gist.github.com/dirnthelord/19cfa44056f641421247
 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