unit IoControl;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls,GTFunc,GlobalVar;

type
  TIoControlFrm = class(TForm)
    Label16: TLabel;
    Label18: TLabel;
    Label24: TLabel;
    Label25: TLabel;
    Label26: TLabel;
    LbControl05: TLabel;
    LbControl06: TLabel;
    LbControl07: TLabel;
    LbControl04: TLabel;
    LbControl02: TLabel;
    LbControl03: TLabel;
    LbControl01: TLabel;
    LbControl00: TLabel;
    LbBit07: TLabel;
    Label6: TLabel;
    LbBit06: TLabel;
    LbBit04: TLabel;
    LbBit05: TLabel;
    LbBit01: TLabel;
    LbBit02: TLabel;
    LbBit03: TLabel;
    LbBit00: TLabel;
    lbEx7: TLabel;
    lbEx6: TLabel;
    lbEx4: TLabel;
    lbEx5: TLabel;
    lbEx1: TLabel;
    lbEx2: TLabel;
    lbEx3: TLabel;
    lbEx0: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    LbControl13: TLabel;
    LbControl14: TLabel;
    LbControl15: TLabel;
    LbControl12: TLabel;
    LbControl10: TLabel;
    LbControl11: TLabel;
    LbControl09: TLabel;
    LbControl08: TLabel;
    LbBit15: TLabel;
    Label31: TLabel;
    LbBit14: TLabel;
    LbBit12: TLabel;
    LbBit13: TLabel;
    LbBit09: TLabel;
    LbBit10: TLabel;
    LbBit11: TLabel;
    LbBit08: TLabel;
    lbEx15: TLabel;
    lbEx14: TLabel;
    lbEx12: TLabel;
    lbEx13: TLabel;
    lbEx9: TLabel;
    lbEx10: TLabel;
    lbEx11: TLabel;
    lbEx8: TLabel;
    Splitter1: TSplitter;
    EdOutput: TEdit;
    LMessage: TLabel;
    btnEnter: TButton;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure EdOutputExit(Sender: TObject);
    procedure LbControl00Click(Sender: TObject);
    procedure LbControl01Click(Sender: TObject);
    procedure LbControl02Click(Sender: TObject);
    procedure LbControl03Click(Sender: TObject);
    procedure LbControl04Click(Sender: TObject);
    procedure LbControl06Click(Sender: TObject);
    procedure LbControl05Click(Sender: TObject);
    procedure LbControl07Click(Sender: TObject);
    procedure LbControl08Click(Sender: TObject);
    procedure LbControl09Click(Sender: TObject);
    procedure LbControl10Click(Sender: TObject);
    procedure LbControl11Click(Sender: TObject);
    procedure LbControl12Click(Sender: TObject);
    procedure LbControl13Click(Sender: TObject);
    procedure LbControl14Click(Sender: TObject);
    procedure LbControl15Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure EdOutputKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    selectNo:Integer;
    procedure OutPutOperate;
    procedure update_Label(Sender: TObject;no:Integer);
    procedure update_VLabel(Value:Word);
    { Public declarations }
  end;

procedure update_output(BitNo:integer;Value:integer);

var
  IoControlFrm: TIoControlFrm;

implementation

{$R *.dfm}

procedure TIoControlFrm.FormCreate(Sender: TObject);
begin
  SelectNo:=0;
end;

