ReadHoldingRegisters hangs · Issue #199 · NModbus/NModbus · GitHub
Skip to content

ReadHoldingRegisters hangs #199

Description

@JuandreG

Hello.

I have a device that supports modbus tcp connections that is currently operating. Now when I attempt get data from the device using this:

`
try
{
using (var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
var ip = IPAddress.Parse("/IP of the device/");
var address = new IPEndPoint(ip, /device port/);

    sock.Connect(address);

    if (sock.Connected)
    {
        Console.WriteLine($"Connected to {ip}:{address.Port}");

        var factory = new ModbusFactory();

        IModbusMaster master = factory.CreateMaster(sock);

        byte id = 1;
        ushort start = 1;
        ushort quantityOfRegisters = 10;

        Console.WriteLine($"Reading registers starting from {start}, quantity: {quantityOfRegisters}");

        var result = master.ReadHoldingRegisters(id, start, quantityOfRegisters);

        if (result != null && result.Length > 0)
        {
            for (int i = 0; i < result.Length; i++)
            {
                Console.WriteLine($"Register {(start + i)}: {result[i]}");
            }
        }
        else
        {
            Console.WriteLine("No valid data received.");
        }
    }
}

}
catch (SocketException ex)
{
Console.WriteLine($"Socket error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
`

When it reaches the master.ReadHoldingRegisters(id, start, quantityOfRegisters); it just hangs with no response.

I tested the credentials I supply with a Modbus tester and there i do get the values I am expecting. This is the tester I used:

https://www.powerhud.com/en/free-modbus-tester/

And this is the result:

image

Anyone have any ideas as to what I am doing wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions