/// <summary> /// Write Value /// </summary> /// <param name="strValue"></param> private void WriteValue(string strValue) { if (itemsListView.SelectedItems.Count > 0) { //get the item session DaSession session = null; string[] itemIds = new string[itemsListView.SelectedItems.Count]; ValueQT[] itemValues = new ValueQT[itemsListView.SelectedItems.Count]; for (int i = 0; i < itemsListView.SelectedItems.Count; i++) { ListViewItem listViewItem = itemsListView.SelectedItems[i]; if (listViewItem == null) { return; } if (listViewItem.Tag == null) { return; } DaItem daItem = listViewItem.Tag as DaItem; if (daItem == null) { return; } //if no VALUE QT return if (daItem.ValueQT.Data == null) { return; } if (session == null) { session = daItem.DaSubscription.DaSession; } string itemId = null; Type dataType = typeof(System.Byte); int startIndex = 0; itemId = daItem.Id; dataType = daItem.NativeDatatype; if (dataType.IsArray) { startIndex = ((daItem.ValueQT.Data) as Array).GetLowerBound(0); } if (String.IsNullOrEmpty(itemId)) { return; } if (dataType == null) { return; } ValueQT itemValue = new ValueQT(); if (!dataType.IsArray) { //data type is not array object objData = null; ConvertToType(dataType, out objData, strValue); itemValue.SetData(objData, EnumQuality.GOOD, DateTime.Now); } else { //data type is array string typeStr = dataType.FullName; Type arrayType = System.Type.GetType(typeStr.Substring(0, typeStr.IndexOf('['))); string[] objDatas = strValue.Trim().Split(';'); if (objDatas == null) { return; } Array array = null; if (startIndex > 0) { //non-zero bound array array = Array.CreateInstance(arrayType, new int[] { objDatas.Length }, new int[] { startIndex }); } else { //zero boud array array = Array.CreateInstance(arrayType, objDatas.Length); } int strIndex = 0; for (int index = array.GetLowerBound(0); index <= array.GetUpperBound(0); index++) { object objData = null; ConvertToType(arrayType, out objData, objDatas[strIndex]); array.SetValue(objData, index); strIndex++; } itemValue.SetData(array, EnumQuality.GOOD, DateTime.Now); } itemIds[i] = itemId; itemValues[i] = itemValue; } //Write DaItem Values ExecutionOptions executionOptions = new ExecutionOptions(); executionOptions.ExecutionType = EnumExecutionType.ASYNCHRONOUS; executionOptions.ExecutionContext = 0; int[] results; session.Write(itemIds, null, itemValues, out results, executionOptions); } }
/// <summary> /// Write Da Value /// </summary> /// <param name="strValue"></param> private void WriteValue(string strValue) { TreeNode selectedNode = browseTreeView.SelectedNode; if (selectedNode == null) { return; } if (selectedNode.Tag == null) { return; } //get the session if (browseTreeView.Nodes.Count == 0) { return; } TreeNode rootNode = browseTreeView.Nodes[0]; if (rootNode == null) { return; } if (rootNode.Tag == null) { return; } if (Type.ReferenceEquals(rootNode.Tag.GetType(), typeof(DaSession))) { DaSession session = rootNode.Tag as DaSession; if (session == null) { return; } //get the itemId string itemId = null; Type dataType = typeof(System.Byte); int startIndex = 0; if (Type.ReferenceEquals(selectedNode.Tag.GetType(), typeof(DaAddressSpaceElement))) { itemId = (selectedNode.Tag as DaAddressSpaceElement).ItemId; DaAddressSpaceElement addSpaceElem = selectedNode.Tag as DaAddressSpaceElement; //Get Data Type DaProperty[] properties = null; DaGetPropertiesOptions propertyGetOptions = new DaGetPropertiesOptions(); propertyGetOptions.WhatPropertyData = EnumPropertyData.ALL; if (ResultCode.SUCCEEDED(session.GetDaProperties(itemId, null, propertyGetOptions, out properties, null))) { if (properties == null) { dataType = typeof(System.Byte); } if (properties.Length < 5) { dataType = typeof(System.Byte); } dataType = DataTypeConverter.GetSysType((short)properties[0].ValueQT.Data); if (dataType.IsArray) { startIndex = ((properties[1].ValueQT.Data) as Array).GetLowerBound(0); } } } else if (Type.ReferenceEquals(selectedNode.Tag.GetType(), typeof(DaProperty))) { itemId = (selectedNode.Tag as DaProperty).Id.ToString(); dataType = (selectedNode.Tag as DaProperty).DataType; if (dataType.IsArray) { startIndex = (((selectedNode.Tag as DaProperty).ValueQT.Data) as Array).GetLowerBound(0); } } if (String.IsNullOrEmpty(itemId)) { return; } if (dataType == null) { return; } ValueQT itemValue = new ValueQT(); if (!dataType.IsArray) { //data type is not array object objData = null; ConvertToType(dataType, out objData, strValue); itemValue.SetData(objData, EnumQuality.GOOD, DateTime.Now); } else { //data type is array string typeStr = dataType.FullName; Type arrayType = System.Type.GetType(typeStr.Substring(0, typeStr.IndexOf('['))); string[] objDatas = strValue.Trim().Split(';'); if (objDatas == null) { return; } Array array = null; if (startIndex > 0) { //non-zero bound array array = Array.CreateInstance(arrayType, new int[] { objDatas.Length }, new int[] { startIndex }); } else { //zero boud array array = Array.CreateInstance(arrayType, objDatas.Length); } int strIndex = 0; for (int index = array.GetLowerBound(0); index <= array.GetUpperBound(0); index++) { object objData = null; ConvertToType(arrayType, out objData, objDatas[strIndex]); array.SetValue(objData, index); strIndex++; } itemValue.SetData(array, EnumQuality.GOOD, DateTime.Now); } //Write DaItem Values ExecutionOptions executionOptions = new ExecutionOptions(); executionOptions.ExecutionType = EnumExecutionType.ASYNCHRONOUS; executionOptions.ExecutionContext = 0; ValueQT[] itemValues = new ValueQT[] { itemValue }; int[] results; session.Write(new string[] { itemId }, null, itemValues, out results, executionOptions); } }