2

My program uses a serial communication port such as COM1. But sometimes this port get locked in a port in use state because of a program failure or something else.

Suppose that another software use it. Can I force using that port?

I would like to use that port anyway. Is it possible?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Davut Gürbüz
  • 5,526
  • 4
  • 47
  • 83
  • 2
    You can't close it for another application, you will need to shutdown that application. It is possible to check if the comport is used by another application. And pick an available port for your app. I can provide you with code for checking all available com port if you like. – Max Sep 30 '13 at 14:49
  • 1
    Is that "another software" something you wrote and can modify or is it a 3rd party program? – Scott Chamberlain Sep 30 '13 at 14:50
  • 1
    SerialPort is IDisposable, ensure that Dispoose method is called when class instance goes out of use. – Alex F Sep 30 '13 at 14:51
  • @MaxMommersteeg if there is method such as `DLL func` ,`WinApi` it would be great. Don't worry I' very stingy about using down-votes. – Davut Gürbüz Sep 30 '13 at 15:09
  • You can't steal a port away from another process. What you are looking for simply isn't possible. – Hans Passant Sep 30 '13 at 16:06
  • @HansPassant some program's visual screen get closed but processes run. There must be a way; finding out the process and requesting kill. I can't say "Reset your machine" or find process which uses your port to my enduser. I should find an elegant way. – Davut Gürbüz Oct 02 '13 at 08:12
  • At last I found a similar question on SO http://stackoverflow.com/a/11183466/413032 – Davut Gürbüz Oct 02 '13 at 08:25

1 Answers1

0

@MaxMommersteeg if there is method such as DLL func ,WinApi it would be great.

It isn't hard getting the available COM ports, and it is easier than you think.

public List<string> GetAllAvailableComPorts()
{
     return System.IO.Ports.SerialPort.GetPortNames().ToList();
} 

Refer to Get Port Names Method (MSDN) for the GetPortnames method.

The code above will return you a List<string> filled with all available COM ports. You can call it with code below:

//Create new list<string>
List<string> availableComPorts = new List<string>();

//Fill list with list<string> returned from GetAllAvailableComports method.
availableComPorts = GetAllAvailableComports();

All COM ports in the list are available, and COM ports that are currently in use will not appear in the list. You can use the List<string> to check if COM1 is available. If so, you can connect to it, and if not, you can give the user a message that it is in use (users will get an "Acces denied" message when connecting to an unavailable port.)

I think you are using Windows Forms, since you tagged C# and SerialPort and I don't think ASP and WPF are able to use the SerialPort class (correct me if I am wrong). So I would create a listbox, filled with all available COM ports and let the user choose which COM port to use. The user isn't able to pick the wrong COM port, since they aren't in the list.

If you need more information about this, you can just leave a comment below. I still got enough code in head left since my last created application which used a serialport connection as well.

I would like to refer you to my own question below. It has some more information:

Get list of available COM ports

Community
  • 1
  • 1
Max
  • 12,622
  • 16
  • 73
  • 101
  • It is a `WPF` you can use SerialPort for ASP and WPF. There are restrictions for Silverlight but with SL5 some options come in Out Of Browser. – Davut Gürbüz Oct 02 '13 at 08:04
  • I have no problem about using ports. I didn't see something for forcing a busy port in your answer. I have already done showing a message if a port is busy for my program. I would like find a way for using a busy port. – Davut Gürbüz Oct 02 '13 at 08:06
  • Sorry to hear that, It isn't possible to force using a busy port, because it is in use already, only one connection per port. – Max Oct 02 '13 at 08:08
  • It seems I must found and kill the process which uses this port with user inform. – Davut Gürbüz Oct 02 '13 at 08:15