unit DemoMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,GTFunc,GlobalVar, ExtCtrls,  ComCtrls,DataDlg,
  LmtData,DataDlg1,SinAxis,IoControl;

type
  Tdemoform = class(TForm)
    Panel1: TPanel;
    btnSaveFile: TButton;
    btnEditFile: TButton;
    btnExit: TButton;
    btnOpenFile: TButton;
    AxisPosition: TGroupBox;
    Lb_Pos1: TLabel;
    Lb_Pos2: TLabel;
    Lb_Pos3: TLabel;
    MotionPage: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    TabSheet4: TTabSheet;
    TabSheet5: TTabSheet;
    btnOpen: TButton;
    btnClose: TButton;
    HardRst: TButton;
    Reset: TButton;
    DrvRst: TButton;
    btnAlarm: TButton;
    CtrlMode: TButton;
    btnLimt: TButton;
    SetStrtVel: TButton;
    SetMaxVel: TButton;
    SetStpAcc: TButton;
    OpenAxis: TButton;
    SetSynAcc: TButton;
    SetSynVel: TButton;
    LnXYZ: TButton;
    LnXY: TButton;
    ArcYZ2: TButton;
    ArcYZ1: TButton;
    ArcXY2: TButton;
    ArcXY1: TButton;
    SetDFlag: TButton;
    ArcZX2: TButton;
    ArcZX1: TButton;
    btnCaptHome: TButton;
    btnSetAltPos: TButton;
    btnRstSts: TButton;
    btnClrSts: TButton;
    btnZeroPos: TButton;
    Button35: TButton;
    Button36: TButton;
    Button37: TButton;
    Button38: TButton;
    Button39: TButton;
    Button40: TButton;
    btnRestoreMtn: TButton;
    btnEStpMtn: TButton;
    btnStpMtn: TButton;
    btnRunCmd: TButton;
    btnOverride: TButton;
    PageControl1: TPageControl;
    TabSheet6: TTabSheet;
    TabSheet8: TTabSheet;
    Lb_CrdStsBit0: TLabel;
    Lb_CrdStsBit1: TLabel;
    Lb_CrdStsBit2: TLabel;
    Lb_CrdStsBit3: TLabel;
    Lb_CrdStsBit4: TLabel;
    Lb_CrdStsBit5: TLabel;
    Lb_CrdStsBit6: TLabel;
    Lb_CrdStsBit7: TLabel;
    Lb_CrdStsBit8: TLabel;
    Lb_CrdStsBit9: TLabel;
    Lb_CrdStsBit10: TLabel;
    Lb_CrdStsShowBit10: TLabel;
    Lb_CrdStsShowBit9: TLabel;
    Lb_CrdStsShowBit8: TLabel;
    Lb_CrdStsShowBit7: TLabel;
    Lb_CrdStsShowBit6: TLabel;
    Lb_CrdStsShowBit5: TLabel;
    Lb_CrdStsShowBit4: TLabel;
    Lb_CrdStsShowBit3: TLabel;
    Lb_CrdStsShowBit2: TLabel;
    Lb_CrdStsShowBit1: TLabel;
    Lb_CrdStsShowBit0: TLabel;
    TabSheet7: TTabSheet;
    Lb_MtnDone: TLabel;
    Lb_ServoAlm: TLabel;
    Lb_BrkReached: TLabel;
    Lb_Captured: TLabel;
    Lb_MtnErr: TLabel;
    Lb_PosLmtTriggered: TLabel;
    Lb_NegLmtTriggered: TLabel;
    Lb_HostCmdErr: TLabel;
    Lb_LoopClosed: TLabel;
    Lb_AxisOn: TLabel;
    Lb_InMtn: TLabel;
    Lb_LmtOn: TLabel;
    Lb_PrflT: TLabel;
    Lb_PrflS: TLabel;
    Lb_PrflV: TLabel;
    Lb_PrflG: TLabel;
    Lb_PrflC: TLabel;
    Lb_HomeCapt: TLabel;
    Lb_IndexCapt: TLabel;
    Lb_IndexCapt1: TLabel;
    Lb_HomeCapt1: TLabel;
    Lb_PrflC1: TLabel;
    Lb_PrflG1: TLabel;
    Lb_PrflV1: TLabel;
    Lb_PrflS1: TLabel;
    Lb_PrflT1: TLabel;
    Lb_LmtOn1: TLabel;
    Lb_InMtn1: TLabel;
    Lb_AxisOn1: TLabel;
    Lb_LoopClosed1: TLabel;
    Lb_HostCmdErr1: TLabel;
    Lb_NegLmtTriggered1: TLabel;
    Lb_PosLmtTriggered1: TLabel;
    Lb_MtnErr1: TLabel;
    Lb_Captured1: TLabel;
    Lb_BrkReached1: TLabel;
    Lb_ServoAlm1: TLabel;
    Lb_MtnDone1: TLabel;
    Lb_Axis11: TLabel;
    Lb_Axis22: TLabel;
    Lb_Axis33: TLabel;
    Lb_MtnDone2: TLabel;
    Lb_ServoAlm2: TLabel;
    Lb_BrkReached2: TLabel;
    Lb_Captured2: TLabel;
    Lb_MtnErr2: TLabel;
    Lb_PosLmtTriggered2: TLabel;
    Lb_NegLmtTriggered2: TLabel;
    Lb_HostCmdErr2: TLabel;
    Lb_LoopClosed2: TLabel;
    Lb_AxisOn2: TLabel;
    Lb_InMtn2: TLabel;
    Lb_LmtOn2: TLabel;
    Lb_PrflT2: TLabel;
    Lb_PrflS2: TLabel;
    Lb_PrflV2: TLabel;
    Lb_PrflG2: TLabel;
    Lb_HomeCapt2: TLabel;
    Lb_IndexCapt2: TLabel;
    Lb_PrflC2: TLabel;
    Lb_MtnDone3: TLabel;
    Lb_ServoAlm3: TLabel;
    Lb_BrkReached3: TLabel;
    Lb_Captured3: TLabel;
    Lb_MtnErr3: TLabel;
    Lb_PosLmtTriggered3: TLabel;
    Lb_NegLmtTriggered3: TLabel;
    Lb_HostCmdErr3: TLabel;
    Lb_LoopClosed3: TLabel;
    Lb_AxisOn3: TLabel;
    Lb_InMtn3: TLabel;
    Lb_LmtOn3: TLabel;
    Lb_PrflT3: TLabel;
    Lb_PrflS3: TLabel;
    Lb_PrflV3: TLabel;
    Lb_PrflG3: TLabel;
    Lb_PrflC3: TLabel;
    Lb_HomeCapt3: TLabel;
    Lb_IndexCapt3: TLabel;
    Bevel1: TBevel;
    CmdStsMemo: TMemo;
    btnSaveCmd: TButton;
    btnClearCmd: TButton;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    GetStsTimer: TTimer;
    Timer2: TTimer;
    Axis1: TLabel;
    Axis2: TLabel;
    Axis3: TLabel;
    Bevel2: TBevel;
    LMessageSts1: TLabel;
    LMessageSts2: TLabel;
    MvXY: TButton;
    MvXYZ: TButton;
    btnIoInput: TButton;
    btnIOOutput: TButton;
    PGMemo: TPageControl;
    TbCmdMemo: TTabSheet;
    TbFileMemo: TTabSheet;
    CmdMemo: TMemo;    
    FileMemo: TMemo;
    btnStrtList: TButton;
    btnEndList: TButton;
    btnAddList: TButton;
    btnStrtMtn: TButton;
    SetCrdOffset: TButton;
    btnGetMtnNo: TButton;
    procedure XChangeLanguage;
    procedure ActiveButton(no:Integer);
    procedure FormCreate(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure HardRstClick(Sender: TObject);
    procedure DrvRstClick(Sender: TObject);
    procedure ResetClick(Sender: TObject);
    procedure CtrlModeClick(Sender: TObject);
    procedure btnAlarmClick(Sender: TObject);
    procedure btnLimtClick(Sender: TObject);
    procedure SetStrtVelClick(Sender: TObject);
    procedure SetStpAccClick(Sender: TObject);
    procedure SetMaxVelClick(Sender: TObject);
    procedure GTStrtListClick(Sender: TObject);
    procedure OpenAxisClick(Sender: TObject);
    procedure SetSynVelClick(Sender: TObject);
    procedure GetStsTimerTimer(Sender: TObject);
    procedure MvXYZClick(Sender: TObject);
    procedure MvXYClick(Sender: TObject);
    procedure SetSynAccClick(Sender: TObject);
    procedure LnXYClick(Sender: TObject);
    procedure LnXYZClick(Sender: TObject);
    procedure ArcXY1Click(Sender: TObject);
    procedure ArcXY2Click(Sender: TObject);
    procedure ArcYZ1Click(Sender: TObject);
    procedure ArcYZ2Click(Sender: TObject);
    procedure ArcZX1Click(Sender: TObject);
    procedure ArcZX2Click(Sender: TObject);
    procedure SetDFlagClick(Sender: TObject);
    procedure SetCrdOffsetClick(Sender: TObject);
    procedure btnSetAltPosClick(Sender: TObject);
    procedure btnRstStsClick(Sender: TObject);
    procedure btnCaptHomeClick(Sender: TObject);
    procedure btnClrStsClick(Sender: TObject);
    procedure btnZeroPosClick(Sender: TObject);
    procedure Button35MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button35MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Timer2Timer(Sender: TObject);
    procedure Button40Click(Sender: TObject);
    procedure Button40MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button40MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button36MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button39MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button37MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button38MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button38MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button37MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button36MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button39MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnRunCmdClick(Sender: TObject);
    procedure btnEStpMtnClick(Sender: TObject);
    procedure btnStpMtnClick(Sender: TObject);
    procedure btnOverrideClick(Sender: TObject);
    procedure btnIoInputClick(Sender: TObject);
    procedure btnIOOutputClick(Sender: TObject);
    procedure btnOpenFileClick(Sender: TObject);
    procedure CmdMemoChange(Sender: TObject);
    procedure btnEditFileClick(Sender: TObject);
    procedure btnSaveFileClick(Sender: TObject);
    procedure PGMemoChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnExitClick(Sender: TObject);
    procedure btnSaveCmdClick(Sender: TObject);
    procedure btnClearCmdClick(Sender: TObject);
    procedure btnStrtListClick(Sender: TObject);
    procedure btnEndListClick(Sender: TObject);
    procedure btnAddListClick(Sender: TObject);
    procedure btnStrtMtnClick(Sender: TObject);
    procedure btnGetMtnNoClick(Sender: TObject);
    procedure btnRestoreMtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    procedure RunGTCmd();
    { Public declarations }
  end;

procedure sts_update(ss1,ss2,ss3,n:word;var Lb1,Lb2,Lb3:TLabel);
function JudgeErrorbyAxis:Boolean;

var
  demoform:Tdemoform;
  AxisNo:integer;
  BaseAtlPos:array [0..3] of LongInt;

implementation

uses MtnNo;

{$R *.dfm}


procedure Tdemoform.ActiveButton(no:Integer);
begin
  DataDlgfrm.SelectNo:=no;
  if no=0 then
  begin
    if lang=1 then
    begin
      	DataFrm.LMessage.Caption:='GT_Open()打开运动控制。';
      	DataFrm.LBusTypeSel.Caption:='运动控制器总线：';
      	DataFrm.LAddress.Caption:='运动控制器基地址：';
      	DataFrm.LIrq.Caption:='运动控制器中断号：';
      	DataFrm.Caption:='打开运动控制器';
    end
    else begin
      	DataFrm.LMessage.Caption:='GT_Open() Open the Motion Control。';
      	DataFrm.LBusTypeSel.Caption:='Bus Type：';
      	DataFrm.LAddress.Caption:='Base Address：';
      	DataFrm.LIrq.Caption:='Interrupt No：';
      	DataFrm.Caption:='Open the Motion Cotrol';
    end;
    DataFrm.LBusTypeSel.Visible:=true;
    DataFrm.CBusTypeSel.Visible:=true;
    DataFrm.CBusTypeSel.Items.Clear();
    DataFrm.CBusTypeSel.Items.Add('PCI');
    DataFrm.CBusTypeSel.Items.Add('ISA');
    DataFrm.CBusTypeSel.ItemIndex:=0;
    DataFrm.LAddress.Visible:=true;
    DataFrm.Address.Visible:=true;
    DataFrm.Address.Text:='65535';
    DataFrm.LIrq.Visible:=true;
    DataFrm.Irq.Visible:=true;
    DataFrm.Irq.Text:='0';
    DataFrm.LClose1.Visible:=false;
    DataFrm.LClose2.Visible:=false;
    DataFrm.CBusTypeSel.ItemIndex:=0;
    DataFrm.Address.Enabled:=false;
  end
  else if (no=1) or (no=2)or (no=4) or (no=231) or (no=232) or (no=233) or (no=234) or (no=113) then
  begin
    DataFrm.LBusTypeSel.Visible:=false;
    DataFrm.CBusTypeSel.Visible:=false;
    DataFrm.LAddress.Visible:=false;
    DataFrm.Address.Visible:=false;
    DataFrm.LIrq.Visible:=false;
    DataFrm.Irq.Visible:=false;
    DataFrm.LClose1.Visible:=true;
    DataFrm.LClose2.Visible:=true;
    if lang=1 then  
    begin           
    	if no=1 then
    	begin
    	  DataFrm.Caption:='关闭运动控制器';
    	  DataFrm.LClose1.Caption:='确认关闭运动控制器吗？';
    	  DataFrm.LClose2.Caption:='关闭运动控制器请点确定；否则请点取消。';
    	  DataFrm.LMessage.Caption:='GT_Close()关闭运动控制器设备。 ';
    	end
    	else if no=2 then
    	begin
    	  DataFrm.Caption:='复位运动控制器';
    	  DataFrm.LClose1.Caption:='确认复位运动控制器吗？';
    	  DataFrm.LClose2.Caption:='复位运动控制器请点确定；否则请点取消。';
    	  DataFrm.LMessage.Caption:='GT_HardRst()重新设置所有的控制寄存器的初始化状态。 ';
    	end
    	else if no=4 then
    	begin
    	  DataFrm.Caption:='软复位运动控制器';
    	  DataFrm.LClose1.Caption:='确认软复位运动控制器吗？';
    	  DataFrm.LClose2.Caption:='软复位运动控制器请点确定；否则请点取消。';
    	  DataFrm.LMessage.Caption:='GT_Reset()以主机命令形式使运动控制复位。 ';
    	end
    	else if no=231 then
    	begin
    	  DataFrm.Caption:='打开并清空缓冲区';
    	  DataFrm.LClose1.Caption:='开始缓冲区命令输入状态吗？';
    	  DataFrm.LClose2.Caption:='开始缓冲区命令输入状态请点确定；否则请点取消。';
    	  DataFrm.LMessage.Caption:='GT_StrtList()打开缓冲区，若缓冲区内有未执行的命令，则清空缓冲区。该命令在缓冲区处于关闭状态时有效。此函数后必须紧接定位缓冲区坐标起点位置命令（GT_MvXY、GT_MvXYZ）。';
    	end
    	else if no=233 then
    	begin
    	  DataFrm.Caption:='再次打开缓冲区';
    	  DataFrm.LClose1.Caption:='再次打开缓冲区命令输入状态吗？';
    	  DataFrm.LClose2.Caption:='再次打开缓冲区命令输入状态请点确定；否则请点取消。';
    	  DataFrm.LMessage.Caption:='GT_AddList()打开已经关闭的缓冲区，向缓冲区增加新的连续轨迹命令。';
    	end
    	else if no=232 then
    	begin
    	  DataFrm.Caption:='关闭缓冲区';
    	  DataFrm.LClose1.Caption:='关闭缓冲区命令输入状态吗？';
    	  DataFrm.LClose2.Caption:='关闭缓冲区命令输入状态请点确定；否则请点取消。';
    	  DataFrm.LMessage.Caption:='GT_EndList()关闭缓冲区，结束对缓冲区的操作。该命令在缓冲区处于打开状态下有效。';
    	end
    	else if no=234 then
    	begin
    	  DataFrm.Caption:='启动缓冲区';
    	  DataFrm.LClose1.Caption:='启动缓冲区中坐标系命令吗？';
    	  DataFrm.LClose2.Caption:='启动缓冲区请点确定；否则请点取消。';
    	  DataFrm.LMessage.Caption:='调用GT_StrtMtn()在启动缓冲区中命令之前，用户必须确定运动控制器的缓冲区内有运动轨迹描述命令可以被执行，也就是说在此之前主机已调用过GT_StrtList()和GT_MvXY（GT_MvXYZ）命令。';
    	end
    	else if no=113 then
    	begin
    	  DataFrm.Caption:='设置减速点标志';
    	  DataFrm.LClose1.Caption:='需设置减速点标志？';
    	  DataFrm.LClose2.Caption:='需要请点确定；否则请点取消。';
    	  DataFrm.LMessage.Caption:='GT_SetDccFlag()设置控制卡的减速标志。用户可在需要减速到启动速度的轨迹段发送该指令，通知运动控制器在该段的终点将速度降到启动速度。';
    	end;
    end else begin
    	if no=1 then
    	begin
    	  DataFrm.Caption:='Close the Motion Control';
    	  DataFrm.LClose1.Caption:='Close the Motion Control?';
    	  DataFrm.LClose2.Caption:='Confirm for Yes ,or Cancel for No.';
    	  DataFrm.LMessage.Caption:='To call GT_Close(): Close the Motion Control.';
    	end
    	else if no=2 then
    	begin
    	  DataFrm.Caption:='Reset the Motion Control';
    	  DataFrm.LClose1.Caption:='Reset the Motion Control?';
    	  DataFrm.LClose2.Caption:='Confirm for Yes ,or Cancel for No.';
    	  DataFrm.LMessage.Caption:='To call GT_HardRst()to reset all control register.';
    	end
    	else if no=4 then
    	begin
    	  DataFrm.Caption:='Reset the Motion Control';
    	  DataFrm.LClose1.Caption:='Reset the Motion Control?';
    	  DataFrm.LClose2.Caption:='Confirm for Yes ,or Cancel for No.';
    	  DataFrm.LMessage.Caption:='To call GT_Reset() to Reset the Motion Control ';
    	end
    	else if no=231 then
    	begin
    	  DataFrm.Caption:='Open buffer command ';
    	  DataFrm.LClose1.Caption:='Open buffer Command?';
    	  DataFrm.LClose2.Caption:='Confirm for Yes ,or Cancel for No.';
    	  DataFrm.LMessage.Caption:='To Call GT_StrtList()';
    	end
    	else if no=232 then
    	begin
    	  DataFrm.Caption:='Close buffer command';
    	  DataFrm.LClose1.Caption:='If you wan to close buffer command?';
    	  DataFrm.LClose2.Caption:='Confirm for Yes ,or Cancel for No.';
    	  DataFrm.LMessage.Caption:='To call GT_AddList()';
    	end
    	else if no=233 then
    	begin
    	  DataFrm.Caption:='Reopen buffer command';
    	  DataFrm.LClose1.Caption:='If you want to repoen buffer command?';
    	  DataFrm.LClose2.Caption:='Confirm for Yes ,or Cancel for No.';
    	  DataFrm.LMessage.Caption:='To call GT_EndList()';
    	end
    	else if no=234 then
    	begin
    	  DataFrm.Caption:='Start buffer command';
    	  DataFrm.LClose1.Caption:='if you want to start buffer command?';
    	  DataFrm.LClose2.Caption:='Confirm for yes,or Cancel for No.';
    	  DataFrm.LMessage.Caption:='To call GT_StrtMtn()';
    	end
    	else if no=113 then
    	begin
    	  DataFrm.Caption:='Set flag of deaccelerate point';
    	  DataFrm.LClose1.Caption:='Need to set flag of deaccelerate point?';
    	  DataFrm.LClose2.Caption:='Confirm for yes,or Cancel for No.';
    	  DataFrm.LMessage.Caption:='To call GT_SetDccFlag()';
    	end;
    end;
  end
  else if no=5 then
  begin
  end
  else if no=6 then
  begin
  end
  else if (no=7) or (no=8) or (no=9) then
  begin
    DataFrm.LBusTypeSel.Visible:=false;
    DataFrm.CBusTypeSel.Visible:=false;
    DataFrm.LAddress.Visible:=true;
    DataFrm.Address.Visible:=true;
    DataFrm.Address.Enabled:=true;
    DataFrm.LIrq.Visible:=false;
    DataFrm.Irq.Visible:=false;
    DataFrm.LClose1.Visible:=false;
    DataFrm.LClose2.Visible:=false;
    if lang=1 then
    begin
    	if no=7 then
    	begin
    	  DataFrm.Caption:='设置起跳频率';
    	  DataFrm.LAddress.Caption:='设置起跳频率';
    	  DataFrm.LMessage.Caption:='GT_SetStrtVel()设定系统的启动速度,该值和电机特性机械系统特性有直接关系，单位：脉冲/控制周期。';
    	end
    	else if no=8 then
    	begin
    	  DataFrm.Caption:='设置停止加速度';
    	  DataFrm.LAddress.Caption:='设置停止加速度';
    	  DataFrm.LMessage.Caption:='GT_SetStpAcc()设定系统的紧急停止的加速度，通常为正常加速度的2-3倍，单位：脉冲/控制周期＊控制周期。';
    	end
    	else if no=9 then
    	begin
    	  DataFrm.Caption:='设置最大加速度';
    	  DataFrm.LAddress.Caption:='设置最大加速度';
    	  DataFrm.LMessage.Caption:='GT_SetMaxVel()设定系统的最高运行速度,用户设定速度大于此速度时按此速度运行，单位：脉冲/控制周期。';
    	end;
    end else begin
      	if no=7 then
     	begin
     	  DataFrm.Caption:='set start frequency';
     	  DataFrm.LAddress.Caption:='set start frequency';
     	  DataFrm.LMessage.Caption:='to call GT_SetStrtVel() unit：pulse/ST';
     	end
     	else if no=8 then
     	begin
     	  DataFrm.Caption:='set stop acceleration';
     	  DataFrm.LAddress.Caption:='set stop acceleration';
     	  DataFrm.LMessage.Caption:='to call GT_SetStpAcc() unit：pulse/(ST*ST)';
     	end
     	else if no=9 then
     	begin
     	  DataFrm.Caption:='set max velocity ';
     	  DataFrm.LAddress.Caption:='set max velocity ';
     	  DataFrm.LMessage.Caption:='to call GT_SetMaxVel() unit：pulse/ST';
     	end;  
    end;
  end
  else if (no=101) or (no=102) or (no=103) or (no=104)  then
  begin
    DataDlgfrm.LbStartX.Visible:=true;
    DataDlgfrm.EdStartX.Visible:=true;
    DataDlgfrm.LbStartY.Visible:=true;
    DataDlgfrm.EdStartY.Visible:=true;
    DataDlgfrm.LbStartZ.Visible:=true;
    DataDlgfrm.EdStartZ.Visible:=true;
    DataDlgfrm.LbSynVel.Visible:=true;
    DataDlgfrm.EdSynVel.Visible:=true;
    DataDlgfrm.LbSynAcc.Visible:=true;
    DataDlgfrm.EdSynAcc.Visible:=true;
    DataDlgfrm.CBDir.Visible:=false;

    DataDlgfrm.EdStartX.Text:=FloatToStr(MvStartX);
    DataDlgfrm.EdStartY.Text:=FloatToStr(MvStartY);
    DataDlgfrm.EdStartZ.Text:=FloatToStr(MvStartZ);
    DataDlgfrm.EdSynVel.Text:=FloatToStr(SynVel);
    DataDlgfrm.EdSynAcc.Text:=FloatToStr(SynAcc);
    if (lang=1) then
    begin
          DataDlgfrm.LbStartX.Caption:='Ｘ起点';
          DataDlgfrm.LbStartY.Caption:='Ｙ起点';
          DataDlgfrm.LbStartZ.Caption:='Ｚ起点';
          DataDlgfrm.LbSynVel.Caption:='合成速度';
          DataDlgfrm.LbSynAcc.Caption:='合成加速度';
    end
    else begin
          DataDlgfrm.LbStartX.Caption:='X Start Point';
          DataDlgfrm.LbStartY.Caption:='Y Start Point';
          DataDlgfrm.LbStartZ.Caption:='Z Start Point';
          DataDlgfrm.LbSynVel.Caption:='Mixed Vel';
          DataDlgfrm.LbSynAcc.Caption:='Mixed Acc';
    end;

    if ((no=101) or (no=102)) then
    begin
          DataDlgfrm.LbStartX.Enabled:=true;
          DataDlgfrm.LbStartY.Enabled:=true;
          DataDlgfrm.LbSynVel.Enabled:=true;
          DataDlgfrm.LbSynAcc.Enabled:=true;
          DataDlgfrm.EdStartX.Enabled:=true;
          DataDlgfrm.EdStartY.Enabled:=true;
          DataDlgfrm.EdSynVel.Enabled:=true;
          DataDlgfrm.EdSynAcc.Enabled:=true;
          if (no=101) then
          begin
             DataDlgfrm.LbStartZ.Enabled:=false;
             DataDlgfrm.EdStartZ.Enabled:=false;
          end else begin
             DataDlgfrm.LbStartZ.Enabled:=true;
             DataDlgfrm.EdStartZ.Enabled:=true;
          end;
    end else begin
          DataDlgfrm.LbStartX.Enabled:=false;
          DataDlgfrm.LbStartY.Enabled:=false;
          DataDlgfrm.LbStartZ.Enabled:=false;
          DataDlgfrm.LbSynVel.Enabled:=false;
          DataDlgfrm.LbSynAcc.Enabled:=false;
          DataDlgfrm.EdStartX.Enabled:=false;
          DataDlgfrm.EdStartY.Enabled:=false;
          DataDlgfrm.EdSynVel.Enabled:=false;
          DataDlgfrm.EdSynAcc.Enabled:=false;
          DataDlgfrm.EdStartZ.Enabled:=false;
          if (no=103) then
          begin
            DataDlgfrm.LbSynVel.Enabled:=true;
            DataDlgfrm.EdSynVel.Enabled:=true;
          end else begin
            DataDlgfrm.LbSynAcc.Enabled:=true;
            DataDlgfrm.EdSynAcc.Enabled:=true;
          end;
    end;
  end
  else if (no=105) or (no=106) then
  begin
    DataDlgfrm.LbStartX.Visible:=true;
    DataDlgfrm.EdStartX.Visible:=true;
    DataDlgfrm.LbStartY.Visible:=true;
    DataDlgfrm.EdStartY.Visible:=true;
    DataDlgfrm.LbStartZ.Visible:=true;
    DataDlgfrm.EdStartZ.Visible:=true;
    DataDlgfrm.LbSynVel.Visible:=true;
    DataDlgfrm.EdSynVel.Visible:=true;
    DataDlgfrm.LbSynAcc.Visible:=true;
    DataDlgfrm.EdSynAcc.Visible:=true;
    DataDlgfrm.CBDir.Visible:=false;

     DataDlgfrm.EdStartX.Text:='0';
     DataDlgfrm.EdStartY.Text:='0';
     DataDlgfrm.EdStartZ.Text:='0';
     DataDlgfrm.EdSynVel.Text:=FloatToStr(SynVel);
     DataDlgfrm.EdSynAcc.Text:=FloatToStr(SynAcc);
     if (lang=1) then
     begin
          DataDlgfrm.LbStartX.Caption:='Ｘ终点';
          DataDlgfrm.LbStartY.Caption:='Ｙ终点';
          DataDlgfrm.LbStartZ.Caption:='Ｚ终点';
          DataDlgfrm.LbSynVel.Caption:='合成速度';
          DataDlgfrm.LbSynAcc.Caption:='合成加速度';
     end
     else begin
          DataDlgfrm.LbStartX.Caption:='X End Point';
          DataDlgfrm.LbStartY.Caption:='Y End Point';
          DataDlgfrm.LbStartZ.Caption:='Z End Point';
          DataDlgfrm.LbSynVel.Caption:='Mixed Vel';
          DataDlgfrm.LbSynAcc.Caption:='Mixed Acc';
     end;
     DataDlgfrm.LbStartX.Enabled:=true;
     DataDlgfrm.LbStartY.Enabled:=true;
     DataDlgfrm.EdStartX.Enabled:=true;
     DataDlgfrm.EdStartY.Enabled:=true;
     DataDlgfrm.LbSynVel.Enabled:=false;
     DataDlgfrm.LbSynAcc.Enabled:=false;
     DataDlgfrm.EdSynVel.Enabled:=false;
     DataDlgfrm.EdSynAcc.Enabled:=false;
     if (no=105) then
     begin
        DataDlgfrm.LbStartZ.Enabled:=false;
        DataDlgfrm.EdStartZ.Enabled:=false;
     end else begin
        DataDlgfrm.LbStartZ.Enabled:=true;
        DataDlgfrm.EdStartZ.Enabled:=true;
     end;
  end
  else if (no=107) or (no=108) or (no=109) or (no=110) or (no=111) or (no=112) then
  begin
    DataDlgfrm.LbStartX.Visible:=false;
    DataDlgfrm.EdStartX.Visible:=false;
    DataDlgfrm.LbStartY.Visible:=true;
    DataDlgfrm.EdStartY.Visible:=true;
    DataDlgfrm.LbStartZ.Visible:=true;
    DataDlgfrm.EdStartZ.Visible:=true;
    DataDlgfrm.LbSynVel.Visible:=true;
    DataDlgfrm.EdSynVel.Visible:=true;
    DataDlgfrm.LbSynAcc.Visible:=true;
    DataDlgfrm.EdSynAcc.Visible:=false;
    DataDlgfrm.CBDir.Visible:=true;

    DataDlgfrm.EdStartX.Text:='0';
    DataDlgfrm.EdStartY.Text:='0';
    DataDlgfrm.EdStartZ.Text:='0';
    DataDlgfrm.EdSynVel.Text:='0';
    DataDlgfrm.EdSynAcc.Text:='0';

    DataDlgfrm.LbStartY.Enabled:=true;
    DataDlgfrm.EdStartY.Enabled:=true;
    DataDlgfrm.LbStartZ.Enabled:=true;
    DataDlgfrm.EdStartZ.Enabled:=true;
    DataDlgfrm.LbSynVel.Enabled:=true;
    DataDlgfrm.EdSynVel.Enabled:=true;
    DataDlgfrm.LbSynAcc.Enabled:=true;
    if lang=1 then                               
      DataDlgfrm.LbSynAcc.Caption:='方向'        
    else                                         
      DataDlgfrm.LbSynAcc.Caption:='direction';  
    DataDlgfrm.CBDir.Enabled:=false;    
    if lang=1 then
    begin
    	if (no=107) then                    
    	begin                               
    	   DataDlgfrm.LbStartY.Caption:='Ｘ圆心';
    	   DataDlgfrm.LbStartZ.Caption:='Ｙ圆心';
    	   DataDlgfrm.LbSynVel.Caption:='角度';
    	end else if (no=108) then
    	begin
    	   DataDlgfrm.LbStartY.Caption:='Ｘ终点';
    	   DataDlgfrm.LbStartZ.Caption:='Ｙ终点';
    	    DataDlgfrm.LbSynVel.Caption:='半径';
    	    DataDlgfrm.CBDir.Enabled:=true;
    	end else if (no=109) then
    	begin
    	   DataDlgfrm.LbStartY.Caption:='Ｙ圆心';
    	   DataDlgfrm.LbStartZ.Caption:='Ｚ圆心';
    	   DataDlgfrm.LbSynVel.Caption:='角度';
    	end else if (no=110) then
    	begin
    	   DataDlgfrm.LbStartY.Caption:='Ｙ终点';
    	   DataDlgfrm.LbStartZ.Caption:='Ｚ终点';
    	   DataDlgfrm.LbSynVel.Caption:='半径';
    	   DataDlgfrm.CBDir.Enabled:=true;
    	end else if (no=111) then
    	begin
    	   DataDlgfrm.LbStartY.Caption:='Ｚ圆心';
    	   DataDlgfrm.LbStartZ.Caption:='Ｘ圆心';
    	   DataDlgfrm.LbSynVel.Caption:='角度';
    	end else if (no=112) then
    	begin
    	   DataDlgfrm.LbStartY.Caption:='Ｚ终点';
    	   DataDlgfrm.LbStartZ.Caption:='Ｘ终点';
    	   DataDlgfrm.LbSynVel.Caption:='半径';
    	   DataDlgfrm.CBDir.Enabled:=true;
    	end;
    end else begin
     	if (no=107) then
     	begin
     	  DataDlgfrm.LbStartY.Caption:='X Center Point';
     	  DataDlgfrm.LbStartZ.Caption:='Y Center Point';
     	  DataDlgfrm.LbSynVel.Caption:='Angle';
     	end else if (no=108) then
     	begin
     	  DataDlgfrm.LbStartY.Caption:='X End Point';
     	  DataDlgfrm.LbStartZ.Caption:='Y End Point';
     	   DataDlgfrm.LbSynVel.Caption:='Radius';
     	   DataDlgfrm.CBDir.Enabled:=true;
     	end else if (no=109) then
     	begin
     	  DataDlgfrm.LbStartY.Caption:='Y Center Point';
     	  DataDlgfrm.LbStartZ.Caption:='Z Center Point';
     	  DataDlgfrm.LbSynVel.Caption:='Angle';
     	end else if (no=110) then
     	begin
     	  DataDlgfrm.LbStartY.Caption:='Y End Point';
     	  DataDlgfrm.LbStartZ.Caption:='Z End Point';
     	  DataDlgfrm.LbSynVel.Caption:='Radius ';
     	  DataDlgfrm.CBDir.Enabled:=true;
     	end else if (no=111) then
     	begin
     	  DataDlgfrm.LbStartY.Caption:='Z Center Point';
     	  DataDlgfrm.LbStartZ.Caption:='X Center Point';
     	  DataDlgfrm.LbSynVel.Caption:='Angle';
     	end else if (no=112) then
     	begin
     	  DataDlgfrm.LbStartY.Caption:='Z End Point';
     	  DataDlgfrm.LbStartZ.Caption:='X End Point';
     	  DataDlgfrm.LbSynVel.Caption:='Radius';
     	  DataDlgfrm.CBDir.Enabled:=true;
     	end;
    end;
  end else if (no=501) then
  begin
    DataDlgfrm.LbStartX.Visible:=false;
    DataDlgfrm.EdStartX.Visible:=false;
    DataDlgfrm.LbStartY.Visible:=false;
    DataDlgfrm.EdStartY.Visible:=false;
    DataDlgfrm.LbStartZ.Visible:=true;
    DataDlgfrm.EdStartZ.Visible:=true;
    DataDlgfrm.EdStartZ.Enabled:=true;    
    DataDlgfrm.LbSynVel.Visible:=false;
    DataDlgfrm.EdSynVel.Visible:=false;
    DataDlgfrm.LbSynAcc.Visible:=false;
    DataDlgfrm.EdSynAcc.Visible:=false;
    DataDlgfrm.CBDir.Visible:=false;
    if lang=1 then                                     
      	DataDlgfrm.LbStartZ.Caption:='速度倍率'          
    else                                               
      	DataDlgfrm.LbStartZ.Caption:='velocity multiple';
    DataDlgfrm.EdStartZ.Text:=FloatToStr(OverrideG1);
  end                                           
  else if (no=301) or (no=302) or (no=3)  or (no=303) or (no=304) or (no=305) then
  begin
    if lang=1 then
    begin
        AxisControl.Label1.Caption:='当前轴';
        AxisControl.CbAxisSel.Items.Clear;
        AxisControl.CbAxisSel.Items.Append('轴1');
        AxisControl.CbAxisSel.Items.Append('轴2');
        AxisControl.CbAxisSel.Items.Append('轴3');
        AxisControl.CbAxisSel.ItemIndex:=0;
        if (no=301) then
        begin
           AxisControl.Caption:='设置实际位置';
           AxisControl.LbSetValue.Caption:='设置轴位置';
           AxisControl.LbMessage.Caption:='GT_SetAtlPos()设置当前轴的实际位置,只在指定轴运动停止时有效，否则将被视为非法命令产生命令出错标识';
        end else if (no=303) then
        begin
           AxisControl.Caption:='设置轴状态';
           AxisControl.LbSetValue.Caption:='设置轴状态';
           AxisControl.LbMessage.Caption:='GT_RstSts()清除当前轴的状态寄存器';
        end else if (no=302) then
        begin
           AxisControl.Caption:='设置捕获Home信号事件';
           AxisControl.LbMessage.Caption:='GT_CaptHome()设置允许当前轴捕获Home信号事件';
        end
        else if (no=304) then
        begin
           AxisControl.Caption:='清除控制轴状态';
           AxisControl.LbMessage.Caption:='GT_ClrSts()清除控制轴状态';
        end
        else if (no=3) then
        begin
           AxisControl.Caption:='驱动器复位';
           AxisControl.LbMessage.Caption:='GT_DrvRst()向当前轴的驱动器发出复位信号使驱动器复位';
        end
        else if (no=305) then
        begin
           AxisControl.Caption:='位置清零';
           AxisControl.LbMessage.Caption:='GT_ZeroPos使当前轴控制轴实际位置和目标位置清零,只在指定轴运动停止时有效，否则将被视为非法命令产生命令出错标识';
        end;
     end  else begin
        AxisControl.Label1.Caption:='Active Axis';
        AxisControl.CbAxisSel.Items.Clear;
        AxisControl.CbAxisSel.Items.Append('Axis 1');
        AxisControl.CbAxisSel.Items.Append('Axis 2');
        AxisControl.CbAxisSel.Items.Append('Axis 3');
        AxisControl.CbAxisSel.ItemIndex:=0;
        if (no=301) then
        begin
           AxisControl.Caption:='Set Actural Position';
           AxisControl.LbSetValue.Caption:='Set Axis Position';
           AxisControl.LbMessage.Caption:='to call GT_SetAtlPos';
        end else if (no=303) then
        begin
           AxisControl.Caption:='Set Axis Status';
           AxisControl.LbSetValue.Caption:='Set Axis Status';
           AxisControl.LbMessage.Caption:='to call GT_RstSts';
        end else if (no=302) then
        begin
           AxisControl.Caption:='Set Capture Home';
           AxisControl.LbMessage.Caption:='to call GT_CaptHome';
        end
        else if (no=304) then
        begin
           AxisControl.Caption:='Reset Axis Status';
           AxisControl.LbMessage.Caption:='to call GT_ClrSts';
        end
        else if (no=3) then
        begin
           AxisControl.Caption:='Reset Driver';
           AxisControl.LbMessage.Caption:='to call GT_DrvRst()';
        end
        else if (no=305) then
        begin
           AxisControl.Caption:='Reset Position';
           AxisControl.LbMessage.Caption:='to call GT_ZeroPos()';
        end;
     end;
     if (no=301) or (no=303) then
     begin
        AxisControl.LbSetValue.Visible:=true;
        AxisControl.EdSetValue.Visible:=true;
     end
     else begin
        AxisControl.LbSetValue.Visible:=false;
        AxisControl.EdSetValue.Visible:=false;
     end;
  end;

end;

procedure Tdemoform.FormCreate(Sender: TObject);
begin
  if lang=1 then
     AppCaptionStr:='GT连续轨迹控制卡功能演示程序 V1.0'
  else
     AppCaptionStr:='GT Curve Control Demo V1.0';
  bOpenControl:=false;
  demoform.Caption:=AppCaptionStr;
  FileNameStr:= '';
  CmdMemo.ScrollBars:=ssHorizontal;
  PGMemo.ActivePageIndex:=0;
  btnOpenFile.Enabled:=false;
  XChangeLanguage;
  MvStartX:=0;
  MvStartY:=0;
  MvStartZ:=0;
  SynVel:=0;
  SynAcc:=0;
  ExOpt:=65535;
  filterStr[0]:='.cmd';
  filterStr[1]:='.nc';
  filterStr[2]:='.txt';
  filterStr[3]:='.ini';
  IsCmdModify:=false;
  MsgString:='';
  LmtVal:=0;
  StrtVel:=0;  MaxVel:=0;  StpAcc:=0;
  Limit1Sts:=0;Limit2Sts:=0;Limit3Sts:=0;
  Ctrl1Sts:=0;Ctrl2Sts:=0;Ctrl3Sts:=0;
  dir:=-1;
  X_End:=0;Y_End:=0;Z_End:=0;r:=0;
  X_Center:=0;Y_Center:=0;Z_Center:=0;angle:=0;
  OverrideG1:=1;OverrideG0:=1;
end;

procedure Tdemoform.btnOpenClick(Sender: TObject);
var
   addr,irq:LongWord;
   rtn:SmallInt;
begin
  ActiveButton(0);
  if DataFrm.ShowModal= mrOK then
  begin
    addr:=StrToInt(DataFrm.Address.Text);
    irq:=StrToInt(DataFrm.Irq.Text);
    rtn:=GT_Open(addr,irq);
    if (rtn=0) then
    begin
      bOpenControl:=true;
      GT_Reset();
      GT_LmtsOn(1);
      GT_LmtsOn(2);
      GT_LmtsOn(3);
      GT_ClrSts(1);
      GT_ClrSts(2);
      GT_ClrSts(3);
      GT_Override(OverrideG1);
      GT_OverrideG0(OverrideG0);
    end;
    ShowResult(rtn,'GT_Open',addr,irq);
  end;
end;

procedure Tdemoform.btnCloseClick(Sender: TObject);
var rtn:SmallInt;
begin
  ActiveButton(1);
  if DataFrm.ShowModal= mrOK then
  begin
    rtn:=GT_Close();
 //   ShowErrorMsg(rtn);
    ShowResult(rtn,'GT_Close');
    if rtn=0 then
        bOpenControl:=false;    
  end;
end;


procedure Tdemoform.HardRstClick(Sender: TObject);
var rtn:SmallInt;
begin
  ActiveButton(2);
  if DataFrm.ShowModal= mrOK then
  begin
    rtn:=GT_HardRst();
 // ShowErrorMsg(rtn);
    ShowResult(rtn,'GT_HardRst');
    if rtn=0 then
      FormCreate(sender); 
  end;
end;

procedure Tdemoform.DrvRstClick(Sender: TObject);
var rtn:SmallInt;
begin
  ActiveButton(3);
  if AxisControl.ShowModal=mrOK then
  begin
    rtn:=GT_DrvRst(AxisControl.CbAxisSel.ItemIndex+1);
    ShowResult(rtn,'GT_DrvRst',AxisControl.CbAxisSel.ItemIndex+1);
  end;

{  ActiveButton(3);
  if DataFrm.ShowModal= mrOK then
  begin
    rtn:=GT_DrvRst();
  //ShowErrorMsg(rtn);
    ShowResult(rtn,'GT_DrvRst');
  end;}
end;

procedure Tdemoform.ResetClick(Sender: TObject);
var rtn:SmallInt;
begin
  ActiveButton(4);
  if DataFrm.ShowModal= mrOK then
  begin
    rtn:=GT_Reset();
  //  ShowErrorMsg(rtn);
    ShowResult(rtn,'GT_Reset');    
  end;
end;

procedure Tdemoform.CtrlModeClick(Sender: TObject);
var rtn:SmallInt;
begin
  LmitDlg.SelectNo:=2;
  LmitDlg.ShowModal;
end;

procedure Tdemoform.btnAlarmClick(Sender: TObject);
var rtn:SmallInt;
begin
  LmitDlg.SelectNo:=3;
  LmitDlg.ShowModal;
end;

procedure Tdemoform.btnLimtClick(Sender: TObject);
var rtn:SmallInt;
begin
  LmitDlg.SelectNo:=0;
  LmitDlg.ShowModal;
end;

procedure Tdemoform.SetStrtVelClick(Sender: TObject);
var rtn:SmallInt;
begin
  ActiveButton(7);
  DataFrm.Address.Text:=FloatToStr(StrtVel);
  if DataFrm.ShowModal= mrOK then
  begin
    StrtVel:=StrToFloat(DataFrm.Address.Text);
    rtn:=GT_SetStrtVel(StrtVel);
 //   ShowErrorMsg(rtn);
    ShowResult(rtn,'GT_SetStrtVel',StrtVel);
  end;
end;

procedure Tdemoform.SetStpAccClick(Sender: TObject);
var rtn:SmallInt;
begin
  ActiveButton(8);
  DataFrm.Address.Text:=FloatToStr(StpAcc);
  if DataFrm.ShowModal= mrOK then
  begin
    StpAcc:=StrToFloat(DataFrm.Address.Text);
    rtn:=GT_SetStpAcc(StrToFloat(DataFrm.Address.Text));
  //  ShowErrorMsg(rtn);
    ShowResult(rtn,'GT_SetStpAcc',StrToFloat(DataFrm.Address.Text));
  end;
end;

procedure Tdemoform.SetMaxVelClick(Sender: TObject);
var rtn:SmallInt;
begin
  ActiveButton(9);
  DataFrm.Address.Text:=FloatToStr(MaxVel);
  if DataFrm.ShowModal= mrOK then
  begin
    MaxVel:=StrToFloat(DataFrm.Address.Text);
    rtn:=GT_SetMaxVel(StrToFloat(DataFrm.Address.Text));
   // ShowErrorMsg(rtn);
    ShowResult(rtn,'GT_SetMaxVel',StrToFloat(DataFrm.Address.Text));
  end;
end;

procedure Tdemoform.GTStrtListClick(Sender: TObject);
var rtn:SmallInt;
begin
  ActiveButton(10);
  if DataFrm.ShowModal= mrOK then
  begin
     rtn:=GT_StrtList();   //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_StrtList');
  end;
end;

procedure Tdemoform.OpenAxisClick(Sender: TObject);
var rtn:SmallInt;
begin

  LmitDlg.SelectNo:=1;
  LmitDlg.ShowModal;
end;

procedure Tdemoform.SetSynVelClick(Sender: TObject);
begin
  ActiveButton(103);
  if DataDlgfrm.ShowModal= mrOK then
  begin
     rtn:=GT_SetSynVel(SynVel); //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_SetSynVel',SynVel);
  end;
end;


procedure Tdemoform.GetStsTimerTimer(Sender: TObject);
var ss1,ss2,ss3:word;
    AtlPos:array [0..3] of LongInt;
    mask:word;
begin
  GetStsTimer.Enabled:=false;
  if (PageControl1.ActivePageIndex=0) then
  begin
    GT_GetSts(1,@ss1);
    GT_GetSts(2,@ss2);
    GT_GetSts(3,@ss3);
    sts_update(ss1,ss2,ss3,0,Lb_MtnDone1,Lb_MtnDone2,Lb_MtnDone3);
    sts_update(ss1,ss2,ss3,1,Lb_ServoAlm1,Lb_ServoAlm2,Lb_ServoAlm3);
    sts_update(ss1,ss2,ss3,2,Lb_BrkReached1,Lb_BrkReached2,Lb_BrkReached3);
    sts_update(ss1,ss2,ss3,3,Lb_Captured1,Lb_Captured2,Lb_Captured3);
    sts_update(ss1,ss2,ss3,4,Lb_MtnErr1,Lb_MtnErr2,Lb_MtnErr3);
    sts_update(ss1,ss2,ss3,5,Lb_PosLmtTriggered1,Lb_PosLmtTriggered2,Lb_PosLmtTriggered3);
    sts_update(ss1,ss2,ss3,6,Lb_NegLmtTriggered1,Lb_NegLmtTriggered2,Lb_NegLmtTriggered3);
    sts_update(ss1,ss2,ss3,7,Lb_HostCmdErr1,Lb_HostCmdErr2,Lb_HostCmdErr3);
    sts_update(ss1,ss2,ss3,8,Lb_LoopClosed1,Lb_LoopClosed2,Lb_LoopClosed3);
    sts_update(ss1,ss2,ss3,9,Lb_AxisOn1,Lb_AxisOn2,Lb_AxisOn3);
    sts_update(ss1,ss2,ss3,10,Lb_InMtn1,Lb_InMtn2,Lb_InMtn3);
    sts_update(ss1,ss2,ss3,11,Lb_LmtOn1,Lb_LmtOn2,Lb_LmtOn3);
    sts_update(ss1,ss2,ss3,14,Lb_HomeCapt1,Lb_HomeCapt2,Lb_HomeCapt3);
    sts_update(ss1,ss2,ss3,15,Lb_IndexCapt1,Lb_IndexCapt2,Lb_IndexCapt3);
  end
  else  if (PageControl1.ActivePageIndex=1) then
  begin
    Gt_GetCrdSts(@ss1);
    if ((ss1 shr 0) and 1)<>0 then      Lb_CrdStsShowBit0.Font.Color:=clRed
    else        Lb_CrdStsShowBit0.Font.Color:=clGray;

    if ((ss1 shr 1) and 1)<>0 then      Lb_CrdStsShowBit1.Font.Color:=clRed
    else        Lb_CrdStsShowBit1.Font.Color:=clGray;

    if ((ss1 shr 2) and 1)<>0 then      Lb_CrdStsShowBit2.Font.Color:=clRed
    else        Lb_CrdStsShowBit2.Font.Color:=clGray;

    if ((ss1 shr 3) and 1)<>0 then      Lb_CrdStsShowBit3.Font.Color:=clRed
    else        Lb_CrdStsShowBit3.Font.Color:=clGray;

    if ((ss1 shr 4) and 1)<>0 then      Lb_CrdStsShowBit4.Font.Color:=clRed
    else        Lb_CrdStsShowBit4.Font.Color:=clGray;

    if ((ss1 shr 5) and 1)<>0 then      Lb_CrdStsShowBit5.Font.Color:=clRed
    else        Lb_CrdStsShowBit5.Font.Color:=clGray;

    if ((ss1 shr 6) and 1)<>0 then      Lb_CrdStsShowBit6.Font.Color:=clRed
    else        Lb_CrdStsShowBit6.Font.Color:=clGray;

    if ((ss1 shr 7) and 1)<>0 then      Lb_CrdStsShowBit7.Font.Color:=clRed
    else        Lb_CrdStsShowBit7.Font.Color:=clGray;

    if ((ss1 shr 8) and 1)<>0 then      Lb_CrdStsShowBit8.Font.Color:=clRed
    else        Lb_CrdStsShowBit8.Font.Color:=clGray;

    if ((ss1 shr 9) and 1)<>0 then      Lb_CrdStsShowBit9.Font.Color:=clRed
    else        Lb_CrdStsShowBit9.Font.Color:=clGray;

    if ((ss1 shr 10) and 1)<>0 then     Lb_CrdStsShowBit10.Font.Color:=clRed
    else        Lb_CrdStsShowBit10.Font.Color:=clGray;

  end;
  if bOpenControl then
  begin
    GT_GetPrfPnt(@AtlPos);
    Lb_Pos1.Caption:=IntToStr(AtlPos[0]);
    Lb_Pos2.Caption:=IntToStr(AtlPos[1]);
    Lb_Pos3.Caption:=IntToStr(AtlPos[2]);
  end;

  if MsgString<>'' then
  begin
      CmdStsMemo.Lines.Append(MsgString);
      MsgString:='';
  end;

  GetStsTimer.Enabled:=true;
end;

procedure sts_update(ss1,ss2,ss3,n:word;var Lb1,Lb2,Lb3:TLabel);
var mask:word;
begin
    mask:=1 shl n;
    if ((ss1 and mask)<>0) then lb1.Font.Color:=clRed else lb1.Font.Color:=clGray;
    if ((ss2 and mask)<>0) then lb2.Font.Color:=clRed else lb2.Font.Color:=clGray;
    if ((ss3 and mask)<>0) then lb3.Font.Color:=clRed else lb3.Font.Color:=clGray;

end;

procedure Tdemoform.MvXYClick(Sender: TObject);
begin
  ActiveButton(101);
  DataDlgfrm.EdStartX.Text:=FloatToStr(MvStartX);
  DataDlgfrm.EdStartY.Text:=FloatToStr(MvStartY);
  DataDlgfrm.EdSynVel.Text:=FloatToStr(SynVel);
  DataDlgfrm.EdSynAcc.Text:=FloatToStr(SynAcc);
  if DataDlgfrm.ShowModal= mrOK then
  begin
     MvStartX:=StrToFloat(DataDlgfrm.EdStartX.Text);
     MvStartY:=StrToFloat(DataDlgfrm.EdStartY.Text);
     SynVel:=StrToFloat(DataDlgfrm.EdSynVel.Text);
     SynAcc:=StrToFloat(DataDlgfrm.EdSynAcc.Text);
     rtn:=GT_MvXY(MvStartX,MvStartY,SynVel,SynAcc); //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_MvXY',MvStartX,MvStartY,SynVel,SynAcc);
  end;
end;

procedure Tdemoform.MvXYZClick(Sender: TObject);
begin
  ActiveButton(102);
  DataDlgfrm.EdStartX.Text:=FloatToStr(MvStartX);
  DataDlgfrm.EdStartY.Text:=FloatToStr(MvStartY);
  DataDlgfrm.EdStartZ.Text:=FloatToStr(MvStartZ);
  DataDlgfrm.EdSynVel.Text:=FloatToStr(SynVel);
  DataDlgfrm.EdSynAcc.Text:=FloatToStr(SynAcc);
  if DataDlgfrm.ShowModal= mrOK then
  begin
     MvStartX:=StrToFloat(DataDlgfrm.EdStartX.Text);
     MvStartY:=StrToFloat(DataDlgfrm.EdStartY.Text);
     MvStartZ:=StrToFloat(DataDlgfrm.EdStartZ.Text);
     SynVel:=StrToFloat(DataDlgfrm.EdSynVel.Text);
     SynAcc:=StrToFloat(DataDlgfrm.EdSynAcc.Text);
     rtn:=GT_MvXYZ(MvStartX,MvStartY,MvStartZ,SynVel,SynAcc); //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_MvXYZ',MvStartX,MvStartY,MvStartZ,SynVel,SynAcc);
  end;
end;

procedure Tdemoform.SetSynAccClick(Sender: TObject);
begin
  ActiveButton(104);
  if DataDlgfrm.ShowModal= mrOK then
  begin
     rtn:=GT_SetSynAcc(SynAcc); //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_SetSynAcc',SynAcc);
  end;
end;

procedure Tdemoform.LnXYClick(Sender: TObject);
begin
  ActiveButton(105);
  if DataDlgfrm.ShowModal= mrOK then
  begin
     rtn:=GT_LnXYG0(X_End,Y_End); //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_LnXY',X_End,Y_End);
  end;
end;

procedure Tdemoform.LnXYZClick(Sender: TObject);
begin
  ActiveButton(106);
  if DataDlgfrm.ShowModal= mrOK then
  begin
     rtn:=GT_LnXYZG0(X_End,Y_End,Z_End); //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_LnXYZ',X_End,Y_End,Z_End);
  end;
end;

procedure Tdemoform.ArcXY1Click(Sender: TObject);
begin
  ActiveButton(107);
  if DataDlgfrm.ShowModal= mrOK then
  begin
     rtn:=GT_ArcXY(X_Center,Y_Center,angle); //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_ArcXY',X_Center,Y_Center,angle);
  end;
end;

procedure Tdemoform.ArcXY2Click(Sender: TObject);
begin
  ActiveButton(108);
  if DataDlgfrm.ShowModal= mrOK then
  begin
     rtn:=GT_ArcXYP(X_End,Y_End,r,dir); //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_ArcXYP',X_End,Y_End,r,dir);
  end;
end;

procedure Tdemoform.ArcYZ1Click(Sender: TObject);
begin
  ActiveButton(109);
  if DataDlgfrm.ShowModal= mrOK then
  begin
     rtn:=GT_ArcYZ(Y_Center,Z_Center,angle); ///ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_ArcYZ',Y_Center,Z_Center,angle);
  end;
end;

procedure Tdemoform.ArcYZ2Click(Sender: TObject);
begin
  ActiveButton(110);
  if DataDlgfrm.ShowModal= mrOK then
  begin
     rtn:=GT_ArcYZP(Y_End,Z_End,r,dir); //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_ArcYZP',Y_End,Z_End,r,dir);
  end;
end;

procedure Tdemoform.ArcZX1Click(Sender: TObject);
begin
  ActiveButton(111);
  if DataDlgfrm.ShowModal= mrOK then
  begin
     rtn:=GT_ArcZX(Z_Center,X_Center,angle); //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_ArcZX',Z_Center,X_Center,angle);
  end;
end;

procedure Tdemoform.ArcZX2Click(Sender: TObject);
begin
  ActiveButton(112);
  if DataDlgfrm.ShowModal= mrOK then
  begin
     rtn:=GT_ArcZXP(Z_End,X_End,r,dir); //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_ArcZXP',Z_End,X_End,r,dir);
  end;
end;

procedure Tdemoform.SetDFlagClick(Sender: TObject);
begin
  ActiveButton(113);
  if DataFrm.ShowModal= mrOK then
  begin
//   rtn:=GT_SetDccFlag(); ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_SetDccFlag');
  end;
end;

procedure Tdemoform.SetCrdOffsetClick(Sender: TObject);
begin
{ ActiveButton(114);
  if DataDlgfrm.ShowModal= mrOK then
  begin
     GT_MapCnt()
  end;
}
end;

procedure Tdemoform.btnSetAltPosClick(Sender: TObject);
begin
  ActiveButton(301);
  AxisControl.EdSetValue.Text:='0';
  if AxisControl.ShowModal=mrOK then
  begin
     rtn:=GT_SetAtlPos(AxisControl.CbAxisSel.ItemIndex+1,StrToInt(AxisControl.EdSetValue.Text));
     //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_SetAtlPos',AxisControl.CbAxisSel.ItemIndex+1,StrToInt(AxisControl.EdSetValue.Text));
  end;
end;

procedure Tdemoform.btnCaptHomeClick(Sender: TObject);
begin
  ActiveButton(302);
  if AxisControl.ShowModal=mrOK then
  begin
    rtn:=GT_CaptHome(AxisControl.CbAxisSel.ItemIndex+1);
    //ShowErrorMsg(rtn);
    ShowResult(rtn,'GT_CaptHome',AxisControl.CbAxisSel.ItemIndex+1);
  end;
end;

procedure Tdemoform.btnRstStsClick(Sender: TObject);
begin
  ActiveButton(303);
  AxisControl.EdSetValue.Text:='0';
  if AxisControl.ShowModal=mrOK then
  begin
    rtn:=GT_RstSts(AxisControl.CbAxisSel.ItemIndex+1,StrToInt(AxisControl.EdSetValue.Text));
    //ShowErrorMsg(rtn);
    ShowResult(rtn,'GT_RstSts',AxisControl.CbAxisSel.ItemIndex+1,StrToInt(AxisControl.EdSetValue.Text));
  end;
end;

procedure Tdemoform.btnClrStsClick(Sender: TObject);
begin
  ActiveButton(304);
  if AxisControl.ShowModal=mrOK then
  begin
    rtn:=GT_ClrSts(AxisControl.CbAxisSel.ItemIndex+1);
    //ShowErrorMsg(rtn);
    ShowResult(rtn,'GT_ClrSts',AxisControl.CbAxisSel.ItemIndex+1);
  end;
end;

procedure Tdemoform.btnZeroPosClick(Sender: TObject);
begin
  ActiveButton(305);
  if AxisControl.ShowModal=mrOK then
  begin
    rtn:=GT_ZeroPos(AxisControl.CbAxisSel.ItemIndex+1);
    //ShowErrorMsg(rtn);
    ShowResult(rtn,'GT_ZeroPos',AxisControl.CbAxisSel.ItemIndex+1);
  end;
end;

procedure Tdemoform.Button35MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   AxisNo:=1;
   if JudgeErrorbyAxis then exit;
   rtn:=GT_GetPrfPnt(@BaseAtlPos);   ShowErrorMsg(rtn);
//    ShowResult(rtn,'GT_GetPrfPnt');
   if (rtn<>0) then exit;
   rtn:=GT_LnXYZ(BaseAtlPos[0]+100000000,BaseAtlPos[1],BaseAtlPos[2]);
   if (rtn=0) then  Timer2.Enabled:=true;
end;

procedure Tdemoform.Button35MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
   Timer2.Enabled:=false;
   Sleep(5);
   rtn:= GT_StpMtn();    //ShowErrorMsg(rtn);
//   ShowResult(rtn,'GT_StpMtn');
end;

procedure Tdemoform.Timer2Timer(Sender: TObject);
var pos1,pos2,pos3:double;
    AxAtlPos:array [0..3] of LongInt;
begin
  Timer2.Enabled:=false;

  if JudgeErrorbyAxis  then exit;

  pos1:=0;  pos2:=0;  pos3:=0;
  rtn:=GT_GetPrfPnt(@AxAtlPos);   ShowErrorMsg(rtn);
  Sleep(2);
  case AxisNo of
  1:   pos1:=100000000;//X正向运动
  -1:  pos1:=-100000000;//X负向运动
  2:   pos2:=100000000;
  -2:  pos1:=-100000000;
  3:   pos3:=100000000;
  -3:  pos1:=-100000000;
  end;
  if (AxisNo=1) or (AxisNo=2) or (AxisNo=3) then
  begin
     if (BaseAtlPos[0]+pos1<=AxAtlPos[0]) or
        (BaseAtlPos[1]+pos2<=AxAtlPos[1]) or
        (BaseAtlPos[2]+pos3<=AxAtlPos[2]) then
     begin
        BaseAtlPos[0]:=AxAtlPos[0];
        BaseAtlPos[1]:=AxAtlPos[1];
        BaseAtlPos[2]:=AxAtlPos[2];
        GT_LnXYZ(AxAtlPos[0]+pos1,AxAtlPos[1]+pos2,AxAtlPos[2]+pos3);
     end;
  end
  else if (AxisNo=-1) or (AxisNo=-2) or (AxisNo=-3) then
  begin
     if (BaseAtlPos[0]+pos1>=AxAtlPos[0]) or
        (BaseAtlPos[1]+pos2>=AxAtlPos[1]) or
        (BaseAtlPos[2]+pos3>=AxAtlPos[2]) then
     begin
        BaseAtlPos[0]:=AxAtlPos[0];
        BaseAtlPos[1]:=AxAtlPos[1];
        BaseAtlPos[2]:=AxAtlPos[2];
        GT_LnXYZ(AxAtlPos[0]+pos1,AxAtlPos[1]+pos2,AxAtlPos[2]+pos3);
     end;
  end;
  Timer2.Enabled:=true;  
end;

procedure Tdemoform.Button40Click(Sender: TObject);
begin
   AxisNo:=-1;
   Timer2.Enabled:=true;
end;

procedure Tdemoform.Button40MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   AxisNo:=-1;
   if JudgeErrorbyAxis then exit;
   rtn:=GT_GetPrfPnt(@BaseAtlPos);   ShowErrorMsg(rtn);
   if (rtn<>0) then exit;
   rtn:=GT_LnXYZ(BaseAtlPos[0]-100000000,BaseAtlPos[1],BaseAtlPos[2]);
   if (rtn=0) then  Timer2.Enabled:=true;
end;

procedure Tdemoform.Button40MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Timer2.Enabled:=false;
  Sleep(5);
  rtn:= GT_StpMtn();    //ShowErrorMsg(rtn);
//  ShowResult(rtn,'GT_StpMtn');
end;

procedure Tdemoform.Button36MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Timer2.Enabled:=false;
  Sleep(5);
  rtn:= GT_StpMtn();    //ShowErrorMsg(rtn);
 // ShowResult(rtn,'GT_StpMtn');
end;

procedure Tdemoform.Button39MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Timer2.Enabled:=false;
  Sleep(5);
  rtn:= GT_StpMtn();    //ShowErrorMsg(rtn);
//  ShowResult(rtn,'GT_StpMtn');
end;

procedure Tdemoform.Button37MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Timer2.Enabled:=false;
  Sleep(5);
  rtn:= GT_StpMtn();    //ShowErrorMsg(rtn);
//  ShowResult(rtn,'GT_StpMtn');
end;

procedure Tdemoform.Button38MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Timer2.Enabled:=false;
  Sleep(5);
  rtn:= GT_StpMtn();    //ShowErrorMsg(rtn);
//  ShowResult(rtn,'GT_StpMtn');
end;

procedure Tdemoform.Button38MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   AxisNo:=-3;
   if JudgeErrorbyAxis then exit;
   rtn:=GT_GetPrfPnt(@BaseAtlPos);   ShowErrorMsg(rtn);
   if (rtn<>0) then exit;
   rtn:=GT_LnXYZ(BaseAtlPos[0],BaseAtlPos[1],BaseAtlPos[2]-100000000);
   if (rtn=0) then   Timer2.Enabled:=true;
end;

procedure Tdemoform.Button37MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   AxisNo:=3;
   if JudgeErrorbyAxis then exit;
   rtn:=GT_GetPrfPnt(@BaseAtlPos);   ShowErrorMsg(rtn);
   if (rtn<>0) then exit;
   rtn:=GT_LnXYZ(BaseAtlPos[0],BaseAtlPos[1],BaseAtlPos[2]+100000000);
   if (rtn=0) then   Timer2.Enabled:=true;
end;

procedure Tdemoform.Button36MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   AxisNo:=2;
   if JudgeErrorbyAxis then exit;
   rtn:=GT_GetPrfPnt(@BaseAtlPos);   ShowErrorMsg(rtn);
   if (rtn<>0) then exit;
   rtn:=GT_LnXYZ(BaseAtlPos[0],BaseAtlPos[1]+100000000,BaseAtlPos[2]);
   if (rtn=0) then   Timer2.Enabled:=true;
end;

procedure Tdemoform.Button39MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   AxisNo:=-2;
   if JudgeErrorbyAxis then exit;
   rtn:=GT_GetPrfPnt(@BaseAtlPos);   ShowErrorMsg(rtn);
   if (rtn<>0) then exit;
   rtn:=GT_LnXYZ(BaseAtlPos[0],BaseAtlPos[1]-100000000,BaseAtlPos[2]);
   if (rtn=0) then   Timer2.Enabled:=true;
end;

procedure Tdemoform.btnRunCmdClick(Sender: TObject);
begin
  RunGTCmd();
end;

procedure Tdemoform.btnEStpMtnClick(Sender: TObject);
begin
   rtn:=GT_EStpMtn();   //ShowErrorMsg(rtn);
   ShowResult(rtn,'GT_EStpMtn');
end;

procedure Tdemoform.btnStpMtnClick(Sender: TObject);
begin
   rtn:=GT_StpMtn();   //ShowErrorMsg(rtn);
   ShowResult(rtn,'GT_StpMtn');
end;

procedure Tdemoform.btnOverrideClick(Sender: TObject);
begin
  ActiveButton(501);
  if DataDlgfrm.ShowModal=mrOK then
  begin
    OverrideG1:=StrToFloat(DataDlgfrm.EdStartZ.Text);
    rtn:=GT_Override(OverrideG1);
    ShowResult(rtn,'GT_Override',OverrideG1);
  end;
end;

procedure Tdemoform.btnIoInputClick(Sender: TObject);
begin
  IoControlFrm.selectNo:=0;
  IoControlFrm.ShowModal;
end;

procedure Tdemoform.btnIOOutputClick(Sender: TObject);
begin
  IoControlFrm.selectNo:=1;
  IoControlFrm.ShowModal;
end;

procedure Tdemoform.btnOpenFileClick(Sender: TObject);
begin
   if OpenDialog1.Execute then
   begin
      FileNameStr:=OpenDialog1.filename;
      if not FileExists(FileNameStr) then
      begin
      	if lang=1 then
        	MessageDlg('打开文件失败,文件不存在.', mtInformation,[mbOk], 0)
        else
        	MessageDlg('Open file fail.', mtInformation,[mbOk], 0);
        exit;
      end else begin
         FileMemo.Lines.Clear;
         demoform.Caption:=AppCaptionStr+'---'+FileNameStr;
         FileMemo.Lines.LoadFromFile(FileNameStr);
      end;
   end;
end;

procedure Tdemoform.CmdMemoChange(Sender: TObject);
begin
   IsCmdModify:=true;
end;

procedure Tdemoform.btnEditFileClick(Sender: TObject);
begin
   if lang=1 then
   begin
      if MessageDlg('是否清空命令区中的所有指令?', mtWarning,[mbCancel,mbOk], 0)=mrOK then
      	CmdMemo.Clear;
   end
   else
   begin
      if MessageDlg('Are you confirm to clear all command?', mtWarning,[mbCancel,mbOk], 0)=mrOK then
        CmdMemo.Clear;
   end;
end;

procedure Tdemoform.btnSaveFileClick(Sender: TObject);
begin
   if SaveDialog1.Execute then
   begin
      IsCmdModify:=false;
      FileNameStr:=SaveDialog1.FileName+filterStr[SaveDialog1.FilterIndex-1];
      CmdMemo.Lines.SaveToFile(FileNameStr);
      demoform.Caption:=AppCaptionStr+'---'+FileNameStr;
   end;
end;

procedure Tdemoform.PGMemoChange(Sender: TObject);
begin
    if PGMemo.ActivePageIndex=0 then
    begin
       btnOpenFile.Enabled:=false;
       btnEditFile.Enabled:=true;
       btnSaveFile.Enabled:=true;
       demoform.Caption:=AppCaptionStr;       
    end
    else begin
       btnOpenFile.Enabled:=true;
       btnEditFile.Enabled:=false;
       btnSaveFile.Enabled:=false;
       if FileNameStr='' then
          demoform.Caption:=AppCaptionStr
       else
          demoform.Caption:=AppCaptionStr+'---'+FileNameStr;
    end;
end;

procedure Tdemoform.FormClose(Sender: TObject; var Action: TCloseAction);
var formrtn:Integer;
    brequire:boolean;
begin
    brequire:=true;
    if IsCmdModify then
    begin
       if lang=1 then
       	  formrtn:=MessageBox(application.Handle,'命令区已被修改，需要保存命令区中的指令吗?','Information',MB_YESNOCANCEL)
      else
          formrtn:=MessageBox(application.Handle,'be modified ，are you save?','Information',MB_YESNOCANCEL);
       if formrtn=IDOK then
       begin
          btnSaveFileClick(sender);
       end
       else if formrtn=IDCANCEL then
       begin
          Action := caNone;
          brequire:=false;
       end;
    end;
    if brequire then
        GT_Close();
end;

procedure Tdemoform.btnExitClick(Sender: TObject);
begin
 close;
end;

procedure Tdemoform.btnSaveCmdClick(Sender: TObject);
Var filename:string;
begin
 filename:=Application.GetNamePath+ErrFileName;
 if Length(CmdStsMemo.Lines.Text)=0 then exit;
 CmdStsMemo.Lines.SaveToFile(filename);
 CmdStsMemo.Lines.SaveToFile(Application.GetNamePath+ErrFileName);
end;

procedure Tdemoform.btnClearCmdClick(Sender: TObject);
begin
  CmdStsMemo.Clear;
end;

procedure Tdemoform.RunGTCmd();
var
    i,l,RRtn:integer;
    BaseAddr:LongWord;
    CommandStr:String;
begin
   BaseAddr:=0;
   i:=1;
   for  i:=1 to FileMemo.Lines.Count do
   begin
        try
            CommandStr:=FileMemo.Lines[i-1];
            FileMemo.SelStart:=BaseAddr;
            FileMemo.SelLength:=Length(CommandStr)+1;
            BaseAddr:=BaseAddr+FileMemo.SelLength+1;
            execute_gtcmd(CommandStr,RRtn);
            Sleep(100);
        except
            on EConvertError do begin
                if lang=1 then
                        Messagedlg(Format('第%d行数据类型不符，命令终止:%s',[i,FileMemo.Lines[i-1]]),mtError,[mbOK],0)
                else
                        Messagedlg(Format('Data type error in line %d, program stopped: %s',[i,FileMemo.Lines[i-1]]),mtError,[mbOK],0);
                exit;
            end;
            on ERangeError do begin

                if lang=1 then
                        Messagedlg(format('%s%d%s%s',['第',i,'行','没有该指令,指令错误']),mtError,[mbOK],0)
                else
                        Messagedlg(format('%s%d%s',['Line ',i,' Command error. No such command']),mtError,[mbOK],0);
                exit;
            end;

        end;
        if RRtn<>0 then begin

                if lang=1 then
                        Messagedlg(format('%s%d%s%s%d%s%s',['第',i,'行','命令执行错误:',RRtn,'=',FileMemo.Lines[i-1]]),mtError,[mbOK],0)
                else
                        Messagedlg(format('%s%d%s%d%s%s',['Line ',i,' Command executing error:',RRtn,'=',FileMemo.Lines[i-1]]),mtError,[mbOK],0);
                exit;
        end;
    end;
    
    if lang=1 then
        showmessage('程序全部执行完毕')
    else
        showmessage('Program running finished');
end;

procedure Tdemoform.btnStrtListClick(Sender: TObject);
begin
  ActiveButton(231);
  if DataFrm.ShowModal= mrOK then
  begin
     rtn:=GT_StrtList();   //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_StrtList');
  end;end;

procedure Tdemoform.btnEndListClick(Sender: TObject);
begin
  ActiveButton(232);
  if DataFrm.ShowModal= mrOK then
  begin
     rtn:=GT_EndList();   //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_EndList');
  end;
end;

procedure Tdemoform.btnAddListClick(Sender: TObject);
begin
  ActiveButton(233);
  if DataFrm.ShowModal= mrOK then
  begin
     rtn:=GT_AddList();   //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_AddList');
  end;
end;

procedure Tdemoform.btnStrtMtnClick(Sender: TObject);
begin
  ActiveButton(234);
  if DataFrm.ShowModal= mrOK then
  begin
     rtn:=GT_StrtMtn();   //ShowErrorMsg(rtn);
     ShowResult(rtn,'GT_StrtMtn');
  end;
end;

procedure Tdemoform.btnGetMtnNoClick(Sender: TObject);
begin
   GetMtnNo.showModal;
end;

function JudgeErrorbyAxis:Boolean;
var ss1,ss2,ss3:Word;
begin
   rtn:=GT_GetSts(1,@Ss1);
   rtn:=GT_GetSts(2,@Ss2);
   rtn:=GT_GetSts(3,@Ss3);

   if(rtn<>0) then
   begin
     JudgeErrorbyAxis:=true;
     ShowErrorMsg(rtn);
   end
   else if ((ss1 and $2)<>0) or ((ss2 and $2)<>0) or ((ss3 and $2)<>0) then
   begin
     JudgeErrorbyAxis:=true;
     if lang=1 then 
     	Messagebox(application.Handle,'运动控制动器存在伺服报警','板卡错误信息',MB_OK)
     else
        showMessage('Driver alarm! ');
   end else if ((ss1 and $6)<>0) or ((ss2 and $6)<>0) or ((ss3 and $6)<>0) then
   begin
     JudgeErrorbyAxis:=true;
     if lang=1 then
     	Messagebox(application.Handle,'运动控制动器轴限位被触发','板卡错误信息',MB_OK)
    else
       showMessage('Limit has breaken!');
   end else
   if ((ss1 and $100)=0) or ((ss2 and $100)=0) or ((ss3 and $100)=0) then
   begin
     JudgeErrorbyAxis:=true;
     if lang=1 then
      	Messagebox(application.Handle,'运动控制动器轴没有被驱动使能','板卡错误信息',MB_OK)
     else
     	showMessage('Axis has closed!');
   end
   else
     JudgeErrorbyAxis:=false;
end;

procedure Tdemoform.btnRestoreMtnClick(Sender: TObject);
begin
   rtn:=GT_RestoreMtn();
   ShowResult(rtn,'GT_RestoreMtn');
end;

procedure Tdemoform.XChangeLanguage;
begin
   if(lang=1) then
   begin
     Lb_MtnDone.Caption:='运动完成';
     Lb_ServoAlm.Caption:='伺服报警';
     Lb_BrkReached.Caption:='断点到';
     Lb_Captured.Caption:='位置捕获到';
     Lb_MtnErr.Caption:='运动错误';
     Lb_PosLmtTriggered.Caption:='正限位动作';
     Lb_NegLmtTriggered.Caption:='负限位动作';
     Lb_HostCmdErr.Caption:='主机命令出错';
     Lb_LoopClosed.Caption:='闭环控制';
     Lb_AxisOn.Caption:='轴开启';
     Lb_InMtn.Caption:='轴在运动';
     Lb_LmtOn.Caption:='限位开关有效';
     Lb_PrflT.Caption:='T形曲线模式';
     Lb_PrflS.Caption:='S形曲线模式';
     Lb_PrflV.Caption:='速度跟踪模式';
     Lb_PrflG.Caption:='电子齿轮模式';
     Lb_PrflC.Caption:='坐标运动模式';
     Lb_HomeCapt.Caption:='Home捕捉';
     Lb_IndexCapt.Caption:='Index捕捉';
     Lb_CrdStsBit0.Caption:='所有运动完成/无插补指令';
     Lb_CrdStsBit1.Caption:='缓冲区插补指令编程结束';
     Lb_CrdStsBit2.Caption:='伺服周期过小';
     Lb_CrdStsBit3.Caption:='面向坐标系的指令错误';
     Lb_CrdStsBit4.Caption:='单段轨迹运动完成';
     Lb_CrdStsBit5.Caption:='加速度超限报警';
     Lb_CrdStsBit6.Caption:='异常时自动停允许';
     Lb_CrdStsBit7.Caption:='立即插补状态';
     Lb_CrdStsBit8.Caption:='当前无映射坐标系命令';
     Lb_CrdStsBit9.Caption:='相关电机轴异常错误';
     Lb_CrdStsBit10.Caption:='坐标系运动异常错误';
     PageControl1.Pages[0].Caption:='单轴状态';
     PageControl1.Pages[1].Caption:='坐标状态';
     PageControl1.Pages[2].Caption:='命令状态';
     Axis1.Caption:='轴1:';
     Axis2.Caption:='轴2:';
     Axis3.Caption:='轴3:';
     MotionPage.Pages[0].Caption:='初始设置';
     MotionPage.Pages[1].Caption:='插补';
     MotionPage.Pages[2].Caption:='IO控制';
     MotionPage.Pages[3].Caption:='单轴控制';
     MotionPage.Pages[4].Caption:='连续运动';
     btnSaveCmd.Caption:='保存';
     btnClearCmd.Caption:='清除';
     LMessageSts1.Caption:='红色代表对应位被置为1，灰色代表对应位被置为0。';
     LMessageSts2.Caption:='红色代表对应位被置为1，灰色代表对应位被置为0。';
     ArcXY1.Caption:='XY圆弧1插补';
     ArcXY2.Caption:='XY圆弧2插补';
     ArcYZ1.Caption:='YZ圆弧1插补';
     ArcYZ2.Caption:='YZ圆弧2插补';
     ArcZX1.Caption:='ZX圆弧1插补';
     ArcZX2.Caption:='ZX圆弧2插补';
     LnXY.Caption:='两轴直线插补';
     LnXYZ.Caption:='三轴直线插补';
     MvXY.Caption:='设定XY起点';
     MvXYZ.Caption:='设定XYZ起点';
     SetSynVel.Caption:='设置速度';
     SetSynAcc.Caption:='设置加速度';
     SetDFlag.Caption:='设置减速标志';
     SetCrdOffset.Caption:='设置三轴偏移量';
     btnStrtList.Caption:='打开缓冲区';
     btnEndList.Caption:='关闭缓冲区';
     btnAddList.Caption:='重新打开缓冲区';
     btnStrtMtn.Caption:='启动缓冲区运动';
     btnGetMtnNo.Caption:='读取执行段号';     
     PGMemo.Pages[0].Caption:='命令显示区';
     PGMemo.Pages[1].Caption:='文件执行区';
 btnOpen.Caption:='打开控制卡';
     btnClose.Caption:='关闭控制卡';
     Reset.Caption:='软件复位';
     DrvRst.Caption:='驱动器复位';
     btnAlarm.Caption:='报警开关';
     CtrlMode.Caption:='运动模式';
     btnLimt.Caption:='限位电平';
     setStrtVel.Caption:='起跳频率';
     SetMaxVel.Caption:='最大速度';
     SetStpAcc.Caption:='停止加速度';
     OpenAxis.Caption:='轴驱动使能';
     HardRst.Caption:='硬件复位';
     SetCrdOffset.Caption:='设置三轴偏移量';
     btnRestoreMtn.Caption:='继续运动';
     btnEStpMtn.Caption:='立即停止';
     btnStpMtn.Caption:='平滑停止';
     btnRunCmd.Caption:='启动运动';
     btnOverride.Caption:='速度倍率';
     btnCaptHome.Caption:='设置Home捕获';
     btnSetAltPos.Caption:='设置轴实际位置';
     btnRstSts.Caption:='清除轴状态位';
     btnClrSts.Caption:='清除轴状态';
     btnZeroPos.Caption:='清除轴位置';
     btnIoInput.Caption:='16路IO输入标志';
     btnIOOutput.Caption:='16路IO输出标志';
     btnOpenFile.Caption:='打开文件(&O)';
     btnSaveFile.Caption:='保存文件(&S)';
     btnExit.Caption:='退出程序(&X)';
     btnEditFile.Caption:='清空命令区(&C)';     
   end
   else
   begin
     Lb_MtnDone.Caption:='Motion Done';
     Lb_ServoAlm.Caption:='Servo Alarm';
     Lb_BrkReached.Caption:='Brk Pnt Reached';
     Lb_Captured.Caption:='Pos Captured';
     Lb_MtnErr.Caption:='Motion Error';
     Lb_PosLmtTriggered.Caption:='Pos Lmt Trig';
     Lb_NegLmtTriggered.Caption:='Neg Lmt Trig';
     Lb_HostCmdErr.Caption:='Host Cmd Err';
     Lb_LoopClosed.Caption:='Loop Closed';
     Lb_AxisOn.Caption:='Axis On';
     Lb_InMtn.Caption:='Axis In Motion';
     Lb_LmtOn.Caption:='Lmt Enabled';
     Lb_PrflT.Caption:='T Curve';
     Lb_PrflS.Caption:='S Curve';
     Lb_PrflV.Caption:='Vel Contouring';
     Lb_PrflG.Caption:='G Mode';
     Lb_PrflC.Caption:='Crd Mode';
     Lb_HomeCapt.Caption:='Home Capture';
     Lb_IndexCapt.Caption:='Index Capture';
     Lb_CrdStsBit0.Caption:='All Motion done or no cmd';
     Lb_CrdStsBit1.Caption:='Interp input finish';
     Lb_CrdStsBit2.Caption:='Sample time too small';
     Lb_CrdStsBit3.Caption:='Crd command error';
     Lb_CrdStsBit4.Caption:='Single segment finished';
     Lb_CrdStsBit5.Caption:='Max acc exceeded';
     Lb_CrdStsBit6.Caption:='Auto stop enabled';
     Lb_CrdStsBit7.Caption:='Immediately Interp mode';
     Lb_CrdStsBit8.Caption:='No crd projections CMD';
     Lb_CrdStsBit9.Caption:='Axis error';
     Lb_CrdStsBit10.Caption:='Crd Motion error';
     PageControl1.Pages[0].Caption:='Axis Status';
     PageControl1.Pages[1].Caption:='Crd Status';
     PageControl1.Pages[2].Caption:='Command Status';
     AxisPosition.Caption:='Axis Position';
     Axis1.Caption:='Axis 1:';
     Axis2.Caption:='Axis 2:';
     Axis3.Caption:='Axis 3:';
     MotionPage.Pages[0].Caption:='Initializtion';
     MotionPage.Pages[1].Caption:='Interpolation';
     MotionPage.Pages[2].Caption:='IO Control';
     MotionPage.Pages[3].Caption:='Axis Control';
     MotionPage.Pages[4].Caption:='Curve Motion';
     btnSaveCmd.Caption:='Save';
     btnClearCmd.Caption:='Clear';
     LMessageSts1.Caption:='Red -- 1 , Gray -- 0.';
     LMessageSts2.Caption:='Red -- 1 , Gray -- 0.';
     ArcXY1.Caption:='ArcXY';
     ArcXY2.Caption:='ArcXYP';
     ArcYZ1.Caption:='ArcYZ';
     ArcYZ2.Caption:='ArcYZP';
     ArcZX1.Caption:='ArcZX';
     ArcZX2.Caption:='ArcZXP';
     LnXY.Caption:='LnXY';
     LnXYZ.Caption:='LnXYZ';
     MvXY.Caption:='Move XY';
     MvXYZ.Caption:='Move XYZ';
     SetSynVel.Caption:='Set Syn Vel';
     SetSynAcc.Caption:='Set Syn Acc';
     SetDFlag.Caption:='GetSegPnt';
     SetCrdOffset.Caption:='MapCnt';
     btnStrtList.Caption:='StrtList';
     btnEndList.Caption:='EndList';
     btnAddList.Caption:='AddList';
     btnStrtMtn.Caption:='StrtMtn';
     btnGetMtnNo.Caption:='GetMtnNm';
     PGMemo.Pages[0].Caption:='Cmd View Area';
     PGMemo.Pages[1].Caption:='File Run Area';
     btnOpen.Caption:='Open GT';
     btnClose.Caption:='Close GT';
     Reset.Caption:='Software Reset';
     DrvRst.Caption:='Driver Reset';
     btnAlarm.Caption:='Alarm';
     CtrlMode.Caption:='Control Mode';
     btnLimt.Caption:='Limit Status';
     setStrtVel.Caption:='Set Start Vel';
     SetMaxVel.Caption:='Set Max Vel';
     SetStpAcc.Caption:='Set Stop Acc';
     OpenAxis.Caption:='Open Axis';
     HardRst.Caption:='Hard Reset';
     SetCrdOffset.Caption:='Set Offset';
     btnRestoreMtn.Caption:='RestoreMtn';
     btnEStpMtn.Caption:='EStpMtn';
     btnStpMtn.Caption:='StpMtn';
     btnRunCmd.Caption:='Run Command';
     btnOverride.Caption:='Override';
     btnCaptHome.Caption:='CaptHome';
     btnSetAltPos.Caption:='Set Actual Position';
     btnRstSts.Caption:='RstSts';
     btnClrSts.Caption:='Clear Status';
     btnZeroPos.Caption:='ZeroPos';
     btnIoInput.Caption:='Input Control';
     btnIOOutput.Caption:='Output Control';
     btnOpenFile.Caption:='Open File(&O)';
     btnSaveFile.Caption:='Save File(&S)';
     btnExit.Caption:='Exit(&X)';
     btnEditFile.Caption:='Clear content(&C)';
   end;
end;


end.
