AdvancedHMI Software
General Category => Support Questions => Topic started by: richmo 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.
-
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
-
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"
-
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
-
Any Luck?
-
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
-
OK, I got it working. Thank you for your help. I added
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.
-
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