如何在C#应用程序中打开现金抽屉?

时间:2015-08-06 00:12:01

标签: c#

我使用条形码为超市申请,我需要通过C#中的点击按钮打开现金抽屉。

此代码用于保存数据和打印收据。

method="post"

我需要代码打开现金抽屉。

4 个答案:

答案 0 :(得分:1)

这是供应商特定的问题。您需要现金抽屉供应商的API和/或设备驱动程序的文档,以了解如何指示它打开或关闭。

答案 1 :(得分:1)

打开现金抽屉的方法取决于现金抽屉连接到应用程序PC的方式。现金抽屉可以直接连接到收银台或通过USB或串行打印机。

  1. 如果直接连接现金抽屉,我们可以使用Microsoft POS for .Net库。
  2. 通过USB打印机连接的现金抽屉 - 使用打印机API,例如STAR或EPSON有自己的库与现金抽屉互动,获取状态等。
  3. 通过串行打印机连接的现金抽屉 - 只需发送一个字节数组来触发现金抽屉打开命令,如下所示:

    byte[] buffer = new byte[5]
    {
      (byte) 27,
      (byte) 112,
      (byte) 0,
      (byte) 25,
      (byte) 250
     };
    //port is an instance of a Serial Port
     this.port.Write(buffer, 0, buffer.Length);
    
  4. 请在以下位置找到对写入方法的进一步参考:

    SerialPort.Write Method (Byte[], Int32, Int32)

答案 2 :(得分:0)

How to program a cash drawer to open using Escape commands http://www.beaglehardware.com/howtoprogramcashdrawer.html

通过ESC / POS命令打开现金抽屉的方法(如果您的打印机与此兼容)需要以字符串形式发送十六进制值。

以下已经过测试并且有效,您需要自己的打印功能实现(SendStringToPrinter)

  

//打印机名称是连接到现金的打印机的名称   抽屉

const string ESC = "\u001B";
const string p = "\u0070";
const string m = "\u0000";
const string t1 = "\u0025";
const string t2 = "\u0250";
const string openTillCommand = ESC + p + m + t1 + t2;

SendStringToPrinter(printerName, openTillCommand);

答案 3 :(得分:0)

