首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

vb.net中怎么直接写并口控制小票打印机

2012-03-29 
vb.net中如何直接写并口控制小票打印机? 比如在VB6中OpenLPT1: ForOutputAs#1Print#1,Chr$(27)E Chr

vb.net中如何直接写并口控制小票打印机?

比如在VB6中

        Open   "LPT1: "   For   Output   As   #1
        Print   #1,   Chr$(27);   "E ";   Chr(&H1B);   Chr(&H26);   "l ";   "1 ";   "O "
        Close   #1

在vb2005里面就不能用open来打开LPT1了,要用什么办法呢?

[解决办法]
这是以前用过的,在OKI上:

Imports System.IO
Imports System.Runtime.InteropServices
Public Class DataInfo
Private mLPTPORT As String '打印机端口
Private mInitText As String '初始化参数
Private mTexts As String() '以数组形式输出
Private mText As String '打印文本
Public Property LPTPORT() As String
Get
Return mLPTPORT
End Get
Set(ByVal Value As String)
mLPTPORT = Value
End Set
End Property
Public Property ToPrintText() As String
Get
Return mText
End Get
Set(ByVal Value As String)
mText = Value
End Set
End Property
Public Property InitPrinterText() As String
Get
Return mInitText
End Get
Set(ByVal Value As String)
mInitText = Value
End Set
End Property
Public Property ToPrintTexts() As String()
Get
Return mTexts
End Get
Set(ByVal Value As String())
mTexts = Value
End Set
End Property
End Class
Public Class Printer
#Region "接口参数 "
Private PARAS As DataInfo
Public WriteOnly Property InputInfo() As DataInfo
Set(ByVal Value As DataInfo)
PARAS = Value
End Set
End Property
Private mStatus As Integer
Public ReadOnly Property STATUS() As Integer
Get
Return mStatus
End Get
End Property
#End Region

#Region "本地变量 "
Private LPTPORT As String '端口
Private hPortP As IntPtr '返回句柄
Private retval As Boolean '关闭打印时返回的句柄
Private outFile As FileStream '打印字符流
#End Region
Public Sub InitializePrinter()
If PARAS.InitPrinterText.Trim = " " Then Exit Sub
LPTPORT = PARAS.LPTPORT
hPortP = CreateFile(LPTPORT, GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
mStatus = hPortP.ToInt32
outFile = New FileStream(hPortP, FileAccess.Write, False)
Dim fileWriter As New StreamWriter(outFile, System.Text.Encoding.Default)
fileWriter.AutoFlush = False
'打印开始
fileWriter.Write(PARAS.InitPrinterText)
fileWriter.Flush()
fileWriter.Close()
outFile.Close()
retval = CloseHandle(hPortP)
End Sub
Public Sub PrintText()
If PARAS.ToPrintText.Trim = " " Then Exit Sub
LPTPORT = PARAS.LPTPORT
hPortP = CreateFile(LPTPORT, GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
mStatus = hPortP.ToInt32
outFile = New FileStream(hPortP, FileAccess.Write, False)
Dim fileWriter As New StreamWriter(outFile, System.Text.Encoding.Default)
fileWriter.AutoFlush = False
'打印单据开始
fileWriter.WriteLine(PARAS.ToPrintText)


fileWriter.Flush()
fileWriter.Close()
outFile.Close()
retval = CloseHandle(hPortP)
End Sub
Public Sub Print(ByVal Text As String)
If Text.Trim = " " Then Exit Sub
LPTPORT = PARAS.LPTPORT
hPortP = CreateFile(LPTPORT, GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
mStatus = hPortP.ToInt32
outFile = New FileStream(hPortP, FileAccess.Write, False)
Dim fileWriter As New StreamWriter(outFile, System.Text.Encoding.Default)
fileWriter.AutoFlush = False
'打印单据开始
fileWriter.WriteLine(Text)
fileWriter.Flush()
fileWriter.Close()
outFile.Close()
retval = CloseHandle(hPortP)
End Sub
Public Sub PrintTexts()
If PARAS.ToPrintTexts Is Nothing OrElse PARAS.ToPrintTexts.Length = 0 Then Exit Sub
LPTPORT = PARAS.LPTPORT
hPortP = CreateFile(LPTPORT, GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
mStatus = hPortP.ToInt32
outFile = New FileStream(hPortP, FileAccess.Write, False)
Dim fileWriter As New StreamWriter(outFile, System.Text.Encoding.Default)
fileWriter.AutoFlush = False
'打印单据开始
For Each s As String In PARAS.ToPrintTexts
fileWriter.WriteLine(s)
Next
fileWriter.Flush()
fileWriter.Close()
outFile.Close()
retval = CloseHandle(hPortP)
End Sub
Public Shared Function GetCodeString(ByVal Numbers As String) As String
If Numbers.Trim = " " Then Return " "
Dim tmp As String
For Each s As String In Numbers.Split( " "c)
tmp &= Chr(CType( "&H " & s, Integer))
Next
Return tmp
End Function

热点排行