rossmann-engineering / EasyModbusTCP.NET

Modbus TCP, Modbus UDP and Modbus RTU client/server library for .NET implementations
947 stars 412 forks source link

Does not handle writes to 'invalid' holding registers on ModbusServer #112

Open Mjacq79 opened 3 months ago

Mjacq79 commented 3 months ago

Based on the server sample, it seems that the intention is for a server application to define the maximum number of holding registers (65535). When a client writes holding registers, the ModbusServer will check that the write is within the bounds of the maximum possible holding register but there is no check to ensure that the write is within the defined holding registers.

I have a bit of experience working with Modbus Servers in an industrial setting, and I do not think I have ever seen a server that supports writes to all possible holding registers. Typically, a write to a register beyond what the server supports will result in a modbus exception being returned to the client.

If I use EasyModbus server to define 100 holding registers (0-100), any write to a holding register outside of that range will cause an IndexOutOfRange exception at ModbusServer.cs:WriteMultipleRegisters(). Instead of a Modbus Exception returned to the client, it just results in the TCP socket being closed.

As far as I am aware, the Modbus protocol allows servers to reject requests to specific register ranges, but I see no way to do that with the EasyModbus library. Ideally there would be some mechanism to define which holding registers within the address space are valid and which are invalid (allowing for distinct chunks of addresses to be used) but at the very least, I think that writes outside the number of registers defined by the holdingRegisters array should return the proper Modbus Exception instead of crashing.

-- As it stands, I cannot find a way to define a server that rejects writes to some holding registers. Either I define all registers, and the ModbusServer accepts a client request to write any of the registers, or I define a subset of the full address space and the ModbusServer fails to handle requests outside of the defined space.