Sign in to follow this  
aleisterbukowski

Unity [.net] System.IO.Ports check to see which ports are available?

Recommended Posts

I would like to find out a clever way of finding which ports are available. I have finally figured out that my microcontroller is connected to COM 5, or at least is being registered at COM 5 when I use my hyperterminal program I wrote in C#. However, I'm looking more toward a way to see which ports are available for open prior to opening them. I tried SerialPort SP = new SerialPort("COM1"); if(SP.IsOpen) //do stuff else SP = new SerialPort("COM2") if(SP.IsOpen) // do stuff here else etc... However, that can't technically be used until you have successfully opened the port, which means as you open the port SP.Open(); you're going to get an error while you're debugging it, and it's going to stop your program, since COM1 - COM4 doesn't exist. Which means I literally had to do the following on page load over and over until I found that yes I can connect to the program through the microcontroller, but it's trial and error, and I don't like it. SP = new SerialPort("COM1"); test program change to SP = new SerialPort("COM2"); test program change it to do this process until we have successfully come to SP = new SerialPort("COM5"); Sp.Open(); if(SP.IsOpen) MessageBox.Show("Connected successfully!); of course when I actually want to run the program, and use it on another system I'm going to need an algorithm to find which ports are open. My previous methods have not work, so I'm asking a community for some help. Thank you,

Share this post


Link to post
Share on other sites
smitty1276    560
I'm confused... why can't you just do:

for (int i=0; i < MAX_PORT_NUM-1; i++)
{
SP = new SerialPort("COM" + i);
try
{
SP.Open(...);
}
catch
{
// That one didn't work...
}
}



Share this post


Link to post
Share on other sites
Quote:
Original post by smitty1276
I'm confused... why can't you just do:
*** Source Snippet Removed ***



for (int i=0; i < 9; i++)
{
SP = new SerialPort("COM" + i);
try
{
SP.Open();
}
catch
{
// That one didn't work...
}
}
if (SP.IsOpen) txtReceiver.AppendText("Connected Successfully to " + SP.PortName.ToString() + " sucessfully. \n\n\n\n" + SP.PortName.ToString() + " is ready to be used\n\n\n\n");
SP.BaudRate = 25;



I have done what you have suggested. I have a terminal/command prompt like textfield that displays how many seconds we've gone since last transmission, what we've sent, etc...

As you can see we check to see if it's opened after we go through the loop and try to open the port.

As it gets to COM5 it should open, and then break, and check the next line of code.

And if it's opened it should say hey, we're open and we're ready for some action.

However that doesn't happen at all.

The only time I got it to work and connected successfully is if I manually connect to COM5 like the following.


SP = new SerialPort("COM5");



then once I checked it was opened it precisely what it should have done if it was opened.
[/source]

Share this post


Link to post
Share on other sites
Magos    218
First, instead of guessing some random numbers use System.IO.Ports.SerialPort.GetPortNames() to get the available ports.

Second I'm not clear what you're trying to achiveve.
A function like this checks if the port can be opened, as was posted earlier.
If the port is used by another program it fails (throws an exception, exception gets caught, function returns false).
Isn't this what you want?

bool IsPortAvailable(string PortName)
{
try
{
var Port = new System.IO.Ports.SerialPort(PortName);
Port.Close();
return true;
}
catch
{
return false;
}
}

Share this post


Link to post
Share on other sites
The code above seems to well see if it can be created and it can, but that's doesn't necessarily mean it can be open.

For instance, i did the following



string[] comPorts = {"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9" };

for (int i = 0; i < comPorts.Length; i++)
{
if(IsPortOpen(comPorts[i]))
{
cmbPorts.Items.Add(comPorts[i]);
}
}



and your code hasn't been modified at all.

In other words all ports are added to the combobox when only the ports that can successfully be open should be added.

Once you add a Open();

to the port you create you're going to get an error flag and it's going to break while you're debugging it unless it is the port that you can connect to.



[/source]

Share this post


Link to post
Share on other sites
I figured it out.

I didn't realize when I try to open a port I needed to give it a try catch, so when the port doesn't exist, or can't be open I can just catch the error and return instead of having the debugger spit out a non-existing object reference error, or whatever it spit out.

The final code is below

thanks for the help.


private bool IsPortOpen(String portName)
{
SerialPort tempPort;
try
{
tempPort = new SerialPort(portName);
try
{
tempPort.Open();
}
catch
{
return false;
}
tempPort.Close();
return true;
}
catch
{
return false;
}
}



Again thank you! Much help it was looking at it that way.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this