![]() |
| ||
|
|
Новости |
Поиск |
FAQ |
Статьи |
Книги |
Новости VCL
| | Форумы ЧАТ |
Орешник |
|
| |||
| |||
« предыдущая ветвь | форум | следующая ветвь »
Проблема с ООП.
Cardinalius © (2007-06-11 17:27:00)Приветствую, мастера!
Пониаю, что вопрос ламерский, но все же...
Есть код:
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
Как с этим бороться? Заранее спасибо!
Cardinalius (2007-06-11 19:59:00) [1]> [2] Плохиш © (11.06.07 17:32)
> Это говорит о том, что данный метод можно вызывать только
> у конкретного созданного объекта данного класса.
Ага. Ясно. Спасибо!
Плохиш (2007-06-11 17:32:00) [2]Это говорит о том, что данный метод можно вызывать только у конкретного созданного объектаданного класса.
TUser (2007-06-11 17:31:00) [3]with TComPort.Create do begin
...
if InitCom () ...
Free;
end;