惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
S
SegmentFault 最新的问题
Project Zero
Project Zero
D
DataBreaches.Net
I
InfoQ
L
Lohrmann on Cybersecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
The Register - Security
The Register - Security
Recorded Future
Recorded Future
Vercel News
Vercel News
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
I
Intezer
The Hacker News
The Hacker News
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
Help Net Security
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Scott Helme
Scott Helme
T
Threatpost
爱范儿
爱范儿
N
Netflix TechBlog - Medium
D
Docker
云风的 BLOG
云风的 BLOG
C
Cisco Blogs
K
Kaspersky official blog
H
Help Net Security
S
Secure Thoughts
T
Threat Research - Cisco Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
N
News and Events Feed by Topic
G
Google Developers Blog
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
B
Blog
Google DeepMind News
Google DeepMind News
Recent Announcements
Recent Announcements
Simon Willison's Weblog
Simon Willison's Weblog
S
Securelist
P
Privacy International News Feed
Spread Privacy
Spread Privacy
The Last Watchdog
The Last Watchdog

博客园 - 水如烟(LzmTW)

.NET的变量在代码集中是不安全的 HOW TO:获取硬盘物理序列号(VB.NET) 一个创建快捷方式类 一个简单的CodeAccessPermission生成器 认识一下Attribute 标记法定义和创建数据库(四) 标记法定义和创建数据库 Sql2005数据类型与Framework类型的对应关系 HOW TO:重启程序(WinForm) HOW TO:设置默认打印机 HOW TO:避免“将COM对象与其基础RCW分开后不能再使用该对象”错误 HOW TO:控制是否允许用户退出ExcelApplication的Workbook 树和自联表(五) 树和自联表(六) 树和自联表(四) 树和自联表(一) 行政区划数据数据库的设计(脚本) 行政区划数据数据库的设计(七) 行政区划数据数据库的设计(六)
HOW TO:端口打印(比较粗糙)
水如烟(LzmTW) · 2007-02-05 · via 博客园 - 水如烟(LzmTW)

Author:水如烟

以前用过的.不过现在多是用EXCEL打了.那时用在OKI和LQ,两种打印机的控制码是不一样的.

要看提供的控制代码说明.那时我是一个个试.

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 StringAs 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
#Region "API所需要的必要信息"
    
Public Structure DCB
        
Public DCBlength As Int32
        
Public BaudRate As Int32
        
Public fBitFields As Int32
        
Public wReserved As Int16
        
Public XonLim As Int16
        
Public XoffLim As Int16
        
Public ByteSize As Byte
        
Public Parity As Byte
        
Public StopBits As Byte
        
Public XonChar As Byte
        
Public XoffChar As Byte
        
Public ErrorChar As Byte
        
Public EofChar As Byte
        
Public EvtChar As Byte
        
Public wReserved1 As Int16 'Reserved; Do Not Use
    End StructurePublic Structure COMMTIMEOUTS
        
Public ReadIntervalTimeout As Int32
        
Public ReadTotalTimeoutMultiplier As Int32
        
Public ReadTotalTimeoutConstant As Int32
        
Public WriteTotalTimeoutMultiplier As Int32
        
Public WriteTotalTimeoutConstant As Int32
    
End StructurePublic Const GENERIC_READ As Int32 = &H80000000
    
Public Const GENERIC_WRITE As Int32 = &H40000000
    
Public Const OPEN_EXISTING As Int32 = 3
    
Public Const FILE_ATTRIBUTE_NORMAL As Int32 = &H80
    
Public Const NOPARITY As Int32 = 0
    
Public Const ONESTOPBIT As Int32 = 0Public Declare Auto Function CreateFile Lib "kernel32.dll" _
       (
ByVal lpFileName As StringByVal dwDesiredAccess As Int32, _
          
ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
             
ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, _
                
ByVal hTemplateFile As IntPtr) As IntPtrPublic Declare Auto Function GetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
       
ByRef lpDCB As DCB) As BooleanPublic Declare Auto Function SetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
       
ByRef lpDCB As DCB) As BooleanPublic Declare Auto Function GetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
       
ByRef lpCommTimeouts As COMMTIMEOUTS) As BooleanPublic Declare Auto Function SetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
       
ByRef lpCommTimeouts As COMMTIMEOUTS) As BooleanPublic Declare Auto Function WriteFile Lib "kernel32.dll" (ByVal hFile As IntPtr, _
       
ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToWrite As Int32, _
          
ByRef lpNumberOfBytesWritten As Int32, ByVal lpOverlapped As IntPtr) As BooleanPublic Declare Auto Function ReadFile Lib "kernel32.dll" (ByVal hFile As IntPtr, _
       
ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToRead As Int32, _
          
ByRef lpNumberOfBytesRead As Int32, ByVal lpOverlapped As IntPtr) As BooleanPublic Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean
#End Region
End ClassPublic Class CODE
    
Public Const C4 As String = Chr(&H34)
    
Public Const 联机 As String = Chr(&H11)
    
Public Const 解除联机 As String = Chr(&H13)
    
