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 Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            points.Columns.Add(New DataColumn("Channel", GetType(Integer))) 'make a columns
            points.Columns.Add(New DataColumn("Value", GetType(Integer)))
            DataGrid1.DataSource = points
            tableStyle.MappingName = "Points"
            DataGrid1.TableStyles.Add(tableStyle)
        Catch ex As Exception
            DataGrid1.TableStyles("Points").Dispose()
            tableStyle.Dispose()
        End Try
    End Sub
    Private masterTcpClient As TcpClient
    Private master As ModbusIpMaster
    Dim points As New System.Data.DataTable("Points")
    Dim tableStyle As New DataGridTableStyle()
    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}
        Dim TextBox() As TextBox = {ch0Volt, ch1Volt, ch2Volt, ch3Volt, ch4Volt, ch5Volt, ch6Volt, ch7Volt, ch8Volt, ch9Volt}
        Try
            Dim slaveID As Byte = 1
            Dim startAddress As UShort = 0
            Dim numInputs As UShort = UShort.Parse(txtChannelNumber.Text)
            Dim inputs() As UShort = master.ReadHoldingRegisters(slaveID, startAddress, numInputs)
            For i As Integer = 0 To numInputs - 1
                points.Rows(i)(1) = Convert.ToInt32(inputs(i)) 'input the value to data table
            Next i
            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
                    TextBox(i).Text = Convert.ToInt32(inputs(i)).ToString()
                    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 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.WriteSingleRegister(slaveID, Address, CUShort(Convert.ToInt32(ch0Volt.Text))) 'mapping 1~10 volt => 3276.7~32767 int
        Else
            master.WriteSingleRegister(slaveID, Address, 0)
        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.WriteSingleRegister(slaveID, Address, CUShort(Convert.ToInt32(ch1Volt.Text)))
        Else
            master.WriteSingleRegister(slaveID, Address, 0)
        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.WriteSingleRegister(slaveID, Address, CUShort(Convert.ToInt32(ch2Volt.Text)))
        Else
            master.WriteSingleRegister(slaveID, Address, 0)
        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.WriteSingleRegister(slaveID, Address, CUShort(Convert.ToInt32(ch4Volt.Text)))
        Else
            master.WriteSingleRegister(slaveID, Address, 0)
        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.WriteSingleRegister(slaveID, Address, CUShort(Convert.ToInt32(ch6Volt.Text)))
        Else
            master.WriteSingleRegister(slaveID, Address, 0)
        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.WriteSingleRegister(slaveID, Address, CUShort(Convert.ToInt32(ch8Volt.Text)))
        Else
            master.WriteSingleRegister(slaveID, Address, 0)
        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.WriteSingleRegister(slaveID, Address, CUShort(Convert.ToInt32(ch3Volt.Text)))
        Else
            master.WriteSingleRegister(slaveID, Address, 0)
        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.WriteSingleRegister(slaveID, Address, CUShort(Convert.ToInt32(ch5Volt.Text)))
        Else
            master.WriteSingleRegister(slaveID, Address, 0)
        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.WriteSingleRegister(slaveID, Address, CUShort(Convert.ToInt32(ch7Volt.Text)))
        Else
            master.WriteSingleRegister(slaveID, Address, 0)
        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.WriteSingleRegister(slaveID, Address, CUShort(Convert.ToInt32(ch9Volt.Text)))
        Else
            master.WriteSingleRegister(slaveID, Address, 0)
        End If

    End Sub

    Private Sub label5_ParentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles label5.ParentChanged

    End Sub

    Private Sub txtChannelVolt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtChannelVolt.TextChanged
        butONOFF.Text = "ON"
    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 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.WriteSingleRegister(slaveID, Address, CUShort(Convert.ToInt32(txtChannelVolt.Text)))
            butONOFF.Text = "OFF"
        Else
            master.WriteSingleRegister(slaveID, Address, 0)
            butONOFF.Text = "ON"
        End If
    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
            label5.Enabled = True
            txtChannelVolt.Enabled = True
        End If
        Dim checkbox_Renamed() As CheckBox = {ckbCH0, ckbCH1, ckbCH2, ckbCH3, ckbCH4, ckbCH5, ckbCH6, ckbCH7, ckbCH8, ckbCH9}
        Dim TextBox() As TextBox = {ch0Volt, ch1Volt, ch2Volt, ch3Volt, ch4Volt, ch5Volt, ch6Volt, ch7Volt, ch8Volt, ch9Volt}
        For i As Integer = 0 To 9
            checkbox_Renamed(i).Enabled = True
            TextBox(i).Enabled = True
        Next i
        Dim numInputs As UShort = UShort.Parse(txtChannelNumber.Text)
        For i As Integer = 0 To numInputs - 1
            points.Rows.Add(New Object() {i, 0}) 'make a Row;
        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
        label5.Enabled = False
        txtChannelVolt.Enabled = False
        Dim checkbox_Renamed() As CheckBox = {ckbCH0, ckbCH1, ckbCH2, ckbCH3, ckbCH4, ckbCH5, ckbCH6, ckbCH7, ckbCH8, ckbCH9}
        Dim TextBox() As TextBox = {ch0Volt, ch1Volt, ch2Volt, ch3Volt, ch4Volt, ch5Volt, ch6Volt, ch7Volt, ch8Volt, ch9Volt}
        For i As Integer = 0 To 9
            checkbox_Renamed(i).Enabled = False
            TextBox(i).Enabled = False
        Next i
        points.Rows.Clear() 'clear row
    End Sub
End Class
