vb.net 开钱箱和打印小票的类

Imports System.Runtime.InteropServices
Imports System.Text

Public Class LPT
	Inherits MarshalByRefObject

	<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 WriteFile(
									ByVal hFile As Integer,
									ByVal Buffer As Byte(),
									ByVal nNumberOfBytesToWrite As Integer,
									ByRef lpNumberOfBytesWritten As Integer,
									ByRef lpOverlapped As OVERLAPPED
									) As Integer
	End Function

	<DllImport("kernel32.dll")>
	Private Shared Function CloseHandle(ByVal hObject As Integer) As Integer
	End Function

	<StructLayout(LayoutKind.Sequential)>
	Private Structure OVERLAPPED
		Public ternal As Integer
		Public ternalHigh As Integer
		Public offset As Integer
		Public OffsetHigh As Integer
		Public hEvent As Integer
	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"

	Public Sub OpenBox()
		If _opened = False Then
			_handle = CreateFile(Port, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
			If _handle = -1 Then
				MsgBox("打开并口失败")
				Exit Sub
			End If
		End If
		_opened = True
		Write(Chr(27) & Chr(112) & Chr(0) & Chr(13) & Chr(10))  '开钱箱
		Close()
	End Sub

	Public Sub Print()
		If _opened = False Then
			_handle = CreateFile(Port, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
			If _handle = -1 Then
				MsgBox("打开并口失败")
				Exit Sub
			End If
		End If
		_opened = True
		Write(Chr(27) & Chr(97) & Chr(49))   '居中对齐
		Write("hello WormJan" & Chr(13) & Chr(10))
		Write("hello WormJan" & Chr(13) & Chr(10))
		Write("hello WormJan" & Chr(13) & Chr(10))
		Write(Chr(27) & Chr(97) & Chr(48))   '居左对齐
		Write("中华人民共和国" & Chr(13) & Chr(10))
		Write(New String("*", 32) & Chr(13) & Chr(10))
		Close()
	End Sub


	Public Sub Write(ByVal data As Byte)
		Dim written As Integer
		Dim buffer As Byte()
		buffer = Array.CreateInstance(GetType(Byte), 1)
		buffer(0) = data
		If _opened Then
			WriteFile(_handle, buffer, 1, written, New OVERLAPPED)
		End If
	End Sub

	Public Sub Write(ByVal sData As String)
		If _opened = False Then
			_handle = CreateFile(Port, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
			If _handle = -1 Then
				MsgBox("打开并口失败")
				Exit Sub
			End If
			_opened = True
		End If
		Dim bytes() As Byte = Encoding.Default.GetBytes(sData)
		For Each b As Byte In bytes
			Write(b)
		Next
	End Sub

	Public Sub Close()
		If _handle <> -1 Then
			CloseHandle(_handle)
			_handle = -1
		End If
		_opened = False
	End Sub
End Class

调用方法:

本实例是单字节传送,无法很好地处理打印机缺纸造成的卡死问题。墙裂建议参考一下另外一篇文章,一次性传递所有字节,更容易处理卡死问题。链接地址:

vb.net LPT端口 开钱箱和小票纸打印超时问题解决办法_WormJan的博客-CSDN博客

准备三个按钮,一个是开钱箱用的,一个是直接调用打印方法用的,打印内容在上面的类里写好了,也可以传递一个一维数组,把要打印的东西传进去。另一个是逐条打印用的想打什么就调用方法。

Public Class Form1
	Dim lpt As New LPT
'开钱箱
	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
		lpt.OpenBox()
	End Sub
'直接调用打印。把要打印的东西可以以数组形式传递
	Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
		lpt.Port = "LPT1"        '本行不要也可
		lpt.Print()
	End Sub
'一条一条地打印,打印完别忘了关闭端口。也就是最后一行代码
	Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
		lpt.Write(Chr(27) & Chr(97) & Chr(49))   '居中对齐
		lpt.Write("hello world" & Chr(13) & Chr(10))
		lpt.Write("hello world" & Chr(13) & Chr(10))
		lpt.Write("hello world" & Chr(13) & Chr(10))
		lpt.Write(Chr(27) & Chr(97) & Chr(48))   '居左对齐
		lpt.Write("中华人民共和国" & Chr(13) & Chr(10))
		lpt.Write(New String("*", 32) & Chr(13) & Chr(10))
		lpt.Close()
	End Sub

End Class

'注意因为小票机和钱箱产品不同,发送的指令可能有所区别。具体什么指令需要参考产品方的说明或者直接咨询产品方

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值