Author Topic: Can't change com port via combobox on Modbus driver  (Read 888 times)

lonconao

  • Newbie
  • *
  • Posts: 11
    • View Profile
Can't change com port via combobox on Modbus driver
« on: September 28, 2020, 10:51:59 PM »
Good day
I have a problem setting com port on modbus rtu driver. I use the next code

Code: [Select]
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

Code: [Select]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim MBDriver As New AdvancedHMIDrivers.ModbusRTUCom

        MBDriver.PortName = ComboBox1.SelectedItem()
        MBDriver.BaudRate = 9600
        MBDriver.StationAddress = 1

        MsgBox("OK", MsgBoxStyle.Information, MBDriver.PortName)

    End Sub

COM ports are listed on a combobox correctly, but, when i try to apply selected com port to driver, it doesn't change. In AHMI v3.99y works perfectly. Now i using AHMI 3.99xR1

Any suggestion?

Additional

« Last Edit: September 29, 2020, 01:12:59 PM by lonconao »

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Can't configure manually com port on Modbus driver
« Reply #1 on: September 29, 2020, 01:34:55 AM »
Code: [Select]
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ports As String() = SerialPort.GetPortNames()

        If ports.Length = 0 Then
            ComboBox1.Items.Add("none found")
        Else
            For Each port As String In ports
                ComboBox1.Items.Add(port)
            Next
        End If

        ComboBox1.Sorted = True
        ComboBox1.SelectedIndex = 0
    End Sub

Code: [Select]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim MBDriver As New AdvancedHMIDrivers.ModbusRTUCom

        MBDriver.PortName = ComboBox1.SelectedItem.ToString
        MBDriver.BaudRate = 9600
        MBDriver.StationAddress = 1

        MsgBox("OK", MsgBoxStyle.Information, MBDriver.PortName)

    End Sub

lonconao

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Can't configure manually com port on Modbus driver
« Reply #2 on: September 29, 2020, 12:18:01 PM »
Thanks Godra for your response, i changed the first segment of code. In second segment, i analyzed variables with debugger and effectively function SelectItem returns an object. However, i used a cast with CSting (before your response) and problem still happens. I think that's a error relationed to component and event (only overwrite the value when event button_click is active).

Any form to evaluate expression MBDriver.PortName with debugger when event button_click changed the value?

Sorry for questions, i am c/c++ programer, i am fully newbie in VB.NET and C# (yet)


lonconao

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Can't configure manually com port on Modbus driver
« Reply #3 on: September 29, 2020, 12:23:37 PM »
If i configure the component from properties box, works well. But, from code on main, doesn't work. In attachment, i change COM port 4 to 6, and nothings happen (COM4 in this case, sends data)

lonconao

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Can't change com port via combobox on Modbus driver
« Reply #4 on: September 29, 2020, 01:45:37 PM »
I found the problem  :o. When i created a new object ModBusRTUCom1, i just reference that name on click button and the other components in form doesn't "know" what is the name of this new component (there was already another object called ModbusRTUCom1 in the form designer), then they did not receive data.
« Last Edit: September 29, 2020, 04:33:50 PM by lonconao »