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;
    Timer1: TTimer;
    btnActive: TButton;
    Image1: TImage;
    Image2: TImage;
    Shape1: TShape;
    PicHostWatchdog: TImage;
    PicModuleWatchdog: TImage;
    sBreakModule: TShape;
    sBreakHostPC: TShape;
    Shape5: TShape;
    Button1: TButton;
    TimerSendHostIsOk: TTimer;
    TimerModuleReset: TTimer;
    TimerHostPCDead: TTimer;
    Label1: TLabel;
    eModuleID: TEdit;
    sBreakLine: TShape;
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    Procedure Opencom ;
    procedure Timer1Timer(Sender: TObject);
    procedure btnActiveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Image1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure TimerSendHostIsOkTimer(Sender: TObject);
    procedure TimerModuleResetTimer(Sender: TObject);
    procedure PicModuleWatchdogClick(Sender: TObject);
    procedure TimerHostPCDeadTimer(Sender: TObject);
    procedure sBreakHostPCMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure sBreakModuleMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image2Click(Sender: TObject);
  private
    { Private declarations }
    procedure FSendHostIsOK;
    procedure Check_Error(sFunctionName :String; wErrCode :Integer);
    procedure FSetupHostWatchdog;
    procedure FStopHostWatchdog;
    procedure FClearHostFailureMode;
    Function  FReadHostWatchdogStatus:Integer;
    Function  FReadModuleResetStatus: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             := 1000;
     Timer1.Enabled              := False;
     Image1.Picture.LoadFromFile('Term.bmp');
     Image2.Picture.LoadFromFile('I7012D.bmp');
     PicHostWatchdog.Picture.LoadFromFile('HWDog.bmp');
     PicModuleWatchdog.Picture.LoadFromFile('MWDog.bmp');
     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.FSetupHostWatchdog;
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
    gw7000[5]     := 1;                               // Enable Host Watchdog
    gw7000[6]     := 1;                               // string debug
    gw7000[7]     := 50;                              //Time Interval 5 Seconds

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

end;

procedure TForm1.FStopHostWatchdog;
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
    gw7000[5]     := 0;                               // Disable Host Watchdog
    gw7000[6]     := 1;                               // string debug
    gw7000[7]     := 0;                               // Time Interval 5 Seconds

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

end;


Function TForm1.FReadHostWatchdogStatus:Integer;
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 );

    Result := gw7000[5];
end;

procedure TForm1.FSendHostIsOK;
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.FClearHostFailureMode;
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 := ResetModuleHostWatchdogStatus(@gw7000[0], @gf7000[0], gszSend, gszReceive);
     Check_Error( 'ResetModuleHostWatchdogStatus() error !!! ', wRet );

end;



Function TForm1.FReadModuleResetStatus:integer;
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 );

    Result := gw7000[5];
end;

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

     if FReadHostWatchdogStatus = 4 then
     begin
          TimerHostPCDead.Enabled := True;
     end;

     if FReadModuleResetStatus = 1 then
        TimerModuleReset.Enabled := True;

     bProcessing := False;
end;

procedure TForm1.btnActiveClick(Sender: TObject);
begin
    If btnActive.Caption = 'Stop' Then
    begin
        btnActive.Caption := 'Active';
        if Button1.Tag = 1 then
           Button1Click( Sender );
        //FClearHostFailureMode;
        TimerHostPCDead.Enabled := False;
        TimerModuleReset.Enabled := False;
        Timer1.Enabled    := False;
        //Close_Com (gcPort);
        GroupBox1.Enabled := True;
        Exit;
    End;

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

    wErrCnt          := 0;
    FReadModuleResetStatus;   //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;
        sBreakLine.Visible   := False;
        sBreakModule.Visible := False;
        Exit;
    End;

    If ((wErrCode = 15) Or (wErrCode = 10)) Then
    begin
        if wErrCnt < 100 then inc( wErrCnt );
        If wErrCnt > 0 Then
        begin
             //beep;
             sBreakLine.Visible   := True;
             sBreakModule.Visible := True;
        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;


procedure TForm1.Image1Click(Sender: TObject);
begin
     if Not bCOMOpen then
        exit;
     TimerSendHostIsOK.Enabled := Not TimerSendHostIsOK.Enabled;
     sBreakHostPC.Visible      := Not TimerSendHostIsOK.Enabled;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     if Not bCOMOpen then
        exit;
     if Button1.Tag = 0 then    //Enable the Host Watchdog
     begin
          Button1.Caption := 'Disable Host Watchdog';
          Button1.Tag     := 1;
          FSetupHostWatchdog;
     end
     else
     begin
          Button1.Caption := 'Enable Host Watchdog';
          Button1.Tag     := 0;
          FClearHostFailureMode;
          FStopHostWatchdog;
          TimerHostPCDead.Enabled := False;
     end;
end;

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

     FSendHostIsOK;

     bProcessing := False;
end;

procedure TForm1.TimerModuleResetTimer(Sender: TObject);
Var
     i, st, lp : integer;
begin
     if Not bCOMOpen then
        exit;
     if bProcessing then
        exit
     else
        bProcessing := True;

     beep;
     sBreakModule.Visible := True;
     st := 10;  //Step interval
     lp := 6;   //total loops
     PicModuleWatchdog.Visible := True;
     for i:= 1 to lp do
     begin
          With PicModuleWatchdog do
          begin
              Top := Top - st;
              Height := Height + st;
              Width := Width + st ;
              Application.ProcessMessages;
              Sleep( 100 );
          end;
     end;
     With PicModuleWatchdog do
     begin
          Visible := False;
          Top     := Top    + st * lp ;
          width   := width  - st * lp;
          Height  := Height - st * lp;
     end;
     sBreakModule.Visible := False;
     bProcessing := False;
end;

procedure TForm1.PicModuleWatchdogClick(Sender: TObject);
begin
     TimerModuleReset.Enabled := False;
end;

procedure TForm1.TimerHostPCDeadTimer(Sender: TObject);
Var
     i, st, lp : integer;
begin
     if Not bCOMOpen then
        exit;
     if bProcessing then
        exit
     else
        bProcessing := True;

     beep;
     st := 10;  //Step interval
     lp := 6;   //total loops
     sBreakLine.Visible      := True;
     sBreakHostPC.Visible    := True;
     PicHostWatchdog.Visible := True;
     for i:= 1 to lp do
     begin
          With PicHostWatchdog do
          begin
              Top    := Top    - st;
              Left   := Left   - st;
              Height := Height + st;
              Width  := Width  + st ;
              Application.ProcessMessages;
              Sleep( 100 );
          end;
     end;
     With PicHostWatchdog do
     begin
          Visible := False;
          Top     := Top    + st * lp;
          Left    := Left   + st * lp;
          width   := width  - st * lp;
          Height  := Height - st * lp;
     end;
     sBreakLine.Visible   := False;
     sBreakHostPC.Visible := False;
     bProcessing := False;
end;

procedure TForm1.sBreakHostPCMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
     if Not bCOMOpen then
        exit;
     TimerSendHostIsOK.Enabled := Not TimerSendHostIsOK.Enabled;
     sBreakHostPC.Visible      := Not TimerSendHostIsOK.Enabled;
end;

procedure TForm1.sBreakModuleMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
     TimerModuleReset.Enabled := False;
end;

procedure TForm1.Image2Click(Sender: TObject);
begin
     TimerModuleReset.Enabled := False;
end;


end.
