In my application I don't to hardcode the serial port number into my code but I want to be able to select the port from a combobox and the start the connection.
So I have created a combobox and populate it with the available com ports. Then I added a button and as soon as I selected the appropriate com port I use it to read from the address I need and write the value to DigitalPanelMeter.
I need to press the button every time I want to read. Is it possible to poll the slave every 500ms as it's done the ModbusRTUCom driver is added in the designer?
Do I need to create an event to achieve this?
I'm not an expert in VB.NET (yet) so any help will be appreciated. Below is my code.
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ports As String() = SerialPort.GetPortNames()
Dim port As String
For Each port In ports
ComboBox1.Items.Add(port)
Next port
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ModbusDriverXR80CX As New ModbusRTUCom
ModbusDriverXR80CX.PortName = ComboBox1.SelectedItem
ModbusDriverXR80CX.BaudRate = 9600
ModbusDriverXR80CX.StationAddress = 5
DigitalPanelMeter1.Value = ModbusDriverXR80CX.Read("40257")
End Sub