public static List <object> OPC_Read(List <string> ItemNames) { if (ItemNames == null || ItemNames.Count == 0) { return(null); } int count = ItemNames.Count; List <object> readRtn = new List <object>();//设置返回值数组 using (var srv = new OPCXML_DataAccess()) { ReadRequestItemList ItemLists = new ReadRequestItemList(); ItemLists.Items = new ReadRequestItem[count]; for (int i = 0; i < count; i++) { ItemLists.Items[i] = new ReadRequestItem(); ItemLists.Items[i].ItemPath = ""; ItemLists.Items[i].ItemName = ItemNames[i + 1]; } RequestOptions opt = new RequestOptions(); ReplyItemList ItemValues = new ReplyItemList();; OPCError[] Errors; opt.ReturnItemTime = true; srv.Read(opt, ItemLists, out ItemValues, out Errors); foreach (var p in ItemValues.Items) { readRtn.Add(p.Value); } } return(readRtn); }
/*---------------------------------------------Reading Function Decleration ----------------------------------------------------------------*/ public List <object> OPCRead(List <string> TagNames) { /*---------------------------------------------Request Instance Decleration ----------------------------------------------------------------*/ List <string> TagName = new List <string>(TagNames); List <object> TagValue = new List <object>(); int RTagNos = TagName.Count(); ReadRequestItemList ReadItemList = new ReadRequestItemList(); ReadItemList.Items = new ReadRequestItem[RTagNos]; RequestOptions.ClientRequestHandle = ""; RequestOptions.LocaleID = "EN-US"; RequestOptions.RequestDeadlineSpecified = false; RequestOptions.ReturnDiagnosticInfo = false; RequestOptions.ReturnErrorText = false; RequestOptions.ReturnItemName = false; RequestOptions.ReturnItemPath = false; RequestOptions.ReturnItemTime = false; ReadItemList.ItemPath = ""; for (int Looper = 0; Looper < RTagNos; Looper++) { ReadItemList.Items[Looper] = new ReadRequestItem(); ReadItemList.Items[Looper].ItemName = TagName[Looper]; } /*---------------------------------------------Reading Function Invoked ----------------------------------------------------------------*/ myClient.Read(RequestOptions, ReadItemList, out ReplyList, out ErrorList); /*---------------------------------------------Output to Console Invoked ----------------------------------------------------------------*/ for (int Looper = 0; Looper < ReplyList.Items.Count(); Looper++) { if ((ReplyList.Items[Looper] != null) && (ReplyList.Items[Looper].Value != null) && (ReplyList.Items[Looper].Value.GetType().Name != "XmlNode[]")) { TagValue.Add(ReplyList.Items[Looper].Value); //Console.WriteLine(ReplyList.Items[Looper].Value.ToString()); //Value.ToString()); } else { TagValue.Add("<Error>"); } } return(TagValue); }
public OpcXmlDaDriver() { RequestOptions ReadOptions = new RequestOptions(); ReadOptions.ClientRequestHandle = ""; ReadOptions.LocaleID = "DE-AT"; ReadOptions.RequestDeadlineSpecified = true; ReadOptions.ReturnDiagnosticInfo = true; ReadOptions.ReturnErrorText = true; ReadOptions.ReturnItemName = true; ReadOptions.ReturnItemPath = true; ReadOptions.ReturnItemTime = true; ReadRequestItemList ReadItemList = new ReadRequestItemList(); ReadRequestItem[] ReadItemArray = new ReadRequestItem[1]; ReadRequestItem ReadItem = new ReadRequestItem(); ReadItem.ItemPath = "SIMOTION"; ReadItem.ItemName = "var/userData.user5"; ReadItemArray[0] = ReadItem; ReadItemList.Items = ReadItemArray; ReplyItemList ReadReplyList; OPCError[] ReadErrorList; Service C230_2_Server = new Service(); C230_2_Server.Url = "http://169.254.11.22/soap/opcxml"; System.Net.ICredentials myCredentials = new System.Net.NetworkCredential("simotion", "simotion"); C230_2_Server.Credentials = myCredentials; C230_2_Server.PreAuthenticate = true; System.Net.ServicePointManager.Expect100Continue = false; C230_2_Server.Read(ReadOptions, ReadItemList, out ReadReplyList, out ReadErrorList); //if ((ReadReplyList.Items[0] != null) && (ReadReplyList.Items[0].Value != null) && (ReadReplyList.Items[0].Value.GetType().Name != "XmlNode[]")) // Output.Text = ReadReplyList.Items[0].Value.ToString(); //else // Output.Text = "<Error>"; MessageBox.Show(ReadReplyList.Items[0].ItemName.ToString() + "\nValue: " + ReadReplyList.Items[0].Value); }
/// <summary> /// Reading all readings from an OPC server. /// </summary> private void ProcessOPCServer(OOPCDAServer opcDaServer) { if (opcDaServer.AutomaticPollingEnabled == 0) return; XmlServer srv = null; try { srv = new XmlServer(opcDaServer.ObjectName); List<OPoint> points = TablesLogic.tPoint.LoadList( TablesLogic.tPoint.OPCDAServerID == opcDaServer.ObjectID); if (points.Count == 0) return; List<ReadRequestItem> readRequestItems = new List<ReadRequestItem>(); Hashtable hash = new Hashtable(); foreach (OPoint point in points) { readRequestItems.Add(NewReadRequestItem(point.OPCItemName)); hash[point.OPCItemName] = point; } ReadRequestItemList readItemList = new ReadRequestItemList(); readItemList.Items = readRequestItems.ToArray(); ReplyBase reply; RequestOptions options = new RequestOptions(); options.ReturnErrorText = true; options.ReturnItemName = true; ReplyItemList rslt; OPCError[] err; reply = srv.Read(options, readItemList, out rslt, out err); if (rslt == null) throw new Exception(err[0].Text); else { int count = 0; foreach (ItemValue iv in rslt.Items) { if (iv.ResultID == null) { try { using (Connection c = new Connection()) { // Create the readings. // OReading reading = TablesLogic.tReading.Create(); OPoint point = hash[iv.ItemName] as OPoint; if (point != null) { reading.PointID = point.ObjectID; reading.LocationID = point.LocationID; //reading.LocationTypeParameterID = point.LocationTypeParameterID; reading.EquipmentID = point.EquipmentID; //reading.EquipmentTypeParameterID = point.EquipmentTypeParameterID; reading.DateOfReading = DateTime.Now; if (iv.Value is bool) reading.Reading = ((bool)iv.Value) ? 1 : 0; else reading.Reading = Convert.ToDecimal(iv.Value.ToString()); reading.Source = ReadingSource.OPCServer; reading.CheckForBreachOfReading(point); reading.Save(); count++; } c.Commit(); } } catch (Exception ex) { LogEvent(ex.Message + "\n\n" + ex.StackTrace, BackgroundLogMessageType.Error); } } else LogEvent("Error reading '" + iv.ItemName + "': " + iv.ResultID.Name, BackgroundLogMessageType.Error); } LogEvent(count + " points out of " + readItemList.Items.Length + " successfully read from server '" + opcDaServer.ObjectName + "'."); } } catch (Exception ex) { LogEvent(ex.Message + "\n\n" + ex.StackTrace, BackgroundLogMessageType.Error); } finally { if (srv != null) srv.Dispose(); } }