⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 delphi写的西门子S7-200 PLC的PPI通讯协议
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit Unit1;

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;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  str_write,str_val,CommRecvTxt: array of Byte;
  Temp_FCS: byte;
implementation

{$R *.dfm}

procedure TForm1.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);        //package len
  str_write[2] := byte($20);        //package len repeated
  str_write[3] := byte($68);
  str_write[4] := byte($02);         //plc station no
  str_write[5] := byte($00);         //pc default zero
  str_write[6] := byte($7C);        //6c read, 7c write
  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);//    data len
  str_write[25] := byte($00);
  str_write[26] := byte($01);
  str_write[27] := byte($84);// 04S ,05SM ,06AI ,07AQ ,1E C ,81I ,82Q ,83M ,84V ,1F T
  
  str_write[28] := byte($00);
  str_write[29] := (strtoint(edit1.Text) * 8) div 256;
  str_write[30] := (strtoint(edit1.Text) * 8) Mod 256;

  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;
  For i := 4 To 35 do
      Temp_FCS := Temp_FCS + str_write[i];
  memo1.Lines.Add(inttostr(temp_Fcs));

  str_write[36] := Temp_FCS Mod 256;
  str_write[37] := byte($16);

  MSComm1.Output := str_write;
  Sleep(100);
  
  SetLength(CommRecvTxt,1);
  CommRecvTxt := mscomm1.Input;
  memo1.Lines.Add('begin');
  if Length(CommRecvTxt) <> 0 then
  begin
    if inttohex(CommRecvTxt[0],2) = 'E5' then
    begin
      memo1.Lines.Add('E5');
      
      MSComm1.Output := str_val;
      Sleep(100);

      SetLength(CommRecvTxt,512);
      CommRecvTxt := mscomm1.Input;

      if Length(CommRecvTxt) <> 0 then
      begin
        for i := Low(CommRecvTxt) to High(CommRecvTxt) do
          tmp := tmp + inttohex(CommRecvTxt[i],2) + ',';
        memo1.Lines.Add(tmp);
      end
      else memo1.Lines.Add('ok...zero');

    end
    else memo1.Lines.Add('error');
  end
  else memo1.Lines.Add('zero...');


end;

procedure TForm1.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);        //package len
  str_write[2] := byte($1B);        //package len repeated
  str_write[3] := byte($68);
  str_write[4] := byte($02);         //plc station no
  str_write[5] := byte($00);         //pc default zero
  str_write[6] := byte($6C);        //6c read, 7c write
  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);  //    data len
  str_write[25] := byte($00);
  str_write[26] := byte($01);  //04为写入其他,03写入位
  str_write[27] := byte($84);   // 04S ,05SM ,06AI ,07AQ ,1E C ,81I ,82Q ,83M ,84V ,1F T
  
  str_write[28] := byte($0);
  str_write[29] := (strtoint(edit1.Text) * 8) div 256;
  str_write[30] := (strtoint(edit1.Text) * 8) Mod 256;

  Temp_FCS := 0;
  For i := 4 To 30 do
      Temp_FCS := Temp_FCS + str_write[i];
      
  memo1.Lines.Add(inttostr(temp_Fcs));

  str_write[31] := Temp_FCS Mod 256;
  str_write[32] := byte($16);

  MSComm1.Output := str_write;

  Sleep(100);

  CommRecvTxt := mscomm1.Input;

  //if inttohex(CommRecvTxt[0],2) = 'E5' then
  begin
    MSComm1.Output := str_val;
    sleep(100);
    CommRecvTxt := mscomm1.Input;
    for i:=0 to 27 do tmp := tmp + inttohex(byte(CommRecvTxt[i]),2)+',';
    memo1.Lines.Add('//'+tmp);

  end;
  //memo1.Lines.Add('error');



end;

procedure TForm1.FormCreate(Sender: TObject);
begin

self.MSComm1.PortOpen := true;

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
self.MSComm1.PortOpen := false;
end;


procedure TForm1.Button3Click(Sender: TObject);
var
  i,j: Integer;
  tmp: string;
begin
  memo1.Lines.Add('R more----------------------');
  //str_write := VarArrayCreate([0,32],varByte);
  SetLength(str_write,33);
  str_write[0] := byte($68);
  str_write[1] := byte($1B);        //package len
  str_write[2] := byte($1B);        //package len repeated
  str_write[3] := byte($68);
  str_write[4] := byte($02);         //plc station no
  str_write[5] := byte($00);         //pc default zero
  str_write[6] := byte($6C);         //6c read, 7c write
  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);   //02 byte 为单位    04 word
  str_write[23] := byte($00);
  str_write[24] := byte(StrToInt(Edit3.Text));  //    data len
  str_write[25] := byte($00);
  str_write[26] := byte($01);  //04为写入其他,03写入位
  str_write[27] := byte($84);   // 04S ,05SM ,06AI ,07AQ ,1E C ,81I ,82Q ,83M ,84V ,1F T

  str_write[28] := byte($0);
  str_write[29] := (strtoint(edit1.Text) * 8) div 256;
  str_write[30] := (strtoint(edit1.Text) * 8) Mod 256;

  Temp_FCS := 0;
  For i := 4 To 30 do
      Temp_FCS := Temp_FCS + str_write[i];

  memo1.Lines.Add(inttostr(temp_Fcs));

  str_write[31] := Temp_FCS Mod 256;
  str_write[32] := byte($16);

  MSComm1.Output := str_write;

  Sleep(100);

  SetLength(CommRecvTxt,1);
  CommRecvTxt := MSComm1.Input;      //
  if Length(CommRecvTxt) <> 0 then  
  if inttohex(CommRecvTxt[0],2) = 'E5' then
  begin
    MSComm1.Output := str_val;
    sleep(100);
    SetLength(CommRecvTxt,512);
    CommRecvTxt := MSComm1.Input;      //
    if Length(CommRecvTxt) <> 0 then
      for j := Low(CommRecvTxt) to High(CommRecvTxt) do
        tmp := tmp + inttohex(byte(CommRecvTxt[j]),2)+',';

    memo1.Lines.Add('//'+tmp);
    memo1.Lines.Add(IntToStr(High(CommRecvTxt)));

      
  end;

