ich habe eine Funktion geschrieben (siehe unten) um Daten aus einer Steuerung mittels Modbus TCP zu lesen.
Insgesamt funktioniert die Funktion, außer dass die Bytes in den Words des Arrays "MyTaw" die falsche Reihenfolge haben.
Die Umwandlung geschieht mit der Deklaration "array of byte absolute MyTaw".
Leider treffen die Bytes nicht in der Reihenfolge ein wie sie im array of word abgelegt werden sollen. (Byteorder?)
Natürlich kann ich die Bytes einzeln in die Words schreiben, ungefähr so:
Code: Alles auswählen
For I := 0 to ...
MyTaw[I] := abAntwort[I*2+1] shl 8 + abAntwort[I*2]
Code: Alles auswählen
type
Taw = array of word;
....
function ReadHoldingRegistersTCP(sIPAdresse, sPort: String; wStartAdr, wCount:word; var MyTaw:Taw): Boolean;
var
con : TTCPBlockSocket;
abFrage : array [0..11] of byte;
abAntwort : array of byte absolute MyTaw;
begin
SetLength(abAntwort, wCount*2+10);
abFrage[0] := 0; // Transaction Identifier
abFrage[1] := 1; // Transaction Identifier
abFrage[2] := 0; // Protocoll Identifier: immer 00 00 Modbus-Protokoll
abFrage[3] := 0; // Protocoll Identifier: immer 00 00 Modbus-Protokoll
abFrage[4] := 0; // Length Hi
abFrage[5] := 6; // Length Low
abFrage[6] := 1; // Unit Identifier
abFrage[7] := 3; // Function code
abFrage[8] := hi(wStartAdr); // Startaddresse Hi Byte
abFrage[9] := lo(wStartAdr); // Startaddresse Low Byte
abFrage[10] := hi(wCount); // Anzahl Daten Hi Byte
abFrage[11] := lo(wCount); // Anzahl Daten Low Byte
con:=TTCPBlockSocket.Create;
try
con.Connect(sIPAdresse,sPort);
con.SendBuffer(@abFrage,12);
Result := (con.RecvBufferEx(@abAntwort[0],Length(abAntwort),150) = wCount*2+9);
abAntwort := copy(abAntwort,9,100);
SetLength(MyTaw,wCount);
finally
con.free;
end;
end;