Attribute VB_Name = "PJQueue"
Option Explicit

'----------------------------------------------------------
'  Function     : InitQueue
'  Return value : None
'  Argument     : None
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub InitQueue()

    Dim i As Integer
    Dim j As Integer
    Dim CommNo As Integer
    
    LanForm.CommandSendTimer.Enabled = False
    
    For i = 1 To MAX_PJ
        If (LanForm.TimeoutTimer(i).Enabled = True) Then
            LanForm.TimeoutTimer(i).Enabled = False
        End If
        Select Case PJInfo(i).Connection.Kind
            Case 0  'Serial
                If (LanForm.MSComm(i).PortOpen = True) Then
                    LanForm.MSComm(i).PortOpen = False
                End If
            Case 1  'Network
                If (LanForm.TCP(i).State <> sckClosed) Then
                    LanForm.TCP(i).Close
                End If
        End Select
        For j = 1 To MAX_QUEUE
            'Queue(i, j).Index = 0
            Queue(i, j).Status = QS_NODATA
            'Queue(i, j).Action = 0
            'Queue(i, j).Operation = 0
            ReDim Queue(i, j).SendData(0)
            Queue(i, j).SendData(0) = 0
            Queue(i, j).SendDataLen = 0
            ReDim Queue(i, j).RecvData(0)
            Queue(i, j).RecvData(0) = 0
            Queue(i, j).RecvDataLen = 0
        Next j
        QueueWP(i) = 1
        QueueRP(i) = 1
    Next i

    LanForm.CommandSendTimer.Enabled = True

End Sub

'----------------------------------------------------------
'  Function     : SetQueue
'  Return value : None
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub SetQueue(ByVal PJIndex As Integer, SendData() As Variant)

    Dim i As Integer
    Dim DataLen As Integer
    
    DataLen = UBound(SendData)
    
    Queue(PJIndex, QueueWP(PJIndex)).Status = QS_READY
    'Queue(PJIndex, QueueWP(PJIndex)).Action = Action
    'Queue(PJIndex, QueueWP(PJIndex)).Operation = Operation
    'ReDim Queue(PJIndex, QueueWP(PJIndex)).SendData(SendDataLen - 1)
    ReDim Queue(PJIndex, QueueWP(PJIndex)).SendData(DataLen)
    'For i = 0 To SendDataLen - 1
    For i = 0 To DataLen
        Queue(PJIndex, QueueWP(PJIndex)).SendData(i) = CByte(SendData(i))
    Next i
    'Queue(PJIndex, QueueWP(PJIndex)).SendDataLen = SendDataLen
    Queue(PJIndex, QueueWP(PJIndex)).SendDataLen = DataLen + 1
    Queue(PJIndex, QueueWP(PJIndex)).RecvDataLen = 0
    QueueWP(PJIndex) = QueueWP(PJIndex) + 1
    If (QueueWP(PJIndex) > MAX_QUEUE) Then QueueWP(PJIndex) = 1
    
End Sub

'----------------------------------------------------------
'  Function     : InitErrorQueue
'  Return value : None
'  Argument     : None
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub InitErrorQueue()

    Dim i As Integer, j As Integer
    
    For i = 1 To MAX_ERR_QUE
        ErrorQueue(i) = ""
        ErrQueWP = 1
        ErrQueRP = 1
    Next i
    
End Sub

'----------------------------------------------------------
'  Function     : ErrorQueueEmptyCheck
'  Return value : None
'  Argument     : None
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Function ErrorQueueEmptyCheck() As Boolean

    Dim Ret As Boolean
    
    If (ErrQueWP <> ErrQueRP) Then
        Ret = False
    Else
        Ret = True
    End If
    ErrorQueueEmptyCheck = Ret
    
End Function

'----------------------------------------------------------
'  Function     : SetErrorQueue
'  Return value :
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub SetErrorQueue(ByVal QueueData As String)

    ErrorQueue(ErrQueWP) = QueueData
    ErrQueWP = ErrQueWP + 1
    If (ErrQueWP > MAX_ERR_QUE) Then ErrQueWP = 1
    
End Sub

'----------------------------------------------------------
'  Function     : SelectPJIndexQueueInit
'  Return value :
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub SelectPJIndexQueueInit(ByVal PJIndex As Integer)

    Dim i As Integer
    
    LanForm.CommandSendTimer.Enabled = False
    
    If (LanForm.TimeoutTimer(PJIndex).Enabled = True) Then
        LanForm.TimeoutTimer(PJIndex).Enabled = False
    End If
    
    Select Case PJInfo(PJIndex).Connection.Kind
        Case 0  'Serial
            If (LanForm.MSComm(PJIndex).PortOpen = True) Then
                LanForm.MSComm(PJIndex).PortOpen = False
            End If
            
        Case 1  'Network
            If (LanForm.TCP(PJIndex).State <> sckClosed) Then
                LanForm.TCP(PJIndex).Close
            End If
    End Select
    
    For i = 1 To MAX_QUEUE
        'Queue(PJIndex, i).Index = 0
        Queue(PJIndex, i).Status = QS_NODATA
        'Queue(PJIndex, i).Action = 0
        'Queue(PJIndex, i).Operation = 0
        ReDim Queue(PJIndex, i).SendData(0)
        Queue(PJIndex, i).SendData(0) = 0
        Queue(PJIndex, i).SendDataLen = 0
        ReDim Queue(PJIndex, i).RecvData(0)
        Queue(PJIndex, i).RecvData(0) = 0
        Queue(PJIndex, i).RecvDataLen = 0
    Next i
    QueueRP(PJIndex) = 1
    QueueWP(PJIndex) = 1
    
    LanForm.CommandSendTimer.Enabled = True

End Sub

