• Libraries
  • Shop
  • Doc
  • Free Modbus
  • Support
  • Login
FieldTalk Modbus Master Library for .NET
FieldTalk Modbus Master Library for .NET
Introduction
What You should know about Modbus
Data and Control Functions for all Modbus Protocol Flavours
Serial Protocols
IP based Protocols
Examples
Design Background
License
Namespaces
Support
Notices
Click or drag to resizeClick or drag to resize

Examples

This topic contains the following sections:

  • Serial Example
  • MODBUS/TCP Example

Serial Example

The following example sersimple.cpp shows how to configure a serial Modbus protocol and read values:

C#
VB
C++
Copy
using System;
using System.Threading;
using FieldTalk.Modbus.Master;


class SerSimpleApp
{

    //MbusAsciiMasterProtocol mbusProtocol = new MbusAsciiMasterProtocol(); // Use this declaration for ASCII
    MbusRtuMasterProtocol mbusProtocol = new MbusRtuMasterProtocol(); // Use this declaration for RTU


    void openProtocol()
    {
        int result;

        result = mbusProtocol.openProtocol("COM1",
                                           19200, // Baudrate
                                           MbusSerialClientBase.SER_DATABITS_8,
                                           MbusSerialClientBase.SER_STOPBITS_1,
                                           MbusSerialClientBase.SER_PARITY_EVEN);
        if (result != BusProtocolErrors.FTALK_SUCCESS)
        {
            Console.WriteLine("Error opening protocol: " + BusProtocolErrors.getBusProtocolErrorText(result));
            Environment.Exit(result);
        }
    }


    void closeProtocol()
    {
        mbusProtocol.closeProtocol();
    }


    void runPollLoop()
    {
        Int16[] dataArr = new Int16[10];

        for (;;)
        {
            int result;

            result = mbusProtocol.readMultipleRegisters(1, 100, dataArr);

            if (result == BusProtocolErrors.FTALK_SUCCESS)
                for (int i = 0; i < dataArr.Length; i++)
                    Console.WriteLine("[" + (100 + i) + "]: " + dataArr[i]);
            else
            {
                Console.WriteLine(BusProtocolErrors.getBusProtocolErrorText(result) + "!");
                // Stop for fatal errors
                if ((result & BusProtocolErrors.FTALK_BUS_PROTOCOL_ERROR_CLASS) != BusProtocolErrors.FTALK_BUS_PROTOCOL_ERROR_CLASS)
                    return;
            }
            Thread.Sleep(1000);
        }
    }


    public static void Main()
    {
        SerSimpleApp app = new SerSimpleApp();

        app.openProtocol();
        app.runPollLoop();
        app.closeProtocol();
    }
}
'
' @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

No code example is currently available or this language may not be supported.

MODBUS/TCP Example

The following example tcpsimple.cpp shows how to configure a MODBUS/TCP protocol and read values:

C#
VB
C++
Copy
using System;
using System.Threading;
using FieldTalk.Modbus.Master;


class TcpSimpleApp
{

    MbusTcpMasterProtocol mbusProtocol = new MbusTcpMasterProtocol();


    void openProtocol()
    {
        int result;

        result = mbusProtocol.openProtocol("127.0.0.1");
        if (result != BusProtocolErrors.FTALK_SUCCESS)
        {
            Console.WriteLine("Error opening protocol: " + BusProtocolErrors.getBusProtocolErrorText(result));
            Environment.Exit(result);
        }
    }


    void closeProtocol()
    {
        mbusProtocol.closeProtocol();
    }


    void runPollLoop()
    {
        Int16[] dataArr = new Int16[10];

        for (;;)
        {
            int result;

            result = mbusProtocol.readMultipleRegisters(1, 100, dataArr);
            if (result == BusProtocolErrors.FTALK_SUCCESS)
            {
                  for (int i = 0; i < dataArr.Length; i++)
                     Console.WriteLine("[" + (100 + i) + "]: " + dataArr[i]);
            }
            else
            {
                Console.WriteLine(BusProtocolErrors.getBusProtocolErrorText(result) + "!");
                // Stop for fatal errors
                if ((result & BusProtocolErrors.FTALK_BUS_PROTOCOL_ERROR_CLASS) != BusProtocolErrors.FTALK_BUS_PROTOCOL_ERROR_CLASS)
                    return;
            }
            Thread.Sleep(1000);
        }
    }


    public static void Main()
    {
        TcpSimpleApp app = new TcpSimpleApp();

        app.openProtocol();
        app.runPollLoop();
        app.closeProtocol();
    }
}
'
' @file tcpsimple.vb
'
' A simple console based example using FieldTalk in Modbus/TCP 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/TCP master mode
'''
Class TcpSimpleApp

    Private mbusProtocol As MbusTcpMasterProtocol = New MbusTcpMasterProtocol

    '''
    ''' Opens protocol
    '''
    Private Sub openProtocol()
        Dim result As Integer
        result = mbusProtocol.openProtocol("127.0.0.1")
        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 TcpSimpleApp = New TcpSimpleApp
        app.openProtocol
        app.runPollLoop
        app.closeProtocol
    End Sub


End Class

No code example is currently available or this language may not be supported.

FieldTalk Modbus Master Library for .NET
Library version 2.15.2
Speak to the Experts
Modbus Organization Member logo

We are member of the Modbus Organization, Inc.

Buy with Confidence
30-day money back guarantee All our FieldTalk web sales are backed by a 30-day Money Back Guarantee.
We Accept
Bank VISA MasterCard PayPal
Customer Info
  • Info & Contact
  • Customer Login
  • Terms of Service
  • Terms of Sale
  • Privacy Policy
© 2005-2025 proconX Pty Ltd. All rights reserved. proconX and FieldTalk are trademarks of proconX Pty Ltd.
All other trademarks and registered trademarks appearing on www.modbusdriver.com are the property of their respective owners.