Read Real-time Tags Synchronously

  • SyncReadTags method is a simple call you can use to read any number of Tags with a blocking call.
  • This is a blocking call and should only be called one at a time.
  • The subscription method using AddTags and the ValuesChangedAll event listed above is more efficient for reading Tag values on a continuous basis.
  • This SyncReadTags method does not make a synchronous call to the OPC Servers, it makes a temporary subscription to the OAS Services and receives the current values of the Tags.
  • No features in Open Automation Software make synchronous calls to OPC Servers, as it often causes high CPU usage and poor performance.
CONTENTS

VB

    Private Sub ButtonSyncRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSyncRead.Click
        Dim Tags(2) As String
        Tags(0) = "Ramp.Value"
        Tags(1) = "Sine.Value"
        Tags(2) = "Random.Value"
        Dim Errors() As Int32 = Nothing
        ' The Errors array will be sized to the same length as your Tags array.
        ' The following values will be returned to you in the Int32 array you pass.
        ' 0 = Good Quality
        ' 1 = Bad Quality
        ' 2 = Timeout
        Dim Values() As Object
        Values = OpcSystemsData.SyncReadTags(Tags, Errors, 10000)
        ' The Values array will be sized to the number of Tags you ask for and contain the values if there is Good Quality.
        Dim TagIndex As Int32
        Dim tempStringBuilder As New System.Text.StringBuilder
        Dim tempDouble As Double
 
        For TagIndex = 0 To Tags.GetLength(0) - 1
            tempStringBuilder.Append(Tags(TagIndex))
            tempStringBuilder.Append(" = ")
            Select Case Errors(TagIndex)
                Case 0
                    tempDouble = Values(TagIndex)
                    tempStringBuilder.Append(tempDouble.ToString())
                Case 1
                    tempStringBuilder.Append("Bad Quality")
                Case 2
                    tempStringBuilder.Append("Timeout")
            End Select
            tempStringBuilder.AppendLine()
        Next
        MessageBox.Show(tempStringBuilder.ToString, "Sync Read Results", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

C#

  private void ButtonSyncRead_Click(object sender, System.EventArgs e)
              {
                     string[] Tags = new string[3];
                     Tags[0] = "Ramp.Value";
                     Tags[1] = "Sine.Value";
                     Tags[2] = "Random.Value";
                     Int32[] Errors = null;
                     // The Errors array will be sized to the same length as your Tags array.
                     // The following values will be returned to you in the Int32 array you pass.
                     // 0 = Good Quality
                     // 1 = Bad Quality
                     // 2 = Timeout
                     object[] Values = null;
                     Values = OpcSystemsData.SyncReadTags(Tags, ref Errors, 10000);
                     // The Values array will be sized to the number of Tags you ask for and contain the values if there is Good Quality.
                     Int32 TagIndex = 0;
                     System.Text.StringBuilder tempStringBuilder = new System.Text.StringBuilder();
                     double tempDouble = 0;
 
                     for (TagIndex = 0; TagIndex < Tags.GetLength(0); TagIndex++)
                     {
                           tempStringBuilder.Append(Tags[TagIndex]);
                           tempStringBuilder.Append(" = ");
                           switch (Errors[TagIndex])
                           {
                                  case 0:
                                         tempDouble = Convert.ToDouble(Values[TagIndex]);
                                         tempStringBuilder.Append(tempDouble.ToString());
                                         break;
                                  case 1:
                                         tempStringBuilder.Append("Bad Quality");
                                         break;
                                  case 2:
                                         tempStringBuilder.Append("Timeout");
                                         break;
                           }
                           tempStringBuilder.AppendLine();
                     }
                     MessageBox.Show(tempStringBuilder.ToString(), "Sync Read Results", MessageBoxButtons.OK, MessageBoxIcon.Information);
              }