'
' @file sersimple.vb
'
' A simple console based example using FieldTalk in Modbus RTU master mode
'
' @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 System
Imports System.Threading
Imports FieldTalk.Modbus.Master


'''
''' A simple console based example using FieldTalk in Modbus RTU master mode
'''
Class SerSimpleApp

    Private mbusProtocol As MbusRtuMasterProtocol = New MbusRtuMasterProtocol
    'Private mbusProtocol As MbusAsciiMasterProtocol = New MbusAsciiMasterProtocol 'Use this declaration for ASCII

    '''
    ''' Opens protocol
    '''
    Private Sub openProtocol()
        Dim result As Integer             ' Use "COM1:" on Windows CE
        result = mbusProtocol.openProtocol("COM1", _
                                           19200, _
                                           MbusSerialClientBase.SER_DATABITS_8, _
                                           MbusSerialClientBase.SER_STOPBITS_1, _
                                           MbusSerialClientBase.SER_PARITY_EVEN)
        If (result <> BusProtocolErrors.FTALK_SUCCESS) Then
            Console.WriteLine(("Error opening protocol: " + BusProtocolErrors.getBusProtocolErrorText(result)))
            Environment.Exit(result)
        End If
    End Sub

    '''
    ''' Closes protocol
    '''
    Private Sub closeProtocol()
        mbusProtocol.closeProtocol
    End Sub


    '''
    ''' Cyclic loop which polls every one second 10 registers starting at
    ''' reference 100 from slave # 1
    '''
    Private Sub runPollLoop()
        Dim dataArr(10) As Int16

        Do While True
            Dim result As Integer

            result = mbusProtocol.readMultipleRegisters(1, 100, dataArr)
            If (result = BusProtocolErrors.FTALK_SUCCESS) Then
                Dim i As Integer = 0
                Do While (i < dataArr.Length)
                    Console.WriteLine("[" & (100 + i) & "]: " & (dataArr(i)))
                    i = i + 1
                Loop
            Else
                Console.WriteLine((BusProtocolErrors.getBusProtocolErrorText(result) + "!"))
                ' Stop for fatal errors
                If ((result And BusProtocolErrors.FTALK_BUS_PROTOCOL_ERROR_CLASS) <> BusProtocolErrors.FTALK_BUS_PROTOCOL_ERROR_CLASS) Then
                    Return
                End If
            End If
            Thread.Sleep(1000)

        Loop
    End Sub


    '''
    ''' Main function.
    '''
    Public Shared Sub Main()
        Dim app As SerSimpleApp = New SerSimpleApp
        app.openProtocol
        app.runPollLoop
        app.closeProtocol
    End Sub


End Class
