Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

serial interface

84 views
Skip to first unread message

Senol

unread,
Oct 13, 1999, 3:00:00 AM10/13/99
to
Hi

Does anybody know how to create a serial interface connection with delphi?

Bye Senol Kandemir

Günther Wimpassinger

unread,
Oct 14, 1999, 3:00:00 AM10/14/99
to
Hi, too.

Do you mean send and receive data over COMx?

The Answer is
Open a file with CreateFile(..) :
example hFilehandle=CreateFile('COM1',GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE,
nil,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_WRITE_THROUGH,0);

Write to the Interface with Writefile(); :
example WriteFile(hFilehandle,abBuf,length(abBuf),cdWriitenbytes,nil);

Read from the Interface with Readfile();:
example ReadFile(hFilehandle,abBuf,10,cdResult,nil);

Here the description of the Class TCom as an example;
{*** TCom ***}
{AOwner not needet set it to nil}
constructor TCom.Create;
begin
inherited Create;
sComPort:='COM1';
hComHandle:=INVALID_HANDLE_VALUE;
wBaud:=CBR_57600;
bData:=8;
bStop:=ONESTOPBIT;
bParityFl:=NOPARITY;
iFlags:=$1011; { fBinary = true,
fDtrControl = DTR_CONTROL_ENABLE
fRtsControl = RTS_CONTROL_ENABLE }
boOpened:=false;
end;

destructor TCom.Destroy;
begin
if Active then Close;
inherited Destroy;
end;

function TCom.boOpen:boolean;
var hComHandleHelp : THandle;
sCom : string;
tHelpDCB : TDCB;
boResult : boolean;
begin
boResult:=false;
if Active then Close;
sCom:=sCOMPort;
hComHandleHelp:=CreateFile(PChar(sCom),GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE,
nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL or
FILE_FLAG_WRITE_THROUGH,0);
if hComHandleHelp<>INVALID_HANDLE_VALUE then begin
hComHandle:=hComHandleHelp;
if GetCommState(hComHandleHelp,tHelpDCB) then begin
tHelpDCB.BaudRate:=wBaud;
tHelpDCB.ByteSize:=bData;
tHelpDCB.StopBits:=bStop;
tHelpDCB.Parity:=bParityFl;
tHelpDCB.Flags:=iFlags;
if SetCommState(hComHandleHelp,tHelpDCB) then begin
if SetupComm(hComHandleHelp,2500,2500) then begin
boOpened:=true;
boResult:=true
end else begin
Close;
MessageDlg('Fehler beim Parametrieren der Schnittstellequeue
'+scom,
mtError,[mbOk],0);
end;
end else begin
Close;
MessageDlg('Fehler beim Parametrieren der Schnittstelle '+scom,
mtError,[mbOk],0);
end;
end else begin
Close;
MessageDlg('Fehler beim Parametrieren der Schnittstelle '+scom,
mtError,[mbOk],0);
end;
end else begin
MessageDlg('Kann '+sCom+' nicht öffnen.',mtError,[mbOK],0);
end;
boOpen:=boResult;
end;

procedure TCom.Close;
begin
if hComHandle<>INVALID_HANDLE_VALUE then begin
boOpened:=false;
CloseHandle(hComHandle);
hComHandle:=INVALID_HANDLE_VALUE;
end;
end;

procedure
TCom.Setup(Com:string;Baud:word;Data,Stop:byte;Parity:byte;Flags:integer);
begin
sComPort:=Com;
wBaud:=Baud;
bData:=Data;
bStop:=Stop;
bParityFl:=Parity;
iFlags:=Flags;
end;

function TCom.SendBuf(var buf;len:integer):integer;
var cdWritten : cardinal;

begin
WriteFile(hComHandle,buf,len,cdWritten,nil);
SendBuf:=cdWritten;
end;

function TCom.RecvBuf(var buf;len:integer):integer;
var cdResult : cardinal;
begin
cdResult:=0;
ReadFile(hComHandle,buf,len,cdResult,nil);
RecvBuf:=cdResult;
end;

function TCom.RecvLen:integer;
var tComSt:TComStat;
lwError:longword;
begin
ClearCommError(hComHandle,lwError,@tComSt);
RecvLen:=tComSt.cbInQue;
end;

function TCom.lwGetError:longword;
var tComSt:TComStat;
lwError:longword;
begin
ClearCommError(hComHandle,lwError,@tComSt);
lwGetError:=lwError;
end;

procedure TCom.Flush;
begin
PurgeComm(hComHandle,PURGE_TXABORT or
PURGE_RXABORT or
PURGE_TXCLEAR or
PURGE_RXCLEAR);
end;

procedure TCom.WriteText(st:string);
begin
SendBuf(PChar(st)^,length(st));
end;

function TCom.ReadText(var st:string):string;
var iLen,iFor:integer;
abBuf:array of byte;
begin
iLen:=RecvLen;
SetLength(abBuf,iLen);
RecvBuf(abBuf,iLen);
SetLength(st,iLen);
for iFor:=0 to iLen-1 do st[iFor]:=char(abBuf[iFor]);
end;

Senol schrieb in Nachricht <7u2mvl$b9...@forums.borland.com>...

0 new messages