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
                    {
                        ValueEventArgs e = new ValueEventArgs(obj, it, 0, null);
                        e.Value = value;
                        ((IGXDLMSBase)obj).SetValue(null, e);
                        foreach (byte[] tmp in client.Write(obj, it))
                        {
                            ReadDataBlock(tmp, "Write object", reply);
                        }
                        obj.ClearDirty(it);
                        //Read data once again to make sure it is updated.
                        reply.Clear();
                        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;
                 }
             }
         }
     }
 }
Exemple #3
0
        public void Write(GXDLMSObject obj, int index)
        {
            object      val;
            GXReplyData reply = new GXReplyData();

            for (int it = 1; it != (obj as IGXDLMSBase).GetAttributeCount() + 1; ++it)
            {
                reply.Clear();
                if (it == index || (index == 0 && obj.GetDirty(it, out val)))
                {
                    bool forced = false;
                    GXDLMSAttributeSettings att = obj.Attributes.Find(it);
                    //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, "Read 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);
                        reply.Clear();
                    }
                    try
                    {
                        if (att != null && att.ForceToBlocks)
                        {
                            forced = client.ForceToBlocks = true;
                        }
                        foreach (byte[] tmp in client.Write(obj, it))
                        {
                            ReadDataBlock(tmp, string.Format("Writing object {0}, interface {1}", obj.LogicalName, obj.ObjectType), reply);
                        }
                        obj.ClearDirty(it);
                        //Read data once again to make sure it is updated.
                        reply.Clear();
                        byte[] data = client.Read(obj, it)[0];
                        ReadDataBlock(data, string.Format("Reading object {0}, interface {1}", obj.LogicalName, obj.ObjectType), reply);
                        val = reply.Value;
                        if (val is byte[] && (type = obj.GetUIDataType(it)) != DataType.None)
                        {
                            val = GXDLMSClient.ChangeType((byte[])val, type);
                        }
                        client.UpdateValue(obj, it, val);
                    }
                    catch (GXDLMSException ex)
                    {
                        if (ex.ErrorCode == 3)
                        {
                            throw new Exception("Read/Write Failed.");
                        }
                        else
                        {
                            throw ex;
                        }
                    }
                    finally
                    {
                        if (forced)
                        {
                            client.ForceToBlocks = false;
                        }
                    }
                }
            }
        }