unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    GroupBox1: TGroupBox;
    Label3: TLabel;
    ComComboBox: TComboBox;
    Label4: TLabel;
    BaudRateComboBox: TComboBox;
    GroupBox2: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    Label5: TLabel;
    Address: TEdit;
    Label6: TLabel;
    Out: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure OpenCom;
    procedure ComComboBoxChange(Sender: TObject);
    procedure BaudRateComboBoxChange(Sender: TObject);
    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 I87000,I7000,I7000u;

Var
   bCfgChg , bComOpen : Boolean ;
   iCounter           : integer ;
   gw7000       : Array[0..10] of LongInt;
   gf7000       : Array[0..10] of single;
   gdwBuf       : Array[0..10] of LongInt;
   gfBuf        : Array[0..10] of Single;

{$R *.DFM}

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

     bCOMOpen   := False;
     bCfgChg    := False;
     gszSend    := StrAlloc( 100 );
     gszReceive := StrAlloc( 100 );
     iCounter   := 0 ;
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;
    bCfgChg  := False;
end;

procedure TForm1.ComComboBoxChange(Sender: TObject);
begin
     bCfgChg := True;
end;

procedure TForm1.BaudRateComboBoxChange(Sender: TObject);
begin
     bCfgChg := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     if bCfgChg and bComOpen then  // Reopen Com port
     begin
        Close_Com(gcPort);
        bComOpen := False;
     end;

     if Not bComOpen then
        OpenCom;

    gw7000[0] := Word(gcPort);         // COM Port
    gw7000[1] := StrToInt('$' + Address.Text); // Module Address
    gw7000[2] := $87024;                // 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
    gw7000[6] := 1;                        // string debug

    gf7000[0] := StrTofloat(Out.Text);
    wRet := AnalogOut_87K(@gw7000[0], @gf7000[0], gszSend, gszReceive);

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

    Inc(iCounter) ;
    Label1.Caption := 'Counter=' + IntToStr(iCounter)
                      + #13
                      + 'The Analog Output value:' + Format('%6.3f', [gf7000[0]] ) ;
    Beep;
end;

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

end.
