unit com_bridge;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 =
class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Label1: TLabel;
Button3: TButton;
Button4: TButton;
Edit2: TEdit;
Label2: TLabel;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
procedure ConnectToCOMPort(Disconnect: Boolean);
function ReadDataFromCom(lange:Integer):
string;
function WriteDataToCOMPort(Text:
String): Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
//Anschluss
ComHandle : THandle;
comport:
string;
implementation
{$R *.dfm}
procedure TForm1.ConnectToCOMPort(Disconnect: Boolean);
var
DCB : TDCB;
TimeOut : TCommTimeouts;
i : Integer;
begin
{ Wenn die Verbindung wieder getrennt werden soll }
if (Disconnect)
then
begin
FileClose(ComHandle);
ComHandle := 0;
exit;
end else
{ Die Verbindung zum COM-Port wird hergestellt - wenn dies nicht
funktioniert
wird es bis zu zehn Mal probiert }
begin
COMPort:=Edit1.Text;
//Com-Ports beginnen bei Com1, Com2, Com3...
i := 0;
while (ComHandle <= 0)
and (i < 10)
do
begin
ComHandle := CreateFile(pchar(COMPort), GENERIC_READ
or
GENERIC_WRITE,0,
nil, OPEN_EXISTING, 0, 0);
inc(i);
end;
{ Einstellungen zum COM-Port zuweisen, wenn die Verbindung aktiv
ist }
if (ComHandle > 0)
then
begin
DCB.DCBlength := SizeOf(DCB);
DCB.ByteSize := 8;
DCB.Parity := NoParity;
DCB.StopBits := ONESTOPBIT;
DCB.BaudRate := 9600;
{
DCB.Flags := 5123; // Wenn 2 Pins belegt sind
DCB.EofChar := #0;
DCB.ErrorChar := #0;
DCB.EvtChar := #0;
DCB.XoffChar := #0;
DCB.XoffLim := 0;
DCB.XonChar := #0;
DCB.XonLim := 0;
}
SetCommState(ComHandle, DCB);
GetCommTimeOuts(ComHandle, TimeOut);
TimeOut.ReadIntervalTimeOut := 100;
TimeOut.ReadTotalTimeoutMultiplier := 0;
TimeOut.ReadTotalTimeoutConstant := 250;
TimeOut.WriteTotalTimeoutMultiplier := 0;
TimeOut.WriteTotalTimeoutConstant := 200;
SetCommTimeouts(ComHandle, TimeOut);
end;
end;
end;
function TForm1.WriteDataToCOMPort(Text:
String): Boolean;
var
i,k : integer;
str:
string;
begin
{ Senden der Daten an den COM-Port - mit Ausgabe ob der Befehl
erfolgreich war }
i:=FileWrite(ComHandle, Text[1], Length(Text));
ShowMessage(str);
if (i <> Length(Text))
then
Result := False
else
Result := True;
end;
function TForm1.ReadDataFromCom(lange:Integer):
string;
var n:
string;
begin
FileRead(ComHandle,n,lange);
ShowMessage(n);
Result:=n;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Application.Terminate;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ConnectToComPort(false);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ConnectToComPort(True);
ShowMessage('
Disconnected from '+Edit1.Text);
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
WriteDataToComPort(Edit2.Text);
ShowMessage('
Write Data Successfull');
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
ReadDataFromCom(length(Edit2.Text))
end;
end.