function TCACClient.ReplaceSendBuffer(s: string; IsSendOrReceive: Boolean = True): string;
const
CR = char($0D);
DLE = char($10);
ESC = char($1B);
var
i: Integer;
DestStr: string;
begin
//CR »»³É DLE ESC
//DLE »»³ÉÁ½¸ö DLE
DestStr := '';
if IsSendOrReceive then begin
for i := 1 to Length(s) do begin
if s[i] = CR then
DestStr := DestStr + DLE + ESC
else if s[i] = DLE then
DestStr := DestStr + DLE + DLE
else
DestStr := DestStr + s[i];
end;
end else begin
//
i := 1;
while i <= Length(s) do begin
if (i < Length(s)) and (s[i] = DLE) and
((s[i + 1] = DLE) or (s[i + 1] = ESC)) then begin
if (s[i + 1] = DLE) then begin
DestStr := DestStr + DLE;
inc(i);
inc(i);
end else if (s[i + 1] = ESC) then begin
DestStr := DestStr + CR;
inc(i);
inc(i);
end;
end else begin
DestStr := DestStr + s[i];
inc(i)
end;
end;
end;
Result := DestStr;
end;