此代码适用于 bpos 热敏打印机。也适用于打印机支持抽屉(usb) 使用 Vb.net 请使用这个样本 [https://github.com/lavahasif/opendrawvb.net][1]

Imports System.Runtime.InteropServices

Module Module1
    Public Sub Main(ByVal args As String())
        Const ESC As String = ChrW(27)
        Const p As String = "p"
        Const m As String = vbNullChar
        Const t1 As String = "%"
        Const t2 As String = "ɐ"
        Const openTillCommand As String = ESC & p & m & t1 & t2
'        printer("POS-X Thermal Printer")
        RawPrinterHelper.SendStringToPrinter("POS-X Thermal Printer", openTillCommand)
    End Sub
 
End Module

Imports System
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Public Class RawPrinterHelper
    <StructLayout(LayoutKind.Sequential, CharSet := CharSet.Ansi)>
    Public Class DOCINFOA
        <MarshalAs(UnmanagedType.LPStr)> Public pDocName As String
        <MarshalAs(UnmanagedType.LPStr)> Public pOutputFile As String
        <MarshalAs(UnmanagedType.LPStr)> Public pDataType As String
    End Class

    <
        DllImport _
            ("winspool.Drv", EntryPoint := "OpenPrinterA", SetLastError := True, CharSet := CharSet.Ansi,
             ExactSpelling := True, CallingConvention := CallingConvention.StdCall)>
    Public Shared Function OpenPrinter(
                                       <MarshalAs(UnmanagedType.LPStr)> ByVal szPrinter As String,
                                       <Out> ByRef hPrinter As IntPtr, ByVal pd As IntPtr) As Boolean
    End Function

    <
        DllImport _
            ("winspool.Drv", EntryPoint := "ClosePrinter", SetLastError := True, ExactSpelling := True,
             CallingConvention := CallingConvention.StdCall)>
    Public Shared Function ClosePrinter(ByVal hPrinter As IntPtr) As Boolean
    End Function

    <
        DllImport _
            ("winspool.Drv", EntryPoint := "StartDocPrinterA", SetLastError := True, CharSet := CharSet.Ansi,
             ExactSpelling := True, CallingConvention := CallingConvention.StdCall)>
    Public Shared Function StartDocPrinter(ByVal hPrinter As IntPtr, ByVal level As Int32,
                                           <[In], MarshalAs(UnmanagedType.LPStruct)> ByVal di As DOCINFOA) As Boolean
    End Function

    <
        DllImport _
            ("winspool.Drv", EntryPoint := "EndDocPrinter", SetLastError := True, ExactSpelling := True,
             CallingConvention := CallingConvention.StdCall)>
    Public Shared Function EndDocPrinter(ByVal hPrinter As IntPtr) As Boolean
    End Function

    <
        DllImport _
            ("winspool.Drv", EntryPoint := "StartPagePrinter", SetLastError := True, ExactSpelling := True,
             CallingConvention := CallingConvention.StdCall)>
    Public Shared Function StartPagePrinter(ByVal hPrinter As IntPtr) As Boolean
    End Function

    <
        DllImport _
            ("winspool.Drv", EntryPoint := "EndPagePrinter", SetLastError := True, ExactSpelling := True,
             CallingConvention := CallingConvention.StdCall)>
    Public Shared Function EndPagePrinter(ByVal hPrinter As IntPtr) As Boolean
    End Function

    <
        DllImport _
            ("winspool.Drv", EntryPoint := "WritePrinter", SetLastError := True, ExactSpelling := True,
             CallingConvention := CallingConvention.StdCall)>
    Public Shared Function WritePrinter(ByVal hPrinter As IntPtr, ByVal pBytes As IntPtr, ByVal dwCount As Int32,
                                        <Out> ByRef dwWritten As Int32) As Boolean
    End Function

    Public Shared Function SendBytesToPrinter(ByVal szPrinterName As String, ByVal pBytes As IntPtr,
                                              ByVal dwCount As Int32) As Boolean
        Dim dwError As Int32 = 0, dwWritten As Int32 = 0
        Dim hPrinter As IntPtr = New IntPtr(0)
        Dim di As DOCINFOA = New DOCINFOA()
        Dim bSuccess As Boolean = False
        di.pDocName = "drawer"
        di.pDataType = "RAW"

        If OpenPrinter(szPrinterName.Normalize(), hPrinter, IntPtr.Zero) Then

            If StartDocPrinter(hPrinter, 1, di) Then

                If StartPagePrinter(hPrinter) Then
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
                    EndPagePrinter(hPrinter)
                End If

                EndDocPrinter(hPrinter)
            End If

            ClosePrinter(hPrinter)
        End If

        If bSuccess = False Then
            dwError = Marshal.GetLastWin32Error()
        End If

        Return bSuccess
    End Function

    Public Shared Function SendFileToPrinter(ByVal szPrinterName As String, ByVal szFileName As String) As Boolean
        Dim fs As FileStream = New FileStream(szFileName, FileMode.Open)
        Dim br As BinaryReader = New BinaryReader(fs)
        Dim bytes As Byte() = New Byte(fs.Length - 1) {}
        Dim bSuccess As Boolean = False
        Dim pUnmanagedBytes As IntPtr = New IntPtr(0)
        Dim nLength As Integer
        nLength = Convert.ToInt32(fs.Length)
        bytes = br.ReadBytes(nLength)
        pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength)
        Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength)
        bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength)
        Marshal.FreeCoTaskMem(pUnmanagedBytes)
        Return bSuccess
    End Function

    Public Shared Function SendStringToPrinter(ByVal szPrinterName As String, ByVal szString As String) As Boolean
        Dim pBytes As IntPtr
        Dim dwCount As Int32
        dwCount = (szString.Length + 1)*Marshal.SystemMaxDBCSCharSize
        pBytes = Marshal.StringToCoTaskMemAnsi(szString)
        SendBytesToPrinter(szPrinterName, pBytes, dwCount)
        Marshal.FreeCoTaskMem(pBytes)
        Return True
    End Function
