Comments (2)
On SendResponse
, i prefer check ValidRequest
and SendError
to avoid an exception.
{$IFDEF DMB_INDY10}
procedure TIdModBusServer.SendResponse(const AContext: TIdContext;
const ReceiveBuffer: TModBusRequestBuffer; const Data: TModRegisterData);
{$ELSE}
procedure TIdModBusServer.SendResponse(const AThread: TIdPeerThread;
const ReceiveBuffer: TModBusRequestBuffer; const Data: TModRegisterData);
{$ENDIF}
var
SendBuffer: TModBusResponseBuffer;
L: Integer;
ValidRequest : Boolean;
{$IFDEF DMB_INDY10}
Buffer: TIdBytes;
{$ENDIF}
begin
if Active then
begin
{Check Valid }
ValidRequest := false;
FillChar(SendBuffer, SizeOf(SendBuffer), 0);
SendBuffer.Header.TransactionID := ReceiveBuffer.Header.TransactionID;
SendBuffer.Header.ProtocolID := ReceiveBuffer.Header.ProtocolID;
SendBuffer.Header.UnitID := ReceiveBuffer.Header.UnitID;
SendBuffer.FunctionCode := ReceiveBuffer.FunctionCode;
SendBuffer.Header.RecLength := Swap16(0);
case ReceiveBuffer.FunctionCode of
mbfReadCoils,
mbfReadInputBits:
begin
L := Swap16(Word((@ReceiveBuffer.MBPData[2])^));
if (L > 0) and (L <= MaxCoils) then
begin
SendBuffer.MBPData[0] := Byte((L + 7) div 8);
PutCoilsIntoBuffer(@SendBuffer.MBPData[1], L, Data);
SendBuffer.Header.RecLength := Swap16(3 + SendBuffer.MBPData[0]);
ValidRequest := true;
end;
end;
mbfReadInputRegs,
mbfReadHoldingRegs:
begin
L := Swap16(Word((@ReceiveBuffer.MBPData[2])^));
if (L > 0) and (L <= MaxBlockLength) then
begin
SendBuffer.MBPData[0] := Byte(L shl 1);
PutRegistersIntoBuffer(@SendBuffer.MBPData[1], L, Data);
SendBuffer.Header.RecLength := Swap16(3 + SendBuffer.MBPData[0]);
ValidRequest := true;
end;
end;
else
begin
SendBuffer.MBPData[0] := ReceiveBuffer.MBPData[0];
SendBuffer.MBPData[1] := ReceiveBuffer.MBPData[1];
SendBuffer.MBPData[2] := ReceiveBuffer.MBPData[2];
SendBuffer.MBPData[3] := ReceiveBuffer.MBPData[3];
SendBuffer.Header.RecLength := Swap16(6);
ValidRequest := true;
end;
end;
{Send buffer if Request is Valid}
if ValidRequest then
begin
{$IFDEF DMB_INDY10}
Buffer := RawToBytes(SendBuffer, Swap16(SendBuffer.Header.RecLength) + 6);
AContext.Connection.Socket.WriteDirect(Buffer);
if FLogEnabled then
LogResponseBuffer(AContext, SendBuffer, Swap16(SendBuffer.Header.RecLength) + 6);
{$ELSE}
AThread.Connection.Socket.Send(SendBuffer, Swap16(SendBuffer.Header.RecLength) + 6);
if FLogEnabled then
LogResponseBuffer(AThread, SendBuffer, Swap16(SendBuffer.Header.RecLength) + 6);
{$ENDIF}
end
else
begin
{Send error for invalid request}
{$IFDEF DMB_INDY10}
SendError(AContext, mbeServerFailure, ReceiveBuffer);
{$ELSE}
SendError(AThread, mbeServerFailure, ReceiveBuffer);
{$ENDIF}
exit;
end;
end;
end;
from delphi-modbus.
Changes look good to me. I have merged them into the develop branch.
from delphi-modbus.
Related Issues (20)
- WriteCoils (WriteCoils sss) Problem
- InputBuffer noty cleared after timeout
- Compatibility with Delphi 10.4 HOT 2
- Incorrect platform identifier in "ModBusPlatforms.inc" for Delphi XE5
- ModBusClient.ReadHoldingRegisters - Amount HOT 1
- ModBusMaster Demo reads values fine but gives an error when writing values
- ModBusClient - cycle read ? HOT 1
- Modbus client for service application HOT 1
- Runtime error 216 HOT 2
- XE11 Alexandria HOT 1
- TIdModBusServer: WriteRegisters gets triggered multiple times HOT 3
- The register number to be read is inconsistent with the PLC register number actually read HOT 2
- Read and write registers 4x HOT 1
- TIdModBusClient.Connect don`t work in manual mode
- Cannot read the Shark270
- Fix issues on poor network and poor Modbus controllers
- TIdModBusServer Connection Status public property
- How to check for errors
- ALGUEM AJUDAR INSTALAR XE7 HOT 1
- Problem install with Lazarus 2.2.6 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from delphi-modbus.