开钱箱和打印小票,在前面的文章中已经发过了。和今天发的内容也一样。主要是打印条码也需要那些api,所以一块发上来了。
Imports System.Runtime.InteropServicesImports System.TextImports System.IOImports System.Runtime.ExceptionServices '为了HandleProcessCorruptedStateExceptionsPublic Class LPTInherits MarshalByRefObject<HandleProcessCorruptedStateExceptions><DllImport("kernel32.dll")>Private Shared Function CreateFile(ByVal lpFileName As String,ByVal dwDesiredAccess As Integer,ByVal dwShareMode As Integer,ByVal lpSecurityAttributes As Integer,ByVal dwCreationDisposition As Integer,ByVal dwFlagsAndAttributes As Integer,ByVal hTemplateFile As Integer) As IntegerEnd Function<DllImport("kernel32.dll")>Private Shared Function CloseHandle(ByVal hObject As Integer) As IntegerEnd Function<DllImport("kernel32.dll")>Private Shared Function GetCommTimeouts(ByVal hfile As Integer, ByRef lpCommTimeouts As COMMTIMEOUTS) As BooleanEnd Function<DllImport("kernel32.dll")>Private Shared Function SetCommTimeouts(ByVal hfile As Integer, ByRef lpCommTimeouts As COMMTIMEOUTS) As BooleanEnd FunctionPrivate Structure COMMTIMEOUTSPublic ReadIntervalTimeout As LongPublic ReadTotalTimeoutMultiplier As LongPublic ReadTotalTimeoutConstant As LongPublic WriteTotalTimeoutMultiplier As LongPublic WriteTotalTimeoutConstant As LongEnd StructurePublic Const FILE_ATTRIBUTE_NORMAL As Short = &H80Public Const INVALID_HANDLE_VALUE As Short = -1Public Const GENERIC_READ As Long = &H80000000Public Const GENERIC_WRITE As UInteger = &H40000000Public Const CREATE_NEW As UInteger = 1Public Const CREATE_ALWAYS As UInteger = 2Public Const OPEN_EXISTING As UInteger = 3Private _opened As Boolean = FalsePrivate _handle As IntPtr = -1Public Property Port() As String = "LPT1"Shared nTime As COMMTIMEOUTSPublic Property LastError() As String = ""Sub New()LastError = ""End Sub'设置LPT端口超时Private Sub setTimeout2()Try_handle = CreateFile(Port, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)nTime.ReadIntervalTimeout = 2000 '10000 为50秒,1000为5秒,但最少15秒nTime.ReadIntervalTimeout = 2000nTime.ReadTotalTimeoutConstant = 2000nTime.ReadTotalTimeoutMultiplier = 2000nTime.WriteTotalTimeoutConstant = 2000nTime.WriteTotalTimeoutMultiplier = 2000SetCommTimeouts(_handle, nTime)Close()Catch ex As ExceptionLastError = "无法设置LPT超时"End TryEnd Sub'打印条码<Obsolete>Public Function printBarcode()setTimeout2()TryIf _opened = False Then_handle = CreateFile(Port, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)If _handle = -1 ThenLastError = "打不开端口,重启电脑再试一次"Return FalseEnd IfEnd IfCatch ex As ExceptionEnd Try_opened = TrueDim fs As FileStream = New FileStream(_handle, FileAccess.Write)Dim sw As StreamWriter = New StreamWriter(fs, System.Text.Encoding.Default)sw.Write(Chr(29) & Chr(104) & Chr(60) & Chr(13) & Chr(10)) '高度sw.Write(Chr(29) & Chr(119) & Chr(2) & Chr(13) & Chr(10)) '线条宽度sw.Write(Chr(29) & Chr(72) & Chr(1) & Chr(13) & Chr(10)) '条码内容的位置。sw.Write(Chr(29) & Chr(107) & Chr(4) & Chr(49) & Chr(50) & Chr(51) & Chr(65) & Chr(49) & Chr(50) & Chr(51) & Chr(65) & Chr(50) & Chr(51) & Chr(65) & Chr(0) & Chr(13) & Chr(10)) '打印条码。从第一个chr(49) 开始到 chr(0)之前为具体内容sw.Close()fs.Close()Close()Return TrueEnd Function'开钱箱<Obsolete>Public Function OpenBox() As BooleansetTimeout2()TryIf _opened = False Then_handle = CreateFile(Port, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)If _handle = -1 ThenLastError = "打不开端口,重启电脑再试一次"Return FalseEnd IfEnd IfCatch ex As ExceptionEnd Try_opened = TrueTryDim fs As FileStream = New FileStream(_handle, FileAccess.Write)Dim sw As StreamWriter = New StreamWriter(fs, System.Text.Encoding.Default)sw.Write(Chr(27) & Chr(112) & Chr(0) & Chr(13) & Chr(10))sw.Close()fs.Close()Close()Return TrueCatch ex As ExceptionLastError = ex.MessageClose()Return FalseEnd TryEnd Function'打印小票<Obsolete>Public Function Write(ByVal sData As String) As BooleansetTimeout2()TryIf _opened = False Then_handle = CreateFile(Port, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)If _handle = -1 ThenLastError = ("打不开端口,重启电脑再试一次吧")Return FalseEnd IfEnd IfCatch ex As ExceptionEnd Try_opened = TrueTryDim fs As FileStream = New FileStream(_handle, FileAccess.Write)Dim sw As StreamWriter = New StreamWriter(fs, System.Text.Encoding.Default)sw.Write(sData)sw.Close()fs.Close()Close()Return TrueCatch ex As ExceptionLastError = ex.MessageClose()Return FalseEnd TryEnd FunctionPublic Sub Close()If _handle <> -1 ThenCloseHandle(_handle)_handle = -1End If_opened = FalseEnd SubEnd Class
调用方法
Dim lpt As New LPTlpt.printBarcode()