unit ExampUnit;

//
(*

=====================================================================
NOTE Delphi 4 : BEFORE you run this example program, FIRST install Compack.bpl
=====================================================================
From Delphi's IDE :
  Component > Install Packages > Add > select compack.bpl > OK
 (TComport will be installed on the Samples page)

 do *NOT* follow this procedure with D5 !



=====================================================================
NOTE Delphi 5 : BEFORE you run this example program, FIRST install Comport.pas
=====================================================================
From Delphi's IDE :

  Component > Install > Into New Package
    Unit file name    :   Browse to comport.pas > OK
    Package File name :   Browse to directory of comport.pas, type a name of
    your choice, "ComPort" for example > OK
      etc..  > OK  etc.. OK

 (TComport will be installed on the Samples page)
 

*)

interface

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

type
  TForm1 = class( TForm )
    Memo1: TMemo;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Edit2: TEdit;
    GroupBox1: TGroupBox;
    shCarrier: TShape;
    StaticText2: TStaticText;
    shCTS: TShape;
    shDSR: TShape;
    StaticText3: TStaticText;
    StaticText4: TStaticText;
    shRing: TShape;
    StaticText5: TStaticText;
    shPortOpen: TShape;
    StaticText6: TStaticText;
    shError: TShape;
    StaticText11: TStaticText;
    cbPorts: TComboBox;
    StaticText1: TStaticText;
    ComPort1: TComPort;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    Image1: TImage;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BitBtn1: TBitBtn;
    Label3: TLabel;
    Label4: TLabel;
    Retardo: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    Edit3: TEdit;
    Edit4: TEdit;
    Label7: TLabel;
    Panel1: TPanel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    BitsDatos: TEdit;
    Paridad: TComboBox;
    Recepcion: TComboBox;
    Parada: TComboBox;
    Baud: TComboBox;
    procedure btnOpenClick( Sender: TObject );
    procedure btnCloseClick( Sender: TObject );
    procedure btnSendClick( Sender: TObject );
    procedure Button6Click( Sender: TObject );
    procedure Button7Click( Sender: TObject );
    procedure Button8Click( Sender: TObject );
    procedure Button9Click( Sender: TObject );
    procedure FormCreate( Sender: TObject );
    procedure FormDestroy( Sender: TObject );
    procedure ComPort1CTSChanged( Status: Boolean );
    procedure ComPort1DSRChanged( Status: Boolean );
    procedure ComPort1Error( Sender: TObject );
    procedure ComPort1PortClose( Sender: TObject );
    procedure ComPort1PortOpen( Sender: TObject );
    procedure ComPort1RingChanged( Status: Boolean );
    procedure ComPort1RLSDChanged( Status: Boolean );
    procedure ComPort1ReceiveCallBack( Data: string );
    procedure Edit2KeyPress( Sender: TObject; var Key: Char );
    procedure FormActivate(Sender: TObject);
    procedure Label2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Memo1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1         : TForm1;
  CadenaGlobal:string;

implementation

{$R *.DFM}

function retardo:boolean;
var
j:integer;
inicio:real;
mili:real;
begin
//for j:=0 to StrToInt(Form1.Retardo.Text)*1000 do;
inicio:=Time;
//1.15740740740740740740740740740741e-5
mili:=1.15740740740740740740740740740741e-5/1000;
while (Time-inicio<(mili*StrToFloat(Form1.Retardo.Text)))  do
begin
//nada
end;

end;

function EjecutaClik(x,y:integer):boolean;
var
i:integer;
aux:string;
begin
try
//x:=StrToInt(PalabraLinea(linea,3));
//y:=StrToInt(PalabraLinea(linea,4));

SetCursorPos(x,y);


SetCursorPos(x,y);

mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);




except
end;
end;


function TecleaTecla(key:word):boolean;//que pleonasmo
begin
if ((key>=48)and(key<90))or(key=13) then
begin
try
keybd_event(key,0,0,0);//telea una key en la pantalla activa
except
end;
end;//if

end;


function EnviaDatosFoco(cadena:string):boolean;
var
i,j:integer;
aux:string;
begin
//ShowMessage(cadena);
if cadena<>'' then
begin
EjecutaClik(StrToInt(Form1.Edit3.Text),StrToInt(Form1.Edit4.Text));
aux:='';
for i:=1 to length(cadena) do
begin
aux:=aux+cadena[i]+'*';
end;

