Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lucapretti opened this issue Oct 4, 2017 · 1 comment

Comments

@lucapretti
Copy link

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
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants