void InitSerial() { GXSerial serial = Media as GXSerial; byte Terminator = (byte)0x0A; if (serial != null && InitializeIEC) { serial.BaudRate = 300; serial.DataBits = 7; serial.Parity = Parity.Even; serial.StopBits = StopBits.One; } Media.Open(); //Query device information. if (Media != null && InitializeIEC) { string data = "/?!\r\n"; if (Trace) { Console.WriteLine("IEC sending:" + data); } ReceiveParameters <string> p = new ReceiveParameters <string>() { Eop = Terminator, WaitTime = WaitTime }; lock (Media.Synchronous) { WriteTrace("<- " + DateTime.Now.ToLongTimeString() + "\t" + GXCommon.ToHex(ASCIIEncoding.ASCII.GetBytes(data), true)); Media.Send(data, null); if (!Media.Receive(p)) { //Try to move away from mode E. try { GXReplyData reply = new GXReplyData(); ReadDLMSPacket(Client.DisconnectRequest(), reply); } catch (Exception) { } data = (char)0x01 + "B0" + (char)0x03; Media.Send(data, null); p.Count = 1; if (!Media.Receive(p)) { } data = "Failed to receive reply from the device in given time."; Console.WriteLine(data); throw new Exception(data); } WriteTrace("-> " + DateTime.Now.ToLongTimeString() + "\t" + GXCommon.ToHex(ASCIIEncoding.ASCII.GetBytes(p.Reply), true)); //If echo is used. if (p.Reply == data) { p.Reply = null; if (!Media.Receive(p)) { //Try to move away from mode E. GXReplyData reply = new GXReplyData(); ReadDLMSPacket(Client.DisconnectRequest(), reply); if (serial != null) { data = (char)0x01 + "B0" + (char)0x03; Media.Send(data, null); p.Count = 1; Media.Receive(p); serial.BaudRate = 9600; data = (char)0x01 + "B0" + (char)0x03 + "\r\n"; Media.Send(data, null); p.Count = 1; Media.Receive(p); } data = "Failed to receive reply from the device in given time."; Console.WriteLine(data); throw new Exception(data); } WriteTrace("-> " + DateTime.Now.ToLongTimeString() + "\t" + GXCommon.ToHex(ASCIIEncoding.ASCII.GetBytes(p.Reply), true)); } } Console.WriteLine("IEC received: " + p.Reply); if (p.Reply[0] != '/') { p.WaitTime = 100; Media.Receive(p); throw new Exception("Invalid responce."); } string manufactureID = p.Reply.Substring(1, 3); UpdateManufactureSettings(manufactureID); char baudrate = p.Reply[4]; int BaudRate = 0; switch (baudrate) { case '0': BaudRate = 300; break; case '1': BaudRate = 600; break; case '2': BaudRate = 1200; break; case '3': BaudRate = 2400; break; case '4': BaudRate = 4800; break; case '5': BaudRate = 9600; break; case '6': BaudRate = 19200; break; default: throw new Exception("Unknown baud rate."); } Console.WriteLine("BaudRate is : " + BaudRate.ToString()); //Send ACK //Send Protocol control character byte controlCharacter = (byte)'2';// "2" HDLC protocol procedure (Mode E) //Send Baudrate character //Mode control character byte ModeControlCharacter = (byte)'2';//"2" //(HDLC protocol procedure) (Binary mode) //Set mode E. byte[] arr = new byte[] { 0x06, controlCharacter, (byte)baudrate, ModeControlCharacter, 13, 10 }; Console.WriteLine("Moving to mode E.", arr); lock (Media.Synchronous) { WriteTrace("<- " + DateTime.Now.ToLongTimeString() + "\t" + GXCommon.ToHex(arr, true)); Media.Send(arr, null); p.Reply = null; if (!Media.Receive(p)) { //Try to move away from mode E. GXReplyData reply = new GXReplyData(); ReadDLMSPacket(Client.DisconnectRequest(), reply); data = "Failed to receive reply from the device in given time."; Console.WriteLine(data); throw new Exception(data); } WriteTrace("-> " + DateTime.Now.ToLongTimeString() + "\t" + GXCommon.ToHex(ASCIIEncoding.ASCII.GetBytes(p.Reply), true)); Console.WriteLine("Received: " + p.Reply); if (serial != null) { System.Threading.Thread.Sleep(400); serial.Close(); serial.BaudRate = BaudRate; serial.DataBits = 8; serial.Parity = Parity.None; serial.StopBits = StopBits.One; System.Threading.Thread.Sleep(400); serial.Open(); serial.ResetSynchronousBuffer(); } } } }