I'm trying to decipher some code I inherited that isn't working. Here's what I'm trying to do...
A 10 character string value is entered at an HMI. This string is sent down to a PLC where it is copied over to an integer file. This integer file is read by the Delphi app via an OPC server. The Delphi app then is supposed to convert the integer back to the string so that it can be searched. Hopefully that explains what it is supposed to do.
Here's the code for reading the data from the OPC server. ArrayToStringSwap is a custom function.
The function ArrayToStringSwap is done below.
The problem is that all of the characters from the original HMI string come back except for one, and the characters are all scrambled as far as the order goes.
EXAMPLE: If I enter 'teststring' at my HMI, it will be stored in the PLC integer files N50:79 as 29797 (decimal equivalent of ASCII 'te'), N50:81 as 29556 (decimal equivalent of ASCII 'st'), etc. When I get my result back I get 'etttsisgr', which is all of the characters except the 'n'.
I know that 'right' way to do this is to probably just read the string from the HMI without all of the converting back and forth, but if I do that I'll have to rewrite the data collection portion of the whole program.
This is something that everyone assumed worked properly until I started looking at it today. If anyone has any ideas, please let me know.
Joe
A 10 character string value is entered at an HMI. This string is sent down to a PLC where it is copied over to an integer file. This integer file is read by the Delphi app via an OPC server. The Delphi app then is supposed to convert the integer back to the string so that it can be searched. Hopefully that explains what it is supposed to do.
Here's the code for reading the data from the OPC server. ArrayToStringSwap is a custom function.
Code:
var
I : integer;
Num : array[0..4] of word;
for I := 0 to 4 do
Num[I] := GetRegValue(I + 79);
IDStr := ArrayToStringSwap(Num,10);
The function ArrayToStringSwap is done below.
Code:
function TDevice.ArrayToStringSwap(var A;Size : integer) : String;
type
Stype = record
SZ : byte;
SA : array[1..255] of char;
end;
Wtype = record
SZ : byte;
WA : array[1..128] of word;
end;
var
S : String[20];
ST : Stype absolute S;
WT : Wtype absolute S;
I, Temp : word;
Len : integer;
begin
if size > 20 then size := 20;
Result := '';
ST.SZ := Size;
move(A,ST.SA,Size);
Len := Size div 2;
for I := 1 to Len do begin
Temp := Swap(WT.WA[I]);
WT.WA[I] := Temp;
end;
Result := TrimLeft(S);
end;
The problem is that all of the characters from the original HMI string come back except for one, and the characters are all scrambled as far as the order goes.
EXAMPLE: If I enter 'teststring' at my HMI, it will be stored in the PLC integer files N50:79 as 29797 (decimal equivalent of ASCII 'te'), N50:81 as 29556 (decimal equivalent of ASCII 'st'), etc. When I get my result back I get 'etttsisgr', which is all of the characters except the 'n'.
I know that 'right' way to do this is to probably just read the string from the HMI without all of the converting back and forth, but if I do that I'll have to rewrite the data collection portion of the whole program.
This is something that everyone assumed worked properly until I started looking at it today. If anyone has any ideas, please let me know.
Joe