unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    GroupBox1: TGroupBox;
    Address: TEdit;
    Label2: TLabel;
    Button1: TButton;
    Button2: TButton;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    RadioGroup1: TRadioGroup;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    TypeComboBox: TComboBox;
    ComComboBox: TComboBox;
    BaudRateComboBox: TComboBox;
    Label6: TLabel;
    Edit2: TEdit;
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    Procedure Opencom ;
    procedure Button1Click(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 bComOpen then  // Reopen Com port
     begin
        Close_Com(gcPort);
        bComOpen := False;
     end;

     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 ); // Module ID

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

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

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

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

    If gw7000[6] = 1 Then
    Begin
        Edit1.Text := gszSend;
        Edit2.Text := gszReceive;
    End;
    Beep;
end;

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

end.
