unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Label2: TLabel;
    cbModuleID: TComboBox;
    Label3: TLabel;
    ComComboBox: TComboBox;
    Label4: TLabel;
    BaudRateComboBox: TComboBox;
    GroupBox2: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    Label5: TLabel;
    Address: TEdit;
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    btnActive: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure OpenCom;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnActiveClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

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

implementation

Uses I7000,I7000u;

Var
   bCfgChg , bComOpen : Boolean ;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
     cbModuleID.ItemIndex       := 0 ;
     ComComboBox.ItemIndex      := 0 ;
     BaudRateComboBox.ItemIndex := 4 ;

     bCOMOpen   := False;
     bCfgChg    := False;
     gszSend    := StrAlloc( 100 );
     gszReceive := StrAlloc( 100 );
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
     if bComOpen then
        Close_Com( gcPort );
     Close;
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;

     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('$' + cbModuleID.Text); // Module ID

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

    gw7000[4] := 200;     // TimeOut = 0.2 second
    gw7000[5] := 0;       // channel no, Don't care in this Demo
    gw7000[6] := 1;       // string debug

    wRet := AnalogInAll(@gw7000[0], @gf7000[0], gszSend, gszReceive);

    If wRet <> 0 Then
    Begin
        Beep;
        Application.MessageBox( PChar('The Error Code:' + IntToStr(wRet) + #13 + IGetErrorString(wRet))
                                , 'AnalogIn8() error !!! ' , mb_IconExclamation );
    end;

    Edit1.Text := Format('%6.3f', [gf7000[0]] );
    Edit2.Text := Format('%6.3f', [gf7000[1]] );
    Edit3.Text := Format('%6.3f', [gf7000[2]] );
    Edit4.Text := Format('%6.3f', [gf7000[3]] );
    Edit5.Text := Format('%6.3f', [gf7000[4]] );
    Edit6.Text := Format('%6.3f', [gf7000[5]] );
    Edit7.Text := Format('%6.3f', [gf7000[6]] );
    Edit8.Text := Format('%6.3f', [gf7000[7]] );
end;

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

procedure TForm1.btnActiveClick(Sender: TObject);
begin
     if btnActive.Caption = 'Stop' then
     begin
        btnActive.Caption := 'Active';
        GroupBox1.Enabled := True;
        Button1.Enabled   := False;
        Button2.Enabled   := True;
        Close_COM( gcPort );
        bCOMOpen          := False;
        exit;
     end;

     btnActive.Caption := 'Stop';
     GroupBox1.Enabled := False;
     Button1.Enabled   := True;
     Button2.Enabled   := False;

     OpenCom;
     if bCOMOpen = False then
     begin
        btnActiveClick(sender);
        exit;
     end;

end;

end.
