Einzelnen Beitrag anzeigen

Mr. Bean

Registriert seit: 25. Jun 2003
4 Beiträge
 
Delphi 7 Enterprise
 
#1

Serielle Schnittstelle Terminalprogramm

  Alt 25. Jun 2003, 18:13
Hallo,
ich habe ein kleines Terminal Programm geschrieben. Allerdings kann ich bloß Zeichen senden aber keine Zeichen empfangen. Vielleicht habet ihr eine Ahnung wo der Fehler liegt.

Delphi-Quellcode:
unit Main;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Label1: TLabel;
    Memo2: TMemo;
    Label2: TLabel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Label3: TLabel;
    Label4: TLabel;
    Button5: TButton;
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Memo1KeyPress(Sender: TObject; var Key: Char);

  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    hComm : THandle; // Handle für die Schnittstelle
  end;


type
  TCommThread = class(TThread)
  protected
     procedure Execute;override;
  published
     procedure read;
  public
     EventMask : DWord;
     ThComm : THandle;
  end;


var
  Form1 : TForm1;
  o : TOverlapped;
  ComPort : PChar; // Name Comport
  CC : TCommConfig;
  Connect : Boolean;
  ThreadCom : TCommThread;

implementation

{$R *.dfm}


procedure TCommThread.Execute;

Begin
  Form1.Label4.Caption:='Thread läuft';
  o.hEvent:=CreateEvent(nil,false,false,nil);
  SetCommMask(Form1.hComm,EV_RXCHAR or EV_ERR);
  WaitCommEvent(Form1.hComm,EventMask,@o);
  if (EventMask and EV_RXCHAR = EV_RXCHAR) Then Read;
  if (EVentMask and EV_ERR = EV_ERR) Then Form1.Label3.Caption:='Error';
end;

procedure TCommThread.read;

Var
  P : Pointer;
  Len : DWord;
  C : Char;


begin
  Form1.Label4.Caption:='Zeichen Empfangen';
  Form1.Memo2.Lines.Add('Zeichen Empfangen');
end;



procedure TForm1.Button2Click(Sender: TObject);
begin
  ThreadCom.Terminate;
  ThreadCom.WaitFor;
  ThreadCom.Free;
  CloseHandle(hComm);
  Connect:=False;
  Label3.Caption:='ComPort ist geschlossen.';
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  If Connect Then CloseHandle(hComm);
  Close;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Connect Then CloseHandle(hComm);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  o.hEvent:=CreateEvent(nil,false,false,nil);
  hComm:=CreateFile(ComPort, GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING,
                            FILE_FLAG_OVERLAPPED, 0);
  if hComm <> INVALID_HANDLE_VALUE Then
  Begin
    CC.dwSize:=SizeOf(CC);
    GetDefaultCommConfig(ComPort,CC, CC.dwSize);
    SetCommState(hComm, CC.DCB);
    Connect:=True;
    Label3.Caption:='ComPort ist geöffnet';
    ThreadCom:=TCommThread.Create(True);
    ThreadCom.Resume;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComPort:='Com1';
  Connect:=False;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  // Com Port Settings
  CC.dwSize:=Sizeof(CC);
  GetDefaultCommConfig(ComPort,CC,CC.dwSize);
  CommConfigDialog(ComPort,Handle,CC);
  SetCommState(hComm,CC.dcb);
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);

var W : DWord;
    P : Pointer;

begin
  P:=@Key;
  WriteFile(hComm,p^,1,w,@o);
  WaitForSingleObject(o.hEvent,INFINITE);
end;


end.
  Mit Zitat antworten Zitat