西门子200PLC通信 ppi协议delphi.docx
《西门子200PLC通信 ppi协议delphi.docx》由会员分享,可在线阅读,更多相关《西门子200PLC通信 ppi协议delphi.docx(42页珍藏版)》请在冰豆网上搜索。
西门子200PLC通信ppi协议delphi
本栏论题:
西门子200PLC通信ppi协议-delphi [582]
由于站点暂时不能上传源代码文件,所以只能先贴上代码了
1、本程序仅用于工控爱好者交流使用,鄙视用于商业的行径
2、已开发出西门子s7-300plc的通信控件,可以与西门子各种型号plc进行
通信。
期望与感兴趣的同仁进行交流。
作者邮箱:
lkl_xz@
qq:
57476662
2008.06.25
*****************************说明**********************
1\上位机通过串口与西门子200通信,基于ppi协议,pc-ppi编程电缆
2\主要基于v区进行通信,实际应用中足够了,故没有对其他区进行操作
3\要点:
1)一次读取v区多个字节,
2)v区的位操作:
置位和复位,在第1个文本框中输入v区地址,在第2个文本框中输入位地址,如程序默认的地址是V0.0
3)写一个字
4\注意:
实际应用中一次最多可以读取v区55个字节,否则会造成读取失败
5\安装:
附带MSCOMM32串口通信控件的安装程序。
代码×××××××××××××××××××××××
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,StdCtrls,MSCommLib_TLB,OleCtrls;
type
TForm1=class(TForm)
MSComm1:
TMSComm;
Button1:
TButton;
Button2:
TButton;
Memo1:
TMemo;
Edit1:
TEdit;
Edit2:
TEdit;
Label1:
TLabel;
Label2:
TLabel;
StaticText1:
TStaticText;
StaticText2:
TStaticText;
Button3:
TButton;
Edit3:
TEdit;
Button4:
TButton;
StaticText3:
TStaticText;
Button7:
TButton;
Button8:
TButton;
Button10:
TButton;
Button5:
TButton;
Button11:
TButton;
procedureButton1Click(Sender:
TObject);
procedureButton2Click(Sender:
TObject);
procedureFormCreate(Sender:
TObject);
procedureFormClose(Sender:
TObject;varAction:
TCloseAction);
procedureButton3Click(Sender:
TObject);
procedureButton5Click(Sender:
TObject);
procedureFormShow(Sender:
TObject);
procedureButton7Click(Sender:
TObject);
procedureButton8Click(Sender:
TObject);
procedureButton9Click(Sender:
TObject);
procedureButton4Click(Sender:
TObject);
procedureButton10Click(Sender:
TObject);
procedureButton11Click(Sender:
TObject);
private
{Privatedeclarations}
public
{Publicdeclarations}
end;
var
Form1:
TForm1;
str_write,str_val,CommRecvTxt:
arrayofByte;
Temp_FCS:
byte;
implementation
{$R*.dfm}
procedureTForm1.Button1Click(Sender:
TObject);
var
i:
integer;
tmp:
string;
begin
memo1.Lines.Add('W--------------------------');
SetLength(str_write,38);
str_write[0]:
=byte($68);
str_write[1]:
=byte($20);//packagelen
str_write[2]:
=byte($20);//packagelenrepeated
str_write[3]:
=byte($68);
str_write[4]:
=byte($02);//plcstationno
str_write[5]:
=byte($00);//pcdefaultzero
str_write[6]:
=byte($7C);//6cread,7cwrite
str_write[7]:
=byte($32);
str_write[8]:
=byte($01);
str_write[9]:
=byte($00);
str_write[10]:
=byte($00);
str_write[11]:
=byte($00);
str_write[12]:
=byte($00);
str_write[13]:
=byte($00);
str_write[14]:
=byte($0E);
str_write[15]:
=byte($00);
str_write[16]:
=byte($05);
str_write[17]:
=byte($05);
str_write[18]:
=byte($01);
str_write[19]:
=byte($12);
str_write[20]:
=byte($0A);
str_write[21]:
=byte($10);
//*****************************************8
str_write[22]:
=byte($02);
str_write[23]:
=byte($00);
str_write[24]:
=byte($01);//datalen
str_write[25]:
=byte($00);
str_write[26]:
=byte($01);
str_write[27]:
=byte($84);//04S,05SM,06AI,07AQ,1EC,81I,82Q,83M,84V,1FT
str_write[28]:
=byte($00);
str_write[29]:
=(strtoint(edit1.Text)*8)div256;
str_write[30]:
=(strtoint(edit1.Text)*8)Mod256;
str_write[31]:
=byte($00);
str_write[32]:
=byte($04);//04为写入其他,03写入位
str_write[33]:
=byte($00);
str_write[34]:
=byte($08);//01bit,08byte,10word,20dword
str_write[35]:
=byte(strtoint(edit2.Text));//data
//str_write[35]:
=$10;
Temp_FCS:
=0;
Fori:
=4To35do
Temp_FCS:
=Temp_FCS+str_write[i];
memo1.Lines.Add(inttostr(temp_Fcs));
str_write[36]:
=Temp_FCSMod256;
str_write[37]:
=byte($16);
MSComm1.Output:
=str_write;
Sleep(100);
SetLength(CommRecvTxt,1);
CommRecvTxt:
=mscomm1.Input;
memo1.Lines.Add('begin');
ifLength(CommRecvTxt)<>0then
begin
ifinttohex(CommRecvTxt[0],2)='E5'then
begin
memo1.Lines.Add('E5');
MSComm1.Output:
=str_val;
Sleep(100);
SetLength(CommRecvTxt,512);
CommRecvTxt:
=mscomm1.Input;
ifLength(CommRecvTxt)<>0then
begin
fori:
=Low(CommRecvTxt)toHigh(CommRecvTxt)do
tmp:
=tmp+inttohex(CommRecvTxt[i],2)+',';
memo1.Lines.Add(tmp);
end
elsememo1.Lines.Add('ok...zero');
end
elsememo1.Lines.Add('error');
end
elsememo1.Lines.Add('zero...');
end;
procedureTForm1.Button2Click(Sender:
TObject);
var
i:
integer;
tmp:
string;
begin
memo1.Lines.Add('R--------------------------');
//str_write:
=VarArrayCreate([0,32],varByte);
str_write[0]:
=byte($68);
str_write[1]:
=byte($1B);//packagelen
str_write[2]:
=byte($1B);//packagelenrepeated
str_write[3]:
=byte($68);
str_write[4]:
=byte($02);//plcstationno
str_write[5]:
=byte($00);//pcdefaultzero
str_write[6]:
=byte($6C);//6cread,7cwrite
str_write[7]:
=byte($32);
str_write[8]:
=byte($01);
str_write[9]:
=byte($00);
str_write[10]:
=byte($00);
str_write[11]:
=byte($00);
str_write[12]:
=byte($00);
str_write[13]:
=byte($00);
str_write[14]:
=byte($0E);
str_write[15]:
=byte($00);
str_write[16]:
=byte($00);
str_write[17]:
=byte($04);
str_write[18]:
=byte($01);
str_write[19]:
=byte($12);
str_write[20]:
=byte($0A);
str_write[21]:
=byte($10);
//*****************************************8
str_write[22]:
=byte($02);//01bit,08byte,10word,20dword
str_write[23]:
=byte($00);
str_write[24]:
=byte($01);//datalen
str_write[25]:
=byte($00);
str_write[26]:
=byte($01);//04为写入其他,03写入位
str_write[27]:
=byte($84);//04S,05SM,06AI,07AQ,1EC,81I,82Q,83M,84V,1FT
str_write[28]:
=byte($0);
str_write[29]:
=(strtoint(edit1.Text)*8)div256;
str_write[30]:
=(strtoint(edit1.Text)*8)Mod256;
Temp_FCS:
=0;
Fori:
=4To30do
Temp_FCS:
=Temp_FCS+str_write[i];
memo1.Lines.Add(inttostr(temp_Fcs));
str_write[31]:
=Temp_FCSMod256;
str_write[32]:
=byte($16);
MSComm1.Output:
=str_write;
Sleep(100);
CommRecvTxt:
=mscomm1.Input;
//ifinttohex(CommRecvTxt[0],2)='E5'then
begin
MSComm1.Output:
=str_val;
sleep(100);
CommRecvTxt:
=mscomm1.Input;
fori:
=0to27dotmp:
=tmp+inttohex(byte(CommRecvTxt[i]),2)+',';
memo1.Lines.Add('//'+tmp);
end;
//memo1.Lines.Add('error');
end;
procedureTForm1.FormCreate(Sender:
TObject);
begin
self.MSComm1.PortOpen:
=true;
end;
procedureTForm1.FormClose(Sender:
TObject;varAction:
TCloseAction);
begin
self.MSComm1.PortOpen:
=false;
end;
procedureTForm1.Button3Click(Sender:
TObject);
var
i,j:
Integer;
tmp:
string;
begin
memo1.Lines.Add('Rmore----------------------');
//str_write:
=VarArrayCreate([0,32],varByte);
SetLength(str_write,33);
str_write[0]:
=byte($68);
str_write[1]:
=byte($1B);//packagelen
str_write[2]:
=byte($1B);//packagelenrepeated
str_write[3]:
=byte($68);
str_write[4]:
=byte($02);//plcstationno
str_write[5]:
=byte($00);//pcdefaultzero
str_write[6]:
=byte($6C);//6cread,7cwrite
str_write[7]:
=byte($32);
str_write[8]:
=byte($01);
str_write[9]:
=byte($00);
str_write[10]:
=byte($00);
str_write[11]:
=byte($00);
str_write[12]:
=byte($00);
str_write[13]:
=byte($00);
str_write[14]:
=byte($0E);//数据块占位字节,长度=4+数据块数*10,条数据时为4+10=0E(H);读M,V,Q三个数据块时4+3*10=22(H)。
str_write[15]:
=byte($00);
str_write[16]:
=byte($00);
str_write[17]:
=byte($04);
str_write[18]:
=byte($01);
str_write[19]:
=byte($12);
str_write[20]:
=byte($0A);
str_write[21]:
=byte($10);
//*****************************************8
str_write[22]:
=byte($02);//02byte为单位04word
str_write[23]:
=byte($00);
str_write[24]:
=byte(StrToInt(Edit3.Text));//datalen
str_write[25]:
=byte($00);
str_write[26]:
=byte($01);//04为写入其他,03写入位
str_write[27]:
=byte($84);//04S,05SM,06AI,07AQ,1EC,81I,82Q,83M,84V,1FT
str_write[28]:
=byte($0);
str_write[29]:
=(strtoint(edit1.Text)*8)div256;
str_write[30]:
=(strtoint(edit1.Text)*8)Mod256;
Temp_FCS:
=0;
Fori:
=4To30do
Temp_FCS:
=Temp_FCS+str_write[i];
memo1.Lines.Add(inttostr(temp_Fcs));
str_write[31]:
=Temp_FCSMod256;
str_write[32]:
=byte($16);
MSComm1.Output:
=str_write;
Sleep(100);
SetLength(CommRecvTxt,1);
CommRecvTxt:
=MSComm1.Input;//
ifLength(CommRecvTxt)<>0then
ifinttohex(CommRecvTxt[0],2)='E5'then
begin
MSComm1.Output:
=str_val;
sleep(100);
SetLength(CommRecvTxt,512);
CommRecvTxt:
=MSComm1.Input;//
ifLength(CommRecvTxt)<>0then
forj:
=Low(CommRecvTxt)toHigh(CommRecvTxt)do
tmp:
=tmp+inttohex(byte(CommRecvTxt[j]),2)+',';
memo1.Lines.Add('//'+tmp);
memo1.Lines.Add(IntToStr(High(CommRecvTxt)));
end;
end;
procedureTFor