You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Modbus.Net v1.4.3 TcpClient每第6次轮询必失败问题排查

Modbus.Net v1.4.3 客户端轮询异常问题

现象描述

使用Modbus.Net v1.4.3实现客户端并定期轮询数据时,出现周期性异常:每第6次轮询返回失败结果,具体返回值如下:

Data = null
Success = false
ErrorCode = 0
ErrorMsg  = ""

其余轮询均正常执行。

轮询核心代码

轮询仅通过以下代码执行:

// 从设备获取数据
var result = await _machine.GetMachineMethods<IMachineMethodDatas>().GetDatasAsync(MachineDataType.CommunicationTag);

Modbus客户端实现代码

using Microsoft.Extensions.Logging;
using Modbus.Net;
using Modbus.Net.Modbus;

/// <summary>
/// 基于Modbus.Net的Modbus客户端
/// </summary>
internal class ModbusClient
{
    public ModbusClient(string ip, byte id, List<AddressUnit<int, int, int>> addressConfig)
    {
        // 使用地址配置初始化设备对象
        _machine = new ModbusMachine<int, int>(id, ModbusType.Tcp, ip, addressConfig, true, id, 0, 2);
    }

    /// <summary>
    /// Modbus.Net设备实例
    /// </summary>
    private IMachineProperty<int> _machine;

    /// <summary>
    /// 轮询配置的地址寄存器
    /// </summary>
    /// <returns>ReturnUnit<double>集合</returns>
    /// <exception cref="IOException">轮询失败时抛出</exception>
    public async Task<ICollection<ReturnUnit<double>>> PollReturnUnits()
    {
        // 从设备获取数据
        var result = await _machine.GetMachineMethods<IMachineMethodDatas>().GetDatasAsync(MachineDataType.CommunicationTag);

        // 错误处理
        if (result.Datas is null || !result.IsSuccess)
        {
            throw new IOException($"code:{result.ErrorCode}, msg:{result.ErrorMsg}");
        }
        // 返回地址单元数据
        return result.Datas.Values;
    }
}

补充说明

  • 不要依赖该包的官方文档,内容已过时,建议直接下载包并结合上述代码分析。
  • GetDatasAsync用于读取配置的寄存器,返回一个名为ReturnStruct的结构体,包含Datas、Success、ErrorMsg、ErrorCode属性(部分属性存在复数命名错误)。

内容的提问来源于stack exchange,提问作者stadla_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 20:46:05