Author Topic: Manually select serial for port for Modbus RTU  (Read 1651 times)

kallileo

  • Newbie
  • *
  • Posts: 7
    • View Profile
Manually select serial for port for Modbus RTU
« on: March 25, 2015, 12:35:23 PM »
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

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5270
    • View Profile
    • AdvancedHMI
Re: Manually select serial for port for Modbus RTU
« Reply #1 on: March 25, 2015, 12:49:46 PM »
- Add a ModbusRTUCom to the form
- Set the PollRateOverride to 500
- Add a DataSubscriber and set PLCAddressValue to 40257
- Double click the DataSubscriber and it will take you to an event handler that fires any time the value changes