Author Topic: How to add a serial port selection dropdown or menu  (Read 1830 times)

richmo

  • Newbie
  • *
  • Posts: 4
    • View Profile
How to add a serial port selection dropdown or menu
« on: September 28, 2022, 06:13:53 AM »
Hello,

Newbie here. I've made a simple HMI which displays some values from a custom device over MODBUS RTU. This works fine, but I need a way for a user to be able to select a com port as the number may be different on a different PC.

Is there a simple way to do this? Either a dropdown or a popup menu would be ok.

alanls1

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: How to add a serial port selection dropdown or menu
« Reply #1 on: September 28, 2022, 09:05:52 AM »
Here is some sample code, let me know if you need more.

I created a basic button on the form to click on. (you can change it later)
Also put the "ComboBox1_SelectedValueChanged" event at the end when they select some port.
I always find it easier to select the component at the top,  then select the event, and let it create the event code for you.

Alan.


  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try

            GetSerialPortNames()
        Catch ex As Exception

        End Try
    End Sub

    Sub GetSerialPortNames()


        ' Show all available COM ports.
        For Each sp As String In My.Computer.Ports.SerialPortNames

            'Put them in a combo box
            ComboBox1.Items.Add(sp)
        Next

    End Sub

    Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged

        'When they make a selection code here.

    End Sub

richmo

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to add a serial port selection dropdown or menu
« Reply #2 on: September 28, 2022, 09:31:17 AM »
Thank you. I'm not quite following what do to there. Do you add a button and a combo box to the main dialog?
 I don't know how to "Also put the "ComboBox1_SelectedValueChanged" event at the end when they select some port"

alanls1

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: How to add a serial port selection dropdown or menu
« Reply #3 on: September 28, 2022, 09:58:30 AM »
I could show you real quick if you have teamviewer installed. 8435321633.

I drag a button to the form, then a combobox, right click on the form & select view code, then from the drop down list up top select the component (Combox1) then on top right select the event(Combobox1_selectedvaluechanged).  this will put in create the code for you.

Alan

alanls1

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: How to add a serial port selection dropdown or menu
« Reply #4 on: September 28, 2022, 06:42:29 PM »
Any Luck?

richmo

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to add a serial port selection dropdown or menu
« Reply #5 on: September 29, 2022, 01:10:14 PM »
Not yet. I will try again later this evening. Though I do not know how to tell the driver to actually change port when "ComboBox1.SelectedValueChanged" is triggered

richmo

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to add a serial port selection dropdown or menu
« Reply #6 on: October 02, 2022, 02:16:39 PM »
OK, I got it working. Thank you for your help. I added
Code: [Select]
ModbusRTUCom1.PortName = ComboBox_SerialPortList.SelectedItem
It does the trick, but if I select a serial port not connected to my device, or I unplug my device, the program becomes very laggy.

I presume this is due to it trying and then timing out after 3s, but how do I stop it from continuously retrying?
For example, just load a message saying "No connection" and then only retry when I press the button.

alanls1

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: How to add a serial port selection dropdown or menu
« Reply #7 on: November 02, 2022, 09:06:37 AM »
You could try this.

 Private Sub SerialPort1_ErrorReceived(sender As Object, e As SerialErrorReceivedEventArgs) Handles SerialPort1.ErrorReceived
        Try
            SerialPort1.Close()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles BtnConnect.Click
        Try
            'Remove all the Items in the ComboBox
            For i As Integer = 0 To ComboBox1.Items.Count - 1
                ComboBox1.Items.RemoveAt(i)
            Next
            'Add all the live comports back to the comboBox
            For Each sp As String In My.Computer.Ports.SerialPortNames
                ComboBox1.Items.Add(sp)
            Next

            Try
                'Re Open the com port
                SerialPort1.Open()
            Catch ex As Exception

            End Try
        Catch ex As Exception

        End Try


    End Sub
« Last Edit: November 02, 2022, 09:08:11 AM by alanls1 »