End Class


使用 c#

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class RawPrinterHelper
{
    // Structure and API declarions:
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class DOCINFOA
    {
        [MarshalAs(UnmanagedType.LPStr)] public string pDocName;
        [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
        [MarshalAs(UnmanagedType.LPStr)] public string pDataType;
    }

    [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi,
        ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter,
        IntPtr pd);

    [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true,
        CallingConvention = CallingConvention.StdCall)]
    public static extern bool ClosePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi,
        ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)]
        DOCINFOA di);

    [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true,
        CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true,
        CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true,
        CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true,
        CallingConvention = CallingConvention.StdCall)]
    public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);

    // SendBytesToPrinter()
    // When the function is given a printer name and an unmanaged array
    // of bytes, the function sends those bytes to the print queue.
    // Returns true on success, false on failure.
    public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
    {
        Int32 dwError = 0, dwWritten = 0;
        IntPtr hPrinter = new IntPtr(0);
        DOCINFOA di = new DOCINFOA();
        bool bSuccess = false; // Assume failure unless you specifically succeed.
        di.pDocName = "My C#.NET RAW Document";
        di.pDataType = "RAW";

        // Open the printer.
        if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
        {
            // Start a document.
            if (StartDocPrinter(hPrinter, 1, di))
            {
                // Start a page.
                if (StartPagePrinter(hPrinter))
                {
                    // Write your bytes.
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                    EndPagePrinter(hPrinter);
                }

                EndDocPrinter(hPrinter);
            }

            ClosePrinter(hPrinter);
        }

        // If you did not succeed, GetLastError may give more information
        // about why not.
        if (bSuccess == false)
        {
            dwError = Marshal.GetLastWin32Error();
        }

        return bSuccess;
    }

    public static bool SendFileToPrinter(string szPrinterName, string szFileName)
    {
        // Open the file.
        FileStream fs = new FileStream(szFileName, FileMode.Open);
        // Create a BinaryReader on the file.
        BinaryReader br = new BinaryReader(fs);
        // Dim an array of bytes big enough to hold the file's contents.
        Byte[] bytes = new Byte[fs.Length];
        bool bSuccess = false;
        // Your unmanaged pointer.
        IntPtr pUnmanagedBytes = new IntPtr(0);
        int nLength;

        nLength = Convert.ToInt32(fs.Length);
        // Read the contents of the file into the array.
        bytes = br.ReadBytes(nLength);
        // Allocate some unmanaged memory for those bytes.
        pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
        // Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
        // Send the unmanaged bytes to the printer.
        bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
        // Free the unmanaged memory that you allocated earlier.
        Marshal.FreeCoTaskMem(pUnmanagedBytes);
        return bSuccess;
    }

    public static bool SendStringToPrinter(string szPrinterName, string szString)
    {
        IntPtr pBytes;
        Int32 dwCount;

        // How many characters are in the string?
        // Fix from Nicholas Piasecki:
        // dwCount = szString.Length;
        dwCount = (szString.Length + 1) * Marshal.SystemMaxDBCSCharSize;

        // Assume that the printer is expecting ANSI text, and then convert
        // the string to ANSI text.
        pBytes = Marshal.StringToCoTaskMemAnsi(szString);
        // Send the converted ANSI string to the printer.
        SendBytesToPrinter(szPrinterName, pBytes, dwCount);
        Marshal.FreeCoTaskMem(pBytes);
        return true;
    }
}

    public static void Main(string[] args)
        {
            const string ESC = "\u001B";
            const string p = "\u0070";
            const string m = "\u0000";
            const string t1 = "\u0025";
            const string t2 = "\u0250";
            const string openTillCommand = ESC + p + m + t1 + t2;

            RawPrinterHelper.SendStringToPrinter("Bill", openTillCommand);
        }

https://github.com/lavahasif/openprintercsha