Author Topic: Edit driver between child forms  (Read 1353 times)

olem

  • Newbie
  • *
  • Posts: 3
    • View Profile
Edit driver between child forms
« on: September 28, 2018, 12:04:01 PM »
Hello, I am having some trouble changing the comport from a child form in a mdi.

"mainform" - mdiparent
|_"oppsettForm"  - Setup
|_"husForm"  - the page that is displaying the components. (modbus rtu driver is on this form)

I have made a function on "husForm" that when called updates the settings for the driver.

Public Sub ReceiveValue(ByVal BaudRate As String, ByVal cbComport As String, ByVal tbBaud As String, ByVal tbDataBits As String, ByVal Parity As String, ByVal tbPollrate As
                                     String, ByVal tbAddresse As String, ByVal StopBits As String)
        Me.ModbusRTUCom1.BaudRate = BaudRate
        Me.ModbusRTUCom1.PortName = cbComport
        Me.ModbusRTUCom1.BaudRate = tbBaud
        Me.ModbusRTUCom1.DataBits = tbDataBits
        Me.ModbusRTUCom1.Parity = Parity
        Me.ModbusRTUCom1.PollRateOverride = tbPollrate
        Me.ModbusRTUCom1.StationAddress = tbAddresse
        Me.ModbusRTUCom1.StopBits = StopBits

        ' MsgBox(cbComport)    '''''This is just to see if i can display the value sendt to the function. (It does show the value i pass to the function)
    End Sub


On the setup page i call the function.

husForm.ReceiveValue(tbBaud.Text, cbComport.Text, tbBaud.Text, tbDataBits.Text, Parity, tbPollrate.Text, tbAddresse.Text, StopBits)


However it does not update the comport. It updates the baud rate and other stuff.

I tried to make a button that gets the values from  ModbusRTUCom1.(baud,parity and so on), but the text does not update when i update the driver. It only list up the default values that i set in Visual Studio.

Tried to set  modifiers ModbusRTUCom1 to Public, still no luck.


Any help would be appreciated. :)

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5323
    • View Profile
    • AdvancedHMI
Re: Edit driver between child forms
« Reply #1 on: September 28, 2018, 08:30:39 PM »
Try adding this:

Me.ModbusRTUCom1.BeginInit()
.
.
.
.
Me.ModbusRTUCom1.EndInit()

olem

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Edit driver between child forms
« Reply #2 on: September 29, 2018, 02:07:24 AM »
Did not get this to work.

If i make a button inside the form where the driver is located. Then I can change the driver, and the label will show the new value with the same code as in the funcion.

    Private Sub BasicButton1_Click(sender As Object, e As EventArgs) Handles BasicButton1.Click
        ModbusRTUCom1.PortName = "COM4"
    End Sub


Sendt the code to your inbox if you would like to take a look.

Godra

  • Hero Member
  • *****
  • Posts: 1448
    • View Profile
Re: Edit driver between child forms
« Reply #3 on: September 29, 2018, 06:57:12 PM »
You might try disabling the subscriptions for the driver before you call the function and then enabling those.

Code: [Select]
     husForm.ModbusRTUCom1.DisableSubscriptions = True
     husForm.ReceiveValue(tbBaud.Text, cbComport.Text, tbBaud.Text, tbDataBits.Text, Parity, tbPollrate.Text, tbAddresse.Text, StopBits)
     husForm.ModbusRTUCom1.DisableSubscriptions = False

Or as a test, try setting the port name directly from the other form after you call the function:

Code: [Select]
     husForm.ReceiveValue(tbBaud.Text, cbComport.Text, tbBaud.Text, tbDataBits.Text, Parity, tbPollrate.Text, tbAddresse.Text, StopBits)
     husForm.ModbusRTUCom1.PortName = cbComport.Text

« Last Edit: September 29, 2018, 07:07:55 PM by Godra »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5323
    • View Profile
    • AdvancedHMI
Re: Edit driver between child forms
« Reply #4 on: September 29, 2018, 07:17:39 PM »
I tried this by adding a breakpoint at this line of code:

        Me.ModbusRTUCom1.PortName = cbComport

- Set the PortName in design view to COM1
- Ran the application and when the box popped up, I changed to comboBox from COM1 to COM3
- When it stopped at the break point, I hovered over the drivers PortName property and it was COM1
- I then stepped over the line of code and check the PortName Property again. It was now changed to COM3

The only possible difference is that I don't have a real device to establish communication with.

Godra

  • Hero Member
  • *****
  • Posts: 1448
    • View Profile
Re: Edit driver between child forms
« Reply #5 on: September 29, 2018, 08:02:25 PM »
The attached pictures show an example of using the MainMenu driven forms and a ModbusRTUCom driver added to the MainForm and communicating with 2 instances of the MODRSsim2 simulator via com0com paired ports (COM1-COM11 and COM4-COM12).

The COM port name is changed with every click of the button from the Page2 form, the code for which is:

Code: [Select]
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If MainForm.ModbusRTUCom1.PortName = "COM11" Then
            MainForm.ModbusRTUCom1.PortName = "COM12"
        Else
            MainForm.ModbusRTUCom1.PortName = "COM11"
        End If
    End Sub
« Last Edit: September 29, 2018, 10:41:40 PM by Godra »

olem

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Edit driver between child forms
« Reply #6 on: September 30, 2018, 06:52:23 AM »
Tank you for all the suggestions. Never got this too work with this solution. I think it is a permission issue between two different child forms from the same parent.
My solution is to just call the setup form from the form where the driver is located. In this way the form with the driver is the parent form for the setup form.

Button.click() on the form where the driver is located
        ' Åpne en dialogboks med innstillingene
        If oppsettForm.ShowDialog <> DialogResult.OK Then
            MessageBox.Show("Ingenting ble lagert!", "Innstillinger", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Else
            MessageBox.Show("De nye innstillingene ble lagert!", "Innstillinger", MessageBoxButtons.OK, MessageBoxIcon.Information)

            ' Endre hva Parity vises som
            Dim Parity As String = 0
            Select Case oppsettForm.cbParity.Text
                Case "None"
                    Parity = 0
                Case "Odd"
                    Parity = 1
                Case "Even"
                    Parity = 2
                Case "Mark"
                    Parity = 3
                Case "Space"
                    Parity = 4
            End Select

            ' Endre hva StopBits vises som
            Dim StopBits As String = 0
            Select Case oppsettForm.cbStopBits.Text
                Case "None"
                    StopBits = 0
                Case "One"
                    StopBits = 1
                Case "Two"
                    StopBits = 2
                Case "OnePointFife"
                    StopBits = 3
            End Select

            Me.ModbusRTUCom1.BaudRate = oppsettForm.tbBaud.Text
            Me.ModbusRTUCom1.PortName = oppsettForm.cbComport.Text
            Me.ModbusRTUCom1.DataBits = oppsettForm.tbDataBits.Text
            Me.ModbusRTUCom1.Parity = Parity
            Me.ModbusRTUCom1.PollRateOverride = oppsettForm.tbPollrate.Text
            Me.ModbusRTUCom1.StationAddress = oppsettForm.tbAddresse.Text
            Me.ModbusRTUCom1.StopBits = StopBits

        End If