Posts Tagged ‘POS’

Microsoft POS Library for .NET

Thursday, October 14th, 2010

I’ve been writing a specialised point of sale system for a local business and had to use the POS printer on their till.  Fortunately, Microsoft have a nice library available for .NET that is freely downloadable that deals with most of the hard work of communicating with POS devices.

I was interested in the POSPrinter class.  They do provide a simulator so that you can test things out using the simulated printer, but it doesn’t support anything other than basic plain text, whereas I needed to use formatted text.

POS printers usually use a control language called ESC/POS.  This was developed by Epson originally for their POS printers, but it has become a standard.  Basically, it is mostly a matter of sending plain text, but you can also send certain escape sequences to change settings on the printer.

Fortunately, Microsoft document a lot of this on MSDN, so you don’t need to work it all out from scratch.  There are things like knowing that the main escape sequence is &H1B or 27 (known as ESC) and then a pipe character followed by optional numbers, small letters or symbols followed by a capital letter.  Eg. ESC|#P cuts the paper, ESC|bC is the code to turn on bold etc.

However, you may find it useful to have some sample code…


Imports Microsoft.PointOfService

Public Class POSPrinter

    Public Const ESC = Chr(&H1B) & "|"
    Public Const SetBold = ESC & "bC"
    Public Const SetUnderline = ESC & "uC"
    Public Const SetItalic = ESC & "iC"
    Public Const SetCentre = ESC & "cA"
    Public Const SetRight = ESC & "rA"
    Public Const ResetFormatting = ESC & "N"

    Public Shared Function SetSize(ByVal Size As Integer) As String
        Return ESC & Size & "C"
    End Function

    Public Shared Function GetAndInitPosPrinter() As Microsoft.PointOfService.PosPrinter
        Dim pe As New PosExplorer()
        Dim ppdi = pe.GetDevice(DeviceType.PosPrinter, My.Settings.PrinterName)
        Dim pp As Microsoft.PointOfService.PosPrinter = pe.CreateInstance(ppdi)
        pp.Open()

        pp.Claim(250)

        pp.DeviceEnabled = True

        Return pp
    End Function

    Public Shared Sub ReleaseAndClosePosPrinter(ByVal pp As Microsoft.PointOfService.PosPrinter)

        pp.Release()

        pp.Close()
    End Sub

    Public Shared Sub PrintTest()

    Dim pp As Microsoft.PointOfService.PosPrinter = Nothing

    pp = GetAndInitPosPrinter()

    Dim msg As String = "This is a test" & vbCrLf & SetBold & SetSize(3) & SetCentre & "it works" & SetSize(1) & " pretty well" & vbCrLf & "OK"

    pp.PrintNormal(PrinterStation.Receipt, msg)

    ReleaseAndClosePosPrinter(pp)

    End Sub


One other thing to watch out for is that if you are running any other POS software on the till, such as Microsoft Dynamics POS, then you might need to go into the settings and tell it to share the printer nicely with the other children applications as some POS software tends to be pretty possessive

Also, always make sure that you release and close any POS devices when you are done with them. For some reason when you start using them you need to open them, claim them and then enable them which seems ridiculous overkill to me, but what do I know?