Public Class Form_C5IO
    Dim C5IO_Relay_Num As UInteger = 20     'C5IO 继电器数
    Dim C5IO_DO_Num As UInteger = 6         'C5IO DO数
    Dim C5IO_DI_Num As UInteger = 7         'C5IO DI数
    Dim C5IO_PKT_Type As Byte = &HF1        'C5IO设备类型 - 这设备类型只用于BLV_BUS协议通讯使用

    Dim Reply_CMD As Byte = &HA0   '回复命令

    Class DEVICE_STATE
        Public DI_Type(13) As Byte
        Public DI_State(13) As Byte
        Public DI_Level As UInt32
        Public DO_State(5) As Byte
        Public DO_Level As UInt32
        Public Relay_State(24) As Byte
        Public Relay_Level As UInt64
    End Class

    Dim Dev_State As New DEVICE_STATE

    ''' <summary>
    ''' C5IO 控制CMD
    ''' </summary>
    Enum CMD
        Inquire = &H20
        Set_Relay = &H21
        Set_Relay_Inching = &H22
        Set_DO = &H23
        Set_DO_Inching = &H24
        Set_DI = &H25
    End Enum

    ''' <summary>
    ''' 获取C5IO别名参数
    ''' </summary>
    Public Sub Get_C5IO_TextBox_Name()
        TextBox_DI1.Text = My.Settings.DI_Name1
        TextBox_DI2.Text = My.Settings.DI_Name2
        TextBox_DI3.Text = My.Settings.DI_Name3
        TextBox_DI4.Text = My.Settings.DI_Name4
        TextBox_DI5.Text = My.Settings.DI_Name5
        TextBox_DI6.Text = My.Settings.DI_Name6
        TextBox_DI7.Text = My.Settings.DI_Name7

        TextBox_DO1.Text = My.Settings.DO_Name1
        TextBox_DO2.Text = My.Settings.DO_Name2
        TextBox_DO3.Text = My.Settings.DO_Name3
        TextBox_DO4.Text = My.Settings.DO_Name4
        TextBox_DO5.Text = My.Settings.DO_Name5
        TextBox_DO6.Text = My.Settings.DO_Name6

        TextBox_Relay1.Text = My.Settings.Relay_Name1
        TextBox_Relay2.Text = My.Settings.Relay_Name2
        TextBox_Relay3.Text = My.Settings.Relay_Name3
        TextBox_Relay4.Text = My.Settings.Relay_Name4
        TextBox_Relay5.Text = My.Settings.Relay_Name5
        TextBox_Relay6.Text = My.Settings.Relay_Name6
        TextBox_Relay7.Text = My.Settings.Relay_Name7
        TextBox_Relay8.Text = My.Settings.Relay_Name8
        TextBox_Relay9.Text = My.Settings.Relay_Name9
        TextBox_Relay10.Text = My.Settings.Relay_Name10
        TextBox_Relay11.Text = My.Settings.Relay_Name11
        TextBox_Relay12.Text = My.Settings.Relay_Name12
        TextBox_Relay13.Text = My.Settings.Relay_Name13
        TextBox_Relay14.Text = My.Settings.Relay_Name14
        TextBox_Relay15.Text = My.Settings.Relay_Name15
        TextBox_Relay16.Text = My.Settings.Relay_Name16
        TextBox_Relay17.Text = My.Settings.Relay_Name17
        TextBox_Relay18.Text = My.Settings.Relay_Name18
        TextBox_Relay19.Text = My.Settings.Relay_Name19
        TextBox_Relay20.Text = My.Settings.Relay_Name20
    End Sub
    ''' <summary>
    ''' 保存C5IO别名参数
    ''' </summary>
    Public Sub Save_C5IO_TextBox_Name()
        My.Settings.DI_Name1 = TextBox_DI1.Text
        My.Settings.DI_Name2 = TextBox_DI2.Text
        My.Settings.DI_Name3 = TextBox_DI3.Text
        My.Settings.DI_Name4 = TextBox_DI4.Text
        My.Settings.DI_Name5 = TextBox_DI5.Text
        My.Settings.DI_Name6 = TextBox_DI6.Text
        My.Settings.DI_Name7 = TextBox_DI7.Text

        My.Settings.DO_Name1 = TextBox_DO1.Text
        My.Settings.DO_Name2 = TextBox_DO2.Text
        My.Settings.DO_Name3 = TextBox_DO3.Text
        My.Settings.DO_Name4 = TextBox_DO4.Text
        My.Settings.DO_Name5 = TextBox_DO5.Text
        My.Settings.DO_Name6 = TextBox_DO6.Text

        My.Settings.Relay_Name1 = TextBox_Relay1.Text
        My.Settings.Relay_Name2 = TextBox_Relay2.Text
        My.Settings.Relay_Name3 = TextBox_Relay3.Text
        My.Settings.Relay_Name4 = TextBox_Relay4.Text
        My.Settings.Relay_Name5 = TextBox_Relay5.Text
        My.Settings.Relay_Name6 = TextBox_Relay6.Text
        My.Settings.Relay_Name7 = TextBox_Relay7.Text
        My.Settings.Relay_Name8 = TextBox_Relay8.Text
        My.Settings.Relay_Name9 = TextBox_Relay9.Text
        My.Settings.Relay_Name10 = TextBox_Relay10.Text
        My.Settings.Relay_Name11 = TextBox_Relay11.Text
        My.Settings.Relay_Name12 = TextBox_Relay12.Text
        My.Settings.Relay_Name13 = TextBox_Relay13.Text
        My.Settings.Relay_Name14 = TextBox_Relay14.Text
        My.Settings.Relay_Name15 = TextBox_Relay15.Text
        My.Settings.Relay_Name16 = TextBox_Relay16.Text
        My.Settings.Relay_Name17 = TextBox_Relay17.Text
        My.Settings.Relay_Name18 = TextBox_Relay18.Text
        My.Settings.Relay_Name19 = TextBox_Relay19.Text
        My.Settings.Relay_Name20 = TextBox_Relay20.Text
    End Sub

    ''' <summary>
    ''' 获取DI类型
    ''' </summary>
    ''' <param name="type"></param>
    ''' <returns></returns>
    Public Function Get_BLV_IO_DI_Type(type As Byte) As String
        Dim temp_string As String

        Select Case type
            Case 1
                temp_string = "按键"
            Case 2
                temp_string = "PIR"
            Case 3
                temp_string = "干接点"
            Case Else
                temp_string = "类型没定义"
        End Select

        Return temp_string
    End Function

    Public Function Get_BLV_IO_DI_Status(state As Byte) As String
        Dim temp_string As String = ""

        Select Case state
            Case 0
                temp_string = "没动作"
            Case 1
                temp_string = "触发/点按"
            Case 2
                temp_string = "长按"
            Case 3
                temp_string = "长按松开"
        End Select

        Return temp_string
    End Function

    Public Function Get_BLV_IO_DO_Status(state As Byte) As String
        Dim temp_string As String = ""

        Select Case state
            Case 0
                temp_string = "关闭"
            Case 1
                temp_string = "打开"

        End Select

        Return temp_string
    End Function

    Public Function Get_BLV_IO_RELAY_Status(state As Byte) As String
        Dim temp_string As String = ""

        Select Case state
            Case 0
                temp_string = "关闭"
            Case 1
                temp_string = "打开"

        End Select

        Return temp_string
    End Function


    ''' <summary>
    ''' 解析C5IO协议回复数据内容
    ''' </summary>
    ''' <param name="data">串口数据</param>
    ''' <returns>0x00:解析数据成功，其他值：解析失败</returns>
    Public Function Parse_RelayData_Protocol_Content(data As Byte(), ByRef parsing_str As String) As Byte
        Dim rev As Byte
        Dim di_num As Byte
        Dim do_num As Byte
        Dim relay_num As Byte
        Dim parsing_type As Byte
        Dim temp_byte As Byte

        rev = BLV_INFO.CheckSum(data, data.Length)          '和校验
        If rev <> &H0 Then  'if(rev!=0x00)
            Return BLV_INFO.PROCESS_RETURN.Check_Error      '和校验错误
        End If

        If data(BLV_INFO.BUS_PKT.PKT_DevType) <> C5IO_PKT_Type Then
            Return BLV_INFO.PROCESS_RETURN.DevType_Error    '设备类型错误
        End If

        If data(BLV_INFO.BUS_PKT.PKT_LEN) <> data.Length Then
            Return BLV_INFO.PROCESS_RETURN.Len_Error        '数据长度错误
        End If

        If data(BLV_INFO.BUS_PKT.PKT_CMD) <> Reply_CMD Then
            Return BLV_INFO.PROCESS_RETURN.Cmd_Error        '回复命令错误
        End If

        BLV_INFO.DEBUG_Printf("C5IO回复数据内容解析")

        '判断一下数据长度，判断当前IO是哪一个版本
        If data(BLV_INFO.BUS_PKT.PKT_LEN) = 17 Then         'C5IO数据解析
            parsing_type = &H1
            di_num = 7
            do_num = 5
            relay_num = 20

            parsing_str += "C5IO回复数据解析 "
            BLV_INFO.DEBUG_Printf("C5IO回复数据解析")
        ElseIf data(BLV_INFO.BUS_PKT.PKT_LEN) = 23 Then     'C12IO数据解析
            parsing_type = &H2
            di_num = 13
            do_num = 5
            relay_num = 13

            parsing_str += "C12IO回复数据解析 "
            BLV_INFO.DEBUG_Printf("C12IO回复数据解析")
        ElseIf data(BLV_INFO.BUS_PKT.PKT_LEN) = 31 Then     'CSIO数据解析
            parsing_type = &H3
            di_num = 13
            do_num = 5
            relay_num = 24

            parsing_str += "CSIO回复数据解析 "
            BLV_INFO.DEBUG_Printf("CSIO回复数据解析")
        End If

        Select Case data(BLV_INFO.BUS_PKT.PKT_PARA)
            Case &H0
                parsing_str += "查询成功" & vbCrLf
                BLV_INFO.DEBUG_Printf("查询成功")
            Case &H1
                parsing_str += "继电器执行成功" & vbCrLf
                BLV_INFO.DEBUG_Printf("继电器执行成功")
            Case &H2
                parsing_str += "继电器点动执行成功" & vbCrLf
                BLV_INFO.DEBUG_Printf("继电器点动执行成功")
            Case &H3
                parsing_str += "DO执行成功" & vbCrLf
                BLV_INFO.DEBUG_Printf("DO执行成功")
            Case &H4
                parsing_str += "DO点动执行成功" & vbCrLf
                BLV_INFO.DEBUG_Printf("DO点动执行成功")
            Case &H5
                parsing_str += "DI执行成功" & vbCrLf
                BLV_INFO.DEBUG_Printf("DI执行成功")
            Case &HF0
                parsing_str += "数据格式错误" & vbCrLf
                BLV_INFO.DEBUG_Printf("数据格式错误")
            Case Else
                parsing_str += "数据格式错误" & vbCrLf
                BLV_INFO.DEBUG_Printf($"暂无该动作回复:{data(BLV_INFO.BUS_PKT.PKT_PARA)}")
        End Select

        '解析继电器、DO、DI状态
        If parsing_type = &H1 Then          'C5IO

            For idx = 0 To di_num - 1
                Dev_State.DI_Type(idx) = (data(BLV_INFO.BUS_PKT.PKT_PARA + 1 + (idx \ 4)) >> ((idx Mod 4) * 2)) And &H3
                Dev_State.DI_State(idx) = (data(BLV_INFO.BUS_PKT.PKT_PARA + 3 + (idx \ 4)) >> ((idx Mod 4) * 2)) And &H3
                Dev_State.DI_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 5) And &HFF

                parsing_str += $"DI{idx + 1} TYPE:{Get_BLV_IO_DI_Type(Dev_State.DI_Type(idx)),4} State:{Get_BLV_IO_DI_Status(Dev_State.DI_State(idx)),4}" & vbCrLf
                BLV_INFO.DEBUG_Printf($"DI{idx} TYPE:{Get_BLV_IO_DI_Type(Dev_State.DI_Type(idx)),4} State:{Get_BLV_IO_DI_Status(Dev_State.DI_State(idx)),4}")
            Next

            Dev_State.DO_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 6) And &HFF

            Dev_State.Relay_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 9) And &HFF
            Dev_State.Relay_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 8) Or (Dev_State.Relay_Level << 8)
            Dev_State.Relay_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 7) Or (Dev_State.Relay_Level << 8)

            parsing_str += $"DO_Level:{Dev_State.DO_Level:X} Relay_Level:{Dev_State.Relay_Level:X}" & vbCrLf
            BLV_INFO.DEBUG_Printf($"DO_Level:{Dev_State.DO_Level:X} Relay_Level:{Dev_State.Relay_Level:X}")

            For idx = 0 To do_num - 1
                Dev_State.DO_State(idx) = (Dev_State.DO_Level) >> idx And &H1
                parsing_str += $"DO_{idx + 1} {Get_BLV_IO_DO_Status(Dev_State.DO_State(idx)),3}" & vbCrLf
            Next

            For idx = 0 To relay_num - 1
                Dev_State.Relay_State(idx) = (Dev_State.Relay_Level >> idx) And &H1
                parsing_str += $"RELAY_{idx + 1} {Get_BLV_IO_RELAY_Status(Dev_State.Relay_State(idx)),3}" & vbCrLf
            Next

        ElseIf parsing_type = &H2 Then          'C12IO
            For idx = 0 To di_num - 1
                Dev_State.DI_Type(idx) = (data(BLV_INFO.BUS_PKT.PKT_PARA + 1 + (idx \ 4)) >> ((idx Mod 4) * 2)) And &H3
                Dev_State.DI_State(idx) = (data(BLV_INFO.BUS_PKT.PKT_PARA + 5 + (idx \ 4)) >> ((idx Mod 4) * 2)) And &H3

                Dev_State.DI_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 10) And &HFF
                Dev_State.DI_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 9) Or (Dev_State.DI_Level << 8)

                parsing_str += $"DI{idx + 1} TYPE:{Get_BLV_IO_DI_Type(Dev_State.DI_Type(idx)),4} State:{Get_BLV_IO_DI_Status(Dev_State.DI_State(idx)),4}" & vbCrLf
                BLV_INFO.DEBUG_Printf($"DI{idx} TYPE:{Get_BLV_IO_DI_Type(Dev_State.DI_Type(idx)),4} State:{Get_BLV_IO_DI_Status(Dev_State.DI_State(idx)),4}")
            Next

            Dev_State.DO_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 12) And &HFF
            Dev_State.DO_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 11) Or (Dev_State.DO_Level << 8)

            Dev_State.Relay_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 15) And &HFF
            Dev_State.Relay_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 14) Or (Dev_State.Relay_Level << 8)
            Dev_State.Relay_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 13) Or (Dev_State.Relay_Level << 8)

            parsing_str += $"DO_Level:{Dev_State.DO_Level:X} Relay_Level:{Dev_State.Relay_Level:X}" & vbCrLf
            BLV_INFO.DEBUG_Printf($"DO_Level:{Dev_State.DO_Level:X} Relay_Level:{Dev_State.Relay_Level:X}")

            For idx = 0 To do_num - 1
                Dev_State.DO_State(idx) = (Dev_State.DO_Level) >> idx And &H1
                parsing_str += $"DO_{idx + 1} {Get_BLV_IO_DO_Status(Dev_State.DO_State(idx)),3}" & vbCrLf
            Next

            For idx = 0 To relay_num - 1
                Dev_State.Relay_State(idx) = (Dev_State.Relay_Level >> idx) And &H1
                parsing_str += $"RELAY_{idx + 1} {Get_BLV_IO_RELAY_Status(Dev_State.Relay_State(idx)),3}" & vbCrLf
            Next

        ElseIf parsing_type = &H3 Then          'CSIO
            '
            parsing_str += $"CSIO版本：{data(BLV_INFO.BUS_PKT.PKT_PARA + 16)，2}     "
            parsing_str += $"RTC时间：20{data(BLV_INFO.BUS_PKT.PKT_PARA + 17)，2}-{data(BLV_INFO.BUS_PKT.PKT_PARA + 18)，2}-{data(BLV_INFO.BUS_PKT.PKT_PARA + 19)，2} {data(BLV_INFO.BUS_PKT.PKT_PARA + 20)，2}:{data(BLV_INFO.BUS_PKT.PKT_PARA + 21)，2}:{data(BLV_INFO.BUS_PKT.PKT_PARA + 22)，2} 星期{data(BLV_INFO.BUS_PKT.PKT_PARA + 23)，2}" & vbCrLf

            For idx = 0 To di_num - 1
                temp_byte = data(BLV_INFO.BUS_PKT.PKT_PARA + 1 + (idx \ 4)) >> ((idx Mod 4) * 2)
                Dev_State.DI_Type(idx) = temp_byte And &H3
                temp_byte = data(BLV_INFO.BUS_PKT.PKT_PARA + 5 + (idx \ 4)) >> ((idx Mod 4) * 2)
                Dev_State.DI_State(idx) = temp_byte And &H3

                parsing_str += $"DI{idx + 1} TYPE:{Get_BLV_IO_DI_Type(Dev_State.DI_Type(idx)),4} State:{Get_BLV_IO_DI_Status(Dev_State.DI_State(idx)),4}" & vbCrLf
                BLV_INFO.DEBUG_Printf($"DI{idx} TYPE:{Get_BLV_IO_DI_Type(Dev_State.DI_Type(idx)),4} State:{Get_BLV_IO_DI_Status(Dev_State.DI_State(idx)),4}")
            Next

            Dev_State.DI_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 10) And &HFF
            Dev_State.DI_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 9) Or (Dev_State.DI_Level << 8)

            Dev_State.DO_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 12) And &HFF
            Dev_State.DO_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 11) Or (Dev_State.DO_Level << 8)

            Dev_State.Relay_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 15) And &HFF
            Dev_State.Relay_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 14) Or (Dev_State.Relay_Level << 8)
            Dev_State.Relay_Level = data(BLV_INFO.BUS_PKT.PKT_PARA + 13) Or (Dev_State.Relay_Level << 8)

            parsing_str += $"DI_Level:{Dev_State.DI_Level:X}  DO_Level:{Dev_State.DO_Level:X}  Relay_Level:{Dev_State.Relay_Level:X}" & vbCrLf
            BLV_INFO.DEBUG_Printf($"DI_Level:{Dev_State.DI_Level:X}  DO_Level:{Dev_State.DO_Level:X}  Relay_Level:{Dev_State.Relay_Level:X}")

            For idx = 0 To do_num - 1
                Dev_State.DO_State(idx) = (Dev_State.DO_Level) >> idx And &H1
                parsing_str += $"DO_{idx + 1} {Get_BLV_IO_DO_Status(Dev_State.DO_State(idx)),3}" & vbCrLf
                BLV_INFO.DEBUG_Printf($"DO_{idx + 1} {Get_BLV_IO_DO_Status(Dev_State.DO_State(idx)),3}")
            Next

            For idx = 0 To relay_num - 1
                Dev_State.Relay_State(idx) = (Dev_State.Relay_Level >> idx) And &H1
                parsing_str += $"RELAY_{idx + 1} {Get_BLV_IO_RELAY_Status(Dev_State.Relay_State(idx)),3}" & vbCrLf
                BLV_INFO.DEBUG_Printf($"RELAY_{idx + 1} {Get_BLV_IO_RELAY_Status(Dev_State.Relay_State(idx)),3}")
            Next

            '后面还有CSIO类型和RTC时间



        Else

        End If





        Return &H0
    End Function

    ''' <summary>
    ''' 解析C5IO通讯控制数据内容
    ''' </summary>
    ''' <param name="data"></param>
    ''' <param name="parsing_str"></param>
    ''' <returns></returns>
    Public Function Parse_ControlData_Protocol_Content(data As Byte(), ByRef parsing_str As String) As Byte
        Select Case data(BLV_INFO.BUS_PKT.PKT_CMD)
            Case CMD.Inquire
                parsing_str += "BLV_IO 查询命令" & vbCrLf
                BLV_INFO.DEBUG_Printf("BLV_IO 查询命令")
            Case CMD.Set_DI
                parsing_str += "BLV_IO 设置DI状态" & vbCrLf
                BLV_INFO.DEBUG_Printf("BLV_IO 设置DI状态")
            Case CMD.Set_DO
                parsing_str += "BLV_IO 设置DO状态" & vbCrLf
                BLV_INFO.DEBUG_Printf("BLV_IO 设置DO状态")
            Case CMD.Set_DO_Inching
                parsing_str += "BLV_IO 设置DO点动状态" & vbCrLf
                BLV_INFO.DEBUG_Printf("BLV_IO 设置DO点动状态")
            Case CMD.Set_Relay
                parsing_str += "BLV_IO 设置继电器状态" & vbCrLf
                BLV_INFO.DEBUG_Printf("BLV_IO 设置继电器状态")
            Case CMD.Set_Relay_Inching
                parsing_str += "BLV_IO 设置继电器点动状态" & vbCrLf
                BLV_INFO.DEBUG_Printf("BLV_IO 设置继电器点动状态")
            Case Else
                parsing_str += "BLV_IO 暂无该命令解析内容" & vbCrLf
                BLV_INFO.DEBUG_Printf("暂无该命令解析内容")
        End Select
    End Function

    ''' <summary>
    ''' 设置C5IO显示状态
    ''' </summary>
    Public Sub Set_C5IO_Show_Status(relay As UInteger, do_state As UInteger, di_state As UInteger)
        Dim temp_state As Byte = 0

        'Relay继电器状态
        For index As Byte = 1 To C5IO_Relay_Num
            If (relay >> (index - 1)) And &H1 Then
                temp_state = 1
            Else
                temp_state = 0
            End If

            'Console.WriteLine(Panel1.Controls.Item(index).Name & " " & Panel1.Controls.Item(index).GetType.ToString)    '获取容器内控件名和类型
            Set_C5IO_Show_Relay_Status(index, temp_state)
        Next

        'DO状态设置
        For index As Byte = 1 To C5IO_DO_Num
            If (do_state >> (index - 1)) And &H1 Then
                temp_state = 1
            Else
                temp_state = 0
            End If
            Set_C5IO_Show_DO_Status(index, temp_state)
        Next

        'DI状态设置
        For index As Byte = 1 To C5IO_DI_Num
            If (di_state >> (index - 1)) And &H1 Then
                temp_state = 1
            Else
                temp_state = 0
            End If

            Set_C5IO_Show_DI_Status(index, temp_state)
        Next

    End Sub
    ''' <summary>
    ''' 设置继电器状态
    ''' </summary>
    ''' <param name="relay_num"> 当前设置继电器序号 </param>
    ''' <param name="state">设置继电器状态</param>
    Public Sub Set_C5IO_Show_Relay_Status(relay_num As Byte, state As Byte)
        Select Case relay_num
            Case 1
                If state = 1 Then
                    Console.WriteLine("Relay1 打开")
                    Relay1.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay1 关闭")
                    Relay1.BackColor = System.Drawing.SystemColors.Control
                End If

            Case 2
                If state = 1 Then
                    Console.WriteLine("Relay2 打开")
                    Relay2.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay2 关闭")
                    Relay2.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 3
                If state = 1 Then
                    Console.WriteLine("Relay3 打开")
                    Relay3.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay3 关闭")
                    Relay3.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 4
                If state = 1 Then
                    Console.WriteLine("Relay4 打开")
                    Relay4.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay4 关闭")
                    Relay4.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 5
                If state = 1 Then
                    Console.WriteLine("Relay5 打开")
                    Relay5.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay5 关闭")
                    Relay5.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 6
                If state = 1 Then
                    Console.WriteLine("Relay6 打开")
                    Relay6.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay6 关闭")
                    Relay6.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 7
                If state = 1 Then
                    Console.WriteLine("Relay7 打开")
                    Relay7.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay7 关闭")
                    Relay7.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 8
                If state = 1 Then
                    Console.WriteLine("Relay8 打开")
                    Relay8.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay8 关闭")
                    Relay8.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 9
                If state = 1 Then
                    Console.WriteLine("Relay9 打开")
                    Relay9.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay9 关闭")
                    Relay9.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 10
                If state = 1 Then
                    Console.WriteLine("Relay10 打开")
                    Relay10.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay10 关闭")
                    Relay10.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 11
                If state = 1 Then
                    Console.WriteLine("Relay11 打开")
                    Relay11.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay11 关闭")
                    Relay11.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 12
                If state = 1 Then
                    Console.WriteLine("Relay12 打开")
                    Relay12.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay12 关闭")
                    Relay12.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 13
                If state = 1 Then
                    Console.WriteLine("Relay13 打开")
                    Relay13.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay13 关闭")
                    Relay13.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 14
                If state = 1 Then
                    Console.WriteLine("Relay14 打开")
                    Relay14.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay14 关闭")
                    Relay14.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 15
                If state = 1 Then
                    Console.WriteLine("Relay15 打开")
                    Relay15.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay15 关闭")
                    Relay15.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 16
                If state = 1 Then
                    Console.WriteLine("Relay16 打开")
                    Relay16.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay16 关闭")
                    Relay16.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 17
                If state = 1 Then
                    Console.WriteLine("Relay17 打开")
                    Relay17.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay17 关闭")
                    Relay17.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 18
                If state = 1 Then
                    Console.WriteLine("Relay18 打开")
                    Relay18.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay18 关闭")
                    Relay18.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 19
                If state = 1 Then
                    Console.WriteLine("Relay19 打开")
                    Relay19.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay19 关闭")
                    Relay19.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 20
                If state = 1 Then
                    Console.WriteLine("Relay20 打开")
                    Relay20.BackColor = Color.Lime
                Else
                    Console.WriteLine("Relay20 关闭")
                    Relay20.BackColor = System.Drawing.SystemColors.Control
                End If
        End Select
    End Sub
    ''' <summary>
    ''' 设置DO状态
    ''' </summary>
    ''' <param name="do_num">当前设置DO序号</param>
    ''' <param name="state">设置DO状态</param>
    Public Sub Set_C5IO_Show_DO_Status(do_num As Byte, state As Byte)
        Select Case do_num
            Case 1
                If state = 1 Then
                    Console.WriteLine("DO1 打开")
                    DO1.BackColor = Color.Lime
                Else
                    Console.WriteLine("DO1 关闭")
                    DO1.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 2
                If state = 1 Then
                    Console.WriteLine("DO2 打开")
                    DO2.BackColor = Color.Lime
                Else
                    Console.WriteLine("DO2 关闭")
                    DO2.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 3
                If state = 1 Then
                    Console.WriteLine("DO3 打开")
                    DO3.BackColor = Color.Lime
                Else
                    Console.WriteLine("DO3 关闭")
                    DO3.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 4
                If state = 1 Then
                    Console.WriteLine("DO4 打开")
                    DO4.BackColor = Color.Lime
                Else
                    Console.WriteLine("DO4 关闭")
                    DO4.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 5
                If state = 1 Then
                    Console.WriteLine("DO5 打开")
                    DO5.BackColor = Color.Lime
                Else
                    Console.WriteLine("DO5 关闭")
                    DO5.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 6
                If state = 1 Then
                    Console.WriteLine("DO6 打开")
                    DO6.BackColor = Color.Lime
                Else
                    Console.WriteLine("DO6 关闭")
                    DO6.BackColor = System.Drawing.SystemColors.Control
                End If

        End Select
    End Sub
    ''' <summary>
    ''' 设置DI状态
    ''' </summary>
    ''' <param name="di_num">当前设置DI序号</param>
    ''' <param name="state">设置DI状态</param>
    Public Sub Set_C5IO_Show_DI_Status(di_num As Byte, state As Byte)
        Select Case di_num
            Case 1
                If state = 1 Then
                    Console.WriteLine("DI1 打开")
                    DI1.BackColor = Color.Lime
                Else
                    Console.WriteLine("DI1 关闭")
                    DI1.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 2
                If state = 1 Then
                    Console.WriteLine("DI2 打开")
                    DI2.BackColor = Color.Lime
                Else
                    Console.WriteLine("DI2 关闭")
                    DI2.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 3
                If state = 1 Then
                    Console.WriteLine("DI3 打开")
                    DI3.BackColor = Color.Lime
                Else
                    Console.WriteLine("DI3 关闭")
                    DI3.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 4
                If state = 1 Then
                    Console.WriteLine("DI4 打开")
                    DI4.BackColor = Color.Lime
                Else
                    Console.WriteLine("DI4 关闭")
                    DI4.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 5
                If state = 1 Then
                    Console.WriteLine("DI5 打开")
                    DI5.BackColor = Color.Lime
                Else
                    Console.WriteLine("DI5 关闭")
                    DI5.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 6
                If state = 1 Then
                    Console.WriteLine("DI6 打开")
                    DI6.BackColor = Color.Lime
                Else
                    Console.WriteLine("DI6 关闭")
                    DI6.BackColor = System.Drawing.SystemColors.Control
                End If
            Case 7
                If state = 1 Then
                    Console.WriteLine("DI7 打开")
                    DI7.BackColor = Color.Lime
                Else
                    Console.WriteLine("DI7 关闭")
                    DI7.BackColor = System.Drawing.SystemColors.Control
                End If
        End Select
    End Sub

    Private Sub Form_C5IO_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '设置显示文本
        Get_C5IO_TextBox_Name()

    End Sub

    Private Sub Form_C5IO_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        '保存显示文本
        Save_C5IO_TextBox_Name()
    End Sub


End Class