Board index » delphi » Delphi 2.0 Serial Port control

Delphi 2.0 Serial Port control

I'm looking for a Delphi 2.0 example that accesses the Serial Port.
the code that I have simply doesnt work.  The code will select COM1,
9600 bps, Odd Parity,  1 Stop.  I would like to read either 1
character at a time or a string of characters.  Also, is there a way
to make the read non-blocking?

var
        sCom: String;
        dcbPort: TDCB; {device control block }

        boolAbort: Boolean;
        sErrMsg: String;
        cbCharsAvailable, cbCharsRead: DWord;  // delete when done
        Buffer : String;
        NumBytes : DWORD;
begin

        sCom := 'COM1';
        hPort := CreateFile(PChar(sCom), GENERIC_READ or

                   GENERIC_WRITE,  0, nil,
              OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, LongInt(0));

        if GetCommState(hPort, dcbPort) then
        begin
                dcbPort.BaudRate := 9600;
                dcbPort.ByteSize := 7;
                dcbPort.Parity := ODDPARITY;
                dcbPort.StopBits := ONESTOPBIT;
                dcbPort.Flags := 1 + 256 + 512 + 1024 + 2048;
                { i would like xon/xoff flow control }

                { flag bit fields: dcb_Binary, dcb_Parity,              
                  dcb_OutxCtsFlow, dcb_fOutxDsrFlow,
                  dcb_fOutX, dcb_fInX, dcb_DtrFlow, dcb_RtsFlow
                }

                SetCommState(hPort, dcbPort);
                SetLength(Buffer, 25 );
                ReadFile( hPort, Pchar(buffer)^, 1, cbCharsRead, nil);

                { never gets past the ReadFile }

        end;

 

Re:Delphi 2.0 Serial Port control


ad...@mail.kdcol.com (Gerald L. Kelly) wrote:

Quote
>I'm looking for a Delphi 2.0 example that accesses the Serial Port.
>the code that I have simply doesnt work.  

I have some code for Delphi 1.0, don't know if it would work on
Delphi 2.0. It uses routines like opencomm,  getcommstate,
setcommstate,  getcommerror, writecomm, and readcomm.

Note that getcommerror will return a structure that has the
number of characters in the input and output queues, so you
can know how many characters you can read without blocking.

Bob
certsoft   at    quest-net    dot    com

Re:Delphi 2.0 Serial Port control


Look  at The Delphi Superpage
http://sunsite.icm.edu.pl/delphi/
for comm32.zip and qccom32.zip

Ugis

Robert Reimiller wrote in article <5f0v9u$...@mufasa.harvard.edu>...

Quote
> ad...@mail.kdcol.com (Gerald L. Kelly) wrote:
> >I'm looking for a Delphi 2.0 example that accesses the Serial Port.
> >the code that I have simply doesnt work.  

Re:Delphi 2.0 Serial Port control


Quote
> Robert Reimiller wrote in article <5f0v9u$...@mufasa.harvard.edu>...
> > ad...@mail.kdcol.com (Gerald L. Kelly) wrote:
> > >I'm looking for a Delphi 2.0 example that accesses the Serial Port.
> > >the code that I have simply doesnt work.
Ugis Baumanis wrote:
> Look  at The Delphi Superpage
> http://sunsite.icm.edu.pl/delphi/
> for comm32.zip and qccom32.zip

Freeware ones are fine for simple applications.  If you're working
on a commercial product or if you're on a tight deadline then
you ought to consider getting
  http://www.tpower.com/products/apro/
Highly respected.  They used to be C++ with some Delphi support,
but now they're primarily Delphi-oriented.

__________________________________________________________________
Grant Walker        Hobart, Tasmania         Ph: +613 62313083
Design Engineer     Australia                Fx: +613 62313086

Other Threads