Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2007.07.08;
Скачать: CL | DM;

Вниз

Проблема с ООП.   Найти похожие ветки 

 
Cardinalius ©   (2007-06-11 17:27) [0]

Приветствую, мастера!
Пониаю, что вопрос ламерский, но все же...
Есть код:


unit Unit1;

interface

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

type
 TComPort = class

 private

 public

   constructor Create;
   destructor Destroy; override;
   function InitCom(BaudRate, PortNo: Integer; Parity: Char;
     CommTimeOuts: TCommTimeouts): Boolean;
   procedure CloseCom;
   function ReceiveCom(var Buffer; Size: DWORD): Integer;
   function SendCom(var Buffer; Size: DWORD): Integer;
   function ClearInputCom: Boolean;

 end;

 TForm1 = class(TForm)
   Edit1: TEdit;
   Button1: TButton;
   procedure Button1Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;
 hFile: THandle;
 ct: tCommTimeOuts;
implementation

{$R *.dfm}

constructor TComPort.Create;
begin
 inherited;
 CloseCom;
end;

destructor TComPort.Destroy;
begin
 CloseCom;
 inherited;
end;

function TComPort.InitCom(BaudRate, PortNo: Integer; Parity: Char;
 CommTimeOuts: TCommTimeouts): Boolean;
var
 FileName: string;
 DCB: TDCB;
 PortParam: string;
begin
 result := FALSE;
 FileName := "Com" + IntToStr(PortNo); {имя файла}
 hFile := CreateFile(PChar(FileName),
   GENERIC_READ or GENERIC_WRITE, 0, nil,
   OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
 if hFile = INVALID_HANDLE_VALUE then
   exit;

 //установка требуемых параметров
 GetCommState(hFile, DCB); //чтение текущих параметров порта
 PortParam := "baud=" + IntToStr(BaudRate) + " parity=" + Parity +
   " data=8 stop=1 " +
   "octs=off";
 if BuildCommDCB(PChar(PortParam), DCB) then
 begin
   result := SetCommState(hFile, DCB) and
     SetCommTimeouts(hFile, CommTimeOuts);
 end;
 if not result then
   CloseCom;
end;

procedure TComPort.CloseCom;
begin
 if hFile <> INVALID_HANDLE_VALUE then
   CloseHandle(hFile);
 hFile := INVALID_HANDLE_VALUE;
end;

function TComPort.ReceiveCom(var Buffer; Size: DWORD): Integer;
var
 Received: DWORD;
begin
 if hFile = INVALID_HANDLE_VALUE then
   raise Exception.Create("Не открыта запись в Com порт");
 if ReadFile(hFile, Buffer, Size, Received, nil) then
 begin
   Result := Received;
 end
 else
   raise Exception.Create("Ошибка приема данных: " + IntToStr(GetLastError));
end;

function TComPort.SendCom(var Buffer; Size: DWORD): Integer;
var
 Sended: DWORD;
begin
 if hFile = INVALID_HANDLE_VALUE then
   raise Exception.Create("Не открыта запись в Com порт");
 if WriteFile(hFile, Buffer, Size, Sended, nil) then
 begin
   Result := Sended;
 end
 else
   raise Exception.Create("Ошибка передачи данных: " + IntToStr(GetLastError));
end;

function TComPort.ClearInputCom: Boolean;
begin
 if hFile = INVALID_HANDLE_VALUE then
   raise Exception.Create("Не открыта запись в Com порт");
 Result := PurgeComm(hFile, PURGE_RXCLEAR);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
TComPort.Create;
ct.ReadIntervalTimeout := 1;
ct.ReadTotalTimeoutMultiplier := 1;
ct.ReadTotalTimeoutConstant := 1;
ct.WriteTotalTimeoutMultiplier := 1000;
ct.WriteTotalTimeoutConstant := 1;
setCommTimeOuts(hFile, ct);
if TComPort.InitCom(9600, 1, "0", ct) then
 begin

 end;
sleep(500);
end;

end.

на строчке
if TComPort.InitCom(9600, 1, "0", ct) then
возникает ошибка: [Error] Unit1.pas(140): This form of method call only allowed for class methods
Как с этим бороться? Заранее спасибо!


 
TUser ©   (2007-06-11 17:31) [1]

with TComPort.Create do begin
 ...
 if InitCom () ...

 Free;
 end;


 
Плохиш ©   (2007-06-11 17:32) [2]

Это говорит о том, что данный метод можно вызывать только у конкретного созданного объекта данного класса.


 
Cardinalius ©   (2007-06-11 19:59) [3]

> [2] Плохиш ©   (11.06.07 17:32)
> Это говорит о том, что данный метод можно вызывать только
> у конкретного созданного объекта данного класса.

Ага. Ясно. Спасибо!



Страницы: 1 вся ветка

Текущий архив: 2007.07.08;
Скачать: CL | DM;

Наверх




Память: 0.47 MB
Время: 0.025 c
15-1181208980
Alex7
2007-06-07 13:36
2007.07.08
Ошибка распаковки библиотеки Ehlib


2-1181812628
emilya_o
2007-06-14 13:17
2007.07.08
key violation


6-1159720721
_Anwy_
2006-10-01 20:38
2007.07.08
Авторизация на сайте с помощью IdHTTP


15-1181225632
Interior
2007-06-07 18:13
2007.07.08
Пакет легализации пиратского ПО


15-1181408080
vch
2007-06-09 20:54
2007.07.08
Dscaler --> AspectRatio --> кнопками