Board index » delphi » Serial Communications

Serial Communications

I'm having problems with serial communications in Delphi (please don't
suggest ASYNC professional, BTW - I'd buy it if I was rich, but alas,
I'm not, and plus I get more satisfaction out of doing things myself).
I've downloaded about 6-7 communications components from various web
pages and FTP sites, and the source code to most of them looks very
similar - like they were all derived from the original source - and they

haven't helped me at all.

My main problem right now, is that I can't seem to figure out how
Windows handles carrier detection.  I've looked through all the
comm-related API calls and structures, and I haven't found any way of
checking whether carrier is present.  How does Windows handle loss of
carrier?  One guess of mine is that it automatically closes the port

handle or something, but I need to know exactly how it's done.  OS/2's
API seems to be more complete when it comes to serial communications. :(

--
Lars Hellsten (la...@ionsys.com) <*> MatrixSoft's home on the Internet:
http://www.ionsys.com/~larsh     <*>
http://www.ionsys.com/~larsh/ms.html

 

Re:Serial Communications


On 09-01-96 19:59 Lars Hellsten said:

LH>I'm having problems with serial communications in Delphi (please
LH>don't suggest ASYNC professional, BTW - I'd buy it if I was rich, but
LH>I'alas, m not, and plus I get more satisfaction out of doing things
LH>I'myself). ve downloaded about 6-7 communications components from
LH>various web pages and FTP sites, and the source code to most of them
LH>looks very similar - like they were all derived from the original
LH>source - and they

LH>haven't helped me at all.

LH>My main problem right now, is that I can't seem to figure out how
LH>Windows handles carrier detection.  I've looked through all the
LH>comm-related API calls and structures, and I haven't found any way of
LH>checking whether carrier is present.  How does Windows handle loss of
LH>carrier?  One guess of mine is that it automatically closes the port

LH>handle or something, but I need to know exactly how it's done.
LH>OS/2's API seems to be more complete when it comes to serial
LH>communications. :(

Here is some code I used in D1 ages ago.  It was used in my BBS
software.  You can handle loss of carrier (DCD) via comm events in the
WIN 3.1 API calls and toggle a status flag in your program, but if you
close the comm port and reopen while carrier is still up you don't know
whether it's still up when you reopen.  Therefore I used this code:

{ This checks the carrier detect signal }

Unit BBSCDCHK;

Interface

uses pbWinCrt;

function Check_Carrier(Port: Integer) : Boolean;

Implementation

function CDCHK(Port: Integer) : Byte; assembler;
asm
         mov     dx,Port         {move port # to DX (COM1 modm stat
reg)}
         in      al,dx           {return contents of port to al}
         cmp     al,127d         {if it's > 127, CD is true}
         ja      @cardet         {jump if carrier detect true}
@nocar:  mov     al,1            {errorlevel = 1 if no carrier}
         jmp     @exiter         {skip over carrier= true}
@cardet: mov     al,2            {errorlevel=2 if carrier}
@exiter: {mov     ah,0}            {set up exit}
end;

function Check_Carrier(Port: Integer) : Boolean;
Var
    CDAddr:    Integer;
begin
    Check_Carrier := false; { Assume no carrier for return unless found!

Quote
}

    Case Port of
        1: CDAddr := 1022; { COM1 port address for CD  }
        2: CDAddr := 766;  { COM2 port address for CD }
    else
        Begin
            Writeln('*ERROR: Invalid COM port.');
            Exit;
        End;
    End;

    If CDCHK(CDAddr) = 2
    then
        Check_Carrier := True;
end;

begin
end.

t CMPQwk 1.42 6001 tGenius is perseverence in disguise.

Re:Serial Communications


Quote
Lars Hellsten <la...@ionsys.com> wrote:

< snip >

Quote
>My main problem right now, is that I can't seem to figure out how
>Windows handles carrier detection.  I've looked through all the
>comm-related API calls and structures, and I haven't found any way of
>checking whether carrier is present.  How does Windows handle loss of
>carrier?  One guess of mine is that it automatically closes the port
>handle or something, but I need to know exactly how it's done.  OS/2's
>API seems to be more complete when it comes to serial communications. :(

I downloaded a comm component the other day, I think it is called
mscomm.  I only used it to send and receive data.  However, I noticed
that you can add event handlers for many conditions.  The UART family
used in PCs can interrupt on changes in modem control signals.  This
event may be available in the component -- I don't have the code with
me, so I can't check.  Since you like to do things for yourself, this
shouldn't bother you too much! :-)

Karl

Re:Serial Communications


Thanks to those who answered my question about carrier detect (I ended
up finding out that it's a lot simpler than writing an event handler,
etc. I can just check the RLSD flag... for some crazy reason Microsoft
decided to name it that instead of DCD, and that's what was confusing
me).

Now, I've run into another, larger problem.  I can't seem to access a
port that's already been open by another program.  This other program (a
front-end mailer for Fido technology (BBS echomail) networks called
BinkleyTerm) provides the comm handle, but I can't seem to access it.

If I use WRITEFILE to try and write data using the handle of the open
port, it doesn't work - the function returns FALSE.  If I use CreateFile
with the OPEN_EXISTING flag to try and open the port, that fails as
well.  I am opening it with the proper settings, according to multiple
serial communications components I looked at, as well as some
documentation I found in Microsoft's Knowledge Base.  

When I try to open the file 'COM4' using Assign/Reset, I get an IOResult
of 5 (file access denied), which I assume is the same problem with
CreateFile, etc.  Apparantly Windows is denying me access to the port
since it's already been opened by another program.  An article I read on
Microsoft's web page says it is possible, but that it was "beyond the
scope of this document" and to refer to "handle duplication in the Win32
SDK".

That doesn't help me, since I have no idea what the Win32 SDK is.  I've
seen dozens of references to it, both in the Delphi/Win32 help files,
and in the documents in Microsoft's Developer's section on their web
site.  However, I tried searching the web for "Windows Software
Development Kit", "Win32 SDK", etc... and couldn't turn up any useful
information on what it is - I could only find references to it.  Could
someone enlighten me on this?

Also, if anyone reading this knows how I can access a port that's
already been opened, I'd appreciate any help to save me having to look
it up elsewhere.  (Oh, I've also looked at the help for the
DuplicateHandle function, and that doesn't seem to be of much use here
either since it apparantly wants the handle of the process that opened
the port.)

--
Lars Hellsten (la...@ionsys.com) <*> MatrixSoft's home on the Internet:
http://www.ionsys.com/~larsh     <*>
http://www.ionsys.com/~larsh/ms.html

Re:Serial Communications


On Thu, 05 Sep 1996 19:54:48 -0400, Lars Hellsten <la...@ionsys.com>
wrote:

~Thanks to those who answered my question about carrier detect (I ended
~up finding out that it's a lot simpler than writing an event handler,
~etc. I can just check the RLSD flag... for some crazy reason Microsoft
~decided to name it that instead of DCD, and that's what was confusing
~me).

Hello Lars,

I have been putting aside a function in my program and that is to
detect the carrier detect.  And I have noticed that you have been
successful in detecting it.  I wonders if you could show me some
sample codes on how to detect the Carrier Detect.  If you don't mind,
kindly email the source to me.  And don't forget to post a copy to the
usenet as well.

By the way, sorry for not answering your questions as I do not know
the answers to your questions.

TIA

Finest Regards,
SAM

 _       __    _____    _____            
| |     / /   / ___/   / ___/ ____   ____   ____ _
| | /| / /    \__ \    \__ \ / __ \ / __ \ / __ `/
| |/ |/ /  _ ___/ / _ ___/ // /_/ // / / // /_/ /
|__/|__/  (_)____/ (_)____/ \____//_/ /_/ \__, /
                                         /____/
**************************************************
   From the desk of Song Weng Sam

   Email : son...@pacific.net.sg
   WWW   : http://home.pacific.net.sg/~songws/SAM
   Tel   : (65)-271-7075 (0ffice)
   Pager : 9-492-4014
**************************************************

Other Threads