Board index » delphi » Try to read from serial port

Try to read from serial port

Hallo @ all,

I write and try to read from the serial port with the
following code. The write works very fine, but the
"WaitCommEvent" does never return. I also tried it before
using the pure ReadFile, without any masks and others, with
absolutely the same result, it never returned.

If I close the program and open the (ugly) Hyperterm to enter
the same AT-command it returns with the expected value. So
I guess I mis-programmed something but have no guess what.

---- Source Begin ----
var ol      : TOverlapped;
    s       : String;

    s := Edit1.Text;

    WriteFile(hComm, s, Length(s), Len, nil);

    ol.hEvent := CreateEvent(nil, False, False, nil);
    SetCommMask(hComm, EV_RXCHAR or EV_ERR);
    GetCommMask(hComm, Eventm);

    if (Eventm and EV_RXCHAR = EV_RXCHAR) then
    begin
        Ready := False;
        Ready := WaitCommEvent(hComm, Eventm, @ol);
-->      ^--- at this point it doesn't return ---^

        if(Eventm and EV_RXCHAR = EV_RXCHAR) then
        begin
            ReadFile(hComm, buffer, 5, Len, nil); // Length(buffer)
            Memo1.Text := buffer;
        end
    end

---- Source End ----

Anybody any idea?? Thanx in advance.

(Win2k, SP2, Delphi 6 Trial)

--
Mfg Matthias

 

Re:Try to read from serial port


In article <9ub3ph$7bt3...@ID-14667.news.dfncis.de>, "Matthias

Quote
=?ISO-8859-1?Q?St=FCbner?=" <matthias.stueb...@gmx.de> writes:
>I write and try to read from the serial port with the
>following code. The write works very fine, but the
>"WaitCommEvent" does never return. I also tried it before
>using the pure ReadFile, without any masks and others, with
>absolutely the same result, it never returned.

I wrote a program to catch the protocol control lines (CTS, DTR, RLSD) not the
data flow, but I used a thread to asynchronously monitor the port (using an
overlapped flag in createfile). I used WaitForSingleObject with a timeout. If
the WFSO returned on the time-out I just refreshed it, otherwise got the modem
status. Be warned that the GetCommModemStatus (if you ever have to use it to
get the control protocols) has some weird behaviour which only loosely matches
the description <g>.

Using a time-out ensures that the code does not lock up. Using a separate
thread to  asynchronously monitor the port ensures that the monitoring code
does not block out the I/O activity.

Alan Lloyd
alangll...@aol.com

Re:Try to read from serial port


"AlanGLLoyd" <alangll...@aol.com> wrote >

Quote

> I wrote a program to catch the protocol control lines (CTS, DTR, RLSD) not the
> data flow, but I used a thread to asynchronously monitor the port (using an
> overlapped flag in createfile). I used WaitForSingleObject with a timeout. If
> the WFSO returned on the time-out I just refreshed it, otherwise got the modem
> status. Be warned that the GetCommModemStatus (if you ever have to use it to
> get the control protocols) has some weird behaviour which only loosely matches
> the description <g>.

That's what I did in another try, but without changes. Actually I doesn't care
about locked apps, I only what to read something to see how to do this.
Later I will take care multithreading etc. But als long as I cannot read one
byte successfully it would be overhead.

Any other idea?

--

regards Matthias

Re:Try to read from serial port


"Matthias Stuebner" <matthias.stueb...@gmx.de> schrieb im Newsbeitrag news:9ub3ph$7bt3g$1@ID-14667.news.dfncis.de...

Quote
> Hallo @ all,

> I write and try to read from the serial port with the
> following code. The write works very fine, but the
> "WaitCommEvent" does never return. I also tried it before
> using the pure ReadFile, without any masks and others, with
> absolutely the same result, it never returned.

> If I close the program and open the (ugly) Hyperterm to enter
> the same AT-command it returns with the expected value. So
> I guess I mis-programmed something but have no guess what.

> ---- Source Begin ----
[ source snipped ]
> ---- Source End ----

Hallo,

I guess I found my problem (at least in theory). There is no CR/LF
that finishes the AT command.

At this point latest, I see some problems chaning from MS VC6
to Delphi, because the (so-called) documentation is really not
that complete / readable. Anyway.

How do I add the CR/LF? A '\r\n' doesn't work. How else?

Thanx, Matthias

Re:Try to read from serial port


Matthias Stbner <matthias.stueb...@gmx.de> schreef in berichtnieuws
9ucuv3$7ld8...@ID-14667.news.dfncis.de...

Quote
> "AlanGLLoyd" <alangll...@aol.com> wrote >

> > I wrote a program to catch the protocol control lines (CTS, DTR, RLSD)
not the
> > data flow, but I used a thread to asynchronously monitor the port (using
an
> > overlapped flag in createfile). I used WaitForSingleObject with a
timeout. If
> > the WFSO returned on the time-out I just refreshed it, otherwise got the
modem
> > status. Be warned that the GetCommModemStatus (if you ever have to use
it to
> > get the control protocols) has some weird behaviour which only loosely
matches
> > the description <g>.

> That's what I did in another try, but without changes. Actually I doesn't
care
> about locked apps, I only what to read something to see how to do this.
> Later I will take care multithreading etc. But als long as I cannot read
one
> byte successfully it would be overhead.

> Any other idea?

> --

> regards Matthias

Hi Matthias,
Your code does not show how you initialised the com handle and the
TOverlapped structure ( which, btw,  _must_ be zeroed before use ).
You _really_ must use a separate thread to handle the comm events,
otherwise, you will face a frozen UI for each little error. I suggest you go
to my website and download TComPort. It's nothing fancy, but it handles all
events, comes with a fully working example app, and ... it works <g>

--
Regards,

Dirk Claessens
---------------------------------------------------------
http://www.claessens16.yucom.be
Attention: All spamshields raised; E-mails will bounce!
---------------------------------------------------------

Other Threads