Article View: borland.public.cppbuilder.nativeapi
Article #6264Re: Problem with serial communications
From: Martin Nijhoff
Date: Tue, 31 May 2005 15:28
Date: Tue, 31 May 2005 15:28
67 lines
3127 bytes
3127 bytes
> You removed some code. Did any of that code contain CancelIO() ?
No.
> You didn't show osRead or osWrite.
Here is (a simplified version of) the class definition:
class TCustomCOMPort : public TComponent
{
friend TCOMStatThread;
private:
TCOMStatThread *StatThread;
OVERLAPPED osRead;
OVERLAPPED osWrite;
TCOMDataEvent FOnDataAvailable;
TCOMErrorEvent FOnError;
void __fastcall WaitForAsync (OVERLAPPED &Overlapped, DWORD &ByteCount);
protected:
bool __fastcall Open ();
bool __fastcall Close ();
DWORD __fastcall Read (void *Buffer, DWORD Size);
DWORD __fastcall Write (void *Buffer, DWORD Size);
__property TCOMDataEvent OnDataAvailable = {read=FOnDataAvailable,
write=FOnDataAvailable};
__property TCOMErrorEvent OnError = {read=FOnError, write=FOnError};
public:
__fastcall TCustomCOMPort (TComponent *Owner);
__fastcall ~TCustomCOMPort ();
};
The thread is created in the Open() method and destroyed in the Close()
thread, which means that the thread runs as long as the COM port is open.
> Your class has One OVERLAPPED struct, not 2, and you wait on 2 events.
My class has 1 OVERLAPPED struct (for WaitCommEvent) and 1 event for
signalling thread termination (doesn't require an OVERLAPPED struct).
> I would think you would want to wait on 3 events, evRead, evWrite and
> evTerminate
In TCOMStatThread? Reading and writing is handled in TCustomCOMPort. I don't
need reading/writing in the background, which would be done in
TCOMStatThread. Blocking read/write (with timeouts) is all I need. The COM
port component is used for communicating with devices that use a
command/reply communication model (the PC sends a request and waits for a
reply from the device).
> You are blocking after read and write, so why are you using overlapped
> instead of blocking i/o?
Non-overlapped I/O doesn't work in WinXP.
> You aren't handling any errors from ReadFile() or WriteFile().
Can you tell me which errors to handle and how to handle them?
Thanks for your reply.
Martin
Technical Details
Message-ID:
<429c826a$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!feed5.newsreader.com!newsreader.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<429c30e1$1@newsgroups.borland.com> <sito91h41an986u4069q49ocejmgt29ae5@4ax.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,ab642e2ba4263114 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Martin Nijhoff" <dont@mail.me> Newsgroups: borland.public.cppbuilder.nativeapi References: <429c30e1$1@newsgroups.borland.com> <sito91h41an986u4069q49ocejmgt29ae5@4ax.com> Subject: Re: Problem with serial communications Date: Tue, 31 May 2005 17:28:25 +0200 Lines: 69 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 80.100.129.156 Message-ID: <429c826a$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1117553258 80.100.129.156 (31 May 2005 08:27:38 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!feed5.newsreader.com!newsreader.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail