【例子介绍】
TCP/IP通讯威纶通HMI,可读取、设置任意地址。
【相关图片】
【源码结构】
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp, RzButton, Mask, RzEdit, RzCmboBx, ExtCtrls,
RzRadChk;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
Edit1: TRzEdit;
Button1: TRzButton;
Edit2: TRzEdit;
Label1: TLabel;
RzEdit1: TRzEdit;
Label2: TLabel;
Label3: TLabel;
RzEdit2: TRzEdit;
RzEdit3: TRzEdit;
RzComboBox1: TRzComboBox;
Label4: TLabel;
RzEdit4: TRzEdit;
Label5: TLabel;
RzComboBox2: TRzComboBox;
Label6: TLabel;
RzButton2: TRzButton;
Shape1: TShape;
RzCheckBox1: TRzCheckBox;
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure RzButton2Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Connecting(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//function StrOfHex(AStr: string): string;
//var
// I, len: Integer;
// tmps: string;
//begin
// len := length(AStr);
// Result := '';
// for I := 1 to Trunc(len / 2) do
// begin
// tmps := Copy(AStr, (I - 1) * 2 1, 2);
// Result := Result Chr(StrToIntdef('$' tmps, 0));
// end;
//end;
Function HexToStr(HexStr:String):string;
var
i,tmplen:Integer;
tmpstr:string;
begin
Result:='';
tmplen:=Length(HexStr) div 2;
for i:=1 to tmplen do
begin
tmpstr:=Copy(HexStr,2*i-1,2);
Result:=Result Chr(StrToInt('$' tmpstr));
end;
end;
Function BuildTCPStr(HexStr:String):string;
var
tmplen:Integer;
begin
tmplen:=Length(HexStr) div 2;
Result:=IntToHex(tmplen,12) HexStr;
end;
Function ToSendStr(HexStr:String):string;
begin
HexStr:=BuildTCPStr(HexStr);
Result:=HexToStr(HexStr);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
tmpHMICMD,tmpTag:string;
tmpReadNum:Integer;
begin
if RzCheckBox1.Checked then
begin
tmpHMICMD:= RzComboBox1.Value; //写标志
tmpHMICMD:=tmpHMICMD InttoHex(StrToIntDef(RzEdit4.Text,0),4);
if RzComboBox1.itemIndex=0 then //位
begin
if RzComboBox2.Text='1' then
tmpHMICMD:=tmpHMICMD 'FF00'
else
tmpHMICMD:=tmpHMICMD '0000';
end
else
begin
tmpHMICMD:=tmpHMICMD InttoHex(StrToIntDef(RzComboBox2.Text,0),4);
end;
tmpHMICMD:=IntToHex(StrToIntDef(RzEdit3.Text,0),2) tmpHMICMD;
end
else
begin
if RzComboBox1.itemIndex=0 then //位
tmpTag:='01'
else
tmpTag:='04';
tmpReadNum:=StrtoIntDef(RzComboBox2.text,1);
tmpHMICMD:=IntToHex(StrToIntDef(RzEdit3.Text,0),2) tmpTag InttoHex(StrToIntDef(RzEdit4.Text,0),4)
inttohex(tmpReadNum,4);
end;
Edit1.Text:= tmpHMICMD;
tmpHMICMD:=ToSendStr(tmpHMICMD);
ClientSocket1.Socket.SendText(tmpHMICMD);
end;
function StrToHex(tmpstr: string): string;
var
i: Integer;
begin
Result := '';
for i := 1 to length(tmpstr) do
Result := Result IntToHex(Ord(tmpstr[i]), 2);
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var tmpstr:string;
begin
tmpstr:= Socket.ReceiveText;
Edit2.Text:=StrToHex(tmpstr);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
RzComboBox1.ItemIndex:=0;
RzComboBox2.ItemIndex:=0;
end;
procedure TForm1.RzButton2Click(Sender: TObject);
begin
ClientSocket1.Active:=False;
ClientSocket1.Address:=RzEdit1.Text;
ClientSocket1.Port:=StrToIntDef(RzEdit2.Text,502);
ClientSocket1.Active:=True;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Shape1.Brush.Color:=clGreen;
Button1.Enabled:=True;
end;
procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Shape1.Brush.Color:=clGray;
Button1.Enabled:=False;
end;
procedure TForm1.ClientSocket1Connecting(Sender: TObject;
Socket: TCustomWinSocket);
begin
Shape1.Brush.Color:=clYellow;
end;
end.
评论