Article View: borland.public.cppbuilder.deployment
Article #163Re: THREAD with SERIAL COMM
From: J�rg Reithaar
Date: Sun, 25 Sep 2005 17:07
Date: Sun, 25 Sep 2005 17:07
113 lines
4237 bytes
4237 bytes
Hi
Your thread consumes too much resources because you requested it to do so.
It checkes for incomming data at maximum speed....
while ((!this->Terminated) && (objSerial->InstanceActive))
{
if (this->CanReceive) Synchronize(ReceiveData);
}
This makes your pc to become sslllooooooowwwww.
You have to add a "delay" into this loop.
// declare a variable as follows...
HANDLE hSemaphore;
// ...then add the following into the infinite loop
hSemaphore = CreateSemaphore( NULL, 0, 10, "X" );
WaitForSingleObject( hSemaphore, 30 );
Resulting code:
while ((!this->Terminated) && (objSerial->InstanceActive))
{
if (this->CanReceive) Synchronize(ReceiveData);
hSemaphore = CreateSemaphore( NULL, 0, 10, "X" );
WaitForSingleObject( hSemaphore, 30 );
}
Some explanation:
Windows is not preemptive. Task do not "suspend" automaticly to give others
a chance to execute in paralell.
Thread programming is very near to system an should respect some rules to
execute smoothly.
Do nothing more than absolutly necessary inside the threads EXECUTE routine.
WaitForSingleObject consumes no time-resources because it realy suspends
execution for the time given.
30 in this case means 30 milliseconds. This time is a minimum. The systems
resumes not before the time given.
In gereral: Avoid to use endless loops inside the thread. Try to leave the
thread "sleeping" as much as possible.
Just "kick" it on when you expect a delayed action.
Please let me know of your success
J�rg Reithaar
Reithaar Elektronik GmbH
""@t" <""fabio(\"@t)techside.it"> schrieb im Newsbeitrag
news:43205bc7@newsgroups.borland.com...
> Hi to all.
>
> I have to realize a small app that receive data through a serial port.
>
> My idea is to build a Thread and poll the serial port for data.
> I have realized this solution but the thread use all resource and when
> thread start my up is blocked (combo, buttons, etc).
>
> This is a portion of the code (objSerial is a object from the Synaser
> class that I used for serial comm):
>
>
> void __fastcall thrSerialReader::Execute()
> {
> SetName();
> FreeOnTerminate = false;
> //---- Place thread code here ----
> if (objSerial != NULL)
> {
> while ((!this->Terminated) && (objSerial->InstanceActive))
> {
> if (this->CanReceive) Synchronize(ReceiveData);
> }
> }
> Terminate();
> }
>
> void __fastcall thrSerialReader::ReceiveData()
> {
> Byte chRead;
>
> do
> {
> chRead = objSerial->RecvByte(5);
> if (chRead > 0) this->strRecvMessage = this->strRecvMessage +
> AnsiString((char)chRead);
> }
> while ((objSerial->LastError == 0) && (chRead != END_MESSAGE));
> // If no data is present objSerial->LastError is != 0
>
> if ((objSerial->LastError == 0) && (chRead > 0))
> // No error and data present
> {
> ...here the code to manage the data received
> }
> }
>
> Any suggestions ? I'm are newer to the thread programming.
> Some links to get documentation about threads in c++ builder ?
>
> Thanks.
> Fabio.
Technical Details
Message-ID:
<4336d8cf$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newscon06.news.prodigy.com!newscon02.news.prodigy.com!prodigy.net!nx02.iad01.newshosting.com!newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<43205bc7@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: f8e54,5fc7742ce46e9625 X-Google-Attributes: gidf8e54,public X-Google-Language: ENGLISH,ASCII From: "J�rg Reithaar" <jreithaar@datacomm.ch> Newsgroups: borland.public.cppbuilder.deployment References: <43205bc7@newsgroups.borland.com> Subject: Re: THREAD with SERIAL COMM Date: Sun, 25 Sep 2005 19:07:02 +0200 Lines: 115 Organization: Reithaar Elektronik GmbH X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 NNTP-Posting-Host: 83.228.184.151 Message-ID: <4336d8cf$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1127667919 83.228.184.151 (25 Sep 2005 10:05:19 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newscon06.news.prodigy.com!newscon02.news.prodigy.com!prodigy.net!nx02.iad01.newshosting.com!newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail