static void Main(string[] args) { Gurux.Net.GXNet conn = new Gurux.Net.GXNet(); conn.HostName = "192.168.0.1"; conn.Port = 5000; conn.Protocol = Gurux.Net.NetworkType.Tcp; }
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(); } } }
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(); } } }