Esempio n. 1
0
        public bool ReadTags(List <ITag> tagList)
        {
            try
            {
                if (!IsAvailable || !IsConnected)
                {
                    return(false);
                }


                List <NodeId>        NodeIdList        = new List <NodeId>();
                List <Type>          ExpectedTypesList = new List <Type>();
                List <object>        ObjectList        = new List <object>();
                List <ServiceResult> ServiceResultList = new List <ServiceResult>();
                foreach (var tag in tagList)
                {
                    NodeIdList.Add(new NodeId(tag.Address));
                    ExpectedTypesList.Add(tag.TagType);
                }

                ClientSession.ReadValues(NodeIdList, ExpectedTypesList, out ObjectList, out ServiceResultList);
                int  index   = 0;
                bool Success = true;
                foreach (var MyServiceResult in ServiceResultList)
                {
                    OPCTag tag = tagList[index] as OPCTag;
                    if (!tag.UpdateTag(ServiceResultList[index], ObjectList[index]))
                    {
                        Success = false;
                    }
                    index++;
                }
                return(Success);
            }
            catch (Exception ex)
            {
                Logger?.LogException($"OPC - Multiple read error. Ex: {ex.Message}.");
                return(false);
            }
        }
Esempio n. 2
0
        public bool WriteTags(List <ITag> tagList, List <object> objectList)
        {
            try
            {
                if (!IsAvailable || !IsConnected)
                {
                    return(false);
                }



                if (tagList.Count() != objectList.Count())
                {
                    Logger?.LogException($"OPC - WriteTags wrong values. Tags request: {tagList.Count()}. Value given: {objectList.Count()}.");
                    return(false);
                }



                WriteValueCollection nodesToWriteCollection = new WriteValueCollection();
                int       index = 0;
                DataValue val   = new DataValue();
                foreach (var tag in tagList)
                {
                    Type   varType       = tagList[index].TagType;
                    object varValue      = objectList[index];
                    var    MyValueCasted = Convert.ChangeType(varValue, varType);
                    val.Value = MyValueCasted;
                    nodesToWriteCollection.Add(new WriteValue()
                    {
                        NodeId      = new NodeId(tagList[index].Address),
                        AttributeId = Attributes.Value,
                        Value       = val
                    });
                    index++;
                }

                StatusCodeCollection     results;
                DiagnosticInfoCollection diagnosticInfos;

                RequestHeader requestHeader = new RequestHeader();
                //requestHeader.ReturnDiagnostics = 0;

                ResponseHeader RS = ClientSession.Write(
                    requestHeader,
                    nodesToWriteCollection,
                    out results,
                    out diagnosticInfos);

                bool Success = true;

                foreach (var result in results)
                {
                    if (result.Code != StatusCodes.Good)
                    {
                        Success = false;
                    }
                }

                index = 0;
                foreach (var item in results)
                {
                    OPCTag tag = tagList[index] as OPCTag;
                    if (!tag.UpdateTag(item))
                    {
                        Success = false;
                    }
                    index++;
                }
                return(Success);
            }
            catch (Exception ex)
            {
                Logger?.LogException($"OPC - Multiple read error. Ex: {ex.Message}.");
                return(false);
            }
        }