unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Address: TEdit;
    Label2: TLabel;
    Button1: TButton;
    Button2: TButton;
    Label4: TLabel;
    Label5: TLabel;
    ComComboBox: TComboBox;
    BaudRateComboBox: TComboBox;
    Label6: TLabel;
    Edit2: TEdit;
    Label1: TLabel;
    eCounter: TEdit;
    Label3: TLabel;
    Edit1: TEdit;
    Label7: TLabel;
    Edit3: TEdit;
    Edit4: TEdit;
    Label8: TLabel;
    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 ;
  wt            : Word ;
  sAddr         : String;
  tT1, tT2      : TDateTime;
  Hour, Min, Sec, MSec : Word;
  lT1, lT2      : longInt;


implementation

Uses I7000,I7000u;

Var
   bComOpen : Boolean ;

{$R *.DFM}

procedure TForm1.Button2Click(Sender: TObject);
begin
     if bComOpen then
        Close_Com( gcPort );
     Close;
end;

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);      // 0: One Stop Bit
     bCOMOpen                    := False;
     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 := 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
    else
        bCOMOpen := True;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
     i : LongInt;
begin
     if Button1.Caption = 'Stop' then
     begin
        Button1.Caption := 'Active';
        Button2.Enabled := True;
        exit;
     end;

     eCounter.Text := '0';
     Button1.Caption := 'Stop';
     Button2.Enabled := False;

     gcPort := Char(ComComboBox.ItemIndex + 1 );      // Setting Com Port
     if bComOpen then  // Reopen Com port
     begin
        Close_Com(gcPort);
        bComOpen := False;
     end;

     if Not bComOpen then
        OpenCom;

    // #AA(cr)(null)>SDDDDDD(cr) = 14 Chars
    // 1 Char = 1 Start bit + 8 Data bits + 1 Stop bit = 10 bits
    // hardware delay 1 mSec every communication

    Edit4.Text := FloatToStrF(1 / (((14 * 10) / StrToInt(BaudRateComboBox.Text)) + 0.001), ffNumber, 8, 3) ;
    Application.ProcessMessages;

    sAddr := IntToHex(StrToInt('$' + Address.Text),2);
    tT1 := Time;

    for i := 0 to 3000 -1 do
    begin
        Application.ProcessMessages;
        if Button1.Caption = 'Active' then
           break;
        StrCopy( gszSend , PChar('#' + sAddr));
        StrCopy( gszReceive ,'' );
        wRet := Send_Receive_Cmd(gcPort, gszSend, gszReceive, 100, 0, wT);
        if wRet <> NoError then
           continue;
        Edit2.Text := String(gszReceive) ;
        eCounter.Text := IntToStr(StrToInt(eCounter.Text) + 1);
    end;

    tT2 := Time;
    DecodeTime(tT1, Hour, Min, Sec, MSec);
    lT1 := Hour * 60 * 60 * 1000 + Min * 60 * 1000 + Sec * 1000 + MSec ;
    DecodeTime(tT2, Hour, Min, Sec, MSec);
    lT2 := Hour * 60 * 60 * 1000 + Min * 60 * 1000 + Sec * 1000 + MSec ;
    Edit1.Text := IntToStr(lT2 - lT1);
    Edit3.Text := FloatToStrF(StrToInt(eCounter.Text) / (StrToInt(Edit1.Text)/ 1000),ffNumber, 8, 4);
    Close_Com( gcPort );

    if Button1.Caption = 'Stop' then
       Button1.Click;

end;

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

end.
