HOW TO:端口打印(比较粗糙)

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   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
#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 Structure

    
Public   Structure  COMMTIMEOUTS
        
Public  ReadIntervalTimeout  As  Int32
        
Public  ReadTotalTimeoutMultiplier  As  Int32
        
Public  ReadTotalTimeoutConstant  As  Int32
        
Public  WriteTotalTimeoutMultiplier  As  Int32
        
Public  WriteTotalTimeoutConstant  As  Int32
    
End Structure

    
Public   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  =   0

    
Public   Declare   Auto   Function  CreateFile  Lib   " kernel32.dll "  _
       (
ByVal  lpFileName  As   String ByVal  dwDesiredAccess  As  Int32, _
          
ByVal  dwShareMode  As  Int32,  ByVal  lpSecurityAttributes  As  IntPtr, _
             
ByVal  dwCreationDisposition  As  Int32,  ByVal  dwFlagsAndAttributes  As  Int32, _
                
ByVal  hTemplateFile  As  IntPtr)  As  IntPtr

    
Public   Declare   Auto   Function  GetCommState  Lib   " kernel32.dll "  ( ByVal  nCid  As  IntPtr, _
       
ByRef  lpDCB  As  DCB)  As   Boolean

    
Public   Declare   Auto   Function  SetCommState  Lib   " kernel32.dll "  ( ByVal  nCid  As  IntPtr, _
       
ByRef  lpDCB  As  DCB)  As   Boolean

    
Public   Declare   Auto   Function  GetCommTimeouts  Lib   " kernel32.dll "  ( ByVal  hFile  As  IntPtr, _
       
ByRef  lpCommTimeouts  As  COMMTIMEOUTS)  As   Boolean

    
Public   Declare   Auto   Function  SetCommTimeouts  Lib   " kernel32.dll "  ( ByVal  hFile  As  IntPtr, _
       
ByRef  lpCommTimeouts  As  COMMTIMEOUTS)  As   Boolean

    
Public   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   Boolean

    
Public   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   Boolean

    
Public   Declare   Auto   Function  CloseHandle  Lib   " kernel32.dll "  ( ByVal  hObject  As  IntPtr)  As   Boolean
#End Region
End Class

Public   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   Integer As   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   Integer As   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   Integer As   String
        
Return  GetString( " 1B 0B 00  "   &  i.ToString)    ' 修改最后两位
     End Function
    
Public   Shared   Function  间距换行( ByVal  i  As   Integer As   String
        
Return  GetString( " 1B 23 35  "   &  i.ToString)   ' 修改最后一数
     End Function
    
Public   Shared   Function  逆向换行( ByVal  i  As   Integer As   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   Integer As   String
        
Return  GetString( " 1B 2B  "   &  i.ToString)
    
End Function
    
Public   Shared   Function  GetString( 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
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 Class

Public   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( 1 41 True , 收费类型)  &   " {2,4}  {3,2}   {4,2} " , _
       
"" , 收费类型, 年, 月, 日)
        tmp 
&=  Code.间距换行( 2 )
        tmp 
&=  Code.换行()  &  Leg  &   String .Format( " {0,12} "   &  CHNFormat( 1 26 True , 用户名称)  &   " {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( 1 39 True , 收费类型)  &   " {2,4}  {3,2}   {4,2} " , _
       
"" , 收费类型, 年, 月, 日)
        tmp 
&=  Code.换行()  &  Leg  &   String .Format( " {0,12} "   &  CHNFormat( 1 24 True , 用户名称)  &   " {2,12}{3,-24} " , _
        
"" , 用户名称,  "" , 用户编码)
                
' ....
         Return  tmp
    
End Function
    
' ....
End Class
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值