unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Button2: TButton;
    Label4: TLabel;
    Label5: TLabel;
    RadioGroup1: TRadioGroup;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    ComComboBox: TComboBox;
    BaudRateComboBox: TComboBox;
    Label2: TLabel;
    Address: TEdit;
    GroupBox2: TGroupBox;
    Label7: TLabel;
    eTimeInterval: TEdit;
    Button1: TButton;
    Button4: TButton;
    Button5: TButton;
    Button3: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label6: TLabel;
    Button6: TButton;
    Label8: TLabel;
    Label9: TLabel;
    Edit2: TEdit;
    Label10: TLabel;
    Edit3: TEdit;
    GroupBox3: TGroupBox;
    Button7: TButton;
    Edit4: TEdit;
    Timer1: TTimer;
    btnActive: TButton;
    Label3: TLabel;
    eModuleID: TEdit;
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    Procedure Opencom ;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure btnActiveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    procedure Check_Error(sFunctionName :String; wErrCode :Integer);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

  iRet, iStatus : Integer;
  iCount        : Integer;
  wRet          : Word ;
  bProcessing   : boolean;

implementation

Uses I7000,I7000u;

Var
   bComOpen : Boolean ;
   wErrCnt  : LongInt ;

{$R *.DFM}

procedure TForm1.Button2Click(Sender: TObject);
begin
     if bComOpen then
        Close_Com( gcPort );
     Close;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
     ComComboBox.ItemIndex       := 0;
     BaudRateComboBox.ItemIndex  := 4;
     bCOMOpen                    := False;
     bProcessing                 := False;
     Timer1.Interval             := 2000;
     Timer1.Enabled              := False;
     gszSend    := StrAlloc( 100 );
     gszReceive := StrAlloc( 100 );
end;

Procedure TForm1.OpenCom;
Var
   iRet , iConfirm : Integer ;

