Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Начинающим";
Текущий архив: 2007.07.08;
Скачать: [xml.tar.bz2];

Вниз

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

 
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;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.46 MB
Время: 0.04 c
2-1181845802
Infinityx93
2007-06-14 22:30
2007.07.08
как чистить стринггрид???


2-1181935276
лЕвый поЗер
2007-06-15 23:21
2007.07.08
паскаль


4-1169703963
DIMAL
2007-01-25 08:46
2007.07.08
MIPS и FPS


15-1181287515
balepa
2007-06-08 11:25
2007.07.08
Че то на RSDN зайти не могу


8-1160420739
Nic
2006-10-09 23:05
2007.07.08
Получить угол





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский