Introduction |
This FieldTalk Modbus Slave Library for .NET allows you to incorporate Modbus slave functionality into your your VB.net and C# programs.
Typical applications are Modbus based Supervisory Control and Data Acquisition Systems (SCADA), Modbus data concentrators, Modbus gateways, User Interfaces and Factory Information Systems (FIS).
Features:
The FieldTalk .NET library consists of two components: a .NET Interop Library with MSIL code (managed code) and a Modbus Core Driver written in native code (unmanaged code). This architecture has significant performance benefits for .NET applications because the time critical communication code is executed as native code.
The two components are contained in separate files: FieldTalk.Modbus.Slave.dll contains the .NET Interop Library and libmbusslave.dll the native Modbus Core Driver. These two library files have to be deployed with your application.
For .NET Core, the deployment and loading of the native code library is taken care of by .NET core's package management. This works for Windows as well as for Linux platforms.
For classic .NET Framework 4.0 and 2.0, the deployment of the native code library is taken care of by the FieldTalk.Modbus.Slave.prop file which the NutGet package manager will automatically add to your project. With that file, when compiling, a bin/x86 and a bin/x64 subfolder is created in the bin directory and the native libraries copied into these subfolders. When the .NET assembly is loaded, the correct native library will be loaded from one of these two sub-folders.
For the .NET Compact Framework (Windows Embedded Compact, Windows CE and Windows Mobile) the deployment of the native libray must be done manually.
The library's API The library is organised in two categories of classes.
One category implements the Server Engines for each Modbus slave protocol flavour. There is one Server Engine class for each protocol flavour and a common Server Engine base class, which applies to all protocol flavours. Because the two serial protocols ASCII and RTU share some common code, an intermediate base class implements the functions specific to serial protocols.
The second category of classes is Data Providers classes. Data Provider classes represent the interface between the Server Engine and your application.
The base class MbusSlaveServer contains a protocol unspecific contains a protocol unspecific Server Engine and the protocol state machine. All protocol flavours inherit from this base class.
The class MbusAsciiSlaveProtocol implements the Modbus ASCII protocol, the class MbusRtuSlaveProtocol implements the Modbus RTU protocol and the class MbusTcpSlaveProtocol implements the MODBUS/TCP protocol.
Before a server can be used, a Data Provider has to be declared. A Data Provider is created by declaring a new class derived from MbusDataTableInterface. The class MbusDataTableInterface is the base class for a Data Provider and implements a set of default methods. An application specific Data Provider simply overrides selected default methods and the Modbus slave is ready.
class MyDatatable: MbusDataTableInterface { ... // Application specific data interface }; MyDatatable dataTable = new MyDatatable();
Class MyDatatable Inherits MbusDataTableInterface ... ' Application specific data interface End Class Dim dataTable As MyDatatable = New MyDatatable
No code example is currently available or this language may not be supported.
In order to use one of the three slave protocols, the desired protocol flavour class has to be instantiated and associated with the Data Provider. The following example creates an RTU protocol and links a data table to slave address 20:
MbusRtuSlaveProtocol mbusServer = new MbusRtuSlaveProtocol(); mbusProtocol.addDataTable(20, dataTable);
Dim mbusServer As MbusRtuSlaveProtocol = New MbusRtuSlaveProtocol mbusProtocol.addDataTable(20, dataTable);
No code example is currently available or this language may not be supported.
After a protocol object has been declared and started up the server loop has to be executed cyclically. The Modbus slave is ready to accept connections and to reply to master queries.