我正在设计一个简单的pos应用程序,但我遇到了问题。我有一个连接到热敏打印机的现金抽屉(来自星微米的tsp100),然后用usb连接到我的电脑。任何人都可以帮助我如何从vb.net打开现金抽屉(不打印任何东西)。 ?
答案 0 :(得分:6)
以前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
答案 1 :(得分:2)
只需按照文档说明并发送打开抽屉所需的一系列代码即可。如果您没有打印机的文档,可以查找序列here。我相信在您的打印机上,只需发送ASCII铃声字符(十进制7)就会打开抽屉。