Author Topic: How to change PLCAddressItems of the ChartBySamplin by code and display series()  (Read 675 times)

sederevichus

  • Newbie
  • *
  • Posts: 3
    • View Profile
Good day.
I try to change this by ComboBox.
Code: [Select]
Private Sub CheckBox6_CheckStateChanged(sender As Object, e As EventArgs) Handles CheckBox6.CheckStateChanged
        If CheckBox6.CheckState Then
            ChartBySampling1.Series(9).Enabled = True
        Else
            ChartBySampling1.Series(9).Enabled = False
        End If
    End Sub

 Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.SelectedItem = "Iген. (А)" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F428731")
            BasicLabel44.PLCAddressValue = ("F428731")
        End If
        If ComboBox1.SelectedItem = "out FCR" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F429285") 'df307
            BasicLabel44.PLCAddressValue = ("F429285")
        End If
        If ComboBox1.SelectedItem = "out AVR" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F429325") 'df327
            BasicLabel44.PLCAddressValue = ("F429325")
        End If
        Label69.Text = ChartBySampling1.PLCAddressItems.Item(9).PLCAddress
    End Sub

If I manualy set PLCAddress of the Item (01.png) ChartBySampling operate normaly and series(9) display correct. But when I try to use ComboBox selection - it will error (error.png)

If I delete PLCAddress of the Item (02.png) ChartBySampling don't display the series(9), but after ComboBox selection PLCAddress is change (Label69.Text)

What is the problem?

Godra

  • Hero Member
  • *****
  • Posts: 1438
    • View Profile
Subscriptions to PLCAddresses are created at runtime, that's why it works when you enter it manually.

Changing PLCAddress via combo box will not trigger new subscription.

Here is a temporary fix that's applicable only for what you are trying to do:

1) Change your code to this:

Code: [Select]
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.SelectedItem = "Iген. (А)" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F428731")
            ChartBySampling1.SubscribeToComDriver(9, "F428731")
            BasicLabel44.PLCAddressValue = ("F428731")
        End If
        If ComboBox1.SelectedItem = "out FCR" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F429285") 'df307
            ChartBySampling1.SubscribeToComDriver(9, "F429285")
            BasicLabel44.PLCAddressValue = ("F429285")
        End If
        If ComboBox1.SelectedItem = "out AVR" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F429325") 'df327
            ChartBySampling1.SubscribeToComDriver(9, "F429325")
            BasicLabel44.PLCAddressValue = ("F429325")
        End If
        Label69.Text = ChartBySampling1.PLCAddressItems.Item(9).PLCAddress
    End Sub

2) Change SubscribeToComDriver sub, inside the ChartBySampling, to this:

Code: [Select]
    Private SubscriptionsCreated As Boolean
    Private additionalSubscriptionsCreated As Boolean
    Public Sub SubscribeToComDriver(Optional ByVal indx As Integer = -1, Optional ByVal plcAddress As String = "")
        If Not DesignMode And IsHandleCreated Then
            If indx <> -1 AndAlso plcAddress <> "" AndAlso SubscriptionsCreated Then
                If additionalSubscriptionsCreated Then
                    SubScriptions.SubscriptionList.RemoveAt(SubScriptions.SubscriptionList.Count - 1)
                    SubScriptions.SubscribeTo(m_PLCAddressItems(indx).PLCAddress, m_PLCAddressItems(indx).NumberOfElements, AddressOf PolledDataReturned, m_PLCAddressItems(indx).PLCAddress, 1, 0)
                Else
                    SubScriptions.SubscribeTo(m_PLCAddressItems(indx).PLCAddress, m_PLCAddressItems(indx).NumberOfElements, AddressOf PolledDataReturned, m_PLCAddressItems(indx).PLCAddress, 1, 0)
                    additionalSubscriptionsCreated = True
                End If
                Exit Sub
            End If

            If Not SubscriptionsCreated Then
                '* Create a subscription handler object
                If SubScriptions Is Nothing Then
                    SubScriptions = New SubscriptionHandler
                    SubScriptions.Parent = Me
                    AddHandler SubScriptions.DisplayError, AddressOf DisplaySubscribeError
                End If
                SubScriptions.ComComponent = m_ComComponent

                Dim index As Integer
                While index < m_PLCAddressItems.Count
                    If Not String.IsNullOrEmpty(m_PLCAddressItems(index).PLCAddress) Then
                        SubScriptions.SubscribeTo(m_PLCAddressItems(index).PLCAddress, m_PLCAddressItems(index).NumberOfElements, AddressOf PolledDataReturned, m_PLCAddressItems(index).PLCAddress, 1, 0)
                    End If
                    index += 1
                End While
                SubscriptionsCreated = True
            End If
            SubScriptions.SubscribeAutoProperties()

        End If
    End Sub

