找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5482|回复: 1
打印 上一主题 下一主题
收起左侧

上位机与S7-200 cpu224自由口通讯之上位机源程序

[复制链接]
跳转到指定楼层
楼主
ID:72519 发表于 2015-1-23 00:34 | 只看该作者 回帖奖励 |正序浏览 |阅读模式








{本程序为s7-200 cpu224自由口通讯实验,由上位机发送,PLC返回查询输入输出状态
1.01 xx FF;若为$01开头则视为只查询输入输出状态
2.02 xx FF;若为$02开头则视为查询输入输出状态,同时写QB0
3.03 xx FF;若为$03开头则视为查询输入输出状态,同时写QB1
   其中FF为自由口结束字符
   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;//奇偶交验
    ByteSize:=_8;//数据位 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,后再分选中和非选中两种状态的处理}
    $01:    if CheckBox1.State= cbchecked then  //选中状态
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[2] and ($fe);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox1.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[2] or ($01);
              str:=Char($02)+char( temp1)+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $02:    if CheckBox2.State= cbchecked then  //选中状态
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[2] and ($fd);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox2.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[2] or ($02);
              str:=Char($02)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $04:    if CheckBox3.State= cbchecked then  //选中状态
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[2] and ($fb);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox3.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[2] or ($04);
              str:=Char($02)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $08:    if CheckBox4.State= cbchecked then  //选中状态
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[2] and ($f7);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox4.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[2] or ($08);
              str:=Char($02)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $10:    if CheckBox5.State= cbchecked then  //选中状态
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[2] and ($ef);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox5.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[2] or ($10);
              str:=Char($02)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $20:    if CheckBox6.State= cbchecked then  //选中状态
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[2] and ($df);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox6.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[2] or ($20);
              str:=Char($02)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $40:    if CheckBox7.State= cbchecked then  //选中状态
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[2] and ($bf);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox7.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[2] or ($40);
              str:=Char($02)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $80:    if CheckBox8.State= cbchecked then  //选中状态
            begin

              str:=Char($02)+char( InputArray[2])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[2] and ($7f);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($02)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox8.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[2] or ($80);
              str:=Char($02)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $01:    if CheckBox9.State= cbchecked then  //选中状态
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[3] and ($fe);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox9.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[3] or ($01);
              str:=Char($03)+char( temp1)+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $02:    if CheckBox10.State= cbchecked then  //选中状态
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[3] and ($fd);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox10.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[3] or ($02);
              str:=Char($03)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $04:    if CheckBox11.State= cbchecked then  //选中状态
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[3] and ($fb);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox11.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[3] or ($04);
              str:=Char($03)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $08:    if CheckBox12.State= cbchecked then  //选中状态
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[3] and ($f7);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox12.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[3] or ($08);
              str:=Char($03)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $10:    if CheckBox13.State= cbchecked then  //选中状态
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[3] and ($ef);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox13.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[3] or ($10);
              str:=Char($03)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $20:    if CheckBox14.State= cbchecked then  //选中状态
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[3] and ($df);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox14.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[3] or ($20);
              str:=Char($03)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $40:    if CheckBox15.State= cbchecked then  //选中状态
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[3] and ($bf);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox15.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[3] or ($40);
              str:=Char($03)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未选中状态
            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,后再分选中和非选中两种状态的处理}
    $80:    if CheckBox16.State= cbchecked then  //选中状态
            begin

              str:=Char($03)+char( InputArray[3])+ Char($ff);  //保持原状态
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin
              temp1:= InputArray[3] and ($7f);   //与1111 1110进行与运算 ,使此位为0
              str:=Char($03)+char(temp1)+ Char($ff);
              Comm1.Output:=str;
            end;
      {判断分两种情况原先为此位为0,后再分选中和非选中两种状态的处理}
    $00:    if CheckBox16.State= cbchecked then  //选中状态
            begin
              temp1:= Inputarray[3] or ($80);
              str:=Char($03)+char( temp1)+ Char($ff);  //
              Comm1.Output:=str;
            end
            else                      //未选中状态
            begin

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

end.
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:75077 发表于 2015-4-3 21:35 | 只看该作者
程序太长了吧。。。。以后编写这么长程序前是不是先要写清思路
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表