栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

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

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

Imports System.Runtime.InteropServices
Imports System.Text

Public Class LPT
	Inherits MarshalByRefObject

	
	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


	
	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

	
	Private Shared Function CloseHandle(ByVal hObject As Integer) As Integer
	End Function

	
	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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/911157.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号