if Form1.Edit1.Text<>'' then
cadena:=cadena+'Ñ'+char(StrToInt(Form1.Edit1.Text));

for i:=1 to length(cadena) do
begin
//ShowMessage(cadena[i]);
TecleaTecla(Ord(cadena[i]));

retardo;

if cadena[i]=#13 then
break;
end;

Form1.Label3.Caption:=IntToStr(i);
//if Form1.Edit1.Text<>'' then
//TecleaTecla(StrToInt(Form1.Edit1.Text));
end;

end;

//==========================================
procedure TForm1.FormCreate( Sender: TObject );
var
arch:TextFile;
dir:string;
linea,puerto1:string;
i,num:integer;
begin
CadenaGlobal:='';
Memo1.Lines.Add('**');
  EnumPorts( cbPorts.Items );
  if cbPorts.Items.Count > 0 then
    cbPorts.ItemIndex := 0; // show first available port


GetDir(0,dir);
AssignFile(arch,dir+'\puerto.cnf');
if FileExists(dir+'\puerto.cnf') then
begin
Reset(arch);
readln(arch,linea);
if linea<>'' then
begin
cbPorts.Text:=linea;
puerto1:=linea;
end;
readln(arch,linea);
if linea<>'' then
Edit1.Text:=linea;
readln(arch,linea);
if linea<>'' then
Retardo.Text:=linea;

readln(arch,linea);
if linea<>'' then
Edit3.Text:=linea;
readln(arch,linea);
if linea<>'' then
Edit4.Text:=linea;


//*****
readln(arch,linea);
if linea<>'' then
BitsDatos.Text:=linea;
ComPort1.DataBits:=StrToInt(BitsDatos.Text);

readln(arch,linea);
if linea<>'' then
Baud.Text:=linea;
ComPort1.Baud:=StrToInt( Baud.Text);

readln(arch,linea);
if linea<>'' then
Paridad.Text:=linea;
ComPort1.StrToParity(Paridad.Text);

readln(arch,linea);
if linea<>'' then
Recepcion.Text:=linea;
if Recepcion.Text='rmRAW' then
ComPort1.ReceiveMode:=rmRAW
else
ComPort1.ReceiveMode:=rmTERM;

readln(arch,linea);
if linea<>'' then
Parada.Text:=linea;

CloseFile(arch);
end;
//*************

if cbPorts.Text<>'' then
begin
num:=0;
for i:=0 to  cbPorts.Items.Count-1 do
begin
if cbPorts.Text=cbPorts.Items[i] then
begin
num:=i;
break;
end;

end;


 cbPorts.ItemIndex := num;
Label2.Caption:=puerto1;
cbPorts.Text:=puerto1;
 ComPort1.Port :=puerto1;
//  cbPorts.Enabled := false;
//  ComPort1.Port := cbPorts.Items[num];
  ComPort1.Port :=puerto1;;
  ComPort1.Open;
cbPorts.Text:=puerto1;
end;
//  ComPort1.send( edit2.Text + #13#10 );

end;

//===========================================
procedure TForm1.FormDestroy( Sender: TObject );
begin
  ComPort1.Free;
end;

