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

📄 unit1.pas

📁 delphi写的西门子S7-200 PLC的PPI通讯协议
💻 PAS
📖 第 1 页 / 共 2 页
字号:
      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.Button8Click(Sender: TObject);
var
  i: integer;
  tmp: string;
begin
  memo1.Lines.Add('W STOP--------------------------');
  SetLength(str_write,35);
  str_write[0] := byte($68);
  str_write[1] := byte($1D);        //package len
  str_write[2] := byte($1D);        //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($10);
  str_write[15] := byte($00);
  str_write[16] := byte($00);
  str_write[17] := byte($29);  //28RUN,29STOP
  str_write[18] := byte($00);
  str_write[19] := byte($00);
  str_write[20] := byte($00 );
  str_write[21] := byte($00);
  
  //*****************************************
  str_write[22] := byte($00);
  str_write[23] := byte($09);
  {
  str_write[24] := byte($FD);
  str_write[25] := byte($00);
  str_write[26] := byte($00);
  str_write[27] := byte($09);
 }
  str_write[24] := byte($50);
  str_write[25] := byte($5F);
  str_write[26] := byte($50);
  str_write[27] := byte($52);

  str_write[28] := byte($4F);
  str_write[29] := byte($47);
  str_write[30] := byte($52);
  str_write[31] := $41;
  str_write[32] := $4D;

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

  str_write[33] := Temp_FCS Mod 256;
  str_write[34] := 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.Button9Click(Sender: TObject);
var
  i: integer;
  tmp: string;
begin
  memo1.Lines.Add('W--------------------------');
  SetLength(str_write,40);
  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($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($02);//    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] := $10;//byte(strtoint(edit2.Text));  //data
  str_write[36] := $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[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);

      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.Button4Click(Sender: TObject);
var
  i,j: Integer;
  tmp: string;
begin
  memo1.Lines.Add('R more word----------------------');
  //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($04);   //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.Button10Click(Sender: TObject);
var
  i: integer;
  tmp: string;
begin
  memo1.Lines.Add('W WORD--------------------------');
  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($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($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($02);
  str_write[23] := byte($00);
  str_write[24] := byte($02);//    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($10);   //01bit, 08byte, 10word, 20dword
  str_write[35] := $0F;//byte(strtoint(edit2.Text));  //data
  str_write[36] := $1F;

  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);

      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.Button11Click(Sender: TObject);
var
  i: integer;
  tmp: string;
begin
  memo1.Lines.Add('bitReset--------------------------');
  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] := $00;  //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;

initialization
    //str_val := VarArrayCreate([0,5],varByte);
    SetLength(str_val,6);
    str_val[0] := byte($10);
    str_val[1] := byte($2);
    str_val[2] := byte($0);
    str_val[3] := byte($5C);
    str_val[4] := byte($5E);
    str_val[5] := byte($16);

    
    //CommRecvTxt := VarArrayCreate([0,27],varByte);
    //str_write := VarArrayCreate([0,40],varByte);
    
finalization

end.

⌨️ 快捷键说明

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