ModbusRTU is a library for working with the Modbus RTU protocol in C#. The library provides simple methods for reading and writing registers, coils, and other Modbus RTU elements via a serial port.
You can copy the source code from the ModbusRTU.cs file directly into your project, or add the ModbusRTU.cs file as a reference to your project.
ATTENTION! It works only with .NET Framework (Project Settings -> Player -> Other Settings -> Api Compatibility Level -> .NET Framework).
- Create an instance of the ModbusRTU class, specifying the serial port name and baud rate.
- Call the
Connect()
method to establish a connection with the device. - Perform the necessary read or write operations on registers, coils, etc., using the appropriate methods of the ModbusRTU class.
- After completing work with the device, call the
Disconnect()
method to close the connection.
using UnityEngine;
using System;
public class Example : MonoBehaviour
{
private ModbusRTU _modbus;
void Start()
{
_modbus = new ModbusRTU("COM1", 9600);
if(_modbus.Connect())
{
// DO SOMETHING IF CONNECTED
}
else
{
// DO SOMETHING IF CONNECTION FAILED
}
}
}
Establishes a connection with the device. Returns true if the connection is established successfully, and false otherwise.
Closes the connection with the device.
Reads the value of a single register.
Asynchronously reads the value of a single register.
Writes a value to a single register.
Asynchronously writes a value to a single register.
Writes values to multiple consecutive registers.
Asynchronously writes values to multiple consecutive registers.
Reads the values of coils.
Asynchronously reads the values of coils.
Writes a value to a single coil.
Asynchronously writes a value to a single coil.
Writes values to multiple coils.
Asynchronously writes values to multiple coils.
Reads the exception status (only for Modbus RTU).
Asynchronously reads the exception status (only for Modbus RTU).
Performs a diagnostic echo request.
Asynchronously performs a diagnostic echo request.
This library is distributed under the MIT license. See the LICENSE file for more details.