Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports Modbus
Imports Modbus.Device
Imports Modbus.Data
Imports System.Net
Imports System.Net.Sockets
Imports System.IO.Ports
Public Class Form1

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

    End Sub
    Private Sub timerScan_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerScan.Tick
        Dim checkbox_Renamed() As CheckBox = {ckbCH0, ckbCH1, ckbCH2, ckbCH3, ckbCH4, ckbCH5, ckbCH6, ckbCH7, ckbCH8, ckbCH9}
        Try
            Dim sKey As String = ""
            Dim slaveID As Byte = 1
            Dim startAddress As UShort = 0
            Dim numInputs As UShort = UShort.Parse(txtChannelNumber.Text)
            Dim inputs() As Boolean = master.ReadCoils(slaveID, startAddress, numInputs)
            For i As Integer = 0 To numInputs - 1
                sKey &= i.ToString() & "=>" & Convert.ToInt32(inputs(i)) & vbCr & vbCr
                If (i + 1) Mod 3 = 0 Then
                    sKey &= vbLf
                End If
            Next i
            txtMsg.Text = sKey
            Dim index As Integer = 0
            If numInputs > 10 Then
                index = 10
            Else
                index = numInputs
            End If
            For i As Integer = 0 To index - 1 'input channel statue
                If Convert.ToInt32(inputs(i)) > 0 Then
                    checkbox_Renamed(i).Checked = True
                Else
                    checkbox_Renamed(i).Checked = False
                End If
            Next i
        Catch ex As Exception
            btDisconnect_Click(sender, e)
            MessageBox.Show(ex.Message)
        End Try
    End Sub





    Private Sub btConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btConnect.Click
        Dim port As Integer
        port = Convert.ToInt16(txtPort.Text)
        masterTcpClient = New TcpClient(txtIP.Text, port)
        master = ModbusIpMaster.CreateIp(masterTcpClient)
        btConnect.Enabled = False
        btDisconnect.Enabled = True
        timerScan.Enabled = True
        txtChannelNumber.Enabled = False
        If Int16.Parse(txtChannelNumber.Text) > 10 Then
            label4.Enabled = True
            txtChannel.Enabled = True
            butONOFF.Enabled = True
        End If
        Dim checkbox_Renamed() As CheckBox = {ckbCH0, ckbCH1, ckbCH2, ckbCH3, ckbCH4, ckbCH5, ckbCH6, ckbCH7, ckbCH8, ckbCH9}
        For i As Integer = 0 To 9
            checkbox_Renamed(i).Enabled = True
        Next i
    End Sub

    Private Sub btDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDisconnect.Click
        masterTcpClient.Close()
        btConnect.Enabled = True
        btDisconnect.Enabled = False
        timerScan.Enabled = False
        txtChannelNumber.Enabled = True
        label4.Enabled = False
        txtChannel.Enabled = False
        butONOFF.Enabled = False
        Dim checkbox_Renamed() As CheckBox = {ckbCH0, ckbCH1, ckbCH2, ckbCH3, ckbCH4, ckbCH5, ckbCH6, ckbCH7, ckbCH8, ckbCH9}
        For i As Integer = 0 To 9
            checkbox_Renamed(i).Enabled = False
        Next i
    End Sub

    Private Sub ckbCH0_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckbCH0.CheckStateChanged
        Dim Address As UShort = 0
        Dim slaveID As Byte = 1
        If ckbCH0.Checked = True Then
            master.WriteSingleCoil(slaveID, Address, True)
        Else
            master.WriteSingleCoil(slaveID, Address, False)
        End If
    End Sub

    Private Sub ckbCH2_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckbCH2.CheckStateChanged
        Dim Address As UShort = 2
        Dim slaveID As Byte = 1
        If ckbCH2.Checked = True Then
            master.WriteSingleCoil(slaveID, Address, True)
        Else
            master.WriteSingleCoil(slaveID, Address, False)
        End If
    End Sub

    Private Sub ckbCH4_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckbCH4.CheckStateChanged
        Dim Address As UShort = 4
        Dim slaveID As Byte = 1
        If ckbCH4.Checked = True Then
            master.WriteSingleCoil(slaveID, Address, True)
        Else
            master.WriteSingleCoil(slaveID, Address, False)
        End If
    End Sub

    Private Sub ckbCH6_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckbCH6.CheckStateChanged
        Dim Address As UShort = 6
        Dim slaveID As Byte = 1
        If ckbCH6.Checked = True Then
            master.WriteSingleCoil(slaveID, Address, True)
        Else
            master.WriteSingleCoil(slaveID, Address, False)
        End If
    End Sub

    Private Sub ckbCH8_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckbCH8.CheckStateChanged
        Dim Address As UShort = 8
        Dim slaveID As Byte = 1
        If ckbCH8.Checked = True Then
            master.WriteSingleCoil(slaveID, Address, True)
        Else
            master.WriteSingleCoil(slaveID, Address, False)
        End If
    End Sub

    Private Sub ckbCH1_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckbCH1.CheckStateChanged
        Dim Address As UShort = 1
        Dim slaveID As Byte = 1
        If ckbCH1.Checked = True Then
            master.WriteSingleCoil(slaveID, Address, True)
        Else
            master.WriteSingleCoil(slaveID, Address, False)
        End If
    End Sub

    Private Sub ckbCH3_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckbCH3.CheckStateChanged
        Dim Address As UShort = 3
        Dim slaveID As Byte = 1
        If ckbCH3.Checked = True Then
            master.WriteSingleCoil(slaveID, Address, True)
        Else
            master.WriteSingleCoil(slaveID, Address, False)
        End If
    End Sub

    Private Sub ckbCH5_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckbCH5.CheckStateChanged
        Dim Address As UShort = 5
        Dim slaveID As Byte = 1
        If ckbCH5.Checked = True Then
            master.WriteSingleCoil(slaveID, Address, True)
        Else
            master.WriteSingleCoil(slaveID, Address, False)
        End If
    End Sub

    Private Sub ckbCH7_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckbCH7.CheckStateChanged
        Dim Address As UShort = 7
        Dim slaveID As Byte = 1
        If ckbCH7.Checked = True Then
            master.WriteSingleCoil(slaveID, Address, True)
        Else
            master.WriteSingleCoil(slaveID, Address, False)
        End If
    End Sub

    Private Sub ckbCH9_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckbCH9.CheckStateChanged
        Dim Address As UShort = 9
        Dim slaveID As Byte = 1
        If ckbCH9.Checked = True Then
            master.WriteSingleCoil(slaveID, Address, True)
        Else
            master.WriteSingleCoil(slaveID, Address, False)
        End If
    End Sub

    Private Sub butONOFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butONOFF.Click
        Dim Address As UShort = UShort.Parse(txtChannel.Text) 'chanell bigger than 10
        Dim slaveID As Byte = 1
        If butONOFF.Text = "ON" Then
            master.WriteSingleCoil(slaveID, Address, True)
            butONOFF.Text = "OFF"
        Else
            master.WriteSingleCoil(slaveID, Address, False)
            butONOFF.Text = "ON"
        End If
    End Sub
    Private Sub txtChannel_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtChannel.TextChanged
        butONOFF.Text = "ON"
    End Sub

    Private Sub label1_ParentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles label1.ParentChanged

    End Sub

    Private Sub label2_ParentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles label2.ParentChanged

    End Sub

    Private Sub txtIP_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtIP.TextChanged

    End Sub

    Private Sub txtPort_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPort.TextChanged

    End Sub

    Private Sub label3_ParentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles label3.ParentChanged

    End Sub

    Private Sub txtChannelNumber_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtChannelNumber.TextChanged

    End Sub
End Class