//============================================
procedure TForm1.btnOpenClick( Sender: TObject );
begin
  cbPorts.Enabled := false;
  ComPort1.Port := cbPorts.Items[cbPorts.ItemIndex];
  ComPort1.Open;

  ComPort1.send( edit2.Text + #13#10 );

end;

//=============================================
procedure TForm1.btnCloseClick( Sender: TObject );
begin
  ComPort1.Close;
  cbPorts.Enabled := true;
end;

//===================================================
procedure TForm1.ComPort1ReceiveCallBack( Data: string );
var
cade:string;
begin
  if Memo1.Lines.Count > 800 then
    Memo1.Clear;
// Edit1.SetFocus;
cade:=Data;
//Memo1.Lines.Add( Data );
//CadenaGlobal:=cade;
CadenaGlobal:=CadenaGlobal+cade;
//sHOWmESSAGE(CADE);
Memo1.Lines.Add(cade);
//    EnviaDatosFoco(cade);
//Memo1.Lines.Add('***');



  Image1.Visible := not Image1.Visible;

end;

//=================================================
procedure TForm1.ComPort1CTSChanged( Status: Boolean );
begin
case Status of
    true: shCTS.Brush.Color := clLIME;
    false: shCTS.Brush.Color := clGRAY;
  end;
end;

//==================================================
procedure TForm1.ComPort1DSRChanged( Status: Boolean );
begin
  case Status of
    true: shDSR.Brush.Color := clLIME;
    false: shDSR.Brush.Color := clGRAY;
  end;
end;

//============================================
procedure TForm1.ComPort1Error( Sender: TObject );
begin
  shError.Brush.Color := clRED;
end;

//===============================================
procedure TForm1.ComPort1PortOpen( Sender: TObject );
begin
  shPortOpen.Brush.Color := clYELLOW;
end;

//================================================
procedure TForm1.ComPort1PortClose( Sender: TObject );
begin
  shPortOpen.Brush.Color := clGRAY;
  shCarrier.Brush.Color := clGRAY;
  shRing.Brush.color := clGRAY;
  shCTS.Brush.Color := clGRAY;
  shDSR.brush.color := clGRAY;
end;

//==================================================
procedure TForm1.ComPort1RingChanged( Status: Boolean );
begin
  case status of
    true: shRing.Brush.Color := clLIME;
    false: shRing.Brush.Color := clGRAY;
  end;
end;

//===================================================
procedure TForm1.ComPort1RLSDChanged( Status: Boolean );
begin
  case Status of
    true: shCarrier.Brush.Color := clLIME;
    false: shCarrier.Brush.Color := clGRAY;
  end;
end;

//===========================================================
procedure TForm1.Edit2KeyPress( Sender: TObject; var Key: Char );
begin
  if Key = #13 then
  begin
    BtnSendClick( Sender );
    Key := #0;
  end;
end;

//===========================================
procedure TForm1.btnSendClick( Sender: TObject );
begin
  ComPort1.send( edit2.Text + #13#10 );
 // ComPort1.send( edit2.Text + #13 );
end;

//===========================================
procedure TForm1.Button6Click( Sender: TObject );
begin
  ComPort1.SetRTS( true );
end;

//===========================================
procedure TForm1.Button7Click( Sender: TObject );
begin
  ComPort1.SetRTS( false );
end;

//===========================================
procedure TForm1.Button8Click( Sender: TObject );
begin
  ComPort1.SetDTR( true );
end;

//============================================
procedure TForm1.Button9Click( Sender: TObject );
begin
  ComPort1.SetDTR( false );
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
//cbPorts.Text:=Label2.Caption;
end;

procedure TForm1.Label2Click(Sender: TObject);
begin
cbPorts.Text:=Label2.Caption;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
arch:TextFile;
dir:string;
begin
GetDir(0,dir);
AssignFile(arch,dir+'\puerto.cnf');
//if FileExists(dir+'\puerto.cnf') then
Rewrite(arch);
if cbPorts.Text<>'' then
begin
Writeln(arch,cbPorts.Text);
Writeln(arch,Edit1.Text);
Writeln(arch,Retardo.Text);
Writeln(arch,Edit3.Text);
Writeln(arch,Edit4.Text);
//*****
Writeln(arch,BitsDatos.Text);
Writeln(arch,Baud.Text);
Writeln(arch,Paridad.Text);
Writeln(arch,Recepcion.Text);
Writeln(arch,Parada.Text);
//******

end
else
begin
Writeln(arch,'COM1');
Writeln(arch,'');
end;

CloseFile(arch);


end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
if Button =mbRight then
ShowMessage(IntToStr(X)+','+IntToStr(Y));
end;

procedure TForm1.Memo1Change(Sender: TObject);
begin

if (Memo1.Lines[Memo1.Lines.count-1]<>'**') then
begin
CadenaGlobal:=Memo1.Lines[Memo1.Lines.count-1];

EnviaDatosFoco(CadenaGlobal);
end;

{//if Memo1.Lines[Memo1.Lines.count-1]='' then
if (length(Memo1.Lines[Memo1.Lines.count-1])<8)and(Memo1.Lines[Memo1.Lines.count-1]<>'**') then
begin
//CadenaGlobal:=CadenaGlobal+Memo1.Lines[Memo1.Lines.count-1];

Memo1.Lines.Add('**');

Label7.Caption:=CadenaGlobal;
//sHOWmESSAGE(Label7.Caption);

EnviaDatosFoco(CadenaGlobal);
CadenaGlobal:='';
end;
}
end;








end.
