Drive a POS printer via USB in c# [closed]
Asked Answered
K

4

7

Any ideas how i can best drive a USB POS printer from c#. POS printers are usually serial, TCP/IP or USB based. I know how to accomplish serial and TCP/IP but have no idea about communications through USB in C#. I know that there is a layer available from Microsoft called POS.NET, but I want to try and avoid using this. Any ideas or any C# libraries that people can recomend would be really appreciated. Thanks

Keon answered 9/10, 2008 at 14:16 Comment(0)
I
6

You should really consider using POS for .NET and OPOS or .NET service objects (Epson, for example provides both). POS for .NET conforms to the UnifiedPOS industry standard for interfacing with these devices.

Isaiasisak answered 4/3, 2009 at 1:46 Comment(0)
T
3

If the printer registers itself as a Human Interface Device, you can P/INVOKE into the appropriate Win32 APIs. Here are the signatures:

    [ DllImport( "hid.dll", SetLastError=true ) ]
    public static extern Boolean 
    HidD_FlushQueue( SafeFileHandle HidDeviceObject );        

    [ DllImport( "hid.dll", SetLastError=true ) ]
    public static extern Boolean 
    HidD_FreePreparsedData( ref IntPtr PreparsedData );        

    [ DllImport( "hid.dll", SetLastError=true ) ]
    public static extern Boolean 
    HidD_GetAttributes(  SafeFileHandle HidDeviceObject
                       , ref HIDD_ATTRIBUTES Attributes );        

    [ DllImport( "hid.dll", SetLastError=true ) ]
    public static extern Boolean 
    HidD_GetFeature(  SafeFileHandle HidDeviceObject
                    , ref Byte lpReportBuffer
                    , Int32 ReportBufferLength );        

    [ DllImport( "hid.dll", SetLastError=true ) ]
    public static extern Boolean 
    HidD_GetInputReport( SafeFileHandle HidDeviceObject
                        ,ref Byte lpReportBuffer
                        ,Int32 ReportBufferLength );        

    [ DllImport( "hid.dll", SetLastError=true ) ]
    public static extern void HidD_GetHidGuid( ref System.Guid HidGuid );

    [ DllImport( "hid.dll", SetLastError=true ) ]
    public static extern Boolean 
    HidD_GetNumInputBuffers(  SafeFileHandle HidDeviceObject
                            , ref Int32 NumberBuffers );        

    [ DllImport( "hid.dll", SetLastError=true ) ]
    public static extern Boolean 
    HidD_GetPreparsedData( SafeFileHandle HidDeviceObject
                          ,ref IntPtr PreparsedData );

    [ DllImport( "hid.dll", SetLastError=true ) ]
    public static extern Boolean 
    HidD_SetFeature(  SafeFileHandle HidDeviceObject
                    , ref Byte lpReportBuffer
                    , Int32 ReportBufferLength );

    [ DllImport( "hid.dll", SetLastError=true ) ]
    public static extern Boolean 
    HidD_SetNumInputBuffers( SafeFileHandle HidDeviceObject
                            ,Int32 NumberBuffers );

    [ DllImport( "hid.dll", SetLastError=true ) ]
    public static extern Boolean 
    HidD_SetOutputReport( SafeFileHandle HidDeviceObject
                         ,ref Byte lpReportBuffer
                         ,Int32 ReportBufferLength );

    [ DllImport( "hid.dll", SetLastError=true ) ]
    public static extern Int32 
    HidP_GetCaps( IntPtr PreparsedData, ref HIDP_CAPS Capabilities );

    [ DllImport( "hid.dll", SetLastError=true ) ]
    public static extern Int32 
    HidP_GetValueCaps(  Int16 ReportType
                      , ref Byte ValueCaps
                      , ref Int16 ValueCapsLength
                      , IntPtr PreparsedData );
Trentontrepan answered 9/10, 2008 at 14:25 Comment(1)
When you connect the printer it trys to install itself, and fails because no drivers are present. It then appears in the "Other Devices" section in Device Manager.Keon
O
1

There is a C# wrapper for Win32 USP api's from the icsharpcode guys here, and I've seen at least one other reference to USB stuff in .Net.

What may be tricky is whether or not the Printer requires OPOS in order to function. I did some work on a POS project in .Net a few years back, and from what I remember OPOS is pretty much the standard for the hardware side of things (And at the time all OPOS APIs utilized COM interop). The devices sometimes also have a documented proprietary communications protocol, in which case you should be able to utilize that to talk to the device using the USB library.

Opposite answered 9/10, 2008 at 14:26 Comment(0)
H
1

If you've already rolled your own printer control code then you can pass the commands straight through to the printer by setting up the printer using the "Generic / Text" printer driver on the USB port. That driver will not re-interpret the control codes and try to print them, but it will just hand them off to the printer as-is.

Then the USB port can be used just like any other printer.

This approach worked for me on a POS system that I developed.

Hainaut answered 9/10, 2008 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.