🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.deployment
6 messages
6 total messages Started by Anon Thu, 08 Sep 2005 15:43
THREAD with SERIAL COMM
#160
Author: Anon
Date: Thu, 08 Sep 2005 15:43
52 lines
2206 bytes
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.
Re: THREAD with SERIAL COMM
#161
Author: Giuliano
Date: Thu, 08 Sep 2005 16:19
19 lines
1396 bytes
On Thu, 08 Sep 2005 17:43:00 +0200, \"@t <""fabio(\"@t)techside.it"> wrote:

>Hi to all.
[snip]
>Any suggestions ? I'm are newer to the thread programming.
>Some links to get documentation about threads in c++ builder ?

Hi,

   have a look at this library:

http://sourceforge.net/projects/tpapro

It's free, stable and well documented.
Also, you don't need to create your own threads,
because the TApdComPort component is already
threaded.

Giuliano
Re: THREAD with SERIAL COMM
#162
Author: Hans Zandbergen
Date: Fri, 09 Sep 2005 09:18
44 lines
2015 bytes
In article <7so0i1tuctkha7rcs95sfbb0kt71g63n5a@4ax.com>, 
gcardinali@remove.adhoc.net says...
> On Thu, 08 Sep 2005 17:43:00 +0200, \"@t <""fabio(\"@t)techside.it"> wrote:
> 
> >Hi to all.
> [snip]
> >Any suggestions ? I'm are newer to the thread programming.
> >Some links to get documentation about threads in c++ builder ?
> 
> Hi,
> 
>    have a look at this library:
> 
> http://sourceforge.net/projects/tpapro
> 
> It's free, stable and well documented.
> Also, you don't need to create your own threads,
> because the TApdComPort component is already
> threaded.
> 
> Giuliano
> 

Hi, 

Sometimes it is just handy to find out yourself to discover how to use 
it. 

To prevent a thread to consume all the cpu time, you need to tel the 
thread to give back control to other processes.

You can do that simple, by including 

SleepEx(10,true)

in your polling loop.

or you can use

WaitForMultipleObjects(....)

(more complicated)

Hans Zandbergen
Re: THREAD with SERIAL COMM
#163
Author: J�rg Reithaar
Date: Sun, 25 Sep 2005 17:07
113 lines
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. 
Re: THREAD with SERIAL COMM
#164
Author: at
Date: Mon, 26 Sep 2005 19:08
63 lines
2874 bytes
Thank you, I have used the WaitForSingleObject function and
this works fine !!
Now my app is vefy fluid, after a small tune of the delay time
now the application is ok!

Thanks.
Fabio

J�rg Reithaar wrote:
> 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
> 
> 
Re: THREAD with SERIAL COMM
#165
Author: Thomas Maeder [TeamB]
Date: Mon, 26 Sep 2005 19:12
5 lines
1405 bytes
Please direct your browser at http://info.borland.com/newsgroups/ and
read the newsgroup guidelines. One of them asks us not to quote entire
posts we are following up to; instead, please trim the quotes to the
parts relevant for your reply. Thanks!
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads