hylasoft-usa / h-opc

OPC client made simpler, for UA and DA
MIT License
300 stars 144 forks source link

Get the quality and timestamp #39

Closed AlexeyTipunin closed 6 years ago

AlexeyTipunin commented 7 years ago

Hello. Is it possible to get quality and a timestamp?

AlexeyTipunin commented 7 years ago

or other arbitrary fields

jmbeach commented 7 years ago

Do you mean while monitoring?

AlexeyTipunin commented 7 years ago

reading and monitoring, and set on write

jmbeach commented 7 years ago

I'm not sure I understand what you're asking.

AlexeyTipunin commented 7 years ago

I want to read the value as well as its quality and time stamp of when it was installed. So I want to set the quality of the installation of values.

jmbeach commented 7 years ago

I'm sorry. When what was installed? The OPC server?

AlexeyTipunin commented 7 years ago

I want to read value, quality and time stamp. And I want to set the quality on value write.

jmbeach commented 7 years ago

I understand now. I'll look into it

jmbeach commented 7 years ago

This is definitely possible. These results are gotten but basically discarded.

MuckersMate commented 7 years ago

I've already done this, here is my DA and UA code...

`public enum TagQualities { [DescriptionAttribute("None")] None, [DescriptionAttribute("Bad")] Bad, [DescriptionAttribute("Good")] Good }

public Practicon.OPC.Common.TagQualities TagQuality(string tagName)
{
    var item = new OpcDa.Item { ItemName = tagName };
    if (Status == OpcStatus.NotConnected)
    {
        throw new OpcException("Server not connected. Cannot read tag.");
    }
    var result = _server.Read(new[] { item })[0];
   if (result.Quality == OpcDa.Quality.Bad)
   {
       return TagQualities.Bad;
   }
   else if (result.Quality == OpcDa.Quality.Good)
   {
       return TagQualities.Good;
   }
   return TagQualities.None;
}

// ///

/// Tag's TimeStamp /// /// /// public DateTime TimeStamp(string tagName) { var item = new OpcDa.Item { ItemName = tagName }; if (Status == OpcStatus.NotConnected) { throw new OpcException("Server not connected. Cannot read tag."); } var result = _server.Read(new[] { item })[0]; return result.Timestamp; }`

UA Code

`/ ///

/// Tag's TimeStamp /// /// /// public DateTime TimeStamp(string tagName) { var nodesToRead = BuildReadValueIdCollection(tagName, Attributes.Value);

        DataValueCollection results;
        DiagnosticInfoCollection diag;
        _session.Read(
            requestHeader: null,
            maxAge: 0,
            timestampsToReturn: TimestampsToReturn.Server,
            nodesToRead: nodesToRead,
            results: out results,
            diagnosticInfos: out diag);
        var val = results[0];

        return val.ServerTimestamp;
    }

public Practicon.OPC.Common.TagQualities TagQuality(string tagName) {

        var nodesToRead = BuildReadValueIdCollection(tagName, Attributes.Value);

        DataValueCollection results;
        DiagnosticInfoCollection diag;
        _session.Read(
            requestHeader: null,
            maxAge: 0,
            timestampsToReturn: TimestampsToReturn.Server,
            nodesToRead: nodesToRead,
            results: out results,
            diagnosticInfos: out diag);
        var val = results[0];

        if (StatusCode.IsGood(val.StatusCode))
        {
            return TagQualities.Good;
        }
        else if (StatusCode.IsBad(val.StatusCode))
        {
            return TagQualities.Bad;
        }
        return TagQualities.None

}`

I've haveto use my own enumeration for Quality since I need a common result type to pass to COM clients...

jmbeach commented 7 years ago

That's very helpful. We'll also want to add it to the Node classes so we can retrieve these when reading tag's value. That way, it could optionally come back in the monitor callback in case you want to monitor it yourself

bajcmartinez commented 7 years ago

Hi guys, great work with the library. This specific point is also very important for me, I can do the changes in a branch, and then we can merge it, would that be fine by you? Thanks

jmbeach commented 7 years ago

@bajcmartinez, of course it would!

bajcmartinez commented 7 years ago

@jmbeach I have the changes ready, but can't push a new branch, can you give me access or you prefer me to do a fork? Thanks

jmbeach commented 7 years ago

I'd prefer you to do a fork and then make a pull request. Thanks!