Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am very Struggle for this one, I am developing small desktop application,(i.e)RS232 port

I have three port that is COM2,COM5 and COM3 , COM2 and COM5 are another machine, COM3 is needed machine,

Three port added in combobox COM2,COM5,COM3 order.

My question is , How do i select Default my machine(COM3)when form loading?

C#
private void Form1_Load(object sender, EventArgs e)
          {

SQL
//Adding available port names to the selection combobox
            portNameSelectionComboBox.Items.AddRange(SerialPort.GetPortNames());
            portNameSelectionComboBox.SelectedItem = SerialPort.GetPortNames()[0];
            //initializing first port name by default
            //devicePort = new SerialPort(SerialPort.GetPortNames()[0]);
            devicePort = new SerialPort(portNameSelectionComboBox.SelectedItem.ToString());
            devicePort.DataReceived += new SerialDataReceivedEventHandler(postDataRecieved);
            devicePort.Open();

}

Thanks in Advance..
Posted
Comments
CPallini 25-Jul-14 4:40am    
Could you please elaborate? Do you want to select, in any case, COM3?
chellapandi160 25-Jul-14 5:35am    
How to identify My Machine Default?

It may be COM1,COM2 or COM3...etc

1 solution

Get all your serial ports by calling SerialPort.GetPortNames().

Select the port you want, but SerialPort won't help with that. You need to either let user select a port, or send something to every port and see which one responds in the way you expect it to. Be prepared for some exceptions on ports that are in use by other software.

Store selected port by its name in Properties.Settings.Default.RS232ConnectionName. That way, you can change it from within your software and get it again OnLoad.
 
Share this answer
 
v2
Comments
chellapandi160 25-Jul-14 5:57am    
How to identify My Machine port Default?

It may be COM1,COM2 or COM3...etc
lukeer 25-Jul-14 6:13am    
I've updated my solution.
chellapandi160 25-Jul-14 6:20am    
Could you please explain me with code ? please i am very struggle with this one..
chellapandi160 25-Jul-14 6:28am    
how to send something and get response?
lukeer 25-Jul-14 8:01am    
The link to MSDN on SerialPort provides an example of how to send and receive data.
The "send something and get response" part requires the device connected to the port to do so. Let's assume you have a thermometer with RS232 connected to Com12 and you know its API.
Let's furthermore assume that you send GETTEMP\r\n and expect to receive TEMP=NNN°C\r\n, where NNN is a number of up to 3 digits in length.
Then you could loop through all your ports, send each one the GETTEMP message and test for reception of a matching response within a bearable time interval.
Upon successful reception of a response that matches your expectation, you would know that a thermometer with the API you know is connected to the port you just tried.
After iterating over all ports and still not having received a "good" answer, you know that something isn't how you would like it to be and present user an error message.

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