' ****打开钱箱控制命令说明**************************************
' ESC p m t1 t2 钱箱命令
' 格式: ASCII:ESC p m t1 t2
' 十进制:27 112 m t1 t2
' 十六进制:1B 70 m t1 t2
' 说明:
' 在钱箱连接器上输出由t1,t2决定的钱箱开启脉冲,要求t2≥t1。
' m=0,48 在钱箱pin2上输出。
' m=1,49 在钱箱pin5上输出。
' 注意:
' 开启时间为t1*2ms
' 关断时间为t2*2ms
' 如果t2<t1,打印机处理t2=t1×2ms;
' 如果t2<50,打印机内部将t2设为50。
' *************************************************************
方式1:
Option
Strict
Off
Option Explicit On
Imports System.IO
Imports System.Runtime.InteropServices
Friend Class Form1 Class Form1
Inherits System.Windows.Forms.Form
Public Const GENERIC_WRITE = &H40000000
Public Const OPEN_EXISTING = 3
Public Const FILE_SHARE_WRITE = &H2
Dim LPTPORT As String
Dim hPort As Integer
Public Declare Function CreateFile()Function CreateFile Lib "kernel32" Alias "CreateFileA" ( _
ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
<MarshalAs(UnmanagedType.Struct)> ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As Integer
Public Declare Function CloseHandle()Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As Integer
Dim retval As Integer
<StructLayout(LayoutKind.Sequential)> Public Structure SECURITY_ATTRIBUTESStructure SECURITY_ATTRIBUTES
Private nLength As Integer
Private lpSecurityDescriptor As Integer
Private bInheritHandle As Integer
End Structure
Private Sub Command1_Click()Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
Dim SA As SECURITY_ATTRIBUTES
Dim outFile As FileStream, hPortP As IntPtr
LPTPORT = "LPT1"
hPort = CreateFile(LPTPORT, GENERIC_WRITE, FILE_SHARE_WRITE, SA, OPEN_EXISTING, 0, 0)
hPortP = New IntPtr(hPort) 'convert Integer to IntPtr
outFile = New FileStream(hPortP, FileAccess.Write) 'Create FileStream using Handle
Dim fileWriter As New StreamWriter(outFile)
fileWriter.Write(Chr(7))
fileWriter.Flush()
fileWriter.Close()
outFile.Close()
retval = CloseHandle(hPort)
End Sub
End Class
Option Explicit On
Imports System.IO
Imports System.Runtime.InteropServices
Friend Class Form1 Class Form1
Inherits System.Windows.Forms.Form
Public Const GENERIC_WRITE = &H40000000
Public Const OPEN_EXISTING = 3
Public Const FILE_SHARE_WRITE = &H2
Dim LPTPORT As String
Dim hPort As Integer
Public Declare Function CreateFile()Function CreateFile Lib "kernel32" Alias "CreateFileA" ( _
ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
<MarshalAs(UnmanagedType.Struct)> ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As Integer
Public Declare Function CloseHandle()Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As Integer
Dim retval As Integer
<StructLayout(LayoutKind.Sequential)> Public Structure SECURITY_ATTRIBUTESStructure SECURITY_ATTRIBUTES
Private nLength As Integer
Private lpSecurityDescriptor As Integer
Private bInheritHandle As Integer
End Structure
Private Sub Command1_Click()Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
Dim SA As SECURITY_ATTRIBUTES
Dim outFile As FileStream, hPortP As IntPtr
LPTPORT = "LPT1"
hPort = CreateFile(LPTPORT, GENERIC_WRITE, FILE_SHARE_WRITE, SA, OPEN_EXISTING, 0, 0)
hPortP = New IntPtr(hPort) 'convert Integer to IntPtr
outFile = New FileStream(hPortP, FileAccess.Write) 'Create FileStream using Handle
Dim fileWriter As New StreamWriter(outFile)
fileWriter.Write(Chr(7))
fileWriter.Flush()
fileWriter.Close()
outFile.Close()
retval = CloseHandle(hPort)
End Sub
End Class
方式2:
Dim
intFileNo
As
Integer
=
FreeFile
()
FileOpen ( 1 , " c:escapes.txt " , OpenMode.Output)
PrintLine ( 1 , Chr ( 27 ) & " p " & Chr ( 0 ) & Chr ( 25 ) & Chr ( 250 ))
FileClose ( 1 )
Shell ( " print /d:COM1 c:escapes.txt " , vbNormalFocus)
FileOpen ( 1 , " c:escapes.txt " , OpenMode.Output)
PrintLine ( 1 , Chr ( 27 ) & " p " & Chr ( 0 ) & Chr ( 25 ) & Chr ( 250 ))
FileClose ( 1 )
Shell ( " print /d:COM1 c:escapes.txt " , vbNormalFocus)