您的位置:首页 > 编程语言 > Delphi

DELPHI XE 与PLC通讯(INTCPSERVER 二进制)

2019-04-10 08:57 2126 查看

一、与PLC通讯采用TCPSERVER方式

二、配合PLC发送二进制数据。

[code]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;

三、用到的几个小函数

[code]// 十进制 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;

四、总结

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