標(biāo)題: 上位機(jī)與S7-200 cpu224自由口通訊之上位機(jī)源程序 [打印本頁(yè)]

作者: liuda    時(shí)間: 2015-1-23 00:34
標(biāo)題: 上位機(jī)與S7-200 cpu224自由口通訊之上位機(jī)源程序








{本程序?yàn)閟7-200 cpu224自由口通訊實(shí)驗(yàn),由上位機(jī)發(fā)送,PLC返回查詢輸入輸出狀態(tài)
1.01 xx FF;若為$01開(kāi)頭則視為只查詢輸入輸出狀態(tài)
2.02 xx FF;若為$02開(kāi)頭則視為查詢輸入輸出狀態(tài),同時(shí)寫(xiě)QB0
3.03 xx FF;若為$03開(kāi)頭則視為查詢輸入輸出狀態(tài),同時(shí)寫(xiě)QB1
   其中FF為自由口結(jié)束字符
   DATE:2013-11
   Auther:hcwp}
unit unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,SPComm, StdCtrls, JvExControls, JvLED, ExtCtrls, iComponent,
  iVCLComponent, iCustomComponent, iPositionComponent, iScaleComponent,
  iGaugeComponent, iLogGauge;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Comm1: TComm;
    JvLED1: TJvLED;
    JvLED2: TJvLED;
    JvLED3: TJvLED;
    JvLED4: TJvLED;
    JvLED5: TJvLED;
    JvLED6: TJvLED;
    JvLED7: TJvLED;
    JvLED8: TJvLED;
    JvLED9: TJvLED;
    JvLED10: TJvLED;
    JvLED11: TJvLED;
    JvLED12: TJvLED;
    JvLED13: TJvLED;
    JvLED14: TJvLED;
    Label1: TLabel;
    Label2: TLabel;
    Timer1: TTimer;
    JvLED0: TJvLED;
    JvLED15: TJvLED;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox6: TCheckBox;
    CheckBox7: TCheckBox;
    CheckBox8: TCheckBox;
    CheckBox9: TCheckBox;
    CheckBox10: TCheckBox;
    CheckBox11: TCheckBox;
    CheckBox12: TCheckBox;
    CheckBox13: TCheckBox;
    CheckBox14: TCheckBox;
    CheckBox15: TCheckBox;
    CheckBox16: TCheckBox;
    Label3: TLabel;
    Label4: TLabel;
    JvLED16: TJvLED;
    JvLED17: TJvLED;
    JvLED18: TJvLED;
    JvLED19: TJvLED;
    JvLED20: TJvLED;
    JvLED21: TJvLED;
    JvLED22: TJvLED;
    JvLED23: TJvLED;
    Label5: TLabel;
    Label6: TLabel;
    JvLED24: TJvLED;
    JvLED25: TJvLED;
    JvLED26: TJvLED;
    JvLED27: TJvLED;
    JvLED28: TJvLED;
    JvLED29: TJvLED;
    JvLED30: TJvLED;
    JvLED31: TJvLED;
    Label7: TLabel;
    Label8: TLabel;
    Timer2: TTimer;
    Timer3: TTimer;
    Label9: TLabel;
    Label10: TLabel;


    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Comm1ReceiveData(Sender: TObject; Buffer: PAnsiChar;
     BufferLength: Word);
    procedure Button2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Timer3Timer(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CheckBox3Click(Sender: TObject);
    procedure CheckBox4Click(Sender: TObject);
    procedure CheckBox5Click(Sender: TObject);
    procedure CheckBox6Click(Sender: TObject);
    procedure CheckBox7Click(Sender: TObject);
    procedure CheckBox8Click(Sender: TObject);
    procedure CheckBox9Click(Sender: TObject);
    procedure CheckBox10Click(Sender: TObject);
    procedure CheckBox11Click(Sender: TObject);
    procedure CheckBox12Click(Sender: TObject);
    procedure CheckBox13Click(Sender: TObject);
    procedure CheckBox14Click(Sender: TObject);
    procedure CheckBox15Click(Sender: TObject);
    procedure CheckBox16Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  //Comm1:TComm;
  InputArray:array[0..3]of Byte;
  IB0,IB1,QB0,QB1:array[0..7]of Boolean;
implementation

{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
  with Comm1 do
  begin
    BaudRate:= 9600;//波特率9600bps
    Parity:=None;//奇偶交驗(yàn)
    ByteSize:=_8;//數(shù)據(jù)位 8
    StopBits:=_1;//停止位1
    Comm1.CommPort:=3;
    InBufferSize:=5;
    InputLen:=5;
    ParityCheck:=False;
    StopBits:=_1;
    DtrControl:=dtrEnable;
  end;


end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   Comm1.StartComm;
   Timer3.Enabled:=True;
end;

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: PAnsiChar;
  BufferLength: Word);
var
    aa: Integer;
    ss: Integer;
    dd:string;
begin
    //aa:=12;
    for  ss:=0 to 4 do
    begin
      aa:=Byte(Buffer[ss]);
      //sleep(100);
      dd:=IntToHex(aa,2);

     //ss:=IntToHex(aa,2);
     // ss:= chr(aa);
      //dd:= IntToHex(aa,2);
     Memo1.Lines.Add(dd);
     //Sleep(10);
     InputArray[ss]:= Byte(Buffer[ss]);
    end;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Timer1.Enabled:=True;
  Timer2.Enabled:=True;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
//const
  // j:array[0..2]of string=('jvled1','jvled2','jvled3');
var
  iFlag:Integer;
  k,I:Integer;
  j:array[0..15]of Boolean;

begin
{ for k := 1 to 14 do
  begin
    j[k]:='JvLED'+ IntToStr(k);
  end; }

  for iFlag:=7 downto 0 do
  begin
    if InputArray[0] and (1 shl iFlag)<>0 then
      IB0[iFlag]:=True
    else
      IB0[iFlag]:=False;
  end;
  for i := 0 to 7 do
  begin
    if IB0=True then
     j[I]:=True
    else j[I]:=False;
  end;
  for iFlag:=7 downto 0 do
  begin
    if InputArray[1] and (1 shl iFlag)<>0 then
      IB1[iFlag]:=True
    else
      IB1[iFlag]:=False;
  end;
  for i := 0 to 7 do
  begin
    if IB1=True then
     j[I+8]:=True
    else j[I+8]:=False;
  end;
  for k :=0  to 15 do
  begin
    case k of
      0: JvLED0.Status:=j[0] ;
      1: JvLED1.Status:=j[1] ;
      2: JvLED2.Status:=j[2] ;
      3: JvLED3.Status:=j[3] ;
      4: JvLED4.Status:=j[4] ;
      5: JvLED5.Status:=j[5] ;
      6: JvLED6.Status:=j[6] ;
      7: JvLED7.Status:=j[7] ;
      8: JvLED8.Status:=j[8] ;
      9: JvLED9.Status:=j[9] ;
      10: JvLED10.Status:=j[10] ;
      11: JvLED11.Status:=j[11] ;
      12: JvLED12.Status:=j[12] ;
      13: JvLED13.Status:=j[13] ;
      14: JvLED14.Status:=j[14] ;
      15: JvLED15.Status:=j[15] ;
    end;
  end;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
var
  iFlag:Integer;
  k,I:Integer;
  j:array[0..15]of Boolean;

begin
{ for k := 1 to 14 do
  begin
    j[k]:='JvLED'+ IntToStr(k);
  end; }

  for iFlag:=7 downto 0 do
  begin
    if InputArray[2] and (1 shl iFlag)<>0 then
      QB0[iFlag]:=True
    else
      QB0[iFlag]:=False;
  end;
  for i := 0 to 7 do
  begin
    if QB0=True then
     j[I]:=True
    else j[I]:=False;
  end;
  for iFlag:=7 downto 0 do
  begin
    if InputArray[3] and (1 shl iFlag)<>0 then
      QB1[iFlag]:=True
    else
      QB1[iFlag]:=False;
  end;
  for i := 0 to 7 do
  begin
    if QB1=True then
     j[I+8]:=True
    else j[I+8]:=False;
  end;
  for k :=0  to 15 do
  begin
    case k of
      0: JvLED16.Status:=j[0] ;
      1: JvLED17.Status:=j[1] ;
      2: JvLED18.Status:=j[2] ;
      3: JvLED19.Status:=j[3] ;
      4: JvLED20.Status:=j[4] ;
      5: JvLED21.Status:=j[5] ;
      6: JvLED22.Status:=j[6] ;
      7: JvLED23.Status:=j[7] ;
      8: JvLED24.Status:=j[8] ;
      9: JvLED25.Status:=j[9] ;
      10: JvLED26.Status:=j[10] ;
      11: JvLED27.Status:=j[11] ;
      12: JvLED28.Status:=j[12] ;
      13: JvLED29.Status:=j[13] ;
      14: JvLED30.Status:=j[14] ;
      15: JvLED31.Status:=j[15] ;
    end;
  end;
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[2] and ($01);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $01:    if CheckBox1.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[2] and ($fe);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox1.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[2] or ($01);
              str:=Char($02)+char( temp1)+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;


  Timer3.Enabled:=True;
end;

procedure TForm1.Timer3Timer(Sender: TObject);
var
  str:string;
begin
  str:=char($01)+ Char($00)+Chr($ff);

    Comm1.Output:=str;

end;

procedure TForm1.CheckBox2Click(Sender: TObject);
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[2] and ($02);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $02:    if CheckBox2.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[2] and ($fd);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox2.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[2] or ($02);
              str:=Char($02)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;


  Timer3.Enabled:=True;
end;

procedure TForm1.CheckBox3Click(Sender: TObject);
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[2] and ($04);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $04:    if CheckBox3.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[2] and ($fb);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox3.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[2] or ($04);
              str:=Char($02)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;
  Timer3.Enabled:=True;
end;

procedure TForm1.CheckBox4Click(Sender: TObject);
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[2] and ($08);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $08:    if CheckBox4.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[2] and ($f7);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox4.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[2] or ($08);
              str:=Char($02)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;
  Timer3.Enabled:=True;
end;

procedure TForm1.CheckBox5Click(Sender: TObject);  //Q0.4
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[2] and ($10);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $10:    if CheckBox5.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[2] and ($ef);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox5.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[2] or ($10);
              str:=Char($02)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;
  Timer3.Enabled:=True;
end;

procedure TForm1.CheckBox6Click(Sender: TObject); //Q0.5
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[2] and ($20);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $20:    if CheckBox6.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[2] and ($df);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox6.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[2] or ($20);
              str:=Char($02)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;
  Timer3.Enabled:=True;
end;

procedure TForm1.CheckBox7Click(Sender: TObject);
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[2] and ($40);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $40:    if CheckBox7.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[2] and ($bf);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox7.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[2] or ($40);
              str:=Char($02)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;
  Timer3.Enabled:=True;
end;

procedure TForm1.CheckBox8Click(Sender: TObject);
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[2] and ($80);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $80:    if CheckBox8.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[2] and ($7f);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox8.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[2] or ($80);
              str:=Char($02)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;
  Timer3.Enabled:=True;
end;

procedure TForm1.CheckBox9Click(Sender: TObject);
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[3] and ($01);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $01:    if CheckBox9.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[3] and ($fe);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox9.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[3] or ($01);
              str:=Char($03)+char( temp1)+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;


  Timer3.Enabled:=True;
end;

procedure TForm1.CheckBox10Click(Sender: TObject);
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[3] and ($02);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $02:    if CheckBox10.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[3] and ($fd);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox10.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[3] or ($02);
              str:=Char($03)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;


  Timer3.Enabled:=True;
end;

procedure TForm1.CheckBox11Click(Sender: TObject);
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[3] and ($04);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $04:    if CheckBox11.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[3] and ($fb);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox11.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[3] or ($04);
              str:=Char($03)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;
  Timer3.Enabled:=True;
end;


procedure TForm1.CheckBox12Click(Sender: TObject);
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[3] and ($08);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $08:    if CheckBox12.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[3] and ($f7);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox12.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[3] or ($08);
              str:=Char($03)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;
  Timer3.Enabled:=True;
end;

procedure TForm1.CheckBox13Click(Sender: TObject);
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[3] and ($10);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $10:    if CheckBox13.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[3] and ($ef);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox13.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[3] or ($10);
              str:=Char($03)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;
  Timer3.Enabled:=True;
end;

procedure TForm1.CheckBox14Click(Sender: TObject);
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[3] and ($20);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $20:    if CheckBox14.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[3] and ($df);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox14.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[3] or ($20);
              str:=Char($03)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;
  Timer3.Enabled:=True;
end;

procedure TForm1.CheckBox15Click(Sender: TObject);
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[3] and ($40);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $40:    if CheckBox15.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[3] and ($bf);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox15.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[3] or ($40);
              str:=Char($03)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;
  Timer3.Enabled:=True;
end;



procedure TForm1.CheckBox16Click(Sender: TObject);
var
  str:string;
  temp,temp1:Byte;
begin

  Timer3.Enabled:=False;
  temp:= InputArray[3] and ($80);
  case temp of
    {判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
    $80:    if CheckBox16.State= cbchecked then  //選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原狀態(tài)
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin
              temp1:= InputArray[3] and ($7f);   //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
    $00:    if CheckBox16.State= cbchecked then  //選中狀態(tài)
            begin
              temp1:= Inputarray[3] or ($80);
              str:=Char($03)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未選中狀態(tài)
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);
              Comm1.Output:=str;
            end;
  end;
  Timer3.Enabled:=True;
end;

end.

作者: 野強(qiáng)強(qiáng)    時(shí)間: 2015-4-3 21:35
程序太長(zhǎng)了吧。。。。以后編寫(xiě)這么長(zhǎng)程序前是不是先要寫(xiě)清思路




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1