Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i want to connect via serial port to a hardware. i need to know which comports is valid. for example, i have just comport3! i want to select this. what shall i do?
Posted
Comments
agent_kruger 19-Nov-13 6:04am    
what do you need the serial port for? sending messages through GSM or something Like that?
mohsenvasefi 20-Nov-13 3:33am    
yes, send special data to Microcontroller

Hi,
Read this article :
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.getportnames(v=vs.110).aspx
But you must give more precision about "Valid" COM port.
This is depending of your hardware.
Generally I do this :
- Open COM port
- send a message to get the serial number for example and wait the response.
- if a good response, hardware is detected
- close COM Port if necessary

Best regards
 
Share this answer
 
Comments
mohsenvasefi 20-Nov-13 3:55am    
i need to put these comports on a combobox and then select one of them for connect to hardware
You can use WMI (System.Management): http://msdn.microsoft.com/en-us/library/system.management%28v=vs.110%29.aspx[^].

It could be something like http://stackoverflow.com/questions/1081871/how-to-find-available-com-ports[^].

[EDIT]

mohsenvasefi wrote:
I need to put these comports on a combobox and then select one of them for connect to hardware.
Sure. I described appropriate technique here: combobox.selectedvalue shows {} in winform[^].

The idea is: instead of putting just the COM port names to the combo box list, you can put the instances of some type carrying all the data needed for your hardware communications, and make each list item showing COM port names by overriding System.Object.ToString(), as shown in the code sample referenced above.

—SA
 
Share this answer
 
v2
Comments
mohsenvasefi 20-Nov-13 3:55am    
i need to put these comports on a combobox and then select one of them for connect to hardware
Sergey Alexandrovich Kryukov 20-Nov-13 11:10am    
Sure. Please see the update to my answer, after [EDIT].
—SA
If your are using Serial Port class then you can use the below method to get the comports available

C#
using System.IO.Ports; // include the namespace

SerialPort sp=new SerialPort(string portName,int baudRate,Parity parity,int dataBits,StopBits stopBits); //set your device baudrate,parity,stopbits bit to talk with the device.

sp.Open(); // open a serial port connection
string[] arr=Serialport.GetPortNames();
foreach(var item in arr)
{
Combobox1.Items.Add(item); //suppose Combobox1 is yours required combobox name
}
//once you access the comport that hardware connected,do sending a commands or what you want once its done 
sp.Close(); //close the serial port connection


In Serial port class,GetPortNames() will fetch all the com ports of PC

GetPortNames -Gets an array of serial port names for the current computer.

Hope this will help you.
 
Share this answer
 
v2
Comments
mohsenvasefi 20-Nov-13 3:55am    
i need to put these comports on a combobox and then select one of them for connect to hardware
keerth516 20-Nov-13 12:21pm    
updated my answer to you question and remember this is very simple way of doing it.hope it fetch you basic knowledge and can do what ever you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900