'
' @file ModbusRtuButtonForm.vb
'
' A simple VB.net example using FieldTalk to control a coil with
' two form buttons using Modbus RTU.
'
' @if NOTICE
'
' Copyright (c) proconX Pty Ltd. All rights reserved.
'
' The following source file constitutes example program code and is
' intended merely to illustrate useful programming techniques.  The user
' is responsible for applying the code correctly.
'
' THIS SOFTWARE IS PROVIDED BY PROCONX AND CONTRIBUTORS ``AS IS'' AND ANY
' EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
' IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
' PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PROCONX OR CONTRIBUTORS BE
' LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
' CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
' SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
' BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
' OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
' ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'
' @endif
'

Imports FieldTalk.Modbus.Master


Public Class Form1

    ' Create an instance of the Modbus/TCP protocol
    Dim modbusProtocol As MbusRtuMasterProtocol = New MbusRtuMasterProtocol


    Private Sub OpenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenButton.Click
        Dim result As Integer

        ' Open the protocol and serial port connection
        ' Note: The \\.\ prefix is required to be able to open COM ports >= 10
        result = modbusProtocol.openProtocol("\\.\" + PortComboBox.Text, Int32.Parse(BaudrateComboBox.Text), 8, 1, ParityComboBox.SelectedIndex)
        ' Change time-out to 300 ms
        modbusProtocol.setTimeout(300)

        ' Indicate result on status line
        StatusLine.Text = "openProtocol: " + BusProtocolErrors.getBusProtocolErrorText(result)
        ' Enable or disable button controls depending on successful open operation
        If result = BusProtocolErrors.FTALK_SUCCESS Then
            OpenButton.Enabled = False
            CloseButton.Enabled = True
            SetButton.Enabled = True
            ResetButton.Enabled = True
            PollTimer.Enabled = True
        End If
    End Sub


    Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
        ' Close protocol and serial port
        modbusProtocol.closeProtocol()
        ' Indicate result on status line
        StatusLine.Text = "closeProtocol"
        ' Disable button controls
        OpenButton.Enabled = True
        CloseButton.Enabled = False
        SetButton.Enabled = False
        ResetButton.Enabled = False
        PollTimer.Enabled = False
        CoilStateLabel.Text = "???"
    End Sub


    Private Sub SetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetButton.Click
        Dim result As Integer

        ' Energize coil number 100 on Modbus Slave with ID 1
        result = modbusProtocol.writeCoil(1, 100, True)
        ' Indicate result on status line
        StatusLine.Text = "writeCoil: " + BusProtocolErrors.getBusProtocolErrorText(result)
    End Sub


    Private Sub ResetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetButton.Click
        Dim result As Integer

        ' Reset coil number 100 on Modbus Slave with ID 1
        result = modbusProtocol.writeCoil(1, 100, False)
        ' Indicate result on status line
        StatusLine.Text = "writeCoil: " + BusProtocolErrors.getBusProtocolErrorText(result)
    End Sub


    Private Sub ModbusRtuButtonForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PortComboBox.SelectedIndex = 0
        BaudrateComboBox.SelectedIndex = 3
        ParityComboBox.SelectedIndex = 2
    End Sub


    Private Sub PollTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PollTimer.Tick
        Dim result As Integer
        Dim coilArr(1) As Boolean

        ' Reset coil number 100 on Modbus Slave with ID 1
        result = modbusProtocol.readCoils(1, 100, coilArr)
        If result = BusProtocolErrors.FTALK_SUCCESS Then
            If coilArr(0) Then
                CoilStateLabel.Text = "ON"
            Else
                CoilStateLabel.Text = "OFF"
            End If
        Else
            CoilStateLabel.Text = "ERROR"
        End If
        ' Indicate result on status line
        StatusLine.Text = "readCoil: " + BusProtocolErrors.getBusProtocolErrorText(result)
    End Sub
End Class
