Author Topic: 呼叫TVicLPT Library 查詢印表機埠狀態的程序函數庫  (Read 6514 times)

admin

  • Administrator
  • *****
  • Posts: 0
    • View Profile
Code: [Select]
Attribute VB_Name = "modLptStatus"
Option Explicit

Public CurrLptStatus As LptStatus
Dim CurrentTvicLpt As TVicLPT
Dim ActiveHW As Boolean
Dim CurrentLpt As Integer
Dim CurrentInstance As Integer

Type LptStatus
    isPaperEnd As Boolean
    isBusy As Boolean
    isOpen As Boolean
    isPrinterReady As Boolean
End Type

Private Const cnt_1_nStrobe = 1
Private Const cnt_2_Data0 = 2
Private Const cnt_3_Data1 = 3
Private Const cnt_4_Data2 = 4
Private Const cnt_5_Data3 = 5
Private Const cnt_6_Data4 = 6
Private Const cnt_7_Data5 = 7
Private Const cnt_8_Data6 = 8
Private Const cnt_9_Data7 = 9
Private Const cnt_10_nAck = 10
Private Const cnt_11_nBusy = 11
Private Const cnt_12_PaperEnd = 12
Private Const cnt_13_Select = 13
Private Const cnt_14_nAutoLF = 14
Private Const cnt_15_Error = 15
Private Const cnt_16_Init = 16
Private Const cnt_17_nSelectIn = 17
Private Const cnt_18_Ground = 18
Private Const cnt_19_Ground = 19
Private Const cnt_20_Ground = 20
Private Const cnt_21_Ground = 21
Private Const cnt_22_Ground = 22
Private Const cnt_23_Ground = 23
Private Const cnt_24_Ground = 24
Private Const cnt_25_Ground = 25

Public Sub CheckLpt(ByRef inputTvicLpt As TVicLPT, InputInstance As Integer, InputLpt As String)

    Set CurrentTvicLpt = inputTvicLpt
    CurrentInstance = InputInstance
    Select Case LCase(InputLpt)
        Case "lpt2"
            CurrentLpt = 2
        Case "lpt3"
            CurrentLpt = 3
        Case Else
            CurrentLpt = 1
    End Select
    CurrLptStatus.isBusy = True
    CurrLptStatus.isPaperEnd = False
    CurrLptStatus.isOpen = False
    CurrLptStatus.isPrinterReady = False
    ActiveHW = OpenTvicLpt
    If ActiveHW = True Then
        SetLptPort
        SetLptPinValue cnt_16_Init, 1
        If Not CurrentTvicLpt.isPrinterReady = True Then
            CurrLptStatus.isPrinterReady = Not CurrentTvicLpt.isPrinterReady
            UpdateCurrStatus
        End If
    Else
        MsgBox ("LPT Connection Error"), vbExclamation, "Info"
    End If
    CloseTvicLpt
   
End Sub

Private Function ChkIsPrinterReady() As Boolean

    ChkIsPrinterReady = Not CurrentTvicLpt.isPrinterReady
   
End Function

Private Sub SetLptPort()

    CurrentTvicLpt.CurrentLpt = CurrentLpt
   
End Sub

Function OpenTvicLpt()

    If CurrentInstance = 1 Then
        CurrentTvicLpt.Active = 1     ' open first driver instance
    Else
        CurrentTvicLpt.Active = 2     ' open second driver instance
    End If
    If CurrentTvicLpt.Active = 0 Then
        OpenTvicLpt = False
    Else
        OpenTvicLpt = True
    End If
       
End Function

Sub CloseTvicLpt()

    CurrentTvicLpt.Active = 0
    ActiveHW = False
   
End Sub

Private Sub UpdateCurrStatus()

Dim CurrReadMode  As Integer
Dim tmpStatus As String

    CurrReadMode = CurrentTvicLpt.ReadMode
    CurrentTvicLpt.ReadMode = 1
    If CurrentTvicLpt.Pin(cnt_11_nBusy) = 0 Then
        CurrLptStatus.isBusy = False
    Else
        tmpStatus = CStr(CurrentTvicLpt.Pin(cnt_11_nBusy)) & CStr(CurrentTvicLpt.Pin(cnt_12_PaperEnd)) & CStr(CurrentTvicLpt.Pin(cnt_15_Error))
        Select Case tmpStatus
            Case "101"
                CurrLptStatus.isOpen = True
                CurrLptStatus.isBusy = False
                'SetLptPinValue cnt_16_Init, 0
            Case "001"
                CurrLptStatus.isBusy = False
            Case "110"
                CurrLptStatus.isPaperEnd = True
                CurrLptStatus.isBusy = False
            Case "111"
                CurrLptStatus.isPrinterReady = False
                CurrLptStatus.isBusy = False
            Case Else
        End Select
    End If
    CurrentTvicLpt.ReadMode = CurrReadMode
   
   
End Sub

Private Sub SetLptPinValue(inputPin As Integer, inputValue As Integer)

    CurrentTvicLpt.ReadMode = 0
    CurrentTvicLpt.Pin(inputPin) = inputValue
   
End Sub