Author Topic: DataSubscriber 2  (Read 2529 times)

maxketcham

  • Newbie
  • *
  • Posts: 20
    • View Profile
DataSubscriber 2
« on: February 04, 2016, 01:31:10 PM »
OK I'm going to try this again. My company just upgraded their server and I can't send or receive any attachment period so I'll have to cut and paste the code. The problem I am having is I'm trying to add items using a loop and the  code  DataSubscriber21.PLCAddressValueItems.Add(New AdvancedHMIDrivers.PLCAddressItem() With {.SubscriptionID = CInt(records(x, 3)) - 1, .PLCAddress = records(x, 1)}) where Records is a string array containing the address I want to look at. I'm using the OPC driver and the program works fine except for one thing, It is taking 4.5 hours to add 772 items to the subscriber? I also just added this code a = e.PlcAddress.ToString & " = " & e.Values(0).ToString & "; subscription ID = " & e.SubscriptionID & vbCrLf & TextBox1.Text and I think that it has shown me what the problem is, but I don't know how to cure it. It seems that when you add an Item to the datasubscriber, it is using soe type of recursion to add it? anyways what I see is an output to the textbox of item 1 printed, item 1 printed, item 1 item two printed, item 1 printed, item 1 item 2 printed, item 1 item 2 1tem 3 printed, ant it progresses that way for each item I add. I'll paste all the code from main form below, the module named tracking only has 1 textbox on it and the data file is comma delimited and consists of two items per line of 772 lines, I'll include a 12 item list at end for sample purposes, but you'll have to make your own as OPC will have to be working for this example to run and the tags will have to exist on that opc server. Could someone tell what is going on and how to fix it. Aslo I've been programing since dartmouth basic came out so please lay off my style comment on on actual errors in code, not how I program. LOL


