Create Project
Launch Visual Studio, select [New]-[Project] from [File] menu.
From the template of [Create a new project] dialog box, select [Windows Forms App] of [C#], click the [Next] button.
From the [Configure your new project] dialog box, specify the project name and the location of the project to be created, click the [Next] button.
From the [Additional information] dialog box, select [.NET 8.0(Long-term support)], and click the [Create] button, then the project will be created.
* The above is for Microsoft Visual Studio Professional 2022 (64-bit). 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
Select [Project] from the menu bar of Visual Studio, and select [Add Existing Item] from the pull-down menu.
From the [Add Existing Item] dialog box, specify the above files, click the [Add] button, then the class library is added to the project.
You can check whether the registered class library exists from the tree view in the [Solution Explorer] window of Visual Studio.
* The above is for Microsoft Visual Studio Professional 2022 (64-bit). Item names and commands may differ depending on the version of Visual Studio.
Add Name Space
Add the 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 ( ....
Notes on building EXE applications in "Any CPU"
In general, an EXE application built with "Any CPU" specified in the [Active solution platform] item in the Visual Studio settings [Configuration Manager] window can be executed in both 64-bit environment and 32-bit OS environment.
However, when developing an EXE application that supports the .NET8 framework, it is necessary to be aware of the target OS.
Please refer ".NET execution environment for .NET8 or later" to know more details.