Begin
    gcPort := Char(ComComboBox.ItemIndex + 1 );       // Setting Com Port
    gdwBaudRate := StrToInt(BaudRateComboBox.Text);

    iRet := IOpenCom(gcPort, gdwBaudRate);
    If iRet > 0 Then
    Begin
        Beep;
        iConfirm := MessageDlg('OPEN_COM Error Code:' + IntToStr(iRet) + #13 + IGetErrorString(iRet) + #13 + 'Quit this demo?'
                               , mtConfirmation , [mbYes,mbNo] , 0 );
        If iConfirm = mrYes Then
           Close;
    End
    else
        bCOMOpen := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin

    gw7000[0] := Word(gcPort);                        // COM Port
    gw7000[1] := StrToInt('$' + Address.Text);        // Module Address
    //gw7000[2] := StrToInt('$' + TypeComboBox.Text); //Not used

    If RadioButton1.Checked Then
        gw7000[3] := 1                                // CheckSum Enable
    Else
        gw7000[3] := 0;

    gw7000[4] := 100;                                 // TimeOut = 0.1 second
    if ((eTimeInterval.Text = '') or (eTimeInterval.Text = '0')) then
       gw7000[5] := 0                                 // disable Host Watchdog
    else
        gw7000[5]:= 1;                                // Enable Host Watchdog
    gw7000[6] := 1;                                   // string debug
    gw7000[7] := StrToInt(eTimeInterval.Text);        //Time Interval

    Beep;
    wRet := ToSetupHostWatchdog(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    Check_Error( 'ToSetupHostWatchdog() error !!! ', wRet );

end;


procedure TForm1.Button3Click(Sender: TObject);
begin

    gw7000[0] := Word(gcPort);                        // COM Port
    gw7000[1] := StrToInt('$' + Address.Text);        // Module Address

    If RadioButton1.Checked Then
        gw7000[3] := 1                                // CheckSum Enable
    Else
        gw7000[3] := 0;

    gw7000[4] := 100;                                 // TimeOut = 0.1 second
    gw7000[6] := 1;                                   // string debug

    wRet := ReadModuleHostWatchdogStatus(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    Check_Error( 'ReadModuleHostWatchdogStatus() error !!! ', wRet );

    Edit1.Text := IntToStr(gw7000[5]);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
     gw7000[0] := Word(gcPort);                      // COM Port
     If RadioButton1.Checked Then
         gw7000[3] := 1                              // CheckSum Enable
     Else
         gw7000[3] := 0;

     gw7000[4] := 100;                               // TimeOut = 0.1 second
     gw7000[6] := 1;                                 // string debug

     Beep;
     wRet := HostIsOK(@gw7000[0], @gf7000[0], gszSend, gszReceive);
     //Check_Error( 'HostIsOK() error !!! '; wRet );
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
     Timer1.Enabled := False;
     gw7000[0] := Word(gcPort);                      // COM Port
     gw7000[1] := StrToInt('$' + Address.Text);      // Module Address
     If RadioButton1.Checked Then
         gw7000[3] := 1                              // CheckSum Enable
     Else
         gw7000[3] := 0;

     gw7000[4] := 100;                               // TimeOut = 0.1 second
     gw7000[6] := 1;                                 // string debug

     wRet := ResetModuleHostWatchdogStatus(@gw7000[0], @gf7000[0], gszSend, gszReceive);
     Check_Error( 'ResetModuleHostWatchdogStatus() error !!! ', wRet );

     Application.MessageBox( PChar('Host Watchdog has been disabled when the Host Watchdog status = 4.' + #13
         + 'You must setup the Host Watchdog again after you clear the status to 0.' + #13 + #13
         + 'Press button [OK] to setup the Host Watchdog' )
         , PChar('Important Informations!!') , mb_IconExclamation );

     Button1Click( Sender );
     Timer1.Enabled := True;
end;



procedure TForm1.Button6Click(Sender: TObject);
begin
    gw7000[0] := Word(gcPort);                        // COM Port
    gw7000[1] := StrToInt('$' + Address.Text);        // Module Address

    If RadioButton1.Checked Then
        gw7000[3] := 1                                // CheckSum Enable
    Else
        gw7000[3] := 0;

    gw7000[4] := 100;                                 // TimeOut = 0.1 second
    gw7000[6] := 1;                                   // string debug

    wRet := ToReadHostWatchdog(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    Check_Error( 'ToReadHostWatchdog() error !!! ', wRet );

    Edit2.Text := IntToStr(gw7000[5]);
    Edit3.Text := IntToStr(gw7000[7]);
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
    gw7000[0] := Word(gcPort);                        // COM Port
    gw7000[1] := StrToInt('$' + Address.Text);        // Module Address
    gw7000[2] := StrToInt('$' + eModuleID.Text);      // Module ID

    If RadioButton1.Checked Then
        gw7000[3] := 1                                // CheckSum Enable
    Else
        gw7000[3] := 0;

    gw7000[4] := 100;                                 // TimeOut = 0.1 second
    gw7000[6] := 1;                                   // string debug

    wRet := ReadModuleResetStatus(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    Check_Error( 'ReadModuleResetStatus() error !!! ', wRet );

    Edit4.Text := IntToStr(gw7000[5]);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
     if Not bCOMOpen then
        exit;
     if bProcessing = True then
        exit
     else
         bProcessing := True ;

     beep;

     Button3Click(Sender);
     If Edit1.Text ='4' Then
        Application.MessageBox(PChar('Host Watchdog Status = 4' + #13 + #13
               + 'Host is down, and this moudle go to safe state.' + #13
               + 'All output command will be ignored when module in safe state.' + #13 + #13
               + 'The safe value is used to prevent your applications from critical damages.' + #13
               + 'To continue your job correctly,' + #13
               + 'you must clear this status firstly and then output the correct value again.')
               , PChar('Host is down !!! ') , mb_IconExclamation );

     Button6Click(Sender);

     Button7Click(Sender);
     if Edit4.Text = '1' then
        Application.MessageBox( PChar('Module Status S = 1' + #13 + #13
            + 'This moudle has been reset and use the PowerOn value!!' + #13
            + 'The PowerOn value may be not appropriate and may be dangerous for your jobs.' + #13
            + 'Thus, you must output the correct value again for your work correctly.')
            , PChar('Reset !!! ') , mb_IconExclamation );

     bProcessing := False;
end;

procedure TForm1.btnActiveClick(Sender: TObject);
begin
    If btnActive.Caption = 'Stop' Then
    begin
        btnActive.Caption := 'Active';
        Timer1.Enabled    := False;
        Close_Com (gcPort);
        GroupBox1.Enabled := True;
        Button1.Enabled   := False;
        Button3.Enabled   := False;
        Button4.Enabled   := False;
        Button5.Enabled   := False;
        Button6.Enabled   := False;
        Button7.Enabled   := False;
        Exit;
    End;

    btnActive.Caption     := 'Stop';
    GroupBox1.Enabled     := False;
    OpenCom;
    If bCOMOpen = False Then  //COM port open failure
    begin
        btnActiveClick( Sender );
        Exit;
    End;

    Button1.Enabled := True;
    Button3.Enabled := True;
    Button4.Enabled := True;
    Button5.Enabled := True;
    Button6.Enabled := True;
    Button7.Enabled := True;
    wErrCnt          := 0;
    Button7Click(Sender);   //Read Module-Reset-Status ==> Clear status
    Timer1.Enabled   := True;

end;

Procedure TForm1.Check_Error(sFunctionName:String; wErrCode:Integer);
begin
    If wErrCode = NoError Then
    begin
        wErrCnt := 0;
        Exit;
    End;

    If ((wErrCode = 15) Or (wErrCode = 10)) Then
    begin
        inc( wErrCnt );
        If wErrCnt > 10 Then
            Exit;
        If wErrCnt > 9 Then
        begin
            Beep;
            Application.MessageBox( PChar('Module has no response for ' + IntToStr(wErrCnt) + ' times continuity.'
                 + #13 + #13 + 'The connection between module and computer may be broken.'
                 + #13 + 'Or, this module has been break down.' + #13 + 'Please check it!!')
                 , PChar(sFunctionName) , mb_IconExclamation );
        End;
    end
    Else
    begin
        Beep;
        Application.MessageBox( PChar('The Error Code:' + IntToStr(wErrCode) + #13
                 + IGetErrorString(wErrCode) )
                 , PChar(sFunctionName) , mb_IconExclamation );
    End;

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
     StrDispose( gszSend );
     StrDispose( gszReceive );
end;

end.
