Smart.Modbus 1.0.1

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Smart.Modbus --version 1.0.1
                    
NuGet\Install-Package Smart.Modbus -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Smart.Modbus" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Smart.Modbus" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Smart.Modbus" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Smart.Modbus --version 1.0.1
                    
#r "nuget: Smart.Modbus, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Smart.Modbus@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Smart.Modbus&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Smart.Modbus&version=1.0.1
                    
Install as a Cake Tool

Smart.Modbus

NuGet
Smart.Modbus 是一个支持 .NET 8/9 的Modbus协议封装库,支持Modbus RTU、Modbus TCP协议,提供了简单易用的API接口,不涉及数据通信,只提供组帧、校验和数据解析功能。

使用示例

csharp

// 创建对象
private readonly IModbusHandler _modbusTcp = new ModbusTcpHandler(1);

// 或者使用如下工厂方法创建Modbus协议处理器对象
private readonly IModbusHandler _modbusTcp = ModbusFactory.Create(ModbusProtocol.ModbusTcp, 1);

public void ReadCoils()
{
    _logger.LogDebug("ModbusService ReadCoils,{}", DateTime.Now);
    // 读取线圈
    byte[] data = _modbusTcp.BuildReadCoilsRequest(0, 10);
    if (!_client.Send(data, data.Length))
    {
        _logger.LogError("Send Failed,Data:{}", SmartConvert.ByteArrayToHexString(data, true));
    }
}

public void ReadDiscreteInputs()
{
    _logger.LogDebug("ModbusService ReadDiscreteInputs,{}", DateTime.Now);
    // 读取离散输入
    byte[] data = _modbusTcp.BuildReadDiscreteInputsRequest(0, 10);
    if (!_client.Send(data, data.Length))
    {
        _logger.LogError("Send Failed,Data:{}", SmartConvert.ByteArrayToHexString(data, true));
    }
}

public void ReadHoldingRegisters()
{
    _logger.LogDebug("ModbusService ReadHoldingRegisters,{}", DateTime.Now);
    // 读取保持寄存器
    byte[] data = _modbusTcp.BuildReadHoldingRegistersRequest(0, 10);
    if (!_client.Send(data, data.Length))
    {
        _logger.LogError("Send Failed,Data:{}", SmartConvert.ByteArrayToHexString(data, true));
    }
}

public void ReadInputRegisters()
{
    _logger.LogDebug("ModbusService ReadInputRegisters,{}", DateTime.Now);
    // 读取输入寄存器
    byte[] data = _modbusTcp.BuildReadInputRegistersRequest(0, 10);
    if (!_client.Send(data, data.Length))
    {
        _logger.LogError("Send Failed,Data:{}", SmartConvert.ByteArrayToHexString(data, true));
    }
}

public void WriteSingleCoil()
{
    _logger.LogDebug("ModbusService WriteSingleCoil,{}", DateTime.Now);
    // 写单个线圈
    byte[] data = _modbusTcp.BuildWriteSingleCoilRequest(0, Random.Shared.Next() % 2 == 0);
    if (!_client.Send(data, data.Length))
    {
        _logger.LogError("Send Failed,Data:{}", SmartConvert.ByteArrayToHexString(data, true));
    }
}

public void WriteSingleRegister()
{
    _logger.LogDebug("ModbusService WriteSingleRegister,{}", DateTime.Now);
    // 写单个保持寄存器
    byte[] data = _modbusTcp.BuildWriteSingleRegisterRequest(0, (ushort)(Random.Shared.Next() % 65535));
    if (!_client.Send(data, data.Length))
    {
        _logger.LogError("Send Failed,Data:{}", SmartConvert.ByteArrayToHexString(data, true));
    }
}

public void WriteMultipleCoils()
{
    _logger.LogDebug("ModbusService WriteMultipleCoils,{}", DateTime.Now);
    // 写线圈
    List<bool> bools = new List<bool>();
    for (int i = 0; i < 10; i++)
    {
        bools.Add(i % 2 == 1);
    }
    byte[] data = _modbusTcp.BuildWriteMultipleCoilsRequest(0, bools.ToArray());
    if (!_client.Send(data, data.Length))
    {
        _logger.LogError("Send Failed,Data:{}", SmartConvert.ByteArrayToHexString(data, true));
    }
}

public void WriteMultipleRegisters()
{
    _logger.LogDebug("ModbusService WriteMultipleRegisters,{}", DateTime.Now);
    // 写保持寄存器
    List<ushort> ushorts = new List<ushort>();
    for (int i = 0; i < 10; i++)
    {
        ushorts.Add((ushort)(Random.Shared.Next() % 65535 + i));
    }

    byte[] data = _modbusTcp.BuildWriteMultipleRegistersRequest(0, ushorts.ToArray());
    if (!_client.Send(data, data.Length))
    {
        _logger.LogError("Send Failed,Data:{}", SmartConvert.ByteArrayToHexString(data, true));
    }
}