You can see in the attached picture how the graph for series10 (which is series(9)) is changing as the combo box selection is changed.
I set register values for "Iген. (А)" / "out FCR" / "out AVR" to: 2.2225 / 0.75 / 0.

Unless Archie decides to implement some changes, you will have to stick with this temporary fix or change your project to work with the original ChartBySampling control.
« Last Edit: June 05, 2019, 03:39:13 AM by Godra »

sederevichus

  • Newbie
  • *
  • Posts: 3
    • View Profile
Subscriptions to PLCAddresses are created at runtime, that's why it works when you enter it manually.

Changing PLCAddress via combo box will not trigger new subscription.

Here is a temporary fix that's applicable only for what you are trying to do:

1) Change your code to this:

Code: [Select]
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.SelectedItem = "Iген. (А)" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F428731")
            ChartBySampling1.SubscribeToComDriver(9, "F428731")
            BasicLabel44.PLCAddressValue = ("F428731")
        End If
        If ComboBox1.SelectedItem = "out FCR" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F429285") 'df307
            ChartBySampling1.SubscribeToComDriver(9, "F429285")
            BasicLabel44.PLCAddressValue = ("F429285")
        End If
        If ComboBox1.SelectedItem = "out AVR" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F429325") 'df327
            ChartBySampling1.SubscribeToComDriver(9, "F429325")
            BasicLabel44.PLCAddressValue = ("F429325")
        End If
        Label69.Text = ChartBySampling1.PLCAddressItems.Item(9).PLCAddress
    End Sub

2) Change SubscribeToComDriver sub, inside the ChartBySampling, to this:

Code: [Select]
    Private SubscriptionsCreated As Boolean
    Private additionalSubscriptionsCreated As Boolean
    Public Sub SubscribeToComDriver(Optional ByVal indx As Integer = -1, Optional ByVal plcAddress As String = "")
        If Not DesignMode And IsHandleCreated Then
            If indx <> -1 AndAlso plcAddress <> "" AndAlso SubscriptionsCreated Then
                If additionalSubscriptionsCreated Then
                    SubScriptions.SubscriptionList.RemoveAt(SubScriptions.SubscriptionList.Count - 1)
                    SubScriptions.SubscribeTo(m_PLCAddressItems(indx).PLCAddress, m_PLCAddressItems(indx).NumberOfElements, AddressOf PolledDataReturned, m_PLCAddressItems(indx).PLCAddress, 1, 0)
                Else
                    SubScriptions.SubscribeTo(m_PLCAddressItems(indx).PLCAddress, m_PLCAddressItems(indx).NumberOfElements, AddressOf PolledDataReturned, m_PLCAddressItems(indx).PLCAddress, 1, 0)
                    additionalSubscriptionsCreated = True
                End If
                Exit Sub
            End If

            If Not SubscriptionsCreated Then
                '* Create a subscription handler object
                If SubScriptions Is Nothing Then
                    SubScriptions = New SubscriptionHandler
                    SubScriptions.Parent = Me
                    AddHandler SubScriptions.DisplayError, AddressOf DisplaySubscribeError
                End If
                SubScriptions.ComComponent = m_ComComponent

                Dim index As Integer
                While index < m_PLCAddressItems.Count
                    If Not String.IsNullOrEmpty(m_PLCAddressItems(index).PLCAddress) Then
                        SubScriptions.SubscribeTo(m_PLCAddressItems(index).PLCAddress, m_PLCAddressItems(index).NumberOfElements, AddressOf PolledDataReturned, m_PLCAddressItems(index).PLCAddress, 1, 0)
                    End If
                    index += 1
                End While
                SubscriptionsCreated = True
            End If
            SubScriptions.SubscribeAutoProperties()

        End If
    End Sub

You can see in the attached picture how the graph for series10 (which is series(9)) is changing as the combo box selection is changed.
I set register values for "Iген. (А)" / "out FCR" / "out AVR" to: 2.2225 / 0.75 / 0.

Unless Archie decides to implement some changes, you will have to stick with this temporary fix or change your project to work with the original ChartBySampling control.

Thank you!

Godra

  • Hero Member
  • *****
  • Posts: 1438
    • View Profile
I just noticed that this line has an optional parameter which is not really used (it was left there as I was experimenting and just forgot to remove it before posting the code here):

          Public Sub SubscribeToComDriver(Optional ByVal indx As Integer = -1, Optional ByVal plcAddress As String = "")

plcAddress is not really required but the code will still work as it is.

If removed then the code will have to look like this:

Code: [Select]
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.SelectedItem = "Iген. (А)" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F428731")
            ChartBySampling1.SubscribeToComDriver(9)
            BasicLabel44.PLCAddressValue = ("F428731")
        End If
        If ComboBox1.SelectedItem = "out FCR" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F429285") 'df307
            ChartBySampling1.SubscribeToComDriver(9)
            BasicLabel44.PLCAddressValue = ("F429285")
        End If
        If ComboBox1.SelectedItem = "out AVR" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F429325") 'df327
            ChartBySampling1.SubscribeToComDriver(9)
            BasicLabel44.PLCAddressValue = ("F429325")
        End If
        Label69.Text = ChartBySampling1.PLCAddressItems.Item(9).PLCAddress
    End Sub

and this:

Code: [Select]
    Private SubscriptionsCreated As Boolean
    Private additionalSubscriptionsCreated As Boolean
    Public Sub SubscribeToComDriver(Optional ByVal indx As Integer = -1)
        If Not DesignMode And IsHandleCreated Then
            If indx <> -1 AndAlso SubscriptionsCreated Then
                If additionalSubscriptionsCreated Then
                    SubScriptions.SubscriptionList.RemoveAt(SubScriptions.SubscriptionList.Count - 1)
                    SubScriptions.SubscribeTo(m_PLCAddressItems(indx).PLCAddress, m_PLCAddressItems(indx).NumberOfElements, AddressOf PolledDataReturned, m_PLCAddressItems(indx).PLCAddress, 1, 0)
                Else
                    SubScriptions.SubscribeTo(m_PLCAddressItems(indx).PLCAddress, m_PLCAddressItems(indx).NumberOfElements, AddressOf PolledDataReturned, m_PLCAddressItems(indx).PLCAddress, 1, 0)
                    additionalSubscriptionsCreated = True
                End If
                Exit Sub
            End If

            If Not SubscriptionsCreated Then
                '* Create a subscription handler object
                If SubScriptions Is Nothing Then
                    SubScriptions = New SubscriptionHandler
                    SubScriptions.Parent = Me
                    AddHandler SubScriptions.DisplayError, AddressOf DisplaySubscribeError
                End If
                SubScriptions.ComComponent = m_ComComponent

                Dim index As Integer
                While index < m_PLCAddressItems.Count
                    If Not String.IsNullOrEmpty(m_PLCAddressItems(index).PLCAddress) Then
                        SubScriptions.SubscribeTo(m_PLCAddressItems(index).PLCAddress, m_PLCAddressItems(index).NumberOfElements, AddressOf PolledDataReturned, m_PLCAddressItems(index).PLCAddress, 1, 0)
                    End If
                    index += 1
                End While
                SubscriptionsCreated = True
            End If
            SubScriptions.SubscribeAutoProperties()

        End If
    End Sub

