3
votes

I'm doing a project for serial port..

I need to select the available com port from the combo box.

but i cant get it.. And i needed some help~

Here's my code.. its in C#:

btn_Open is a button to activate the serial port

    private void btnOpen_Click(object sender, EventArgs e)
    {
        string [] myPort;

        int COM1 = cbCommPorts.SelectedIndex;
        int COM2 = cbCommPorts.SelectedIndex;
        int COM3 = cbCommPorts.SelectedIndex;
        Object selectedItem = serialPort1.PortName;

        myPort = System.IO.Ports.SerialPort.GetPortNames();
        cbCommPorts.Items.AddRange(myPort);

        serialPort1.PortName = cbCommPorts.SelectedItem.ToString();
        serialPort1.BaudRate = 115200;

        if (serialPort1.IsOpen) {
            serialPort1.PortName = cbCommPorts.SelectedItem.ToString();

            serialPort1.Open();
            btnTransmit.Enabled = true;
            btn2.Enabled = true;
            btn3.Enabled = true;
        }
  1. cbCommPorts is the name i got for the Combo Box

    private void cbCommPorts_SelectedIndexChanged(object sender, EventArgs e) { int COM1 = cbCommPorts.SelectedIndex; int COM2 = cbCommPorts.SelectedIndex; int COM3 = cbCommPorts.SelectedIndex; Object selectedItem = serialPort1.PortName;

        MessageBox.Show("COM PORT: " + selectedItem.ToString() + " Selected");
    }
    

Is there any problem to my codes? Thanks..

3
One error in your logic is you populate the combobox in the button press. Assuming you want this to operate on a 'somewhat normal' manner you should load the items in say the form shown event and the button just opens the selected one. I also can't see why you've got COM1, COM2 and COM3 also assigned to the same value. - PeterJ
What Peter said :) Anyway I'd just like to add a question, are you using WPF or WinForms, that way we may be able to provide a better example - TimothyP
This is my 1 st time using combo box and i'm rather confuse on how i populate the combo box.. - user1670247
I'm using winforms. thanks - user1670247

3 Answers

6
votes

Here's one way you might use it with Windows Forms

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Load += Form1_Load;
    }

    void Form1_Load(object sender, EventArgs e)
    {
        var ports = SerialPort.GetPortNames();
        cmbSerialPorts.DataSource = ports;
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
        if (cmbSerialPorts.SelectedIndex > -1)
        {
            MessageBox.Show(String.Format("You selected port '{0}'", cmbSerialPorts.SelectedItem));
            Connect(cmbSerialPorts.SelectedItem.ToString());
        }
        else
        {
            MessageBox.Show("Please select a port first");
        }
    }

    private void Connect(string portName)
    {
        var port = new SerialPort(portName);
        if (!port.IsOpen)
        {
            port.BaudRate = 19200;
            port.Open();
            //Continue here....
        }
    }
}

That being said, unless you are maintaining legacy software, it might be a good idea to take a look at WPF. Learning how to use WPF instead of WinForms will ready you for development on Windows 8, Windows Phone etc... And the databinding features make what you're trying to do really easy.

0
votes

WPF Code style

public void comboBox_DropDownOpened(object sender, EventArgs e)
    {   
        string[] ports = SerialPort.GetPortNames();          
        foreach ( string comport in ports)
            {
                comboBox.Items.Add(comport);
            }
    }
.... /*Two control item combobox&button, comboxbox's item is COM port and   It's first argument of Function 「System.IO.Ports.SerialPort 」. Using (comboBox.text) */
private void button1_Click(object sender, RoutedEventArgs e)
    {

        System.IO.Ports.SerialPort Port = new SerialPort
            ((comboBox.Text), 115200, Parity.None, 8, StopBits.One);
        try
        {
            Port.Open();
            Port.Write(cmdByteArray, 0, cmdByteArray.Length );
        }
        catch { Exception ex; }
        Port.Read(readbyte, 0, readbyte.Length);
0
votes
private void comboBox_DropDownOpened(object sender, EventArgs e)
{
    string[] ports = SerialPort.GetPortNames();
    comboBox.Items.Clear();
    foreach (string comport in ports)
    {
        comboBox.Items.Add(comport);
    }
}

Addition to ysjia post.

If you not add .Clear(); everytime you click on it it will expand further. You will have duplicates.