Search code examples
c#opcopc-dakepserverex

Looking for assistance on getting the actual value from OPC server


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.


Solution

  • 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.