1

我正在设计一个简单的 pos 应用程序,但我遇到了一个问题。我有一个现金抽屉连接到热敏打印机(来自star micronics 的tsp100),然后用USB 连接到我的电脑。谁能帮我从 vb.net 打开钱箱(不打印任何东西)。?

4

2 回答 2

7

LPT端口过去很简单,不到5行。对于 USB 收据打印机,我在其他地方找到了这个 VB 解决方案,它对我来说很好用。

要打开钱箱,只需执行 OpenCashdrawer

Imports System.Runtime.InteropServices

Module mdlprint
   Public Class RawPrinter
      ' ----- Define the data type that supplies basic print job information to the spooler.
      <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
      Public Structure DOCINFO
         <MarshalAs(UnmanagedType.LPWStr)> _
         Public pDocName As String
         <MarshalAs(UnmanagedType.LPWStr)> _
         Public pOutputFile As String
         <MarshalAs(UnmanagedType.LPWStr)> _
         Public pDataType As String
      End Structure

      ' ----- Define interfaces to the functions supplied in the DLL.
      <DllImport("winspool.drv", EntryPoint:="OpenPrinterW", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Public Shared Function OpenPrinter(ByVal printerName As String, ByRef hPrinter As IntPtr, ByVal printerDefaults As Integer) As Boolean
      End Function

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

      <DllImport("winspool.drv", EntryPoint:="StartDocPrinterW", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Public Shared Function StartDocPrinter(ByVal hPrinter As IntPtr, ByVal level As Integer, ByRef documentInfo As DOCINFO) As Boolean
      End Function

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

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

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

      <DllImport("winspool.drv", EntryPoint:="WritePrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Public Shared Function WritePrinter(ByVal hPrinter As IntPtr, ByVal buffer As IntPtr, ByVal bufferLength As Integer, ByRef bytesWritten As Integer) As Boolean
      End Function

      Public Shared Function PrintRaw(ByVal printerName As String, ByVal origString As String) As Boolean
         ' ----- Send a string of  raw data to  the printer.
         Dim hPrinter As IntPtr
         Dim spoolData As New DOCINFO
         Dim dataToSend As IntPtr
         Dim dataSize As Integer
         Dim bytesWritten As Integer

         ' ----- The internal format of a .NET String is just
         '       different enough from what the printer expects
         '       that there will be a problem if we send it
         '       directly. Convert it to ANSI format before
         '       sending.
         dataSize = origString.Length()
         dataToSend = Marshal.StringToCoTaskMemAnsi(origString)

         ' ----- Prepare information for the spooler.
         spoolData.pDocName = "OpenDrawer" ' class='highlight'
         spoolData.pDataType = "RAW"

         Try
            ' ----- Open a channel to  the printer or spooler.
            Call OpenPrinter(printerName, hPrinter, 0)

            ' ----- Start a new document and Section 1.1.
            Call StartDocPrinter(hPrinter, 1, spoolData)
            Call StartPagePrinter(hPrinter)

            ' ----- Send the data to the printer.
            Call WritePrinter(hPrinter, dataToSend, _
               dataSize, bytesWritten)

            ' ----- Close everything that we opened.
            EndPagePrinter(hPrinter)
            EndDocPrinter(hPrinter)
            ClosePrinter(hPrinter)
            PrintRaw = True
         Catch ex As Exception
            MsgBox("Error occurred: " & ex.ToString)
            PrintRaw = False
         Finally
            ' ----- Get rid of the special ANSI version.
            Marshal.FreeCoTaskMem(dataToSend)
         End Try
      End Function
   End Class
End Module

Public Class Main
   Public Sub OpenCashdrawer()
      'Modify DrawerCode to your receipt printer open drawer code
      Dim DrawerCode As String = Chr(27) & Chr(112) & Chr(48) & Chr(64) & Chr(64)
      'Modify PrinterName to your receipt printer name
      Dim PrinterName As String = "Your receipt printer name"

      RawPrinter.PrintRaw(PrinterName, DrawerCode)
   End Sub
End Class
于 2012-10-16T02:56:54.153 回答
2

只需按照文档发送打开抽屉所需的代码序列即可。如果您没有打印机的文档,您可以在此处查找序列。我相信在您的打印机上,只需发送一个 ASCII 响铃字符(十进制 7)即可打开抽屉。

于 2012-08-20T21:34:15.387 回答