unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Label2: TLabel;
    TypeComboBox: TComboBox;
    Label3: TLabel;
    ComComboBox: TComboBox;
    Label4: TLabel;
    BaudRateComboBox: TComboBox;
    GroupBox2: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    Label5: TLabel;
    Address: TEdit;
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label6: TLabel;
    Out: TEdit;
    Label7: TLabel;
    Edit2: 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 ;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
     TypeComBobox.ItemIndex     := 0 ;
     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 );
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] := 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] := StrToInt( '$' + Out.Text ); //the 16-bit digital data to output
    gw7000[6] := 1;                          // string debug

    wRet := DigitalOut(@gw7000[0], @gf7000[0], gszSend, gszReceive);
    If wRet <> 0 Then
    Begin
        Beep;
        Application.MessageBox( PChar('The Error Code:' + IntToStr(wRet) + #13 + IGetErrorString(wRet))
                                , 'DigitalOut() error !!! ' , mb_IconExclamation );
        Close_Com (gcPort);
        Exit;
    end;
    // for digital out
    Edit2.Text := IntToHex( gw7000[5] , 0 );

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

    gw7000[4] := 100;                        // TimeOut = 0.1 second
    //gw7000[5] := StrToInt( '$' + Out.Text ); //the 16-bit digital data to output
    gw7000[6] := 1;                          // string debug
    wRet := DigitalIn(@gw7000[0], @gf7000[0], gszSend, gszReceive);

    If wRet <> 0 Then
    Begin
        Beep;
        Application.MessageBox( PChar('The Error Code:' + IntToStr(wRet) + #13 + IGetErrorString(wRet))
                                , 'DigitalIn() error !!! ' , mb_IconExclamation );
        Close_Com (gcPort);
        Exit;
    end;
    //for digital in
    Edit1.Text := IntToHex( gw7000[5] , 0 );
end;

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

end.
