coassoftwaresystems / delphi-modbus

Delphi ModbusTCP components
MIT License
119 stars 64 forks source link

Read coils and read digital inputs on Modbus Server return only 125 registers #23

Open lucapretti opened 7 years ago

lucapretti commented 7 years ago

Read coils and Read Discrete Inputs should be able to return up to 2000 registers with one request. idModbusServer only returns the first 125 registers. I beleave that the problem is in the InternalReadCoils and in the InternalReadInputBits methods. Both methods at the end have this statement: for i := 0 to (Count - 1) do begin if CoilData[i] then Data[i] := 1; end; CoilData is of type TModCoilData (array of 2000 ByteBool) but Data is of type TModRegisterData (array of 125 words). So when i>125 the statement fails.

Daijobou commented 4 years ago

Shorted from: https://github.com/coassoftwaresystems/delphi-modbus/blob/develop/source/IdModbusClient.pas#L207

  case AModBusFunction of
    mbfReadCoils,
    mbfReadInputBits:
      begin
        BlockLength := ABlockLength;
      { Don't exceed max length }
        if (BlockLength > 2000) then
          BlockLength := 2000;
      end;
    mbfReadHoldingRegs,
    mbfReadInputRegs:
      begin
        BlockLength := ABlockLength;
        if (BlockLength > 125) then
          BlockLength := 125; { Don't exceed max length }
      end;
    mbfWriteCoils:
      begin
        BlockLength := ABlockLength;
      { Don't exceed max length }
        if (BlockLength > 1968) then
          BlockLength := 1968;
      end;
    mbfWriteRegs:
      begin
        BlockLength := ABlockLength;
      { Don't exceed max length }
        if (BlockLength > 120) then
          BlockLength := 120;
      end;
  end;

https://en.wikipedia.org/wiki/Modbus

Function code 1 (read coils) and function code 2 (read discrete inputs)

Because the byte count returned in the reply message is only 8 bits wide and the protocol overhead is 5 bytes, a maximum of 2008 (251 x 8) discrete inputs or coils can be read at once.

Function code 4 (read input registers) and function code 3 (read holding registers)

Because the number of bytes for register values is 8-bit wide and maximum modbus message size is 256 bytes, only 125 registers for Modbus RTU and 123 registers for Modbus TCP can be read at once.

Can you provide your code or point to the source where the error should be?