Microsoft POS Library for .NET

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?

No related posts.

Tags: , , ,

3 Responses to “Microsoft POS Library for .NET”

  1. Juan Pablo Farber says:

    I put your PosPrinter Code. It opens the printer fine, it prints fine, but when it time to close the printer it shows me this error: “Error 5: Acceso denegado, ruta de acceso: C:\Documents and Settings\All Users\Epson\pos\tm\Statistics\Printer.TM-T88IV.J4PF065676.xml”. Sorry is in spanish but it says that there is a Access Denied to that Path.
    Can you help me to find out what is happen?
    Thanks a lot

    • wizzard says:

      It sounds like your specific POS driver is trying to write some sort of statistical log file to that path and it doesn’t have access, probably because of a user rights issue. Check the folder rights in Windows Explorer to see whether the user who is logged in when you run it has access to that folder or possibly you can change the location that it is set to write files to.

  2. Juan Pablo Farber says:

    Ok. I disable the statistics for the printer and it seems it works fine now.
    Thanks

Leave a Reply