// 处理响应
var (Valid, Fail) = _modbusTcp.ValidateResponse(response);
if (Valid)
{
    FunctionCode function = _modbusTcp.DetectionFunctionCode(response);
    switch (function)
    {
        case FunctionCode.ReadHoldingRegisters:
        case FunctionCode.ReadInputRegisters:
            var registers = _modbusTcp.ParseRegistersResponse(response);
            _logger.LogInformation("{} Success,Registers:{}", function, string.Join(",", registers));
            break;
        case FunctionCode.ReadCoils:
        case FunctionCode.ReadDiscreteInputs:
            var coils = _modbusTcp.ParseCoilsResponse(response, 10);
            _logger.LogInformation("{} Success,Coils:{}", function, string.Join(",", coils));
            break;
        case FunctionCode.WriteSingleRegister:
        case FunctionCode.WriteSingleCoil:
        case FunctionCode.WriteMultipleRegisters:
        case FunctionCode.WriteMultipleCoils:
            _logger.LogInformation("{} Success,Response:{}", function, SmartConvert.ByteArrayToHexString(response, true));
            break;
        default:
            _logger.LogError("Unknown FunctionCode:{},response:{}", function, SmartConvert.ByteArrayToHexString(response, true));
            break;
    }
}
else
{
    _logger.LogError("ValidateResponse Failed,Reason:{},response:{}", Fail, SmartConvert.ByteArrayToHexString(response, true));
}

Developed by zenglei

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0-beta.40 228 11/17/2025
2.0.0-beta.39 136 10/28/2025
2.0.0-beta.38 128 10/22/2025
2.0.0-beta.37 129 10/22/2025
2.0.0-beta.36 127 10/22/2025
2.0.0-beta.35 125 10/22/2025
2.0.0-beta.34 129 10/21/2025
2.0.0-beta.33 123 10/21/2025
2.0.0-beta.32 126 10/21/2025
2.0.0-beta.31 112 10/17/2025
2.0.0-beta.30 122 10/16/2025
2.0.0-beta.29 126 10/15/2025
2.0.0-beta.28 129 10/15/2025
2.0.0-beta.27 128 10/14/2025
2.0.0-beta.26 129 10/14/2025
2.0.0-beta.25 128 10/14/2025
2.0.0-beta.24 125 10/14/2025
2.0.0-beta.23 133 10/13/2025
2.0.0-beta.22 136 10/13/2025
2.0.0-beta.21 110 10/12/2025
2.0.0-beta.20 114 10/12/2025
2.0.0-beta.19 129 10/8/2025
2.0.0-beta.18 135 10/1/2025
2.0.0-beta.17 130 10/1/2025
2.0.0-beta.16 129 10/1/2025
2.0.0-beta.15 132 9/30/2025
2.0.0-beta.14 130 9/29/2025
2.0.0-beta.13 155 9/4/2025
2.0.0-beta.12 162 8/31/2025
2.0.0-beta.11 177 8/30/2025
2.0.0-beta.10 88 8/24/2025
2.0.0-beta.9 54 8/23/2025
2.0.0-beta.8 115 8/17/2025
2.0.0-beta.7 108 8/17/2025
2.0.0-beta.6 110 8/17/2025
2.0.0-beta.5 106 8/17/2025
2.0.0-beta.4 60 8/16/2025
2.0.0-beta.3 139 8/10/2025
2.0.0-beta.2 138 7/28/2025
2.0.0-beta.1 227 7/26/2025
1.1.0-beta.11 135 7/17/2025
1.1.0-beta.10 135 7/16/2025
1.1.0-beta.9 138 7/15/2025
1.1.0-beta.8 142 7/15/2025
1.1.0-beta.7 134 7/14/2025
1.1.0-beta.6 115 7/11/2025
1.1.0-beta.5 140 7/7/2025
1.1.0-beta.4 139 7/2/2025
1.1.0-beta.3 91 6/28/2025
1.1.0-beta.1 133 6/26/2025
1.0.1 203 6/7/2025
1.0.0 188 6/1/2025
1.0.0-rc.7 153 5/26/2025
1.0.0-rc.6 159 5/7/2025
1.0.0-rc.4 256 4/29/2025 1.0.0-rc.4 is deprecated because it is no longer maintained.
1.0.0-rc.3 264 4/24/2025 1.0.0-rc.3 is deprecated because it is no longer maintained.
1.0.0-rc.2 249 4/23/2025 1.0.0-rc.2 is deprecated because it is no longer maintained.
1.0.0-rc.1 248 4/22/2025 1.0.0-rc.1 is deprecated because it is no longer maintained.