Option Explicit On   ' Each variable must be declared well

Imports Quicker

Public Class Form1



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim current_mode As Integer

        ' enable Timer1
        Timer1.Interval = 250  ' 250 ms
        Timer1.Enabled = True

        ' read current mode at Modbus address 21
        Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(21), current_mode)

        ' set initial Mode as 1   (Index = mode - 1)
        If current_mode >= 1 And current_mode <= 3 Then  ' make sure the mode is between 1 and 3
            ComboBox1.SelectedIndex = current_mode - 1
        End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ' scan DI and DO every Timer1 interval
        ScanDI()
        ScanDO()
    End Sub


    Private Sub ScanDO()

        Dim iValue As Byte

        ' get boolean at Modbus No.=1
        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(1), iValue)
        If iValue = 1 Then
            PanelDO1.BackColor = Color.LightGreen
        Else
            PanelDO1.BackColor = Color.DarkGreen
        End If

        ' get boolean at Modbus No.=2
        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(2), iValue)
        If iValue = 1 Then
            PanelDO2.BackColor = Color.LightGreen
        Else
            PanelDO2.BackColor = Color.DarkGreen
        End If

        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(3), iValue)
        If iValue = 1 Then
            PanelDO3.BackColor = Color.LightGreen
        Else
            PanelDO3.BackColor = Color.DarkGreen
        End If

        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(4), iValue)
        If iValue = 1 Then
            PanelDO4.BackColor = Color.LightGreen
        Else
            PanelDO4.BackColor = Color.DarkGreen
        End If

        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(5), iValue)
        If iValue = 1 Then
            PanelDO5.BackColor = Color.LightGreen
        Else
            PanelDO5.BackColor = Color.DarkGreen
        End If

        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(6), iValue)
        If iValue = 1 Then
            PanelDO6.BackColor = Color.LightGreen
        Else
            PanelDO6.BackColor = Color.DarkGreen
        End If

        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(7), iValue)
        If iValue = 1 Then
            PanelDO7.BackColor = Color.LightGreen
        Else
            PanelDO7.BackColor = Color.DarkGreen
        End If

        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(8), iValue)
        If iValue = 1 Then
            PanelDO8.BackColor = Color.LightGreen
        Else
            PanelDO8.BackColor = Color.DarkGreen
        End If

    End Sub



    Private Sub ScanDI()

        Dim iValue As Byte

        ' get boolean at Modbus No.=9
        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(9), iValue)
        If iValue = 1 Then
            PanelDI1.BackColor = Color.Red
        Else
            PanelDI1.BackColor = Color.DarkRed
        End If

        ' get boolean value at Modbus No.=10
        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(10), iValue)
        If iValue = 1 Then
            PanelDI2.BackColor = Color.Red
        Else
            PanelDI2.BackColor = Color.DarkRed
        End If

        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(11), iValue)
        If iValue = 1 Then
            PanelDI3.BackColor = Color.Red
        Else
            PanelDI3.BackColor = Color.DarkRed
        End If

        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(12), iValue)
        If iValue = 1 Then
            PanelDI4.BackColor = Color.Red
        Else
            PanelDI4.BackColor = Color.DarkRed
        End If

        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(13), iValue)
        If iValue = 1 Then
            PanelDI5.BackColor = Color.Red
        Else
            PanelDI5.BackColor = Color.DarkRed
        End If

        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(14), iValue)
        If iValue = 1 Then
            PanelDI6.BackColor = Color.Red
        Else
            PanelDI6.BackColor = Color.DarkRed
        End If

        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(15), iValue)
        If iValue = 1 Then
            PanelDI7.BackColor = Color.Red
        Else
            PanelDI7.BackColor = Color.DarkRed
        End If

        Quicker.UserShare.UserGetCoil(Convert.ToUInt16(16), iValue)
        If iValue = 1 Then
            PanelDI8.BackColor = Color.Red
        Else
            PanelDI8.BackColor = Color.DarkRed
        End If

    End Sub


    Private Sub ON_01_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ON_01.Click
        ' set true to Modbus address 1
        Quicker.UserShare.UserSetCoil(Convert.ToUInt16(1), 1)
    End Sub

    Private Sub ON_02_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ON_02.Click
        ' set true to Modbus address 2
        Quicker.UserShare.UserSetCoil(Convert.ToUInt16(2), 1)
    End Sub

    Private Sub ON_03_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ON_03.Click
        ' set true to Modbus address 3
        Quicker.UserShare.UserSetCoil(Convert.ToUInt16(3), 1)
    End Sub

    Private Sub ON_04_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ON_04.Click
        ' set true to Modbus address 4
        Quicker.UserShare.UserSetCoil(Convert.ToUInt16(4), 1)
    End Sub

    Private Sub OFF_01_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OFF_01.Click
        ' set False to Modbus address 1
        Quicker.UserShare.UserSetCoil(Convert.ToUInt16(1), 0)
    End Sub

    Private Sub OFF_02_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OFF_02.Click
        ' set False to Modbus address 2
        Quicker.UserShare.UserSetCoil(Convert.ToUInt16(2), 0)
    End Sub

    Private Sub OFF_03_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OFF_03.Click
        ' set False to Modbus address 3
        Quicker.UserShare.UserSetCoil(Convert.ToUInt16(3), 0)
    End Sub

    Private Sub OFF_04_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OFF_04.Click
        ' set False to Modbus address 4
        Quicker.UserShare.UserSetCoil(Convert.ToUInt16(4), 0)
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        ' write long val (32-bit sined integer) to Modbus No.=21
        Quicker.UserShare.UserSetReg_Long(Convert.ToUInt16(21), Convert.ToInt32(ComboBox1.SelectedIndex + 1))
    End Sub

End Class
