unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    gbConfig: TGroupBox;
    Label2: TLabel;
    cbModuleID: TComboBox;
    Label3: TLabel;
    cbCom: TComboBox;
    Label4: TLabel;
    cbBaudRate: TComboBox;
    btnActive: TButton;
    btnExit: TButton;
    cbChecksum: TComboBox;
    Label7: TLabel;
    Label5: TLabel;
    eAddress: TEdit;
    tmLoop: TTimer;
    Label8: TLabel;
    eInVal: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure btnActiveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure tmLoopTimer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1  : TForm1;
  wRtn   : WORD;

implementation

Uses I7000,I7000u, I87000;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
     // Initalize Setting for GUI
     cbModuleID.ItemIndex := 0;
     cbCom.ItemIndex      := 0;
     cbBaudRate.ItemIndex := 4;
     cbChecksum.ItemIndex := 0;

     // Setting COM port strings
     ISetCOMString( cbCOM, 1, 255 );

     // Allocate the memory space
     gszSend    := StrAlloc( 100 );
     gszReceive := StrAlloc( 100 );
end;

procedure TForm1.btnExitClick(Sender: TObject);
begin
     Close;
end;

procedure TForm1.btnActiveClick(Sender: TObject);
begin
     // Stop by user
     if btnActive.Caption = 'Stop' then
     begin
          tmLoop.Enabled := False; // Stop Timer
          Sleep(500);              // Wait the tmLoop to end
          Close_Com( gcPort );     // Close COM port
          btnActive.Caption := 'Active';
          btnExit.Enabled   := True;
          gbConfig.Enabled  := True;
          exit;
     end;

     gcPort      := Char(cbCom.ItemIndex + 1 );  // Com Port
     gdwBaudRate := StrToInt( cbBaudRate.Text ); // Baud Rate
     wRtn := IOpenCom(gcPort, gdwBaudRate);      // Open COM port
     if wRtn <> NoError then
     begin
          ShowMessage( 'COM port open error!!' );
          exit;
     end;

     gdwBuf[0] := Word(gcPort);                   // COM Port
     gdwBuf[1] := StrToInt('$' + eAddress.Text);  // Module Address
     gdwBuf[2] := StrToInt('$' + cbModuleID.Text);// Module ID
     gdwBuf[3] := cbChecksum.ItemIndex;           // CheckSum Disable or Enable
     gdwBuf[4] := 100;                            // TimeOut = 0.1 second
     //gdwBuf[5] := StrToInt( '$' + eOutVal.Text ); // 16-bit digital data to output
     gdwBuf[6] := 1;                              // string debug

     btnActive.Caption := 'Stop';
     btnExit.Enabled   := False;
     gbConfig.Enabled  := False;
     tmLoop.Enabled    := True;    // Start Timer
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
     // Free the memory space
     StrDispose(gszSend);
     StrDispose(gszReceive);
end;

procedure TForm1.tmLoopTimer(Sender: TObject);
begin
     gdwBuf[5] := 0;
     // calling the Digital Output Read-Back function for 87K series
     wRtn := DigitalIn_87K(@gdwBuf[0], @gfBuf[0], gszSend, gszReceive);
     if wRtn <> 0 then
     begin
          Beep;
          eInVal.Text := 'Error';
     end
     else
          eInVal.Text := IntToHex( gdwBuf[5], 4 );
end;

end.
