/// <summary> /// Remove server handles for new items. /// </summary> private void UndoRead() { mValues_ = null; mItem_ = null; valuesCtrl_.Initialize(mServer_, null); }
/// <summary> /// Edits items in the specified subscription. /// </summary> private void EditItem(TreeNode node, TsCDaSubscription subscription, TsCDaItem item) { try { // save existing item id. OpcItem itemId = new OpcItem(item); TsCDaItem[] items = new ItemListEditDlg().ShowDialog(new TsCDaItem[] { item }, false, false); if (items == null) { return; } // modify an existing item. if (itemId.Key == items[0].Key) { subscription.ModifyItems((int)TsCDaStateMask.All, items); } // add/remove item because the item id changed. else { items = subscription.AddItems(items); subscription.RemoveItems(new OpcItem[] { itemId }); } node.Text = items[0].ItemName; node.Tag = items[0]; } catch (Exception e) { MessageBox.Show(e.Message); } }
/// <summary> /// Remove server handles for new items. /// </summary> private void UndoRead() { m_values = null; m_item = null; ValuesCTRL.Initialize(m_server, null); }
/// <summary> /// Copy object values into controls. /// </summary> public void Set(object value) { // check for null value. if (value == null) { SetDefaults(); return; } // cast value to item object. TsCDaItemValue item = (TsCDaItemValue)value; // save item identifier (including client and server handles). m_identifier = new OpcItem(item); ItemNameTB.Text = item.ItemName; ItemPathTB.Text = item.ItemPath; ValueCTRL.ItemID = new OpcItem(item); ValueCTRL.Value = item.Value; ValueSpecifiedCB.Checked = item.Value != null; QualitySpecifiedCB.Checked = item.QualitySpecified; QualityBitsCTRL.Value = item.Quality.QualityBits; LimitBitsCTRL.Value = item.Quality.LimitBits; VendorBitsCTRL.Value = item.Quality.VendorBits; TimestampCTRL.Value = TimestampCTRL.MinDate; TimestampSpecifiedCB.Checked = item.TimestampSpecified; // set timestamp - jump through some hoops to handle invalid values. if (item.TimestampSpecified) { TimestampCTRL.Value = (item.Timestamp > TimestampCTRL.MinDate)?item.Timestamp:TimestampCTRL.MinDate; } }
/// <summary> /// Sends an asynchronous request to read the attributes of an item. /// </summary> /// <param name="startTime">The beginning of the history period to read.</param> /// <param name="endTime">The end of the history period to be read.</param> /// <param name="item">The item to read (must include the item name).</param> /// <param name="attributeIDs">The attributes to read.</param> /// <param name="requestHandle">An identifier for the request assigned by the caller.</param> /// <param name="callback">A delegate used to receive notifications when the request completes.</param> /// <param name="request">An object that contains the state of the request (used to cancel the request).</param> /// <returns>A set of results containing any errors encountered when the server validated the attribute ids.</returns> internal TsCHdaResultCollection ReadAttributes( TsCHdaTime startTime, TsCHdaTime endTime, OpcItem item, int[] attributeIDs, object requestHandle, TsCHdaReadAttributesCompleteEventHandler callback, out IOpcRequest request) { LicenseHandler.ValidateFeatures(LicenseHandler.ProductFeature.HistoricalAccess); if (Server == null) { throw new NotConnectedException(); } TsCHdaResultCollection results = ((ITsCHdaServer)Server).ReadAttributes( startTime, endTime, item, attributeIDs, requestHandle, callback, out request); return(results); }
/// <summary> /// Prompts the user to specify values to insert for an item. /// </summary> public bool ShowDialog(TsCHdaServer server, OpcItem item, bool synchronous) { if (server == null) throw new ArgumentNullException("server"); mServer_ = server; mSynchronous_ = synchronous; mItem_ = item; mValues_ = null; mResults_ = null; // create new trend. mTrend_ = new TsCHdaTrend(mServer_); // set reasonable defaults. mTrend_.StartTime = new TsCHdaTime("YEAR"); mTrend_.EndTime = new TsCHdaTime("YEAR+1H"); browseCtrl_.Browse(mServer_, null); valuesCtrl_.Initialize(mServer_, null); resultsCtrl_.Initialize(mServer_, mValues_, mResults_); asyncResultsCtrl_.Initialize(mServer_, null); // update dialog state. SetState(); // show dialog. bool result = (ShowDialog() == DialogResult.OK); // release item handles. mTrend_.ClearItems(); // return dialog result. return result; }
/// <summary> /// Displays the address space for the specified server. /// </summary> public OpcItem ShowDialog(TsCDaServer server) { try { if (server == null) { throw new ArgumentNullException("server"); } mServer_ = server; mItemId_ = null; TsCDaBrowseFilters filters = new TsCDaBrowseFilters(); filters.ReturnAllProperties = false; filters.ReturnPropertyValues = false; browseCtrl_.ShowSingleServer(mServer_, filters); propertiesCtrl_.Initialize(null); if (ShowDialog() != DialogResult.OK) { return(null); } return(mItemId_); } finally { // ensure server connection in the browse control are closed. browseCtrl_.Clear(); } }
/// <summary> /// Prompts the use to edit the properties of an item identifier object. /// </summary> public bool ShowDialog(OpcItem item) { if (item == null) { throw new ArgumentNullException("item"); } // cannot edit item id in this context. itemNameTb_.ReadOnly = false; itemPathTb_.ReadOnly = false; aggregateCb_.Enabled = false; aggregateSpecifiedCb_.Enabled = false; aggregateLb_.Enabled = false; // fill in the item id. itemNameTb_.Text = item.ItemName; itemPathTb_.Text = item.ItemPath; // show the dialog. if (ShowDialog() != DialogResult.OK) { return(false); } // update the item. item.ItemName = itemNameTb_.Text; item.ItemPath = itemPathTb_.Text; return(true); }
/// <summary> /// Displays the address space for the specified server. /// </summary> public OpcItem ShowDialog(TsCDaServer server) { try { if (server == null) { throw new ArgumentNullException("server"); } m_server = server; m_itemID = null; TsCDaBrowseFilters filters = new TsCDaBrowseFilters(); filters.ReturnAllProperties = false; filters.ReturnPropertyValues = false; BrowseCTRL.ShowSingleServer(m_server, filters); if (ShowDialog() != DialogResult.OK) { return(null); } return(m_itemID); } finally { // ensure server connection in the browse control are closed. BrowseCTRL.Clear(); } }
/// <summary> /// Fetches the type dictionary for the item. /// </summary> /// <param name="itemID">The item id.</param> public static string GetTypeDictionary(OpcItem itemID) { if (itemID == null) { return(null); } lock (typeof(TsCCpxComplexTypeCache)) { string dictionary = (string)_dictionaries[itemID.Key]; if (dictionary != null) { return(dictionary); } TsCCpxComplexItem item = GetComplexItem(itemID); if (item != null) { dictionary = item.GetTypeDictionary(_server); } return(dictionary); } }
/// <summary> /// Adds an item to the list. /// </summary> private void AddMI_Click(object sender, System.EventArgs e) { try { // prompt user to create a new item. OpcItem item = new OpcItem(); if (!new ItemEditDlg().ShowDialog(item)) { return; } // only add item if a vali item id was specified. if (item.ItemName != null && item.ItemName != "") { // add to list view. AddListItem(item); // adjust columns. AdjustColumns(); } } catch (Exception exception) { MessageBox.Show(exception.Message); } }
/// <summary> /// Display the current selection in the result control. /// </summary> private void ItemsCTRL_ResultSelected(OpcItem result) { if (typeof(TsCHdaItemValueCollection).IsInstanceOfType(result)) { resultsCtrl_.Initialize(mServer_, (TsCHdaItemValueCollection)result); } }
TsCDaBrowseElement[] Browse(TsCDaServer server) { TsCDaBrowseFilters filters; List<TsCDaBrowseElement> elementList; TsCDaBrowseElement[] elements; TsCDaBrowsePosition position; OpcItem path = new OpcItem(); filters = new TsCDaBrowseFilters(); filters.BrowseFilter = TsCDaBrowseFilter.All; filters.ReturnAllProperties = true; filters.ReturnPropertyValues = true; elementList = new List<TsCDaBrowseElement>(); elements = server.Browse(path, filters, out position); foreach (var item in elements) { item.ItemPath = OpcDaTag.ROOT + OpcDaTag.SPLITTER + item.ItemName; elementList.Add(item); if (!item.IsItem) { path = new OpcItem(item.ItemPath, item.Name); BrowseChildren(path, filters, elementList, server); } } return elementList.ToArray(); }
/// <summary> /// Fetches the type description for the item. /// </summary> /// <param name="itemID">The item id.</param> public static string GetTypeDescription(OpcItem itemID) { if (itemID == null) { return(null); } lock (typeof(TsCCpxComplexTypeCache)) { string description = null; TsCCpxComplexItem item = GetComplexItem(itemID); if (item != null) { description = (string)_descriptions[item.TypeItemID.Key]; if (description != null) { return(description); } _descriptions[item.TypeItemID.Key] = description = item.GetTypeDescription(_server); } return(description); } }
/// <summary> /// Copy object values into controls. /// </summary> public void Set(object value) { // check for null value. if (value == null) { SetDefaults(); return; } // cast value to item object. TsCDaItemValue item = (TsCDaItemValue)value; // save item identifier (including client and server handles). mIdentifier_ = new OpcItem(item); itemNameTb_.Text = item.ItemName; itemPathTb_.Text = item.ItemPath; valueCtrl_.ItemID = new OpcItem(item); valueCtrl_.Value = item.Value; valueSpecifiedCb_.Checked = item.Value != null; qualitySpecifiedCb_.Checked = item.QualitySpecified; qualityBitsCtrl_.Value = item.Quality.QualityBits; limitBitsCtrl_.Value = item.Quality.LimitBits; vendorBitsCtrl_.Value = item.Quality.VendorBits; timestampCtrl_.Value = timestampCtrl_.MinDate; timestampSpecifiedCb_.Checked = item.TimestampSpecified; // set timestamp - jump through some hoops to handle invalid values. if (item.TimestampSpecified) { timestampCtrl_.Value = (item.Timestamp > timestampCtrl_.MinDate)?item.Timestamp:timestampCtrl_.MinDate; } }
/////////////////////////////////////////////////////////////////////// #region Public Methods /// <summary> /// Returns the complex item for the specified item id. /// </summary> /// <param name="itemID">The item id.</param> public static TsCCpxComplexItem GetComplexItem(OpcItem itemID) { if (itemID == null) { return(null); } lock (typeof(TsCCpxComplexTypeCache)) { TsCCpxComplexItem item = new TsCCpxComplexItem(itemID); try { item.Update(_server); } catch { // item is not a valid complex data item. item = null; } _items[itemID.Key] = item; return(item); } }
/// <summary> /// Display the current selection in the result control. /// </summary> private void ItemsCTRL_ResultSelected(OpcItem result) { if (typeof(TsCHdaAnnotationValueCollection).IsInstanceOfType(result)) { ResultsCTRL.Initialize(m_server, (TsCHdaAnnotationValueCollection)result); } }
public IList <OpcItem> Get() { var result = new List <OpcItem>(); var device = DeviceData.GetDeviveData(); try { device.Start(); var timeDelayer = new TimeDelayer(5000); timeDelayer.Start(); while (true) { //当设备状态为连接状态时 if (device.Status == 1) { //循环读取每个tag的值 foreach (var tag in device.Groups[0].Tags) { //结果集中已经存在当前tag时,循环下一个 if (result.Any(r => r.ItemId.Equals(tag.Address))) { continue; } //根据tag的地址读取数据 var value = device.Read(tag.Address); //获取到数据后,将数据加入到结果集中 if (value != null) { var opcItem = new OpcItem { ItemId = tag.Address, Value = value }; result.Add(opcItem); } } //结果集的总数大于等于tag集总数时,退出while循环 if (result.Count >= device.Groups[0].Tags.Count) { break; } } if (timeDelayer.IsTimeout()) { throw new Exception("读取数据超时"); } Thread.Sleep(100); } } catch (Exception) { throw; } finally { device.Abort(); } return(result); }
/// <summary> /// Browses for children of the element at the current node. /// </summary> private void GetProperties(TreeNode node) { try { // get the server for the current node. TsCDaServer server = FindServer(node); // get the current element to use for a get properties. TsCDaBrowseElement element = null; if (node.Tag != null && node.Tag.GetType() == typeof(TsCDaBrowseElement)) { element = (TsCDaBrowseElement)node.Tag; } // can only get properties for an item. if (!element.IsItem) { return; } // clear the node children. node.Nodes.Clear(); // begin a browse. OpcItem itemID = new OpcItem(element.ItemPath, element.ItemName); TsCDaItemPropertyCollection[] propertyLists = server.GetProperties( new OpcItem[] { itemID }, m_filters.PropertyIDs, m_filters.ReturnPropertyValues); if (propertyLists != null) { foreach (TsCDaItemPropertyCollection propertyList in propertyLists) { foreach (TsCDaItemProperty property in propertyList) { AddItemProperty(node, property); } // update element properties. element.Properties = (TsCDaItemProperty[])propertyList.ToArray(typeof(TsCDaItemProperty)); } } node.Expand(); // send notification that property list changed. if (ElementSelected != null) { ElementSelected(element); } } catch (Exception e) { MessageBox.Show(e.Message); } }
/// <summary> /// Initializes the object with the specified item identifier. /// </summary> public TsCDaItemPropertyCollection(OpcItem itemId) { if (itemId != null) { ItemName = itemId.ItemName; ItemPath = itemId.ItemPath; } }
/// <summary> /// Called when [read opc address space response]. /// </summary> /// <param name="addressSpace">The address space.</param> private void OnReadOpcAddressSpaceResponse(OpcItem addressSpace) { var readOpcAddressSpaceResponse = this.ReadOpcAddressSpaceResponse; if (readOpcAddressSpaceResponse != null) { readOpcAddressSpaceResponse(this, new AddressSpaceDataEventArgs(addressSpace)); } }
/// <summary> /// Activates the next button when an item is picked. /// </summary> private void BrowseCTRL_ItemPicked(OpcItem[] items) { if (items != null && items.Length == 1) { mItem_ = items[0]; nextBtn_.Enabled = true; NextBTN_Click(browseCtrl_, null); } }
/// <summary> /// Initializes a browse position /// </summary> internal BrowsePosition( OpcItem itemID, TsCDaBrowseFilters filters, string continuationPoint) : base(itemID, filters) { ContinuationPoint = continuationPoint; }
/// <summary> /// Activates the next button when an item is picked. /// </summary> private void BrowseCTRL_ItemPicked(OpcItem[] items) { if (items != null && items.Length == 1) { m_item = items[0]; NextBTN.Enabled = true; NextBTN_Click(BrowseCTRL, null); } }
/// <summary> /// Saves the parameters for an incomplete browse information. /// </summary> public TsCDaBrowsePosition(OpcItem itemId, TsCDaBrowseFilters filters) { if (filters == null) { throw new ArgumentNullException(nameof(filters)); } itemId_ = (OpcItem)itemId?.Clone(); browseFilters_ = (TsCDaBrowseFilters)filters.Clone(); }
/// <summary> /// Initializes the object with the specified item identifier and result. /// </summary> public TsCDaItemPropertyCollection(OpcItem itemId, OpcResult result) { if (itemId != null) { ItemName = itemId.ItemName; ItemPath = itemId.ItemPath; } result_ = result; }
/// <summary> /// Initializes the object with and ItemIdentifier object. /// </summary> public TsCDaItemValue(OpcItem item) { if (item != null) { ItemName = item.ItemName; ItemPath = item.ItemPath; ClientHandle = item.ClientHandle; ServerHandle = item.ServerHandle; } }
/////////////////////////////////////////////////////////////////////// #region Constructors, Destructor, Initialization /// <summary> /// Saves the parameters for an incomplete browse information. /// </summary> public TsCDaBrowsePosition(OpcItem itemID, TsCDaBrowseFilters filters) { if (filters == null) { throw new ArgumentNullException("Filters"); } _itemID = (itemID != null) ? (OpcItem)itemID.Clone() : null; _filters = (TsCDaBrowseFilters)filters.Clone(); }
/// <summary> /// Initializes object with the specified ItemIdentifier object. /// </summary> public TsCDaItem(OpcItem item) { if (item == null) { return; } ItemName = item.ItemName; ItemPath = item.ItemPath; ClientHandle = item.ClientHandle; ServerHandle = item.ServerHandle; }
/// <summary>Fetches the children of a branch that meet the filter criteria.</summary> /// <returns>The set of elements found.</returns> /// <requirements>OPC XML-DA Server or OPC Data Access Server V2.x / V3.x</requirements> /// <param name="itemID">The identifier of branch which is the target of the search.</param> /// <param name="filters">The filters to use to limit the set of child elements returned.</param> /// <param name="position">An object used to continue a browse that could not be completed.</param> public TsCDaBrowseElement[] Browse( OpcItem itemID, TsCDaBrowseFilters filters, out Da.TsCDaBrowsePosition position) { if (_server == null) { throw new NotConnectedException(); } return(((ITsDaServer)_server).Browse(itemID, filters, out position)); }
//---------------------------------------------------------------------------------------------------------------------- // btnConnect_Click //----------------------- // This method tries to connect to the specified OPC Server. // If connected successfully, this method also registers the name of the client with the server and // adds an OPC Group. //---------------------------------------------------------------------------------------------------------------------- private void btnConnect_Click(object sender, EventArgs e) { TsCDaBrowseElement[] BrowserElement; TsCDaBrowsePosition BrowserPos; OpcItem Path = new OpcItem(""); _filter.BrowseFilter = TsCDaBrowseFilter.All; _filter.ReturnAllProperties = true; _filter.ReturnPropertyValues = true; try { Cursor = Cursors.WaitCursor; if (!_myServer.IsConnected) { // No connection to server, connect to it _textBoxServerUrl.Enabled = false; _myServer.Connect(_textBoxServerUrl.Text); // Connect now with Server BrowserElement = _myServer.Browse(Path, _filter, out BrowserPos); /// All succeeded, update buttons and text fields _textBoxServerUrl.Enabled = false; _btnConnect.Text = "Disconnect"; BrowseCTRL.ShowSingleServer(_myServer, _filter); PropertiesCTRL.Initialize(null); } else { // connection to server exists, disconnect to it _textBoxServerUrl.Enabled = true; BrowseCTRL.Clear(); PropertiesCTRL.Initialize(null); _myServer.Disconnect(); _btnConnect.Text = "Connect"; } Cursor = Cursors.Default; // Set default cursor } catch (OpcResultException exe) { Cursor = Cursors.Default; /// Set default cursor MessageBox.Show(exe.Message, exe.Source, MessageBoxButtons.OK, MessageBoxIcon.Warning); _textBoxServerUrl.Enabled = true; _btnConnect.Text = "Connect"; } catch (Exception ex) { Cursor = Cursors.Default; /// Set default cursor MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Warning); _textBoxServerUrl.Enabled = true; _btnConnect.Text = "Connect"; } }
void BrowseChildren(OpcItem opcItem, TsCDaBrowseFilters filters, IList<TsCDaBrowseElement> elementList, TsCDaServer server) { TsCDaBrowsePosition position; OpcItem path; var elements = server.Browse(opcItem, filters, out position); if (elements != null) { foreach (var item in elements) { item.ItemPath = opcItem.ItemPath + OpcDaTag.SPLITTER + item.ItemName; elementList.Add(item); if (!item.IsItem) { path = new OpcItem(item.ItemPath, item.ItemName); BrowseChildren(path, filters, elementList, server); } } } }
TsCDaBrowseElement[] Browse() { TsCDaBrowseFilters filters; List<TsCDaBrowseElement> elementList; TsCDaBrowseElement[] elements; TsCDaBrowsePosition position; OpcItem path = new OpcItem(); filters = new TsCDaBrowseFilters(); filters.BrowseFilter = TsCDaBrowseFilter.All; filters.ReturnAllProperties = true; filters.ReturnPropertyValues = true; elementList = new List<TsCDaBrowseElement>(); elements = _activeOpcServer.Browse(path, filters, out position); foreach(var item in elements) { //!!! OpcItem.ItemPath всегда равно null и видимо не участвует в коде. Буду заполнять руками item.ItemPath = ROOT + SPLITTER + item.ItemName; elementList.Add(item); if (!item.IsItem) { //if (item.HasChildren) На данный момент это не работает. Если элемент это группа, то всега true //{ path = new OpcItem(item.ItemPath, item.Name); BrowseChildren(path, filters, elementList); //} } } return elementList.ToArray(); }
void BrowseChildren(OpcItem opcItem, TsCDaBrowseFilters filters, IList<TsCDaBrowseElement> elementList) { TsCDaBrowsePosition position; OpcItem path; var elements = _activeOpcServer.Browse(opcItem, filters, out position); if (elements != null) { foreach (var item in elements) { item.ItemPath = opcItem.ItemPath + SPLITTER + item.ItemName; elementList.Add(item); if (!item.IsItem) { //if (item.HasChildren) //{ path = new OpcItem(item.ItemPath, item.ItemName); BrowseChildren(path, filters, elementList); //} } } } }