3

I'm writting a SerialPort program. Sometimes when I want to open the SerialPort, an UnauthorizedAccessException is thrown :access to COM1 is denied. This is beause another program( which is written by someone else and I don't have its soucecode ) is using COM1, and that program doesn't close COM1 when it exits. I want to ask 2 questions:

  1. How to close COM1 with my code (especially C# code) when it is used by another program?
  2. If I cannot close COM1 with C# code, how can I close or free COM1 in Windows OS?

Thanks.

Jerry
  • 435
  • 4
  • 12
  • You might be able to power cycle the port using WMI. http://msdn.microsoft.com/en-us/library/windows/desktop/aa393485(v=vs.85).aspx – Kane Jun 25 '12 at 04:08
  • My guess is that the program that is hogging the Com Port is not exiting completely because windows should recover the Com Port, have you tried looking to see if the process is still running? – Mark Hall Jun 25 '12 at 04:15
  • I also think some program doesn't close SerialPort after using it. But I am not sure which one. – Jerry Jun 25 '12 at 04:32

2 Answers2

6

This Stack Overflow question is the first step: How do I get the list of open file handles by process in C#?

Once you have that, you can modify the code there to close the handle, or simply kill the process that has the handle open.

This, however, is very risky*, prone to problems, and will need to be re-engineered based on specific deployments (x32 vs x64 vs ARM) plus what 'COM1' maps to on that particular machine.

On Risk: If the serial port is being used to, for instance, install new firmware on a phone or similar - closing the port may result in bricking the device. If the port is being used to transfer data, then closing the port may result in losing data.

Good luck. But please do NOT blame me if you destroy your system or lose data.

Community
  • 1
  • 1
  • Thanks. I But I don't know the file name or file handle of COM1, can you tell me about that? – Jerry Jun 25 '12 at 04:27
  • 1
    It will look something like `\Device\Com0` or `\Device\Serial0` - See [Win32 File Naming](http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#win32_device_namespaces) - and use [SysInternals WinObj](http://technet.microsoft.com/en-us/sysinternals/bb896657.aspx) to browse your machine's devices. –  Jun 25 '12 at 04:33
-2

You can close comm port in C# by calling

serialPort.Close();

But you can't close the COMM port unless it is opened by you only.

Make sure while closing your application you are closing the comm port. If you can place code we can look into it.

Sudhakar B
  • 1,465
  • 9
  • 16
  • 2
    You didn't answer my question. I know serialPort.close() can close the SerialPort I have openned. I want to close COM1 opened by another program which is written by someone else. – Jerry Jun 25 '12 at 04:04
  • Thats what I said, you can't close the COMMPort accessed by other application. Only the way you can do is identify which application is accessing and kill that process. – Sudhakar B Jun 25 '12 at 04:05
  • I don't think so. I use the computer , I can write any code running on it, I have full control of it. There must be a way to close COM1 used by another program. For example, find and kill the thread used COM1. I want to find a simple way. – Jerry Jun 25 '12 at 04:09