Namespace Frame
    ''' <summary>
    ''' Classe de base pour les trames d'erreurs
    ''' </summary>
    ''' <remarks></remarks>
    Public MustInherit Class ClsFrameModbusErr
        Inherits ClsFrameModbusBase


        Public Enum eErrors As Byte
            ''' <summary>
            ''' Code de fonction incorrect
            ''' </summary>
            ''' <remarks></remarks>
            IllegalFunctionCode = 1
            ''' <summary>
            ''' Adresse incorrect
            ''' </summary>
            ''' <remarks></remarks>
            IllegalAddress = 2
            ''' <summary>
            ''' Données non valides
            ''' </summary>
            ''' <remarks></remarks>
            IllegalDataValue = 3
            ''' <summary>
            ''' Longueur de réponse incorrect
            ''' </summary>
            ''' <remarks></remarks>
            IllegalResponseLength = 4
            ''' <summary>
            ''' Acquittement
            ''' </summary>
            ''' <remarks></remarks>
            ACK = 5
            ''' <summary>
            ''' Esclave occupé
            ''' </summary>
            ''' <remarks></remarks>
            SlaveBusy = 6
            ''' <summary>
            ''' 
            ''' </summary>
            ''' <remarks></remarks>
            NAK = 7
            ''' <summary>
            ''' 
            ''' </summary>
            ''' <remarks></remarks>
            MemoryParityError = 8
            ''' <summary>
            ''' 
            ''' </summary>
            ''' <remarks></remarks>
            GatewayPathUnavailable = 10
            ''' <summary>
            ''' 
            ''' </summary>
            ''' <remarks></remarks>
            GatewayTargetDeviceFailedToRespond = 11
        End Enum

        ''' <summary>
        ''' Numéro d'erreurs
        ''' </summary>
        ''' <remarks></remarks>
        Private mErrorId As Byte

        ''' <summary>
        ''' Constructeur de la classe
        ''' </summary>
        ''' <param name="pFrame1">Trame reçue</param>
        ''' <param name="pFrame2">Trame reçue (copie)</param>
        ''' <remarks></remarks>
        Friend Sub New(ByVal pFrame1 As Byte(), ByVal pFrame2 As Byte())
            MyBase.New(pFrame1)
            If pFrame1.Length <> 1 Then
                Throw New LenErrorException(pFrame2)
            End If
            mErrorId = pFrame1(0)
        End Sub
        ''' <summary>
        ''' Constructeur de la classe
        ''' </summary>
        ''' <param name="pSlaveId">Numéro d'esclave</param>
        ''' <param name="pFunction">Numéro de fonction (automatiquement OR 0x80)</param>
        ''' <param name="pErrorId">Numéro d'erreur</param>
        ''' <remarks></remarks>
        Friend Sub New(ByVal pSlaveId As Byte, ByVal pFunction As Byte, ByVal pErrorId As Byte)
            MyBase.New(CByte(pSlaveId Or &H80), pFunction)
            mErrorId = pErrorId
        End Sub

        ''' <summary>
        ''' Retourne la description d'une erreur
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetErrDescription() As String
            Dim lStr As String
            Select Case mErrorId
                Case 1
                    lStr = "Code de fonction incorrecte"
                Case 2
                    lStr = "Adresse incorrecte"
                Case 3
                    lStr = "Données incorrectes"
                Case 4
                    lStr = "Erreur de longueur de la réponse"
                Case 5
                    lStr = "Acquittement"
                Case 6
                    lStr = "Automate non prêt"
                Case 7
                    lStr = "NACK"
                Case 8
                    lStr = "Erreur de parité en mémoire"
                Case 10
                    lStr = "GATEWAY PATH UNAVAILABLE"
                Case 11
                    lStr = "GATEWAY TARGET DEVICE FAILED TO RESPOND"
                Case Else
                    lStr = String.Format("Code inconnu ({0})", mErrorId)
            End Select
            Return lStr
        End Function
        ''' <summary>
        ''' Retourne le numéro de l'erreur
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property ErrNum() As Integer
            Get
                Return mErrorId
            End Get
        End Property

        ''' <summary>
        ''' Retourne la trame correspondante aux données
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Friend NotOverridable Overloads Overrides Function GetFrame() As Byte()
            Return MyBase.GetFrame(New Byte() {mErrorId})
        End Function
        ''' <summary>
        ''' Retourne le type de trame
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property FrameType() As ClsFrameModbusBase.EFrameType
            Get
                Return EFrameType.Error
            End Get
        End Property
    End Class
End Namespace