Visual C# (.NET Framework)


Create Project

 

Launch Visual Studio, select [New]-[Project] from [File] menu.
From the [Templates] of [New Project] dialog box, select [Windows Form Application] of [Visual C#],
and specify the name and location of the project to be created.
When click the [OK] button, the project will be created.

* Item names and commands may differ depending on the version of Visual Studio.

 

Include Class Library

 

In order to use the libmodbus functions in Visual C#, the following files are required.

 

- For ModbusTCP

  ModbusCs.cs

  ModbusTcpCs.cs

 

- For ModbusRTU

  ModbusCs.cs

  ModbusRtuCs.cs

 

Add each file from [Project]-[Add Existing Item].

 

Add Name Space

 

Add name space to use the methods in class library.
At the beginning of the C# source code file (with .cs extension), please add the following two lines.

 

using ModbusCs;

using ModbusTcpCs;

* For ModbusRTU, enter "using ModbusRtuCs;".

 

Create Class Instance

 

In order to use libmodbus class library, create a class instance.
In the C# source code file (with .cs extension), add the following lines at the beginning of form class.

public class Form1 : System.Windows.Forms.Form
{

Modbus  modbus = new Modbus();//<- add

ModbusTcp  modbustcp = new ModbusTcp();//<- add

* For ModbusRTU, enter "ModbusRtu  modbusrtu = new ModbusRtu();".

 

 

How to Use Class Library Methods

 

In C#, instead of calling the libmodbus functions directly, you call the class library methods.
The libmodbus functions are called from the class library methods.

All the methods in the class library are defined with the name by omitting the prefix from the original function name.
When using with C#, please use the method without prefix.

For example, when using modbus_new_tcp function

Ret = modbustcp.NewTcp  ( ....

 

For example, when using modbus_read_bits function

Ret = modbus.ReadBits  ( ....