Sharing COM Port access between Processes

  • Thread starter Thread starter Lemon Juice
  • Start date Start date
L

Lemon Juice

Hello All,

Is it possible to access the same COM port from two different processes?

For instance I have written a DLL that connects to and manages a COM port.
Internally my dll uses the .net(3.5) SerialPort component. On the same
machine I have two different applications that use my dll. I want both
applications to receive events and set pin states on the COM port
(ie. CDHolding, DTR, RTS etc) However when the second application tries to
open the COM port I get an exception 'system.UnauthorizedAccessException:
Access to the port 'COM3' is denied.'

Is this possible?

What's the right way to do this?
Would creating a service and connecting clients to it using IPC be better?
 
Hi,

You will have to write a wrapper class for the serial port access to allow
this sharing. In the wrapper, you publish the methods that you want to
share (and events), then handle the shared access using standard Threading
sharing constructs -- though threading isn't actually being used explicitly.

I have example code for this (a VB6 ActiveX EXE) in my book, see below.
However, .NET code would make it even cleaner.

Of course, your wrapper class (dll) becomes a new object, and is no longer
an instance of SerialPort -- you can, however, inherit SerialPort, and
overload the methods that you want to share to add the additional "stuff"
that I mention above.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
Back
Top