Public Class MainForm
    '*******************************************************************************
    '* Stop polling when the form is not visible in order to reduce communications
    '* Copy this section of code to every new form created
    '*******************************************************************************
    Private FinishedStartUp As Boolean = False
    Private start_time As DateTime
    Private stop_time As DateTime
    Private WriteToSQL As Boolean = False
    Dim elapsed_time As TimeSpan
    Dim firstscan As Boolean = False
    Private First_start_time As DateTime
    Private AlarmArray(2001) As String
    Private WriteToTracking As Boolean = False
    '*******************************************************************************
    '* Stop polling when the form is not visible in order to reduce communications
    '* Copy this section of code to every new form created
    '*******************************************************************************
    Private NotFirstShow As Boolean
    Public records(773, 4) As String
    Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Tracking.Hide()
        Me.Hide()
        AdvancedHMIDrivers.Utilities.StopComsOnHidden(components, Me)
        DataSubscriber21.PLCAddressValueItems.Clear()
        OpcDaCom1.DisableSubscriptions = True
        OpcDaCom1.Dispose()
        Tracking.Dispose()
        Me.Dispose()
    End Sub
    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles Me.Load
        First_start_time = Now
        Dim TempText As String
        Tracking.Show()
        Dim a As String = ""
        Dim b As Integer = 0
        ReadCSV()
        DataSubscriber21.PLCAddressValueItems.Clear()
        For x = 1 To 10 Step 1
            If records(x, 2) <> "" Then
                DataSubscriber21.PLCAddressValueItems.Add(New AdvancedHMIDrivers.PLCAddressItem() With {.SubscriptionID = CInt(records(x, 3)) - 1, .PLCAddress = records(x, 1)})
                TempText = Tracking.TextBox1.Text
                Tracking.TextBox1.Text = x.ToString & " - Tag name = " & records(x, 1) & ", error description = " & records(x, 2) & ", Subscription Id = " & records(x, 3) & vbCrLf & TempText
                Tracking.TextBox1.Refresh()
                Me.Refresh()
            End If
        Next x
        stop_time = Now
        Dim elapsed_Time As TimeSpan = DateTime.Parse(stop_time.ToString).Subtract(DateTime.Parse(First_start_time.ToString))
        TempText = Tracking.TextBox1.Text
        Me.Refresh()
        Tracking.TextBox1.Text = "Time to load all parameters was" & elapsed_Time.ToString & vbCrLf & TempText
        Me.Refresh()
    End Sub
    Private Sub ReadCSV() 'FileName As String)
        Try
            Dim WholeFile As String = ""
            Dim LineData() As String
            Dim FieldData() As String
            Dim ParmCount As Integer = 1
            Dim VarCount As Integer = 1
            Dim FilePath As String = "C:\work\Alarm message lean.csv"
            WholeFile = My.Computer.FileSystem.ReadAllText(FilePath)
            LineData = Split(WholeFile, vbNewLine)
            For Each LineOfText As String In LineData
                VarCount = 1
                FieldData = LineOfText.Split(",")
                For Each WordOfText As String In FieldData
                    records(ParmCount, VarCount) = WordOfText
                    VarCount += 1
                Next WordOfText
                records(ParmCount, 3) = (ParmCount - 1).ToString
                ParmCount += 1
            Next LineOfText
        Catch EX As Exception
            Dim A As String
            Dim CurrentDateTime As DateTime = DateTime.Now
            Dim TimeStamp As String = vbCrLf & "current date and time = " & CurrentDateTime.ToString("yyyy/MM/dd - HH:mm:ss.ffffff") & vbCrLf
            Dim ErrorLocation As String = "In - Form1 Private Sub ReadCSV(FileName As String)" & vbCrLf
            Dim ErrorString As String = " current error = " & ErrorToString() & vbCrLf
            Dim B As String = Tracking.TextBox1.Text
            A = TimeStamp & ErrorLocation & ErrorString & B & vbCrLf
            Tracking.TextBox1.Text = ""
            Tracking.TextBox1.Text = A
        End Try
    End Sub
    Private Sub INSERTDATA(ByVal MAIN_ASSY_VAR As String, ByVal SUB_ASSY_VAR As String, ByVal SUB_ASSY_NUM_VAR As String, ByVal CURRENT_STATE_VAR As String, ByVal SENDER_NAME_VAR As String, ByVal SENDER_VALUE_VAR As String, ByVal FULL_DATE_TIME_VAR As String, THIS_DATE_VAR As String, ByVal THIS_TIME_VAR As String, ByVal THIS_ID_VAR As String)
        If WriteToSQL = False Then Exit Sub
        Try
            Dim MyConnect As New SqlClient.SqlConnection
            MyConnect.ConnectionString = "Data Source=MESTEST\ONLINE;Integrated Security=False;User ID=SPIRE_SUN_SIMULATOR;Password=SPIRE_SUN_SIMULATOR;Connect TimeOut=15;Encrypt=False;TrustServerCertificate=False"
            Dim MyCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand()
            Dim TodayTime1 As DateTime = Now
            Dim ID As Guid
            ID = Guid.NewGuid
            MyCommand.Connection = MyConnect
            MyCommand.CommandText = "INSERT INTO dbo.MACHINE_TIME (MAIN_ASSY, SUB_ASSY, SUB_ASSY_NUM, CURRENT_STATE, SENDER_NAME, SENDER_VALUE, FULL_DATE_TIME, THIS_DATE, THIS_TIME, THIS_ID) VALUES (@MAIN_ASSY, @SUB_ASSY, @SUB_ASSY_NUM, @CURRENT_STATE, @SENDER_NAME, @SENDER_VALUE, @FULL_DATE_TIME, @THIS_DATE, @THIS_TIME, @THIS_ID)"
            MyConnect.Open()
            MyCommand.Parameters.AddWithValue("@MAIN_ASSY", MAIN_ASSY_VAR)
            MyCommand.Parameters.AddWithValue("@SUB_ASSY", SUB_ASSY_VAR)
            MyCommand.Parameters.AddWithValue("@SUB_ASSY_NUM", SUB_ASSY_NUM_VAR)
            MyCommand.Parameters.AddWithValue("@CURRENT_STATE", CURRENT_STATE_VAR)
            MyCommand.Parameters.AddWithValue("@SENDER_NAME", SENDER_NAME_VAR)
            MyCommand.Parameters.AddWithValue("@SENDER_VALUE", SENDER_VALUE_VAR)
            MyCommand.Parameters.AddWithValue("@FULL_DATE_TIME", FULL_DATE_TIME_VAR)
            MyCommand.Parameters.AddWithValue("@THIS_DATE", THIS_DATE_VAR)
            MyCommand.Parameters.AddWithValue("@THIS_TIME", THIS_TIME_VAR)
            MyCommand.Parameters.AddWithValue("@THIS_ID", THIS_ID_VAR)
            MyCommand.ExecuteNonQuery()
            MyConnect.Close()
        Catch EX As Exception
            Dim A As String
            Dim CurrentDateTime As DateTime = DateTime.Now
            Dim TimeStamp As String = vbCrLf & "current date and time = " & CurrentDateTime.ToString("yyyy/MM/dd - HH:mm:ss.ffffff") & vbCrLf
            Dim ErrorLocation As String = "In - Form1 Private Sub InsertData()" & vbCrLf
            Dim ErrorString As String = " current error = " & ErrorToString() & vbCrLf
            Dim B As String = TextBox1.Text
            A = TimeStamp & ErrorLocation & ErrorString & B & vbCrLf
            TextBox1.Text = ""
            TextBox1.Text = A
        End Try
    End Sub
    Private Sub Form_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged
        If NotFirstShow Then
            AdvancedHMIDrivers.Utilities.StopComsOnHidden(components, Me)
        Else
            NotFirstShow = True
        End If
    End Sub
    Private Sub DataSubscriber21_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber21.DataChanged
        Dim a As String
        Dim temptext As String = TextBox1.Text
        a = e.PlcAddress.ToString & " = " & e.Values(0).ToString & "; subscription ID = " & e.SubscriptionID & vbCrLf & TextBox1.Text
        stop_time = Now
        TextBox1.Text = a & vbCrLf & temptext
        Dim elapsed_Time As TimeSpan = DateTime.Parse(stop_time.ToString).Subtract(DateTime.Parse(First_start_time.ToString))
        temptext = TextBox1.Text
        TextBox1.Text = "Elapsed time = was" & elapsed_Time.ToString & vbCrLf & temptext
        Me.Refresh()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        End
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        WriteToSQL = Not WriteToSQL
        If WriteToSQL = True Then
            Button3.BackColor = Color.Blue
        Else
            Button3.BackColor = Color.Red
        End If
    End Sub
End Class


Module Line Omron PLCs.Lay Up 01.Alarm
  • .Alarm[0000],[0]ETHERCAT COMMUCATION ERROR

Module Line Omron PLCs.Lay Up 01.Alarm
  • .Alarm[0001],[1]ETHERCAT Node 01 ERROR

Module Line Omron PLCs.Lay Up 01.Alarm
  • .Alarm[0002],[2]ETHERCAT Node 02 ERROR

Module Line Omron PLCs.Lay Up 01.Alarm
  • .Alarm[0003],[3]ETHERCAT Node 03 ERROR

Module Line Omron PLCs.Lay Up 01.Alarm
  • .Alarm[0004],[4]ETHERCAT Node 04 ERROR

Module Line Omron PLCs.Lay Up 01.Alarm
  • .Alarm[0005],[5]ETHERCAT Node 05 ERROR

Module Line Omron PLCs.Lay Up 01.Alarm
  • .Alarm[0006],[6]ETHERCAT Node 06 ERROR

Module Line Omron PLCs.Lay Up 01.Alarm
  • .Alarm[0007],[7]ETHERCAT Node 07 ERROR

Module Line Omron PLCs.Lay Up 01.Alarm
  • .Alarm[0008],[8]ETHERCAT Node 08 ERROR

Module Line Omron PLCs.Lay Up 01.Alarm
  • .Alarm[0009],[9]ETHERCAT Node 09 ERROR

Module Line Omron PLCs.Lay Up 01.Alarm
  • .Alarm[0010],[10]ETHERCAT Node 10 ERROR

Module Line Omron PLCs.Lay Up 01.Alarm
  • .Alarm[0011],[11]ETHERCAT Node 11 ERROR


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: DataSubscriber 2
« Reply #1 on: February 04, 2016, 01:43:03 PM »
Are you using any of the AdvancedHMI visual controls? If not, I would consider directly accessing the OPC server then bypassing OpcDaCom and the DataSubscriber. The main purpose of OpcDaCom is to provide a wrapper to interface an OPC server to AdvancedHMI controls.

maxketcham

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: DataSubscriber 2
« Reply #2 on: February 04, 2016, 02:00:09 PM »
yes I am as soon as I can get this part of it to work. I Have several programs that just access opcdaauto,dll and they work OK, just didn't want to have to deal with that plus the graphics too.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: DataSubscriber 2
« Reply #3 on: February 04, 2016, 07:09:46 PM »
Here is something to consider:

Each time the list of subscribed values change, the DataSubscriber2 will remove all subscriptions and create all new ones. So let's say your loop adds 200 new items, it will subscribe items in the OPC server a number of times equal to 1+2+3+4+5+6+....+200. In other words thousands of times. For each subscription, it will also perform a synchronous read which can take OPC 50ms or more, I've even seen several seconds. (OPC can be horribly slow)

To avoid this, the DataSubscriber was meant to implement ISupportInitialize (but it never was fully implemented). This is how you would use it:
Code: [Select]
CType(Me.DataSubscriber21, System.ComponentModel.ISupportInitialize).BeginInit()
'* Add your subscriptions here

CType(Me.DataSubscriber21, System.ComponentModel.ISupportInitialize).EndInit()

To complete the implementation, edit DataSubscriber2.vb and go to line 272 and add the additional condition like this:
Code: [Select]
If Not Me.DesignMode And Not Initializing Then