coassoftwaresystems / delphi-modbus

Delphi ModbusTCP components
114 stars 61 forks source link

TIdModBusServer: WriteRegisters gets triggered multiple times #52

Open BobanSpasic opened 2 years ago

BobanSpasic commented 2 years ago

Hi, I have converted your Delphi Slave demo (Indy10) to Lazarus (Version 2.2.0 64bit on Windows 10). I am sending 32-bit values from PLC to TIdModBusServer by using 16-PresetMultipleRegisters method. For the sake of debugging, I have modified the code of your demo as follows (added logging to mmoErrorLog):

procedure TfrmMain.msrPLCWriteRegisters(const Sender: TIdContext;
  const RegNr, Count: integer; const Data: TModRegisterData;
  const RequestBuffer: TModBusRequestBuffer; var ErrorCode: byte);
var
  i: integer;
begin
  mmoErrorLog.Lines.Add('Message register = ' + IntToStr(RegNr) +
    ';  Message length= ' + IntToStr(Count));
  for i := 0 to (Count - 1) do
  begin
    SetRegisterValue(RegNr + i, Data[i]);
    mmoErrorLog.Lines.Add('Message value ' + IntToStr(i) + ' = ' + IntToStr(Data[i]));
  end;
end;

I sent the values 3 and 7993, and this is what I get in the ErrorLog:

Message register = 11;  Message length= 2
Message value 0 = 3
Message value 1 = 7993
Message register = 11;  Message length= 2
Message value 0 = 0
Message value 1 = 7993

As you can see, the event gets triggered twice, where the second one contains a wrong value (0 instead of 3). Any solution to this problem?

JensMertelmeyer commented 2 years ago
BobanSpasic commented 2 years ago

Hi Jens, you may close the issue - the PLC is sending weird data. I apologize for the inconvenience.

To explain - the PLC is sending data even if the output registers are not ready, so it is sending "something" that is in the register at the moment. The second problem is the repetition of sending - the PLC sends the Modbus message 1-3 times in one shot, and it happens that the registers do not contain valid data for every repetition.

JensMertelmeyer commented 2 years ago

Yes, I also once had a problem with a client whose PLCs was acting like this. It's good if you're able to tell if you can trust the data you receive.

PS: I cannot close the issue, I am not the repo owner, just a user like you 😉