How I can use COM-port if its uses by another process ?

Hi all.

I have a class which works with some hardware device connected to my PC through COM-port.

When I`m trying to create an instance of my class there's exception occured: UnauthorizedAccessException. It happens often but not all the time (!). It may happens right after reboot the computer.

I think this is because COM-port is used by anothor process in OS. But I'm sure that there's no any program which can use this port except mine.

So how I can release all sources which can work with COM-port and then connect to this COM-port from my class ?

p.s.: I use SerialPort class in .NET 2.0.

[648 byte] By [EugeneOstroukhov] at [2007-12-23]
# 1
I think you diagnosed this correctly, a program is already using the serial port. Sharing access is out of the question, Windows won't allow it and it fundamentally cannot work.

You'll have a hard time finding that other program. Look for some hardware driver that came with the device or some kind of modem dialer. First place to look though is your own program. Are you sure you aren't opening the port yourself twice?

nobugz at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2

As I said the problem can appears right after reboot PC.

with first run of my application...

And there`s just few programs installed in Windows, like VS, MS Office... Just office programs. Nothing more. Espessially there`s no any programs which can use COM ports...

EugeneOstroukhov at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3
That's why I think it might be your own program trying to open the port twice. Set a breakpoint on any Open() method call you have.

As an alternative, try poking around with the Handle utility from www.sysinternals.com, it shows you all handles owned by every process running on your PC. I'm not sure what an opened handle to a serial port looks like, practice by running it when your program does manage to open the port. Then run it again when it doesn't...

nobugz at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4
You can also use PortMon by SysInternals to see traffic on your serial port. That may help you diagnose the problem if you cqan recognise any data going across.
RabbiJosephGordon at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 5

Ok.

Thank you all.

I`ll reconsider my code.

EugeneOstroukhov at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified