MSCommLib ActiveX Control in a C# Windows Service?

  • Thread starter Thread starter Will
  • Start date Start date
W

Will

I'm working on a C# Windows Service that needs to monitor serial port
communication. Because the .Net framework does not include support for
serial communications, I've decided to use the Microsoft Communication
Control (MSCommLib) that comes with Visual Studio 6. It seems easy to use
and works well enough for me within the context of a Windows Application.
Because I need the serial ports to be monitored 24/7, however, a Windows
Service seems like the better choice.

Because the MSCommLib control is an OCX, it needs to be placed on a Form.
I've added a Form that presumably won't be displayed during runtime to my
Windows Service project. At this point, with just a Windows Service and an
Empty Form, I am able to install and start the service with no problems.
After adding the MSCommLib Control to the form, however, when I start the
service I get an error message that says something like:

"Your Service started and then stopped. Some services do this"

Placing Event Log debug statements in the code reveals that the Service
never appears to return from the Form.InitializeComponents() function, in
which the MACommLib control is instantiated and initialized.

Does anyonone have any advice regarding my situation? Perhaps I am trying
to do the impossible...Any workarounds or alternate solutions for monitoring
a server's serial ports would be greatly appreciated.

Thanks,

Will
 
Will,

Generally speaking, trying to access UI elements is a bad idea when
running in the context of a service. Granted, you can have your service
interact with the desktop (by clicking "allow service to interact with
desktop"), but you can't guarantee that there will always be a desktop.

Because of this, you might want to conisder making calls to the Windows
API through the P/Invoke layer to access the serial port. There are a few
classes out there that do this already, and I believe there is an
implementation on gotdotnet.com.

Hope this helps.
 
Hi Will,
Because the MSCommLib control is an OCX, it needs to be placed on a
Form.

This is not true, you do not have to place the MSComm control in a form.
Simply add a reference in your C# project to MSCommLib. Click on the COM
tab and browse to select your MSCOMM32.OCX .

Then create an instance of it in your C# class:

MSCommLib.MSComm mscomm = new MSCommLib.MSCommClass();

The MSComm control has been working fine for me however I am still looking
for a better .NET way to do it:

Some alternatives to using the MSComm control in .NET

C#
http://www.gotdotnet.com/Community/...mpleGuid=b06e30f9-1301-4cc6-ac14-dfe325097c69

Managed C++
http://www.codeproject.com/managedcpp/howtocomport.asp

VB .NET
http://www.mentalis.org/classlib/class.php?id=15


http://www.gotdotnet.com/Community/...mpleGuid=8aaa0158-95b6-49a7-bb20-93391fc4c196

C# ( This uses thread pooling that is not supported under Win98)
http://www.gotdotnet.com/Community/...mpleGuid=b06e30f9-1301-4cc6-ac14-dfe325097c69

C#
http://www.gotdotnet.com/Community/...mpleGuid=fcba7fc5-666e-4eb0-863f-0045b0c79ec7

-Ed
 
This is not true, you do not have to place the MSComm control in a
form. Simply add a reference in your C# project to MSCommLib. Click
on the COM tab and browse to select your MSCOMM32.OCX .

Then create an instance of it in your C# class:

MSCommLib.MSComm mscomm = new MSCommLib.MSCommClass();

Ed, thanks a ton for the tip. I'm going to give this a shot first before I
punt on the MSCommLib control.

Will.
 
Thanks for the link...it'll serve as a backup plan if Ed Sutton's suggestion
doesn't work.

Thanks,
Will.
 
Ed, thanks a ton for the tip. I'm going to give this a shot first
before I punt on the MSCommLib control.

I agree that is the least riskiest approach.

The MSComm control is very reliable. I am using it in my C# application to
reliably communicate at high speeds 460,800 bps using a USB/Serial
interface.

Whenever I get enough time though, I want to use a more .NET approach.

This guy says "MSCOMM is the most bug-free component that Microsoft has ever
released.". Actually, I think Microsoft purchased it from Sax.

http://www.vbrad.com/source/src_mscomm_tutorial.htm

-Ed
 
Back
Top