Board index » delphi » Serial Port treatment under Delphi

Serial Port treatment under Delphi

Hi all!

How does Delphi treat serial ports? I wanna write something which does
ports and I dunno how.

Thanks in advance, gbe.

 

Re:Serial Port treatment under Delphi


Depends on the OS. Under Win95/98 you (almost) pretend they are text
files. If want to spend some $$$, I hear AsyncPro from
http://www.turbopower.com/ is nice. Here are some samples.

procedure TCommStatusDlg.OpenPort(CommPort: string);
Var CommPortTimeOuts: TCommTimeouts;
    DCB: TDCB;
    ReadBuffer: array[0..100] of char;
begin
     { Open the comm port }
     hCommFile := CreateFile(PChar(CommPort),GENERIC_READ +
GENERIC_WRITE,0,nil,OPEN_EXISTING,FILE_FLAG_WRITE_THROUGH,0);
     if hCommFile = INVALID_HANDLE_VALUE then
     begin
          ShowMessage('Unable to open ' + CommPort + '!');
          exit;
     end;

     { Set Timeout values for the comm port. - Whatever you like
here!}
     CommPortTimeOuts.ReadIntervalTimeout         := 100;
     CommPortTimeOuts.ReadTotalTimeoutMultiplier  := 100;
     CommPortTimeOuts.ReadTotalTimeoutConstant    := 0;
     CommPortTimeOuts.WriteTotalTimeoutMultiplier := 0;
     CommPortTimeOuts.WriteTotalTimeoutConstant   := 0;
     if not SetCommTimeOuts(hCommFile,CommPortTimeOuts) then
     begin

FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,nil,GetLastError,0,ReadBuffer
,100,nil);
          MessageDlg(ReadBuffer,mtError,[mbOk],0);
     end;

     { Set the comm port parameters. - Again, it's up to you.}
     GetCommState(hCommFile,DCB);
     DCB.BaudRate := 1200;
     DCB.ByteSize := 7;
     DCB.StopBits := ONESTOPBIT;
     DCB.Parity   := EVENPARITY;
     if not SetCommState(hCommFile,DCB) then
     begin

FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,nil,GetLastError,0,ReadBuffer
,100,nil);
          MessageDlg(ReadBuffer,mtError,[mbOk],0);
     end;
end;

{ Careful, you may want more than 100 chars! }
function TCommStatusDlg.ReceiveString: string;
var
   ReadBuffer: array[0..100] of char;
   NumberRead: DWord;
begin
     ReadBuffer := '';
     ReadFile(hCommFile,ReadBuffer,100,NumberRead,NIL);
     Result := ReadBuffer;
end;

{ Again, look out for my 100 char limit. }
procedure TCommStatusDlg.SendString(aString: string);
var
   ReadBuffer: array[0..100] of char;
   NumberRead: DWord;
begin
     ReadBuffer := '';
     StrPCopy(ReadBuffer,aString + #13 + #10); // #13 & #10 are CR &
LF for modem use.

WriteFile(hCommFile,ReadBuffer,Length(ReadBuffer),NumberRead,NIL);
end;

procedure TCommStatusDlg.ClosePort;
begin
     { Close the port. }
     CloseHandle(hCommFile);
end;

HTH,
Scott

Quote
Liran wrote in message <36CD66E1.52004...@internet-zahav.net>...
>Hi all!

>How does Delphi treat serial ports? I wanna write something which
does
>ports and I dunno how.

>Thanks in advance, gbe.

Re:Serial Port treatment under Delphi


There are several very good free and shareware delphi components
available that do a good job with serial comm.  One that i've
used recently that works well and is easy to understand is
TSerial3 (or 1,2, or 4) by Rick Crowther (shareware).
Have a look at torry's at www.torry.ru , at Delphi Super
Page, HelpMakers, or any of the other excellent Delphi
sites on the web.  Torry has links to most of the other good
sites as well.
Quote

>Liran wrote in message <36CD66E1.52004...@internet-zahav.net>...
>>Hi all!

>>How does Delphi treat serial ports? I wanna write something which
>does
>>ports and I dunno how.

>>Thanks in advance, gbe.

Other Threads