end;

procedure TForm1.Button5Click(Sender: TObject);
var
  i: integer;
  tmp: string;
begin
  memo1.Lines.Add('bitset--------------------------');
  SetLength(str_write,38);
  str_write[0] := byte($68);
  str_write[1] := byte($20);        //package len
  str_write[2] := byte($20);        //package len repeated
  str_write[3] := byte($68);

  str_write[4] := byte($02);         //plc station no
  str_write[5] := byte($00);         //pc default zero
  str_write[6] := byte($6C);        //6c read, 7c write
  
  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);    // 06
  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($01);
  str_write[23] := byte($00);
  str_write[24] := byte($01);//    data len
  str_write[25] := byte($00);
  str_write[26] := byte($01);
  str_write[27] := byte($84);// 04S ,05SM ,06AI ,07AQ ,1E C ,81I ,82Q ,83M ,84V ,1F T
  
  str_write[28] := byte($00);
  str_write[29] := (strtoint(edit1.Text) * 8 + strtoint(Edit2.Text)) div 256;    //dizhi
  str_write[30] := (strtoint(edit1.Text) * 8 + strtoint(Edit2.Text)) Mod 256;    //dizhi

  str_write[31] := byte($00);
  str_write[32] := byte($03);   //04为写入其他,03写入位
  str_write[33] := byte($00);
  str_write[34] := byte($01);   //01bit, 08byte, 10word, 20dword  //F0
  str_write[35] := $01;  //data

  Temp_FCS := 0;
  For i := 4 To 35 do
      Temp_FCS := Temp_FCS + str_write[i];
  memo1.Lines.Add(inttostr(temp_Fcs));

  str_write[36] := Temp_FCS Mod 256;
  str_write[37] := byte($16);

  MSComm1.Output := str_write;
  Sleep(100);
  
  SetLength(CommRecvTxt,1);
  CommRecvTxt := mscomm1.Input;
  memo1.Lines.Add('begin');
  if Length(CommRecvTxt) <> 0 then
  begin
    if inttohex(CommRecvTxt[0],2) = 'E5' then
    begin
      memo1.Lines.Add('E5');
      
      MSComm1.Output := str_val;
      Sleep(100);

      SetLength(CommRecvTxt,512);
      CommRecvTxt := mscomm1.Input;

      if Length(CommRecvTxt) <> 0 then
      begin
        for i := Low(CommRecvTxt) to High(CommRecvTxt) do
          tmp := tmp + inttohex(CommRecvTxt[i],2) + ',';
        memo1.Lines.Add(tmp);
      end
      else memo1.Lines.Add('ok...zero');

    end
    else memo1.Lines.Add('error');
  end
  else memo1.Lines.Add('zero...');


end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Form1.Caption := IntToStr(Length(str_val));
end;

procedure TForm1.Button7Click(Sender: TObject);
var
  i: integer;
  tmp: string;
begin
  memo1.Lines.Add('W RUN--------------------------');
  SetLength(str_write,39);
  str_write[0] := byte($68);
  str_write[1] := byte($21);        //package len
  str_write[2] := byte($21);        //package len repeated
  str_write[3] := byte($68);

  str_write[4] := byte($02);         //plc station no
  str_write[5] := byte($00);         //pc default zero
  str_write[6] := byte($7C);        //6c read, 7c write
  
  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($14);
  str_write[15] := byte($00);
  str_write[16] := byte($00);
  str_write[17] := byte($28);
  str_write[18] := byte($00);
  str_write[19] := byte($00);
  str_write[20] := byte($00 );
  str_write[21] := byte($00);
  
  //*****************************************8
  str_write[22] := byte($00);
  str_write[23] := byte($00);
  str_write[24] := byte($FD);
  str_write[25] := byte($00);
  str_write[26] := byte($00);
  str_write[27] := byte($09);
  
  str_write[28] := byte($50);
  str_write[29] := byte($5F);
  str_write[30] := byte($50);

  str_write[31] := byte($52);
  str_write[32] := byte($4F);
  str_write[33] := byte($47);
  str_write[34] := byte($52);
  str_write[35] := $41;
  str_write[36] := $4D;

  Temp_FCS := 0;
  For i := 4 To 36 do
      Temp_FCS := Temp_FCS + str_write[i];
  memo1.Lines.Add(inttostr(temp_Fcs));

  str_write[37] := Temp_FCS Mod 256;
  str_write[38] := byte($16);

  MSComm1.Output := str_write;
  Sleep(100);
  
  SetLength(CommRecvTxt,1);
  CommRecvTxt := mscomm1.Input;
  memo1.Lines.Add('begin');
  if Length(CommRecvTxt) <> 0 then
  begin
    if inttohex(CommRecvTxt[0],2) = 'E5' then
    begin
      memo1.Lines.Add('E5');
      
      MSComm1.Output := str_val;
      Sleep(100);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -