GXDLMS implements methods to communicate with DLMS/COSEM metering devices.
Esempio n. 1
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public GXCommunicatation(Gurux.DLMS.GXDLMS dlms, IGXMedia media, bool initializeIEC, Gurux.DLMS.Authentication authentication, string password)
 {
     m_Parser                = dlms;
     Media                   = media;
     InitializeIEC           = initializeIEC;
     m_Parser.Authentication = authentication;
     m_Parser.Password       = password;
 }
Esempio n. 2
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public GXCommunicatation(Gurux.DLMS.GXDLMS dlms, IGXMedia media, bool initializeIEC, Gurux.DLMS.Authentication authentication, string password)
 {
     m_Parser = dlms;
     Media = media;
     InitializeIEC = initializeIEC;
     m_Parser.Authentication = authentication;
     m_Parser.Password = password;
 }
Esempio n. 3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="useLogicalNameReferencing">Is Logical or short name referencing used.</param>
 /// <param name="clientID">Client ID. Default is 0x21</param>
 /// <param name="ServerID">Server ID. Default is 3.</param>
 /// <param name="authentication">Authentication type. Default is None</param>
 /// <param name="password">Password if authentication is used.</param>
 /// <param name="interfaceType">Interface type. Default is general.</param>
 public GXDLMSClient(bool useLogicalNameReferencing, 
     object clientID, object ServerID, Authentication authentication,
     string password, InterfaceType interfaceType)
 {
     m_Base = new GXDLMS(false);
     Objects = new GXDLMSObjectCollection();
     this.UseLogicalNameReferencing = useLogicalNameReferencing;
     this.InterfaceType = interfaceType;
     this.Authentication = authentication;
     this.ClientID = clientID;
     this.ServerID = ServerID;
     this.Password = ASCIIEncoding.ASCII.GetBytes(password);
 }
Esempio n. 4
0
 public void Close()
 {
     if (Media != null && m_Parser != null)
     {
         try
         {
             Console.WriteLine("Disconnecting from the meter.");
             ReadDLMSPacket(m_Parser.DisconnectRequest());
             Media.Close();
         }
         catch
         {
         }
         Media    = null;
         m_Parser = null;
     }
 }
Esempio n. 5
0
        public void Close()
        {
            if (Media != null && m_Parser != null)
            {
                try
                {
                    Console.WriteLine("Disconnecting from the meter.");
                    ReadDLMSPacket(m_Parser.DisconnectRequest());
                    Media.Close();
                }
                catch
                {

                }
                Media = null;
                m_Parser = null;
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public GXDLMSClient()
 {
     m_Base = new GXDLMS(false);
     this.Authentication = Authentication.None;
     Objects = new GXDLMSObjectCollection();
 }
Esempio n. 7
0
 /// <summary>
 /// Constructor.
 /// </summary>        
 public GXDLMSServerBase(bool logicalNameReferencing)
 {
     m_Base = new GXDLMS(true);
     m_Base.UseLogicalNameReferencing = logicalNameReferencing;
     //TODO: StartProtocol = StartProtocolType.DLMS;
     Reset();
     Items = new GXDLMSObjectCollection(this);
     m_Base.LNSettings = new GXDLMSLNSettings(new byte[] { 0x00, 0x7E, 0x1F });
     m_Base.SNSettings = new GXDLMSSNSettings(new byte[] { 0x1C, 0x03, 0x20 });
     ServerIDs = new List<object>();
     this.InterfaceType = InterfaceType.General;
 }
Esempio n. 8
0
        static void Main(string[] args)
        {
            IGXMedia media = null;
            GXCommunicatation comm = null;
            try
            {
                ////////////////////////////////////////
                //Handle command line parameters.
                String id = "", host = "", port = "", pw = "";
                bool trace = false, iec = true, isSerial = false;
                Authentication auth = Authentication.High;

                foreach (string it in args)
                {
                    String item = it.Trim().ToLower();
                    if (string.Compare(item, "/u", true) == 0)//Update
                    {
                        //Get latest manufacturer settings from Gurux web server.
                        GXManufacturerCollection.UpdateManufactureSettings();
                    }
                    else if (item.StartsWith("/m="))//Manufacturer
                    {
                        id = item.Replace("/m=", "");
                    }
                    else if (item.StartsWith("/h=")) //Host
                    {
                        host = item.Replace("/h=", "");
                    }
                    else if (item.StartsWith("/p="))// TCP/IP Port
                    {
                        media = new Gurux.Net.GXNet();
                        port = item.Replace("/p=", "");
                    }
                    else if (item.StartsWith("/sp="))//Serial Port
                    {
                        port = item.Replace("/sp=", "");
                        isSerial = true;
                        media = new GXSerial();
                    }
                    else if (item.StartsWith("/t"))//Are messages traced.
                    {
                        trace = true;
                    }
                    else if (item.StartsWith("/s="))//Start
                    {
                        String tmp = item.Replace("/s=", "");
                        iec = string.Compare(tmp, "dlms", true) != 0;
                    }
                    else if (item.StartsWith("/a="))//Authentication
                    {
                        auth = (Authentication)Enum.Parse(typeof(Authentication), it.Trim().Replace("/a=", ""));
                    }
                    else if (item.StartsWith("/pw="))//Password
                    {
                        pw = it.Trim().Replace("/pw=", "");
                    }
                    else
                    {
                        ShowHelp();
                        return;
                    }
                }
                //if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(port) || (!isSerial && string.IsNullOrEmpty(host)))
                //{
                   // ShowHelp();
                   // return;
                //}
                ////////////////////////////////////////
                //Initialize connection settings.
                if (isSerial)
                {
                    GXSerial serial = media as GXSerial;
                    string[] t = GXSerial.GetPortNames();
                    serial.PortName = port;
                    if (iec)
                    {
                        serial.BaudRate = 300;
                        serial.DataBits = 7;
                        serial.Parity = System.IO.Ports.Parity.Even;
                        serial.StopBits = System.IO.Ports.StopBits.One;
                    }
                    else
                    {
                        serial.BaudRate = 9600;
                        serial.DataBits = 8;
                        serial.Parity = System.IO.Ports.Parity.None;
                        serial.StopBits = System.IO.Ports.StopBits.One;
                    }
                }
                else
                {
                    Gurux.Net.GXNet net = media as Gurux.Net.GXNet;
                    net.Port = Convert.ToInt32(port);
                    net.HostName = host;
                    net.Protocol = Gurux.Net.NetworkType.Tcp;

                }
                ////////////////////////////////////////
                //Update manufacturer debended settings.
                GXManufacturerCollection Manufacturers = new GXManufacturerCollection();
                GXManufacturerCollection.ReadManufacturerSettings(Manufacturers);
                GXManufacturer man = Manufacturers.FindByIdentification(id);
                if (man == null)
                {
                    throw new Exception("Unknown manufacturer: " + id);
                }
                Gurux.DLMS.GXDLMS dlms = new Gurux.DLMS.GXDLMS();
                //Update Obis code list so we can get right descriptions to the objects.
                dlms.ObisCodes = man.ObisCodes;
                comm = new GXCommunicatation(dlms, media, iec, auth, pw);
                comm.Trace = trace;
                comm.InitializeConnection(man);

                //Save Association view to the cache so it is not needed to retreave every time.
                string path = man.Identification + ".xml";
                Gurux.DLMS.GXDLMSObjectCollection objects = null;
                Type[] extraTypes = new Type[] { typeof(GXManufacturerCollection), typeof(GXManufacturer), typeof(GXObisCodeCollection), typeof(GXObisCode), typeof(GXObisValueItem), typeof(GXObisValueItemCollection), typeof(GXDLMSProfileGenericObject) };
                XmlSerializer x = new XmlSerializer(typeof(GXDLMSObjectCollection), extraTypes);
                if (File.Exists(path))
                {
                    try
                    {
                        using (Stream stream = File.Open(path, FileMode.Open))
                        {
                            Console.WriteLine("Get available objects from the cache.");
                            objects = x.Deserialize(stream) as Gurux.DLMS.GXDLMSObjectCollection;
                            stream.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                        throw ex;
                    }
                }
                else
                {
                    Console.WriteLine("Get available objects from the device.");
                    objects = comm.GetAssociationView();
                    try
                    {
                        using (Stream stream = File.Open(path, FileMode.Create))
                        {
                            TextWriter writer = new StreamWriter(stream);
                            x.Serialize(writer, objects);
                            writer.Close();
                            stream.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                        throw ex;
                    }
                    Console.WriteLine("--- Available objects ---");
                    foreach (Gurux.DLMS.GXDLMSObject it in objects)
                    {
                        Console.WriteLine(it.Name + " " + it.Description);
                    }
                }
                //Read available clock and data objects.
                foreach (Gurux.DLMS.GXDLMSObject it in objects.GetObjects(new ObjectType[] { ObjectType.Clock, ObjectType.Data}))
                {
                    object value = comm.Read(it, 2);
                    Console.WriteLine(it.Name + " " + it.Description + " " + value);
                }
                //Read Profile Generic columns.
                GXDLMSObjectCollection cols = null;
                foreach (Gurux.DLMS.GXDLMSObject it in objects.GetObjects(ObjectType.ProfileGeneric))
                {
                    Console.WriteLine("Profile Generic " + it.Name + " Columns:");
                    cols = comm.GetColumns(it);
                    foreach (GXDLMSObject col in cols)
                    {
                        Console.Write(col.Name + " | ");
                    }
                    Console.WriteLine("");
                    //Read first row from Profile Generic.
                    Console.WriteLine("Profile Generic " + it.Name + " Columns:");
                    object[] rows = comm.ReadRowsByEntry(it, 0, 1, cols);
                    foreach (object[] row in rows)
                    {
                        foreach (object cell in row)
                        {
                            Console.Write(cell + " | ");
                        }
                        Console.WriteLine("");
                    }
                    //Read last dayfrom Profile Generic.
                    Console.WriteLine("Profile Generic " + it.Name + " Columns:");
                    rows = comm.ReadRowsByRange(it, DateTime.Now.Date, DateTime.MaxValue, cols);
                    foreach (object[] row in rows)
                    {
                        foreach (object cell in row)
                        {
                            Console.Write(cell + " | ");
                        }
                        Console.WriteLine("");
                    }
                }
            }
            catch (Exception ex)
            {
                if (comm != null)
                {
                    comm.Close();
                }
                Console.WriteLine(ex.Message);
                if (!System.Diagnostics.Debugger.IsAttached)
                {
                    Console.ReadKey();
                }
            }
            finally
            {
                comm.Close();
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    Console.ReadKey();
                }
            }
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            IGXMedia          media = null;
            GXCommunicatation comm  = null;

            try
            {
                ////////////////////////////////////////
                //Handle command line parameters.
                String         id = "", host = "", port = "", pw = "";
                bool           trace = false, iec = true, isSerial = false;
                Authentication auth = Authentication.High;

                foreach (string it in args)
                {
                    String item = it.Trim().ToLower();
                    if (string.Compare(item, "/u", true) == 0)//Update
                    {
                        //Get latest manufacturer settings from Gurux web server.
                        GXManufacturerCollection.UpdateManufactureSettings();
                    }
                    else if (item.StartsWith("/m="))//Manufacturer
                    {
                        id = item.Replace("/m=", "");
                    }
                    else if (item.StartsWith("/h=")) //Host
                    {
                        host = item.Replace("/h=", "");
                    }
                    else if (item.StartsWith("/p="))// TCP/IP Port
                    {
                        media = new Gurux.Net.GXNet();
                        port  = item.Replace("/p=", "");
                    }
                    else if (item.StartsWith("/sp="))//Serial Port
                    {
                        port     = item.Replace("/sp=", "");
                        isSerial = true;
                        media    = new GXSerial();
                    }
                    else if (item.StartsWith("/t"))//Are messages traced.
                    {
                        trace = true;
                    }
                    else if (item.StartsWith("/s="))//Start
                    {
                        String tmp = item.Replace("/s=", "");
                        iec = string.Compare(tmp, "dlms", true) != 0;
                    }
                    else if (item.StartsWith("/a="))//Authentication
                    {
                        auth = (Authentication)Enum.Parse(typeof(Authentication), it.Trim().Replace("/a=", ""));
                    }
                    else if (item.StartsWith("/pw="))//Password
                    {
                        pw = it.Trim().Replace("/pw=", "");
                    }
                    else
                    {
                        ShowHelp();
                        return;
                    }
                }
                //if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(port) || (!isSerial && string.IsNullOrEmpty(host)))
                //{
                // ShowHelp();
                // return;
                //}
                ////////////////////////////////////////
                //Initialize connection settings.
                if (isSerial)
                {
                    GXSerial serial = media as GXSerial;
                    string[] t      = GXSerial.GetPortNames();
                    serial.PortName = port;
                    if (iec)
                    {
                        serial.BaudRate = 300;
                        serial.DataBits = 7;
                        serial.Parity   = System.IO.Ports.Parity.Even;
                        serial.StopBits = System.IO.Ports.StopBits.One;
                    }
                    else
                    {
                        serial.BaudRate = 9600;
                        serial.DataBits = 8;
                        serial.Parity   = System.IO.Ports.Parity.None;
                        serial.StopBits = System.IO.Ports.StopBits.One;
                    }
                }
                else
                {
                    Gurux.Net.GXNet net = media as Gurux.Net.GXNet;
                    net.Port     = Convert.ToInt32(port);
                    net.HostName = host;
                    net.Protocol = Gurux.Net.NetworkType.Tcp;
                }
                ////////////////////////////////////////
                //Update manufacturer debended settings.
                GXManufacturerCollection Manufacturers = new GXManufacturerCollection();
                GXManufacturerCollection.ReadManufacturerSettings(Manufacturers);
                GXManufacturer man = Manufacturers.FindByIdentification(id);
                if (man == null)
                {
                    throw new Exception("Unknown manufacturer: " + id);
                }
                Gurux.DLMS.GXDLMS dlms = new Gurux.DLMS.GXDLMS();
                //Update Obis code list so we can get right descriptions to the objects.
                dlms.ObisCodes = man.ObisCodes;
                comm           = new GXCommunicatation(dlms, media, iec, auth, pw);
                comm.Trace     = trace;
                comm.InitializeConnection(man);

                //Save Association view to the cache so it is not needed to retreave every time.
                string path = man.Identification + ".xml";
                Gurux.DLMS.GXDLMSObjectCollection objects = null;
                Type[]        extraTypes = new Type[] { typeof(GXManufacturerCollection), typeof(GXManufacturer), typeof(GXObisCodeCollection), typeof(GXObisCode), typeof(GXObisValueItem), typeof(GXObisValueItemCollection), typeof(GXDLMSProfileGenericObject) };
                XmlSerializer x          = new XmlSerializer(typeof(GXDLMSObjectCollection), extraTypes);
                if (File.Exists(path))
                {
                    try
                    {
                        using (Stream stream = File.Open(path, FileMode.Open))
                        {
                            Console.WriteLine("Get available objects from the cache.");
                            objects = x.Deserialize(stream) as Gurux.DLMS.GXDLMSObjectCollection;
                            stream.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                        throw ex;
                    }
                }
                else
                {
                    Console.WriteLine("Get available objects from the device.");
                    objects = comm.GetAssociationView();
                    try
                    {
                        using (Stream stream = File.Open(path, FileMode.Create))
                        {
                            TextWriter writer = new StreamWriter(stream);
                            x.Serialize(writer, objects);
                            writer.Close();
                            stream.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                        throw ex;
                    }
                    Console.WriteLine("--- Available objects ---");
                    foreach (Gurux.DLMS.GXDLMSObject it in objects)
                    {
                        Console.WriteLine(it.Name + " " + it.Description);
                    }
                }
                //Read available clock and data objects.
                foreach (Gurux.DLMS.GXDLMSObject it in objects.GetObjects(new ObjectType[] { ObjectType.Clock, ObjectType.Data }))
                {
                    object value = comm.Read(it, 2);
                    Console.WriteLine(it.Name + " " + it.Description + " " + value);
                }
                //Read Profile Generic columns.
                GXDLMSObjectCollection cols = null;
                foreach (Gurux.DLMS.GXDLMSObject it in objects.GetObjects(ObjectType.ProfileGeneric))
                {
                    Console.WriteLine("Profile Generic " + it.Name + " Columns:");
                    cols = comm.GetColumns(it);
                    foreach (GXDLMSObject col in cols)
                    {
                        Console.Write(col.Name + " | ");
                    }
                    Console.WriteLine("");
                    //Read first row from Profile Generic.
                    Console.WriteLine("Profile Generic " + it.Name + " Columns:");
                    object[] rows = comm.ReadRowsByEntry(it, 0, 1, cols);
                    foreach (object[] row in rows)
                    {
                        foreach (object cell in row)
                        {
                            Console.Write(cell + " | ");
                        }
                        Console.WriteLine("");
                    }
                    //Read last dayfrom Profile Generic.
                    Console.WriteLine("Profile Generic " + it.Name + " Columns:");
                    rows = comm.ReadRowsByRange(it, DateTime.Now.Date, DateTime.MaxValue, cols);
                    foreach (object[] row in rows)
                    {
                        foreach (object cell in row)
                        {
                            Console.Write(cell + " | ");
                        }
                        Console.WriteLine("");
                    }
                }
            }
            catch (Exception ex)
            {
                if (comm != null)
                {
                    comm.Close();
                }
                Console.WriteLine(ex.Message);
                if (!System.Diagnostics.Debugger.IsAttached)
                {
                    Console.ReadKey();
                }
            }
            finally
            {
                comm.Close();
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    Console.ReadKey();
                }
            }
        }
Esempio n. 10
0
 /// <summary>
 /// Constructor.
 /// </summary>        
 public GXDLMSServer(bool logicalNameReferencing)
 {
     m_Base = new GXDLMS(true);
     m_Base.UseLogicalNameReferencing = logicalNameReferencing;
     //TODO: StartProtocol = StartProtocolType.DLMS;
     Reset();
     Items = new GXDLMSObjectCollection();            
     m_Base.LNSettings = new GXDLMSLNSettings(new byte[] { 0x00, 0x7E, 0x1F });
     m_Base.SNSettings = new GXDLMSSNSettings(new byte[] { 0x1C, 0x03, 0x20 });
     Authentications = new List<GXAuthentication>();
     Authentications.Add(new GXAuthentication(Authentication.None, "", (byte) 0x10));
     Authentications.Add(new GXAuthentication(Authentication.Low, "GuruxLow", (byte) 0x20));
     Authentications.Add(new GXAuthentication(Authentication.High, "GuruxHigh", (byte) 0x40));
     ServerIDs = new List<object>();
     ServerIDs.Add(CountServerID((byte)1, 0));
 }