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
3条答案
按热度按时间eufgjt7s1#
它曾经是非常简单的LPT端口,与不到5行。对于USB收据打印机,我发现这个VB解决方案在其他地方,为我工作得很好。
要打开现金抽屉,只需执行OpenCashdrawer
rryofs0p2#
只要按照文档并发送打开抽屉所需的代码序列。如果您没有打印机的文档,您可以查找序列here。我相信在您的打印机上,只需发送ASCII铃声字符(十进制7)就可以打开抽屉。
vhmi4jdf3#
注意,我用ImportsSystem.Runtime.InteropServices & Modulemdlprint创建了一个新的模块,然后把公共SubOpenCashdrawer()放到我自己的公共类Main中