public void Write(GXDLMSObject obj, object target, int index, List<object> UpdatedObjects) { object value; GXReplyData reply = new GXReplyData(); for (int it = 1; it != (obj as IGXDLMSBase).GetAttributeCount() + 1; ++it) { reply.Clear(); if (obj.GetDirty(it, out value)) { //Read DLMS data type if not known. DataType type = obj.GetDataType(it); if (type == DataType.None) { byte[] data = client.Read(obj, it)[0]; ReadDataBlock(data, "Write object type " + obj.ObjectType, reply); type = reply.DataType; if (type == DataType.None) { throw new Exception("Failed to write value. Data type not set."); } obj.SetDataType(it, type); } try { foreach (byte[] tmp in client.Write(obj.Name, value, type, obj.ObjectType, it)) { ReadDataBlock(tmp, "Write object", reply); } obj.ClearDirty(it); //Read data once again to make sure it is updated. byte[] data = client.Read(obj, it)[0]; ReadDataBlock(data, "Read object " + obj.ObjectType, reply); value = reply.Value; if (value is byte[] && (type = obj.GetUIDataType(it)) != DataType.None) { value = GXDLMSClient.ChangeType((byte[])value, type); } client.UpdateValue(obj, it, value); } catch (GXDLMSException ex) { if (ex.ErrorCode == 3) { throw new Exception("Read/Write Failed."); } else { throw ex; } } } } }
public void Write(GXDLMSObject obj, object target, int index, List<object> UpdatedObjects) { for (int it = 1; it != (obj as IGXDLMSBase).GetAttributeCount() + 1; ++it) { object value; if (obj.GetDirty(it, out value)) { //Read DLMS data type if not known. DataType type = obj.GetDataType(it); if (type == DataType.None) { byte[] data = m_Cosem.Read(obj.Name, obj.ObjectType, it)[0]; data = ReadDataBlock(data, "Write object type " + obj.ObjectType); type = m_Cosem.GetDLMSDataType(data); if (type == DataType.None) { throw new Exception("Failed to write value. Data type not set."); } obj.SetDataType(it, type); } try { foreach (byte[] tmp in m_Cosem.Write(obj.Name, value, type, obj.ObjectType, it)) { ReadDataBlock(tmp, "Write object"); } obj.ClearDirty(it); //Read data once again to make sure it is updated. byte[] data = m_Cosem.Read(obj.Name, obj.ObjectType, it)[0]; data = ReadDataBlock(data, "Read object " + obj.ObjectType); value = m_Cosem.GetValue(data); obj.SetValue(it, value); } catch (GXDLMSException ex) { if (ex.ErrorCode == 3) { throw new Exception("Read/Write Failed."); } else { throw ex; } } } } }
public object ReadElementContentAsObject(string name, object defaultValue, GXDLMSObject obj, byte index) { GetNext(); if (string.Compare(name, reader.Name, true) == 0) { object ret; DataType uiType; DataType dt = (DataType)Enum.Parse(typeof(DataType), reader.GetAttribute(0)); if (obj != null) { obj.SetDataType(index, dt); } if (reader.AttributeCount > 1) { uiType = (DataType)Enum.Parse(typeof(DataType), reader.GetAttribute(1)); } else { uiType = dt; } if (obj != null && obj.GetUIDataType(index) == DataType.None) { obj.SetUIDataType(index, uiType); } if (dt == DataType.Array || dt == DataType.Structure) { reader.Read(); GetNext(); ret = ReadArray(); ReadEndElement(name); return(ret); } else { string str = reader.ReadElementContentAsString(); ret = GXDLMSConverter.ChangeType(str, uiType, CultureInfo.InvariantCulture); } while (!(reader.NodeType == XmlNodeType.Element || reader.NodeType == XmlNodeType.EndElement)) { reader.Read(); } return(ret); } return(defaultValue); }
public void ReadValue(GXDLMSObject it, int attributeOrdinal) { GXReplyData reply = new GXReplyData(); ReadDataBlock(Read(it, attributeOrdinal), "Read object", reply); //If data type is unknown if (it.GetDataType(attributeOrdinal) == DataType.None) { it.SetDataType(attributeOrdinal, reply.DataType); } client.UpdateValue(it, attributeOrdinal, reply.Value); }
private static void UpdateOBISCodeInfo(GXStandardObisCodeCollection codes, GXDLMSObject it) { if (!string.IsNullOrEmpty(it.Description)) { return; } GXStandardObisCode code = codes.Find(it.LogicalName, it.ObjectType)[0]; it.Description = code.Description; //If string is used if (code.DataType.Contains("10")) { code.UIDataType = "10"; } //If date time is used. else if (code.DataType.Contains("25") || code.DataType.Contains("26")) { code.UIDataType = code.DataType = "25"; } //Time stamps of the billing periods objects (first scheme if there are two) else if (code.DataType.Contains("9")) { if ((GXStandardObisCodeCollection.EqualsMask("0.0-64.96.7.10-14.255", it.LogicalName) || //Time stamps of the billing periods objects (second scheme) GXStandardObisCodeCollection.EqualsMask("0.0-64.0.1.5.0-99,255", it.LogicalName) || //Time of power failure GXStandardObisCodeCollection.EqualsMask("0.0-64.0.1.2.0-99,255", it.LogicalName) || //Time stamps of the billing periods objects (first scheme if there are two) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.1.2.0-99,255", it.LogicalName) || //Time stamps of the billing periods objects (second scheme) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.1.5.0-99,255", it.LogicalName) || //Time expired since last end of billing period GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.0.255", it.LogicalName) || //Time of last reset GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.6.255", it.LogicalName) || //Date of last reset GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.7.255", it.LogicalName) || //Time expired since last end of billing period (Second billing period scheme) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.13.255", it.LogicalName) || //Time of last reset (Second billing period scheme) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.14.255", it.LogicalName) || //Date of last reset (Second billing period scheme) GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.15.255", it.LogicalName))) { code.UIDataType = "25"; } //Local time else if (GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.1.255", it.LogicalName)) { code.UIDataType = "27"; } //Local date else if (GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.2.255", it.LogicalName)) { code.UIDataType = "26"; } //Active firmware identifier else if (GXStandardObisCodeCollection.EqualsMask("1.0.0.2.0.255", it.LogicalName)) { code.UIDataType = "10"; } } if (code.DataType != "*" && code.DataType != string.Empty && !code.DataType.Contains(",")) { DataType type = (DataType)int.Parse(code.DataType); switch (it.ObjectType) { case ObjectType.Data: case ObjectType.Register: case ObjectType.RegisterActivation: case ObjectType.ExtendedRegister: it.SetDataType(2, type); break; default: break; } } if (!string.IsNullOrEmpty(code.UIDataType)) { DataType uiType = (DataType)int.Parse(code.UIDataType); switch (it.ObjectType) { case ObjectType.Data: case ObjectType.Register: case ObjectType.RegisterActivation: case ObjectType.ExtendedRegister: it.SetUIDataType(2, uiType); break; default: break; } } }
/// <summary> /// Read attribute value. /// </summary> public object Read(GXDLMSObject it, int attributeIndex) { byte[] reply = ReadDataBlock(Client.Read(it.Name, it.ObjectType, attributeIndex)[0]); //Update data type. if (it.GetDataType(attributeIndex) == DataType.None) { it.SetDataType(attributeIndex, Client.GetDLMSDataType(reply)); } return Client.UpdateValue(reply, it, attributeIndex); }
/// <summary> /// Read attribute value. /// </summary> public object Read(GXDLMSObject it, int attributeIndex) { GXReplyData reply = new GXReplyData(); if (!ReadDataBlock(Client.Read(it, attributeIndex), reply)) { reply.Clear(); Thread.Sleep(1000); if (!ReadDataBlock(Client.Read(it, attributeIndex), reply)) { if (reply.Error != 0) { throw new GXDLMSException(reply.Error); } } } //Update data type. if (it.GetDataType(attributeIndex) == DataType.None) { it.SetDataType(attributeIndex, reply.DataType); } return Client.UpdateValue(it, attributeIndex, reply.Value); }