unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Button2: TButton;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    RadioGroup1: TRadioGroup;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    TypeComboBox: TComboBox;
    ComComboBox: TComboBox;
    BaudRateComboBox: TComboBox;
    Label2: TLabel;
    Address: TEdit;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label6: TLabel;
    ePowerOn: TEdit;
    eSafe: TEdit;
    Button3: TButton;
    Label7: TLabel;
    eTimeInterval: TEdit;
    Button1: TButton;
    Button4: TButton;
    Button5: TButton;
    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 FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

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

implementation

Uses I7000,I7000u;

Var
   iIndex : integer ;
   bComOpen : Boolean ;

{$R *.DFM}

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

procedure TForm1.FormCreate(Sender: TObject);
begin
     TypeComboBox.ItemIndex      := 0;
     ComComboBox.ItemIndex       := 0;
     BaudRateComboBox.ItemIndex  := 4;
     bCOMOpen  := False;
     iIndex    := 0;
     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
     if Not bComOpen then
        OpenCom;
     if Not bComOpen then exit;

    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] := StrToInt(eTimeInterval.Text); //Time Interval

    Beep;
    wRet := ToSetupHostWatchdog(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    If wRet <> 0 Then
    Begin
        Application.MessageBox( PChar('The Error Code:' + IntToStr(wRet) + #13 + IGetErrorString(wRet))
                                , 'ToSetupHostWatchdog() error !!! ' , mb_IconExclamation );
    end;
end;


procedure TForm1.Button3Click(Sender: TObject);
Var
     sMaxValue : String;
begin
     if bComOpen then  // Reopen Com port
     begin
        Close_Com(gcPort);
        bComOpen := False;
     end;

     if Not bComOpen then
        OpenCom;
     if Not bComOpen then exit;

    //------- firstly, set the PowerOn Value -----------
    gw7000[0] := Word(gcPort);         // COM Port
    gw7000[1] := StrToInt('$' + Address.Text); // Module Address
    gw7000[2] := StrToInt('$' + TypeComboBox.Text);
    sMaxValue := '';
    case gw7000[2] of
         $7042 :   sMaxValue := '1FFF';
         $7043 :   sMaxValue := 'FFFF';
         $7044 :   sMaxValue := 'FF';
         $7050 :   sMaxValue := 'FF';
         $7060 :   sMaxValue := 'F';
         $7065 :   sMaxValue := '1F';
         $7067 :   sMaxValue := '7F';
    end;

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

    gw7000[4] := 100;                     // TimeOut = 0.1 second
    gw7000[5] := StrToInt(ePowerOn.Text); // PowerOn
    gw7000[6] := 1;                       // string debug

    wRet := SetPowerOnValueForDo(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    If wRet <> 0 Then
    Begin
        if wRet = 4 then
                Application.MessageBox( PChar('The Error Code:' + IntToStr(wRet)
                                        + #13 + IGetErrorString(wRet)
                                        + #13 + sMaxValue )
                                , 'SetPowerOnValueForDo() error !!! ' , mb_IconExclamation )
        else
                Application.MessageBox( PChar('The Error Code:' + IntToStr(wRet) + #13 + IGetErrorString(wRet))
                                , 'SetPowerOnValueForDo() error !!! ' , mb_IconExclamation );
    end;

    //------- secondly, set the Safe Value -----------
    gw7000[5] := StrToInt(eSafe.Text); // Safe Value

    Beep;
    wRet := SetSafeValueForDo(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    If wRet <> 0 Then
    Begin
        if wRet = 4 then
                Application.MessageBox( PChar('The Error Code:' + IntToStr(wRet)
                                        + #13 + IGetErrorString(wRet)
                                        + #13 + sMaxValue )
                                , 'SetSafeValueForDo() error !!! ' , mb_IconExclamation )
        else
                Application.MessageBox( PChar('The Error Code:' + IntToStr(wRet) + #13 + IGetErrorString(wRet))
                                , 'SetSafeValueForDo() error !!! ' , mb_IconExclamation );
    end;

end;

procedure TForm1.Button4Click(Sender: TObject);
begin
     if Not bComOpen then
        OpenCom;
     if Not bComOpen then exit;

     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);
     If wRet <> 0 Then
     Begin
        Application.MessageBox( PChar('The Error Code:' + IntToStr(wRet) + #13 + IGetErrorString(wRet))
                                , 'HostIsOK() error !!! ' , mb_IconExclamation );
     end;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
     if Not bComOpen then
        OpenCom;
     if Not bComOpen then exit;

     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

     Beep;
     wRet := ResetModuleHostWatchdogStatus(@gw7000[0], @gf7000[0], gszSend, gszReceive);
     If wRet <> 0 Then
     Begin
        Application.MessageBox( PChar('The Error Code:' + IntToStr(wRet) + #13 + IGetErrorString(wRet))
                                , 'ResetModuleHostWatchdogStatus() error !!! ' , mb_IconExclamation );
     end;
end;

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

end.
