COM communication ceases when window loses focus

  • Thread starter Thread starter Joris Dobbelsteen
  • Start date Start date
J

Joris Dobbelsteen

I have a very strange situation here:

I have an console application that reads from a the COM port. I have 1
thread busy doing this.

Next I have 512 threads which perform a write/read cycle on the COM port
with some restrictions:
For every command (there are 254) there can be only one doing the
write/read, the rest is queued up in order (tnx Nicholas).
Next there is a restrictions only 16 (currently) can be doing the write/read
similtanous (using WaitAny on 16 AutoResetEvents).

Now when the application HAS the focus it does it job nicely.
When the focus is moved to another application it stops doing anything (my
scope on the RS232 data channels also stops displaying anything). Sometimes
before the stop its working very rapidly.
Moving the focus back to the applicaiton continues.

Now why is that?

- Joris
 
Joris,

How are you doing the reads and the writes to the COM port? It might be
that for some reason, the mechanism is reliant on windows messages (why, I
do not know, and I hope this isn't the case).

Can you elaborate?
 
Nicholas,

I'm using a separate thread and this uses the ReadFile function.
Another thread is doing a write with WriteFile.

I have a two mutexes which sit arround these functions, so only one thread
is actively reading (there will always be only one)
And there is one arround the WriteFile, which might be more important, so
only 1 thread can simultanously write.
I don't think this is the problem.

- Joris

Nicholas Paldino said:
Joris,

How are you doing the reads and the writes to the COM port? It might be
that for some reason, the mechanism is reliant on windows messages (why, I
do not know, and I hope this isn't the case).

Can you elaborate?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joris Dobbelsteen said:
I have a very strange situation here:

I have an console application that reads from a the COM port. I have 1
thread busy doing this.

Next I have 512 threads which perform a write/read cycle on the COM port
with some restrictions:
For every command (there are 254) there can be only one doing the
write/read, the rest is queued up in order (tnx Nicholas).
Next there is a restrictions only 16 (currently) can be doing the write/read
similtanous (using WaitAny on 16 AutoResetEvents).

Now when the application HAS the focus it does it job nicely.
When the focus is moved to another application it stops doing anything (my
scope on the RS232 data channels also stops displaying anything). Sometimes
before the stop its working very rapidly.
Moving the focus back to the applicaiton continues.

Now why is that?

- Joris
 
Hi Glenn

Can you point me to some examples on the com port programming,



My Serial Port FAQ:

..NET 1.0/1.1 has no support for legacy ports (RS-232/COM/LPT).
It will be included in .NET 2.0 (2005)

.NET 2.0 Beta 1 Documentation:
http://lab.msdn.microsoft.com/library/en-us/cpref/html/N_System_IO_Ports.asp
Serial Support in Whidbey Demo:
http://www.gotdotnet.com/team/clr/bcl/demos/demos.aspx



With 1.0/1.1 you have to use PInvoke or Interop :

First understand the Win32 API as described here (C++):
http://msdn.microsoft.com/library/en-us/dnfiles/html/msdn_serial.asp

MSDN article for .NET (mostly C#):
http://msdn.microsoft.com/msdnmag/issues/02/10/NETSerialComm/

PInvoke samples for C#:
http://www.gotdotnet.com/community/usersamples/Default.aspx?query=SerialPort
http://msdn.microsoft.com/msdnmag/issues/02/10/NETSerialComm/
http://www.gotdotnet.com/Community/...mpleGuid=9dde5cf3-4842-4d4b-baa7-73e09c4d5890
http://www.gotdotnet.com/community/usersamples/Default.aspx?query=SerialStream
http://www.gotdotnet.com/Community/...mpleGuid=5a96c071-e9e8-47c4-bb9e-5413384f0c25

or for VB.NET:
http://support.microsoft.com/?kbid=823179
http://msdn.microsoft.com/library/en-us/dnvssamp/html/vbcs_usingthecomportinvbnet.asp
http://www.gotdotnet.com/community/usersamples/Default.aspx?query=rs232
http://www.mentalis.org/classlib/class.php?id=15
http://www.corradocavalli.cjb.net/

or you can use the "Managed Extensions for C++" and write wrappers.
http://msdn.microsoft.com/library/en-us/vcmex/html/vcconMCOverview.asp
http://www.gotdotnet.com/team/cplusplus/
http://msdn.microsoft.com/library/en-us/dncscol/html/csharp12192002.asp
on your VS.NET path:
...\VC7\managedextensionsspec.doc
...\VC7\migration_guide.doc
MC++ Sample:
http://www.codeproject.com/managedcpp/howtocomport.asp

or reusing the VB6 MSComm ActiveX is easy, but it has some 'problems' (license)
http://support.microsoft.com/?kbid=318597
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=320
http://ourworld.compuserve.com/homepages/richard_grier/NETCommOCX.htm

for the Compact Framework (Windows CE)
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/PISAPICF.asp
http://www.microsoft.com/downloads/details.aspx?FamilyID=206645de-63ba-4e49-b9a3-2d3b9cef4aa5
http://ourworld.compuserve.com/homepages/richard_grier/CFSerial.htm

commercial:
http://www.sax.net/dotnet/communications/
(Community edition in : http://msdn.microsoft.com/vbasic/vbrkit/ )
http://www.winsoft.sk/ncomport.htm
http://www.charonsoftware.com/net/communications/
http://www.scientificcomponent.com/portcontroller_net.htm
http://www.aftek-software.com/CommControl.htm
http://www.componentscience.net/Elements/TransPort/
http://franson.biz/serialtools/

for Interop, use newsgroup:
microsoft.public.dotnet.framework.interop
 
http://www.freevbcode.com/ShowCode.asp?ID=4666
was what I used...
(Some slight changes are needed, because there are a couple issues).
I did write a small wrapper arround it, so it can be handled as a stream
(slightly non-standard).

Perhaps someone has a good (freely available) solution that is preferably
thread-safe?

- Joris
 
Back
Top