procedure TIoControlFrm.FormShow(Sender: TObject);
begin
   if lang=1 then
   begin
        btnEnter.Caption:='确定(&o)';
        Label24.Caption:='状态值各位的电平状态如下表所示：';
        Label25.Caption:='位';
        Label2.Caption:='位';
        label6.Caption:='位定义';
        label26.Caption:='电平状态';
        label31.Caption:='位定义';
        label3.Caption:='电平状态';
        LbBit00.Caption:='EXI0';LbBit01.Caption:='EXI1';LbBit02.Caption:='EXI2';
   	LbBit03.Caption:='EXI3';LbBit04.Caption:='EXI4';LbBit05.Caption:='EXI5';
   	LbBit06.Caption:='EXI6';LbBit07.Caption:='EXI7';LbBit08.Caption:='EXI8';
   	LbBit09.Caption:='EXI9';LbBit10.Caption:='EXI10';LbBit11.Caption:='EXI11';
   	LbBit12.Caption:='EXI12';LbBit13.Caption:='EXI13';LbBit14.Caption:='EXI14';
   	LbBit15.Caption:='EXI15';
   	if (selectNo=0) then
   	begin
   	   Timer1.Enabled:=true;
   	   Caption:='IO输入控制';
   	   Label18.Caption:='输入电平状态 (1--高电平，0--低电平):';
   	   Label16.Caption:='获得的电平状态值：';
           LbEx0.Caption:='第0位';LbEx1.Caption:='第1位';LbEx2.Caption:='第2位';
           LbEx3.Caption:='第3位';LbEx4.Caption:='第4位';LbEx5.Caption:='第5位';
           LbEx6.Caption:='第6位';LbEx7.Caption:='第7位';LbEx8.Caption:='第8位';
           LbEx9.Caption:='第9位';LbEx10.Caption:='第10位';LbEx11.Caption:='第11位';
           LbEx12.Caption:='第12位';LbEx13.Caption:='第13位';LbEx14.Caption:='第14位';
           LbEx15.Caption:='第15位';EdOutput.Text:='';
   	   EdOutput.Enabled:=false;
   	   LMessage.Caption:='调用GT_ExInpt从运动控制器通用数字量输入口获得16位的输入状态。';
   	end else begin
   	   Caption:='IO输出控制';
   	   Label18.Caption:='输出电平状态 (1--高电平，0--低电平):';
   	   Label16.Caption:='输出的电平状态值：';
   	   LbEx0.Caption:='EXO0';LbEx1.Caption:='EXO1';LbEx2.Caption:='EXO2';
   	   LbEx3.Caption:='EXO3';LbEx4.Caption:='EXO4';LbEx5.Caption:='EXO5';
   	   LbEx6.Caption:='EXO6';LbEx7.Caption:='EXO7';LbEx8.Caption:='EXO8';
   	   LbEx9.Caption:='EXO9';LbEx10.Caption:='EXO10';LbEx11.Caption:='EXO11';
   	   LbEx12.Caption:='EXO12';LbEx13.Caption:='EXO13';LbEx14.Caption:='EXO14';
   	   LbEx15.Caption:='EXO15';
   	   EdOutPut.Enabled:=true;
   	   LMessage.Caption:='调用GT_ExOpt设置16位的运动控制器通用数字量输出的状态。可点击电平状态或修改状态值改变输出状态';
   	   OutPutOperate;
   	end;
   end else begin
        btnEnter.Caption:='Confirm(&o)';
        Label24.Caption:='Status Showed by this table:';
        label6.Caption:='Definition of bit';
        Label25.Caption:='Bit';
        Label2.Caption:='Bit';
        label6.Caption:='Definition';
        label26.Caption:='Status';
        label31.Caption:='Definition';
        label3.Caption:='Status';

        Label18.Caption:='Output Status(1--High，0--Low):';
        LbBit00.Caption:='BIT0';LbBit01.Caption:='BIT1';LbBit02.Caption:='BIT2';
   	LbBit03.Caption:='BIT3';LbBit04.Caption:='BIT4';LbBit05.Caption:='BIT5';
   	LbBit06.Caption:='BIT6';LbBit07.Caption:='BIT7';LbBit08.Caption:='BIT8';
   	LbBit09.Caption:='BIT9';LbBit10.Caption:='BIT10';LbBit11.Caption:='BIT11';
   	LbBit12.Caption:='BIT12';LbBit13.Caption:='BIT13';LbBit14.Caption:='BIT14';
        LbBit15.Caption:='BIT15';
        if (selectNo=0) then
   	begin
           Caption:='IO Input Control';
   	   Timer1.Enabled:=true;
   	   Label16.Caption:='the status value getted from the input port：';
   	   LbEx0.Caption:='EXI0';LbEx1.Caption:='EXI1';LbEx2.Caption:='EXI2';
   	   LbEx3.Caption:='EXI3';LbEx4.Caption:='EXI4';LbEx5.Caption:='EXI5';
   	   LbEx6.Caption:='EXI6';LbEx7.Caption:='EXI7';LbEx8.Caption:='EXI8';
   	   LbEx9.Caption:='EXI9';LbEx10.Caption:='EXI10';LbEx11.Caption:='EXI11';
   	   LbEx12.Caption:='EXI12';LbEx13.Caption:='EXI13';LbEx14.Caption:='EXI14';
   	   LbEx15.Caption:='EXI15'; EdOutput.Text:='';
   	   EdOutput.Enabled:=false;
   	   LMessage.Caption:='to call GT_ExInpt()';
   	end else begin
           Caption:='IO Output Control';
   	   Label16.Caption:='output the status value：';
   	   LbEx0.Caption:='EXO0';LbEx1.Caption:='EXO1';LbEx2.Caption:='EXO2';
   	   LbEx3.Caption:='EXO3';LbEx4.Caption:='EXO4';LbEx5.Caption:='EXO5';
   	   LbEx6.Caption:='EXO6';LbEx7.Caption:='EXO7';LbEx8.Caption:='EXO8';
   	   LbEx9.Caption:='EXO9';LbEx10.Caption:='EXO10';LbEx11.Caption:='EXO11';
   	   LbEx12.Caption:='EXO12';LbEx13.Caption:='EXO13';LbEx14.Caption:='EXO14';
   	   LbEx15.Caption:='EXO15';
   	   EdOutPut.Enabled:=true;
   	   LMessage.Caption:='to call GT_ExOpt';
   	   OutPutOperate;
   	end;
   end;
end;

procedure TIoControlFrm.EdOutputExit(Sender: TObject);
begin
   if (EdOutput.Text='') or (StrToInt(EdOutput.Text)>65535) or (StrToInt(EdOutput.Text)<0)  then
   begin
        if lang=1 then
	    ShowMessage('输入值超过范围，重新输入')     
	else                                          
	    ShowMessage('error input'); 
      	EdOutput.Text:=IntToStr(exopt);
       	EdOutput.SetFocus();
      	exit;
   end;
   exopt:=StrToInt(EdOutput.Text);
   selectNo:=0;
   update_VLabel(exopt);
   selectNo:=1;
   rtn:=GT_ExOpt(StrToInt(EdOutput.Text));
   ShowResult(rtn,'GT_ExOpt',StrToInt(EdOutput.Text))
end;

procedure TIoControlFrm.OutPutOperate;
var i:integer;
begin
   EdOutput.Text:=IntToStr(exopt);
   selectNo:=0;
   update_VLabel(exopt);
   selectNo:=1;
end;

procedure TIoControlFrm.update_VLabel(Value:Word);
begin
     if (Value and (1 shl 0))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl00,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl00,0);

     if (Value and (1 shl 1))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl01,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl01,0);

     if (Value and (1 shl 2))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl02,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl02,0);

     if (Value and (1 shl 3))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl03,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl03,0);

     if (Value and (1 shl 4))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl04,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl04,0);

     if (Value and (1 shl 5))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl05,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl05,0);

     if (Value and (1 shl 6))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl06,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl06,0);

     if (Value and (1 shl 7))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl07,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl07,0);

     if (Value and (1 shl 8))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl08,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl08,0);

     if (Value and (1 shl 9))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl09,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl09,0);

     if (Value and (1 shl 10))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl10,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl10,0);

     if (Value and (1 shl 11))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl11,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl11,0);

     if (Value and (1 shl 12))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl12,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl12,0);

     if (Value and (1 shl 13))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl13,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl13,0);

     if (Value and (1 shl 14))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl14,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl14,0);

     if (Value and (1 shl 15))<>0 then
        IoControlFrm.update_Label(IoControlFrm.LbControl15,1)
     else
        IoControlFrm.update_Label(IoControlFrm.LbControl15,0);
