unit Commess;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
const filterfaktor = 8;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Label1: TLabel;
Timer1: TTimer;
Label2: TLabel;
Shape1: TShape;
OpenDialog1: TOpenDialog;
Button4: TButton;
Shape2: TShape;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
CheckBox1: TCheckBox;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
// CheckBox2: TCheckBox;
procedure FormActivate(Sender: TObject);
procedure ComboBox1Exit(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
private
{ Private-Deklarationen }
hComm: THandle; // Com Handle
NComm: PChar; // Com Name
CC: TCommConfig; // Com Parameter Struktur
Filename : String;
Counter : Cardinal;
Ticks: Cardinal;
recording: Boolean;
filterwert : Integer;
altwert : Integer;
mal : Integer;
durch : Integer;
einheit : String;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
Connect: boolean = false; // verbunden ?
implementation
{$R *.dfm}
procedure TForm1.FormActivate(Sender: TObject);
var i: Integer;
begin
recording := false;
Filename := '';
// gestartet := false;
for i := 1 to 32 do
begin
ComboBox1.Items.Clear;
end;
for i := 1 to 24 do
begin
NComm := PChar('\\.\COM' + IntToStr(i));
// Versuchsweise verbinden
hComm:= CreateFile(NComm, Generic_Read or Generic_Write , 0 , nil, Open_Existing, FILE_ATTRIBUTE_NORMAL, 0); // Com �ffnen
If hComm <> Invalid_Handle_Value then
begin
ComboBox1.Items.Add('COM' + IntToStr(i));
CC.dwSize:= SizeOf(CC); // L�nge der Parameter Struktur
GetCommState(hComm, CC.DCB);
CloseHandle(hComm);
end;
end;
ComboBox1.ItemIndex := 0;
Shape1.Brush.Color := clOlive;
end;
procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
NComm := Pchar(ComBoBox1.Text); //('COM' + IntToStr(ComboBox1.ItemIndex + 1));
CC.dwSize:= SizeOf(CC); // L�nge der Parameter Struktur
GetDefaultCommConfig(NComm, CC, CC.dwSize);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button1Click(Sender: TObject);
var timeouts: TCommTimeouts;
TxtFile : TextFile;
buffer : string;
begin
NComm := Pchar('\\.\' + ComBoBox1.Text); //('COM' + IntToStr(ComboBox1.ItemIndex + 1));
if Connect then exit;
hComm:= CreateFile(NComm, Generic_Read or Generic_Write , 0 , nil, Open_Existing, FILE_ATTRIBUTE_NORMAL, 0); // Com �ffnen
If hComm <> Invalid_Handle_Value then // Com offen ?
Connect:= true else begin Label1.Caption:= 'Error'; exit; end;
cc.dcb.BaudRate := 115200; // Fuer die Messung
SetCommState(hComm, CC.DCB); // Com Parameter setzen
GetCommTimeouts(hComm,Timeouts); // Com Timeouts einlesen
Timeouts.ReadIntervalTimeout:=1;
Timeouts.WriteTotalTimeoutMultiplier:= 10;
Timeouts.WriteTotalTimeoutConstant:= 10;
Timeouts.ReadTotalTimeoutMultiplier:= 1000;
Timeouts.ReadTotalTimeoutConstant:= 1000; // nach etwa 1 Sekunde Timeout f�r lesen
SetCommTimeouts(hComm,Timeouts); // Com Timeouts setzen
// Datei calibrate.txt oeffnen und lesen
mal := 1000; // Alles normal
durch := 1000;
einheit := '';
AssignFile(TxtFile, 'calibrate.txt') ;
try
Reset(TxtFile) ;
buffer := '';
ReadLn(TxtFile, buffer) ;
mal := StrToint(buffer);
buffer := '';
ReadLn(TxtFile, buffer) ;
durch := StrToint(buffer);
ReadLn(TxtFile, einheit) ;
finally
CloseFile(TxtFile) ;
end;
// Datei calibrate.txt oeffnen und lesen
Button1.Enabled := false;
Button3.Enabled := false;
Timer1.Enabled := true;
Connect := true;
Label1.Caption := 'Connected';
Combobox1.Enabled := false;
Ticks := GetTickCount;
filterwert := 0;
altwert := 0;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if Not(Connect) then exit;
Timer1.Enabled := false;
Connect:= false;
CloseHandle(hComm); // Com schlie�en
Button1.Enabled := true;
Button3.Enabled := true;
Label1.Caption := ' Closed';
Shape1.Brush.Color := clOlive;
Label2.Caption := '0';
ComboBox1.Enabled := true;
// gestartet := false;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var W: DWord;
P: Pointer; // Zeiger f�r LeseBuffer
D : array [1..10000] of Char;
ComStatus:TCOMSTAT;
ER: DWORD;
Zahl, k,l : Integer;
f : Textfile;
DT : Integer;
begin
if Connect then
begin
DT := GetTickcount - Ticks;
Ticks := GettickCount;
Label5.Caption := IntToStr(DT);
P:= @D; // P zeigt auf C
ClearCommError(hComm,ER,@ComStatus); //Test ob Zeichen empfangen wurden //
Zahl := ComStatus.cbInQue;
k := Zahl;
if ComStatus.cbInQue>0 then // Wenn in der Queue Zeichen sind dann ...
begin
if (ReadFile(hComm, P^, Zahl, W, nil)) then // 1 Zeichen lesen
begin
// Wenn der PC aufgehalten wird, und die Timerzeit
// nicht stimmen sollte
label8.Caption := IntTostr(Integer(D[1]));
k := k * 1000 DIV DT;
// Bewertung mit Umrechnungsfaktor aus Datei
k := k * mal;
k := k div durch;
if CheckBox1.Checked then
begin
altwert := altwert + (k - filterwert);
filterwert := altwert div filterfaktor;
// Teiler ist zusammen mit Timerzeit Filterkonstante
end
else
begin
filterwert := k;
end;
l := filterwert;
Label7.Caption := IntTostr(altwert);
Label2.Caption := IntToStr(l)+ einheit;
Shape1.Brush.Color := clLime;
Label1.Caption := ' Signal';
// Hier Schreiben in Datei
if Filename <> '' then
begin
if recording = true then
begin
try
Counter := Counter + 1;
AssignFile(f, Filename);
Append(f);
Writeln(f, IntToStr(Counter) + '; ' + IntTostr(l) + ';');
CloseFile(f);
except
recording := false;
filename := '';
Shape2.Brush.Color := clMaroon;
Button4.Caption := 'Start';
end; // End try
end;
end;
end;
ClearCommError(hComm,ER,nil);
end
else
begin
k := 0;
l := 0;
if CheckBox1.Checked then
begin
altwert := altwert + (k - filterwert);
filterwert := altwert div 8;
Label7.Caption := IntTostr(altwert);
l := filterwert;
end;
Label2.Caption := IntToStr(l) + einheit;
Shape1.Brush.Color := clYellow;
Label1.Caption := 'No Signal';
end;
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
var f: Textfile;
begin
if Button4.Caption = 'Start' then
begin
if Filename = '' then
begin
OpenDialog1.Execute;
if OpenDialog1.Filename <> '' then
begin
Rewrite(f, OpenDialog1.FileName);
Counter := 0;
CloseFile(f);
Filename := OpenDialog1.FileName;
end;
end;
if Filename <> '' then
begin
Shape2.Brush.Color := clRed;
recording := true;
Button4.Caption := 'Stop';
end;
end
else
begin
Button4.Caption := 'Start';
Shape2.Brush.Color := clMaroon;
recording := false;
end;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
altwert := filterwert * filterfaktor;
end;
end.