开钱箱和打印小票,在前面的文章中已经发过了。和今天发的内容也一样。主要是打印条码也需要那些api,所以一块发上来了。
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.IO
Imports System.Runtime.ExceptionServices '为了HandleProcessCorruptedStateExceptions
Public Class LPT
Inherits 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 Integer
End Function
<DllImport("kernel32.dll")>
Private Shared Function CloseHandle(ByVal hObject As Integer) As Integer
End Function
<DllImport("kernel32.dll")>
Private Shared Function GetCommTimeouts(ByVal hfile As Integer, ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean
End Function
<DllImport("kernel32.dll")>
Private Shared Function SetCommTimeouts(ByVal hfile As Integer, ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean
End Function
Private Structure COMMTIMEOUTS
Public ReadIntervalTimeout As Long
Public ReadTotalTimeoutMultiplier As Long
Public ReadTotalTimeoutConstant As Long
Public WriteTotalTimeoutMultiplier As Long
Public WriteTotalTimeoutConstant As Long
End Structure
Public Const FILE_ATTRIBUTE_NORMAL As Short = &H80
Public Const INVALID_HANDLE_VALUE As Short = -1
Public Const GENERIC_READ As Long = &H80000000
Public Const GENERIC_WRITE As UInteger = &H40000000
Public Const CREATE_NEW As UInteger = 1
Public Const CREATE_ALWAYS As UInteger = 2
Public Const OPEN_EXISTING As UInteger = 3
Private _opened As Boolean = False
Private _handle As IntPtr = -1
Public Property Port() As String = "LPT1"
Shared nTime As COMMTIMEOUTS
Public 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 = 2000
nTime.ReadTotalTimeoutConstant = 2000
nTime.ReadTotalTimeoutMultiplier = 2000
nTime.WriteTotalTimeoutConstant = 2000
nTime.WriteTotalTimeoutMultiplier = 2000
SetCommTimeouts(_handle, nTime)
Close()
Catch ex As Exception
LastError = "无法设置LPT超时"
End Try
End Sub
'打印条码
<Obsolete>
Public Function printBarcode()
setTimeout2()
Try
If _opened = False Then
_handle = CreateFile(Port, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
If _handle = -1 Then
LastError = "打不开端口,重启电脑再试一次"
Return False
End If
End If
Catch ex As Exception
End Try
_opened = True
Dim 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 True
End Function
'开钱箱
<Obsolete>
Public Function OpenBox() As Boolean
setTimeout2()
Try
If _opened = False Then
_handle = CreateFile(Port, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
If _handle = -1 Then
LastError = "打不开端口,重启电脑再试一次"
Return False
End If
End If
Catch ex As Exception
End Try
_opened = True
Try
Dim 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 True
Catch ex As Exception
LastError = ex.Message
Close()
Return False
End Try
End Function
'打印小票
<Obsolete>
Public Function Write(ByVal sData As String) As Boolean
setTimeout2()
Try
If _opened = False Then
_handle = CreateFile(Port, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
If _handle = -1 Then
LastError = ("打不开端口,重启电脑再试一次吧")
Return False
End If
End If
Catch ex As Exception
End Try
_opened = True
Try
Dim 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 True
Catch ex As Exception
LastError = ex.Message
Close()
Return False
End Try
End Function
Public Sub Close()
If _handle <> -1 Then
CloseHandle(_handle)
_handle = -1
End If
_opened = False
End Sub
End Class
调用方法
Dim lpt As New LPT
lpt.printBarcode()