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


本文整理自网络,侵删。

 一、与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 计算运行耗时的方法2

Delphi气泡提示

Delphi 从indy9升级到indy10时idtcpserver的变化

Delphi findwindow的一些用法

Delphi 极速字符串替换函数

Delphi ansi字符串转unicode编码

Delphi ansiendstext 用法之一(路径结尾自动加\)

Delphi webbroker iis cgi 的配置

Delphi 如何设置文件属性

Delphi-adoquery查询、插入、删除、修改

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...