Search code examples
c#dcomopc

Read data from HDA server returns E_INVALIDHANDLE


I'm using the OpcNetApi assembly to connect and read data from a Matrikon OPC HDA server. Connection is taking place however I'm receiving an E_INVALIDHANDLE error as ResultID when trying to pull some test data from the server. This is how far I have got:

Opc.URL url = new Opc.URL("opchda://localhost/Matrikon.OPC.Simulation.1");
OpcCom.Factory fact = new OpcCom.Factory();
hdaServer = new Opc.Hda.Server(fact, url);
hdaServer.Connect();

// Read data from historian
DateTime[] times = new DateTime[1];
times[0] = DateTime.Now;

Opc.ItemIdentifier[] items = new Opc.ItemIdentifier[1];
items[0] = new Opc.ItemIdentifier();
items[0].ItemName = "Random.ArrayOfString";
items[0].ClientHandle = null; // which handle should I use?
ItemValueCollection[] itemsColl = hdaServer.ReadAtTime(times, items);

No error is thrown but no results can be found inside itemsColl.

Random.ArrayOfString is an existing OPC item with historical values available.


Solution

  • I think the date you use in the method ReadAtTime is wrong. This method reads data at a specific time. Try using the ReadRaw or ReadProcessed method where you can read data from a date range.