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;
    Label2: TLabel;
    ChNo: TEdit;
    Label7: TLabel;
    RB1: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    RB2: TEdit;
    RB3: TEdit;
    RB0: TEdit;
    Label12: TLabel;
    Label13: TLabel;
    RB20: TEdit;
    RB21: TEdit;
    RB22: TEdit;
    RB23: TEdit;
    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 I7000, I7000u;

Var
   bCfgChg , bComOpen : Boolean ;
   iCounter           : integer ;

{$R *.DFM}

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

     gcDataBit  := Char(8);      // 8 data bit
     gcParity   := Char(0);      // Non Parity
     gcStopBit  := Char(0);      // One Stop Bit
     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 := Open_Com(gcPort, gdwBaudRate, gcDataBit, gcParity, gcStopBit);
    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] := $7024;                // Module ID

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

    gw7000[4] := 200;                      // TimeOut = 0.2 second
    gw7000[5] := StrToInt(ChNo.Text);      // Channel No
    gw7000[6] := 1;                        // string debug

    gf7000[0] := StrToFloat(Out.Text);
    wRet := AnalogOut(@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);
        Exit;
    end;

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

    //********************************************** AnalogOutReadBack AA6
    gw7000[5] := 0;                        // command ReadBack AA6
    gw7000[7] := 0;                        // Channel 0
    wRet := AnalogOutReadBack(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    If wRet <> 0 Then
        RB0.Text := 'Err'
    else
        RB0.Text := FloatToStr(gf7000[0]);

    gw7000[7] := 1;                        // Channel 1
    wRet := AnalogOutReadBack(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    If wRet <> 0 Then
        RB1.Text := 'Err'
    else
        RB1.Text := FloatToStr(gf7000[0]);

    gw7000[7] := 2;                        // Channel 2
    wRet := AnalogOutReadBack(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    If wRet <> 0 Then
        RB2.Text := 'Err'
    else
        RB2.Text := FloatToStr(gf7000[0]);

    gw7000[7] := 3;                        // Channel 3
    wRet := AnalogOutReadBack(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    If wRet <> 0 Then
        RB3.Text := 'Err'
    else
        RB3.Text := FloatToStr(gf7000[0]);


    //********************************************** AnalogOutReadBack AA8
    gw7000[5] := 1;                        // command ReadBack AA8
    gw7000[7] := 0;                        // Channel 0
    wRet := AnalogOutReadBack(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    If wRet <> 0 Then
        RB20.Text := 'Err'
    else
        RB20.Text := FloatToStr(gf7000[0]);

    gw7000[7] := 1;                        // Channel 1
    wRet := AnalogOutReadBack(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    If wRet <> 0 Then
        RB21.Text := 'Err'
    else
        RB21.Text := FloatToStr(gf7000[0]);

    gw7000[7] := 2;                        // Channel 2
    wRet := AnalogOutReadBack(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    If wRet <> 0 Then
        RB22.Text := 'Err'
    else
        RB22.Text := FloatToStr(gf7000[0]);

    gw7000[7] := 3;                        // Channel 3
    wRet := AnalogOutReadBack(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    If wRet <> 0 Then
        RB23.Text := 'Err'
    else
        RB23.Text := FloatToStr(gf7000[0]);


end;

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










end.