Public Shared Function 页长(ByVal i As IntegerAs String
        
Return GetString("1B 43 " & i.ToString)  '3英寸长
    End Function
    
Public Shared Function 解除重打模式() As String
        
Return GetString("1B 6E")
    
End Function
    
Public Shared Function 双向打印() As String
        
Return GetString("1B 23 42")
    
End Function
    
Public Shared Function 行距(ByVal i As IntegerAs String
        
Return GetString("1B 25 39 00 " & i.ToString) '18
    End Function
    
Public Shared Function 高速打印() As String
        
Return GetString("1B 44")
    
End Function
    
Public Shared Function 垂直跳格(ByVal i As IntegerAs String
        
Return GetString("1B 0B 00 " & i.ToString)   '修改最后两位
    End Function
    
Public Shared Function 间距换行(ByVal i As IntegerAs String
        
Return GetString("1B 23 35 " & i.ToString)  '修改最后一数
    End Function
    
Public Shared Function 逆向换行(ByVal i As IntegerAs String
        
Return GetString("1B 23 38 " & i.ToString) '修改最后一数
    End Function
    
Public Shared Function 初始化() As String
        
Return GetString("1B 42")
    
End Function
    
Public Shared Function 下页() As String
        
Return GetString("0C")
    
End Function
    
Public Shared Function 换行() As String
        
Return GetString("0A")
    
End Function
    
Public Shared Function L行距(ByVal i As IntegerAs String
        
Return GetString("1B 2B " & i.ToString)
    
End Function
    
Public Shared Function GetString(ByVal Numbers As StringAs 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
End Class

使用示意:

Imports LzmTW.PortPrint
Imports LzmTW.Common.SubAndFunction
Public Class OKIPrinter
    
Private Shared mPrinter As Printer
    
Private Shared mDataInfo As DataInfo
    
Private mPrinterInfo As PrinterInfo '读取打印机端口定义
    Private mInitialzePrinterText As String = CODE.初始化 & CODE.页长(3& CODE.双向打印 & CODE.高速打印 & CODE.行距(18)
    
Sub New()
        
If mPrinter Is Nothing Then
            mPrinter 
= New Printer
            mDataInfo 
= New DataInfo
            mPrinterInfo 
= New PrinterInfo
            mPrinter.InputInfo 
= mDataInfo
        
End If
    
End Sub
    
Public Property LocatePrinterInfo() As PrinterInfo
        
Get
            
Return mPrinterInfo
        
End Get
        
Set(ByVal Value As PrinterInfo)
            mPrinterInfo 
= Value
        
End Set
    
End Property
    
Private Sub InitializePrinter()
        mDataInfo.LPTPORT 
= mPrinterInfo.LPTPORT
    
End Sub
    
Public Sub 打印(ByVal Bill As 票据, ByVal BillName As String)
        InitializePrinter()
        
If BillName.Equals("结算单"Then
            mPrinter.Print(Bill.ToPrint结算单(mPrinterInfo.PrinterType))
        
End If
        
If BillName.Equals("发票"Then
            mPrinter.Print(Bill.ToPrint发票(mPrinterInfo.PrinterType))
        
End If
    
End Sub
    
Public Sub 打印收费卡(ByVal Card As 收费卡)
        InitializePrinter()
        mPrinter.Print(Card.PrinterText)
    
End Sub
    
Public Class PrinterInfo
        
Public PrinterType As PrinterType
        
Public LPTPORT As String
    
End Class
End ClassPublic Enum PrinterType
    OKI
    LQ
End Enum

Public Class 票据
    
Public 用户编码 As String
    
Public NO As String
    
Public 用户名称 As String
    
Public 所属月份 As String
    
'...
    
    
Public Function ToPrint发票(ByVal PrinterType As PrinterType) As String
        
If PrinterType = PrinterType.OKI Then
            
Return ToPrintOKI发票()
        
Else
            
Return ToPrintLQ发票()
        
End If
    
End Function
    
Private Function ToPrintOKI发票() As String
        
Dim Code As Code
        
Dim Leg As String = Space(10)
        
Dim tmp As String
        tmp 
&= Code.页长(5& Code.双向打印 & Code.高速打印 & Code.行距(21& Code.下页()
        tmp 
&= Code.换行() & Code.换行() & Code.间距换行(9)
        tmp 
&= Leg & String.Format("{0,12}" & CHNFormat(141True, 收费类型) & "{2,4}  {3,2}   {4,2}", _
       
"", 收费类型, 年, 月, 日)
        tmp 
&= Code.间距换行(2)
        tmp 
&= Code.换行() & Leg & String.Format("{0,12}" & CHNFormat(126True, 用户名称) & "{2,12}{3,-24}", _
        
"", 用户名称, "", 用户编码)
        
'....
        tmp &= Code.下页
        
Return tmp
    
End Function
    
Private Function ToPrintLQ发票() As String
        
Dim Code As Code
        
Dim Leg As String = Space(10)
        
Dim tmp As String
        tmp 
= Code.GetString("1B 40 1B 43 0 03"& Code.L行距(62)
        tmp 
&= Code.换行() & Code.换行()
        tmp 
&= Leg & String.Format("{0,12}" & CHNFormat(139True, 收费类型) & "{2,4}  {3,2}   {4,2}", _
       
"", 收费类型, 年, 月, 日)
        tmp 
&= Code.换行() & Leg & String.Format("{0,12}" & CHNFormat(124True, 用户名称) & "{2,12}{3,-24}", _
        
"", 用户名称, "", 用户编码)
                
'....
        Return tmp
    
End Function
    
'....
End Class