/// <summary> /// Write Address Space Element /// </summary> /// <param name="session"></param> /// <param name="itemId"></param> public void WriteAddresSpaceItem(DaSession session, string itemId, object selectedObject, bool isAddSpaceElem) { Helper.DataTypes dataTypeHelper = new DataTypes(); this.panelBottom.Visible = true; isDaItem = false; this.session = session; ValueQT[] itemValues; int[] itemResults; int result = session.Read(MAX_AGE_DEVICE, new string[] { itemId }, null, out itemValues, out itemResults, null); if (itemResults.Length == 0) { return; } if (itemValues.Length == 0) { return; } Type itemDataType = typeof(short); EnumAccessRights itemAccessRighs = EnumAccessRights.READWRITEABLE; bool hasWriteAccess = true; if (ResultCode.SUCCEEDED(result) && ResultCode.SUCCEEDED(itemResults[0])) { DaGetPropertiesOptions propertyGetOptions = new DaGetPropertiesOptions(); propertyGetOptions.WhatPropertyData = EnumPropertyData.ALL; DaProperty[] properties = null; if (isAddSpaceElem) { if (ResultCode.SUCCEEDED(session.GetDaProperties(itemId, null, propertyGetOptions, out properties, null))) { if (properties == null) { this.propertyGrid.SelectedObject = FillEmptyValueQT(itemId, null); return; } if (properties.Length < 5) { this.propertyGrid.SelectedObject = FillEmptyValueQT(itemId, null); return; } itemDataType = DataTypeConverter.GetSysType((short)properties[0].ValueQT.Data); itemAccessRighs = ObjToAccesssRights(properties[4].ValueQT.Data); } } else { itemDataType = (selectedObject as DaProperty).DataType; } //the item value is null if (itemValues[0] == null) { this.propertyGrid.SelectedObject = FillEmptyValueQT(itemId, itemDataType); return; } if (itemAccessRighs == EnumAccessRights.READABLE) { hasWriteAccess = false; } } this.propertyGrid.SelectedObject = FillWriteService(itemId, itemDataType, hasWriteAccess, itemValues[0]); }
/// <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); } }