end;

procedure update_output(BitNo:integer;Value:integer);
var o,temp:word;
    rtn:smallint;
begin
   if BitNo=0 then temp:=$0001;
   if BitNo=1 then temp:=$0002;
   if BitNo=2 then temp:=$0004;
   if BitNo=3 then temp:=$0008;
   if BitNo=4 then temp:=$0010;
   if BitNo=5 then temp:=$0020;
   if BitNo=6 then temp:=$0040;
   if BitNo=7 then temp:=$0080;
   if BitNo=8 then temp:=$0100;
   if BitNo=9 then temp:=$0200;
   if BitNo=10 then temp:=$0400;
   if BitNo=11 then temp:=$0800;
   if BitNo=12 then temp:=$1000;
   if BitNo=13 then temp:=$2000;
   if BitNo=14 then temp:=$4000;
   if BitNo=15 then temp:=$8000;

   if value=0 then begin
          o:=exopt and (not(temp));
                   end;
   if value=1 then begin
          o:=exopt or temp;
                   end;
    rtn:=GT_ExOpt(o);
    if rtn=0 then exopt:=o;
    ShowResult(rtn,'GT_ExOpt',o);
end;

procedure TIoControlFrm.update_Label(Sender: TObject;no:Integer);
begin
   if SelectNo=0 then
   begin
         (Sender As TLabel).Caption:=IntToStr(no);
   end
   else begin
      if (Sender As TLabel).Caption='1' then
      begin
         (Sender As TLabel).Caption:='0';
         update_output(no,0);
      end else begin
         (Sender As TLabel).Caption:='1';
         update_output(no,1);
      end;
      EdOutput.Text:=IntToStr(exopt);
   end;
end;

procedure TIoControlFrm.LbControl00Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl00,0);
end;

procedure TIoControlFrm.LbControl01Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl01,1);
end;

procedure TIoControlFrm.LbControl02Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl02,2);
end;

procedure TIoControlFrm.LbControl03Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl03,3);
end;

procedure TIoControlFrm.LbControl04Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl04,4);
end;

procedure TIoControlFrm.LbControl06Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl06,6);
end;

procedure TIoControlFrm.LbControl05Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl05,5);
end;

procedure TIoControlFrm.LbControl07Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl07,7);
end;

procedure TIoControlFrm.LbControl08Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl08,8);
end;

procedure TIoControlFrm.LbControl09Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl09,9);
end;

procedure TIoControlFrm.LbControl10Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl10,10);
end;

procedure TIoControlFrm.LbControl11Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl11,11);
end;

procedure TIoControlFrm.LbControl12Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl12,12);
end;

procedure TIoControlFrm.LbControl13Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl13,13);
end;

procedure TIoControlFrm.LbControl14Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl14,14);
end;

procedure TIoControlFrm.LbControl15Click(Sender: TObject);
begin
   if SelectNo=0 then exit;
   update_Label(LbControl15,15);
end;

procedure TIoControlFrm.Timer1Timer(Sender: TObject);
var InOpt:Word;
begin
     Timer1.Enabled:=false;
     GT_ExInpt(@InOpt);
     if (EdOutput.Text='') then
        EdOutput.Text:=IntToStr(InOpt)
     else if (StrToInt(EdOutput.Text)=InOpt) then
     begin
           timer1.Enabled:=true;
           exit;
     end;
     EdOutput.Text:=IntToStr(InOpt);
     update_VLabel(InOpt);
     Timer1.Enabled:=true;
end;

procedure TIoControlFrm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
   Timer1.Enabled:=false;
end;

procedure TIoControlFrm.EdOutputKeyPress(Sender: TObject; var Key: Char);
begin
   if not (key in DigitsSet) then
   begin
       key:=#0;
       exit;
   end;
end;

end.
