Sunday, December 18, 2016

Opening cash drawer without printing

Programming an accounting software was probably the most boring thing I have ever done. There was however one interesting problem. The software was recording transactions from customers and printing bills, very simple. The printer was a small cashier style printer connected to cash drawer with RJ11 connector.


Now everytime the bill was printed the drawer was opened automatically. It is a standard behavior of a cashier printer and can be enabled in driver settings. One of the program features was to make a function that opens the cash drawer without printing any bill.

The tricky part is that the cash drawer is connected only to the printer and not the computer itself. So the opening command must come from the printer. By looking into printer's documentation I found series of commands required for generating pulse to open the drawer.


As you can see the command contains five ASCII characters:

16 + 20 + 1 + 0 + 3


In .NET there is a helper object called RawPrinterHelper provided by Microsoft which can be used for sending raw commands to the printer. The code can be found on this website. I added the code into the project and ended up making this function:


private void OpenDrawer()
{
   var command = 
   Char.ConvertFromUtf32(16) +
   Char.ConvertFromUtf32(20) +
   Char.ConvertFromUtf32(1) +
   Char.ConvertFromUtf32(0) +
   Char.ConvertFromUtf32(3);

   RawPrinterHelper.SendStringToPrinter(printer.PrinterSettings.PrinterName, command);
}


Calling OpenDrawer() does really open the drawer without printing any paper.