sederevichus

  • Newbie
  • *
  • Posts: 3
    • View Profile
I just noticed that this line has an optional parameter which is not really used (it was left there as I was experimenting and just forgot to remove it before posting the code here):

          Public Sub SubscribeToComDriver(Optional ByVal indx As Integer = -1, Optional ByVal plcAddress As String = "")

plcAddress is not really required but the code will still work as it is.

If removed then the code will have to look like this:

Code: [Select]
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.SelectedItem = "Iген. (А)" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F428731")
            ChartBySampling1.SubscribeToComDriver(9)
            BasicLabel44.PLCAddressValue = ("F428731")
        End If
        If ComboBox1.SelectedItem = "out FCR" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F429285") 'df307
            ChartBySampling1.SubscribeToComDriver(9)
            BasicLabel44.PLCAddressValue = ("F429285")
        End If
        If ComboBox1.SelectedItem = "out AVR" Then
            ChartBySampling1.PLCAddressItems.Item(9).PLCAddress = ("F429325") 'df327
            ChartBySampling1.SubscribeToComDriver(9)
            BasicLabel44.PLCAddressValue = ("F429325")
        End If
        Label69.Text = ChartBySampling1.PLCAddressItems.Item(9).PLCAddress
    End Sub

and this:

Code: [Select]
    Private SubscriptionsCreated As Boolean
    Private additionalSubscriptionsCreated As Boolean
    Public Sub SubscribeToComDriver(Optional ByVal indx As Integer = -1)
        If Not DesignMode And IsHandleCreated Then
            If indx <> -1 AndAlso SubscriptionsCreated Then
                If additionalSubscriptionsCreated Then
                    SubScriptions.SubscriptionList.RemoveAt(SubScriptions.SubscriptionList.Count - 1)
                    SubScriptions.SubscribeTo(m_PLCAddressItems(indx).PLCAddress, m_PLCAddressItems(indx).NumberOfElements, AddressOf PolledDataReturned, m_PLCAddressItems(indx).PLCAddress, 1, 0)
                Else
                    SubScriptions.SubscribeTo(m_PLCAddressItems(indx).PLCAddress, m_PLCAddressItems(indx).NumberOfElements, AddressOf PolledDataReturned, m_PLCAddressItems(indx).PLCAddress, 1, 0)
                    additionalSubscriptionsCreated = True
                End If
                Exit Sub
            End If

            If Not SubscriptionsCreated Then
                '* Create a subscription handler object
                If SubScriptions Is Nothing Then
                    SubScriptions = New SubscriptionHandler
                    SubScriptions.Parent = Me
                    AddHandler SubScriptions.DisplayError, AddressOf DisplaySubscribeError
                End If
                SubScriptions.ComComponent = m_ComComponent

                Dim index As Integer
                While index < m_PLCAddressItems.Count
                    If Not String.IsNullOrEmpty(m_PLCAddressItems(index).PLCAddress) Then
                        SubScriptions.SubscribeTo(m_PLCAddressItems(index).PLCAddress, m_PLCAddressItems(index).NumberOfElements, AddressOf PolledDataReturned, m_PLCAddressItems(index).PLCAddress, 1, 0)
                    End If
                    index += 1
                End While
                SubscriptionsCreated = True
            End If
            SubScriptions.SubscribeAutoProperties()

        End If
    End Sub


Everything is working.
Wonderful.
Thank you again!