本文整理自网络,侵删。
一、与PLC通讯采用TCPSERVER方式
二、配合PLC发送二进制数据。
var
i: integer;
RecClient: TIdContext;
buf: TIdBytes;
begin
SetLength(buf, 2);
buf[0] := ord('W');
buf[1] := BintoInt(Edit7.Text);
with c_tcp_list_all.LockList do
try
for i := 0 to count - 1 do
begin
RecClient := items[i];
if (RecClient.Binding.PeerPort = strtoint(Edit2.Text)) and
(RecClient.Binding.PeerIP = Edit1.Text) then
begin
RecClient := TIdContext(items[i]);
RecClient.Connection.Socket.write(buf);
end;
end;
finally
c_tcp_list_all.UnlockList;
end;
三、用到的几个小函数
// 十进制 to 二进制
function IntToBin(Value: LongInt; Size: integer): String;
var
i: integer;
begin
Result := '';
for i := Size - 1 downto 0 do
begin
if Value and (1 shl i) <> 0 then
begin
Result := Result + '1';
end
else
begin
Result := Result + '0';
end;
end;
end;
// 二进制 to 十进制
function BintoInt(Value: String): LongInt;
var
i, Size: integer;
begin
Result := 0;
Size := Length(Value);
for i := Size downto 1 do
begin
if Copy(Value, i, 1) = '1' then
Result := Result + (1 shl (Size - i));
end;
end;
function floatBintoInt(Value: String): real;
var
i, Size: integer;
begin
Result := 0;
Size := Length(Value);
for i := Size downto 1 do
begin
if Copy(Value, i, 1) = '1' then
Result := Result + 1 / (1 shl i);
end;
end;
四、总结
---------------------
作者:以后换名字
原文:https://blog.csdn.net/weixin_44387646/article/details/89173215
相关阅读 >>
Delphi 从indy9升级到indy10时idtcpserver的变化
Delphi ansiendstext 用法之一(路径结尾自动加\)
更多相关阅读请进入《Delphi》频道 >>