Skip to content

bug(Modbus): 使用IModbusTcpClient.WriteMultipleRegistersAsync发送1个寄存器时生成的数据不正确 #8475

Description

@water1983

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

使用modbusTcp.WriteMultipleRegistersAsync(1, 0, new ushort[] { 6 });发送只有一个寄存器的内容时,生成的字节数据不正确,PLC返回错误3(错误的数据)。

见图片,红色箭头和上面的蓝色箭头是相同的数据,BB生成的和调试助手生成的不同
对比调试助手生成的内容,应该是BB使用了FC16功能码但数据包的格式却是FC6的。

如果发送多个寄存器modbusTcp.WriteMultipleRegistersAsync(1, 0, new ushort[] { 6, 8 });
则正常
见图片,绿色箭头和下面的蓝色箭头是相同的数据,BB生成的和调试助手生成相同

Image

我查看了modbus的源码
https://github.com/LongbowEnterprise/Longbow.Utility2026/blob/main/src/Longbow.Modbus/Utility/MessageBuilder.cs#L68
这里拼接数据时似乎没有考虑FC06
https://github.com/LongbowEnterprise/Longbow.Utility2026/blob/main/src/Longbow.Modbus/Interfaces/ModbusClientBase.cs#L45
和FC16
https://github.com/LongbowEnterprise/Longbow.Utility2026/blob/main/src/Longbow.Modbus/Interfaces/ModbusClientBase.cs#L58
功能码数据结构不同的问题
当调用FC16(WriteMultipleRegistersAsync)但数据只有1个寄存器时,却按照FC06的格式拼接的数据

Expected Behavior

modbusTcp.WriteMultipleRegistersAsync能够正常发送只有1个寄存器内容的数据

Interactive render mode

Interactive Server (Interactive server-side rendering (interactive SSR) using Blazor Server)

Steps To Reproduce

压缩包为啥上传不了,请看一下描述,如果需要工程请回复。

核心代码很简单

private void SendFC16()
    {
        var modbusTcp = ModbusFactory.GetOrCreateTcpMaster();

        modbusTcp.ConnectAsync("192.168.1.97", 502);
        modbusTcp.WriteMultipleRegistersAsync(1, 0, new ushort[] { 6 });
    }

private void SendFC16two()
    {
        var modbusTcp = ModbusFactory.GetOrCreateTcpMaster();

        modbusTcp.ConnectAsync("192.168.1.97", 502);
        modbusTcp.WriteMultipleRegistersAsync(1, 0, new ushort[] { 6, 8 });
    }

Exceptions (if any)

No response

.NET Version

NET10.0

Anything else?

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions