0

I am working with an existing C# program communicating with a PLC over using Kepserver (I know the PLC and Kepserver sides, but am new on C#). I keep getting "Opc.Da.Item" as the value (not the actual PLC value). I know it's probably a basic question, but where do I get the actual value (what do I put in the last line of logic)? Thanks for any help.

This is how the other sections that read data from the OPC are, but I can't seem to see what I am doing wrong.

I am finally getting back to this issue and am still having a problem. With the method added below, I get a null value in results[0].value.

private void ReadCompleteCallback_NotApplicable (object clientHandle, Opc.Da.ItemValueResult[] results)
{
HMINotApp_TextBox.Invoke(new EventHandler(delegate { HMINotApp_TextBox.Text = Convert.ToString(results[0].Value); }));
}
Opc.Da.Item[] OPC_NotApplicable = new Opc.Da.Item[1];
OPC_NotApplicable[0] = new Opc.Da.Item();
OPC_NotApplicable[0].ItemName = Brake_Press_ID + "B1156_barcode_DINT_value";
OPC_Not_Applicable.Add(OPC_NotApplicable[0]);
NotApplicable_GroupRead.AddItems(OPC_Not_Applicable.ToArray());
Opc.IRequest req;
NotApplicable_GroupRead.Read(NotApplicable_GroupRead.Items, 123, new Opc.Da.ReadCompleteEventHandler(ReadCompleteCallback_NotApplicable), out req);
label23.Text = OPC_Not_Applicable[0].ToString();

I expect the value to be 9999999 but I get Opc.Da.Item.

Sean
  • 17
  • 4
  • The default `.ToString()` method on an object instance will return the type name of the instance, which is probably what's happening here. I don't know the "Opc" library, but from a brief google, perhaps `OPC_Not_Applicable[0].Value.ToString();` might work here? – steve16351 Aug 12 '19 at 19:50
  • The .Value. portion will not work. "Item does not contain a definition for Value ....." – Sean Aug 12 '19 at 20:24

1 Answers1

3

You're almost there. When calling the Read method, you provided a callback ReadCompleteCallback_NotApplicable. This is the method which gets invoked after the read request is complete.

As you don't seem to be getting an exception, it looks like the method is already declared somewhere. Try to locate it.. an example how to read items from that callback can look something like that:

private void ReadCompleteCallback_NotApplicable(object handle, Opc.Da.ItemValueResult[] results)
{
    Console.WriteLine("Read completed.");
    foreach(Opc.Da.ItemValueResult readResult in results)
    {
        Console.WriteLine($"{readResult.ItemName}\tval:{readResult.Value}");
    }
}

So readResult.Value will contain the value you are looking for.

div
  • 905
  • 1
  • 7
  • 20
  • 1
    Thanks. I found the logic but part of it was commented out. Now, I know what to work on. – Sean Aug 13 '19 at 20:25