public void InitializeConnection() { if (!string.IsNullOrEmpty(Parent.Manufacturer)) { UpdateManufactureSettings(Parent.Manufacturer); } if (Media is GXSerial) { GXLogWriter.WriteLog("Initializing serial connection."); InitSerial(); ConnectionStartTime = DateTime.Now; } else if (Media is GXNet) { GXLogWriter.WriteLog("Initializing Network connection."); InitNet(); //Some Electricity meters need some time before first message can be send. System.Threading.Thread.Sleep(500); } else if (Media is Gurux.Terminal.GXTerminal) { GXLogWriter.WriteLog("Initializing Terminal connection."); InitTerminal(); } else { throw new Exception("Unknown media type."); } try { byte[] data, reply = null; data = SNRMRequest(); if (data != null) { GXLogWriter.WriteLog("Send SNRM request.", data); reply = ReadDLMSPacket(data, 1); GXLogWriter.WriteLog("Parsing UA reply.", reply); //Has server accepted client. ParseUAResponse(reply); GXLogWriter.WriteLog("Parsing UA reply succeeded."); } //Generate AARQ request. //Split requests to multiple packets if needed. //If password is used all data might not fit to one packet. foreach (byte[] it in AARQRequest()) { GXLogWriter.WriteLog("Send AARQ request", it); reply = ReadDLMSPacket(it); } GXLogWriter.WriteLog("Parsing AARE reply", (byte[])reply); try { //Parse reply. ParseAAREResponse(reply); } catch (Exception Ex) { ReadDLMSPacket(DisconnectRequest(), 1); throw Ex; } //If authentication is required. if (m_Cosem.IsAuthenticationRequired) { foreach (byte[] it in m_Cosem.GetApplicationAssociationRequest()) { GXLogWriter.WriteLog("Authenticating", it); reply = ReadDLMSPacket(it); } m_Cosem.ParseApplicationAssociationResponse(reply); } } catch (Exception ex) { if (Media is GXSerial && Parent.StartProtocol == StartProtocolType.IEC) { ReceiveParameters <string> p = new ReceiveParameters <string>() { Eop = (byte)0xA, WaitTime = Parent.WaitTime * 1000 }; lock (Media.Synchronous) { string data = (char)0x01 + "B0" + (char)0x03 + "\r\n"; Media.Send(data, null); Media.Receive(p); } } throw ex; } GXLogWriter.WriteLog("Parsing AARE reply succeeded."); Parent.KeepAliveStart(); }
public void InitializeConnection(GXManufacturer man) { Manufacturer = man; UpdateManufactureSettings(man.Identification); if (Media is GXSerial) { Console.WriteLine("Initializing serial connection."); InitSerial(); } else if (Media is GXNet) { Console.WriteLine("Initializing Network connection."); InitNet(); //Some Electricity meters need some time before first message can be send. System.Threading.Thread.Sleep(500); } else { throw new Exception("Unknown media type."); } GXReplyData reply = new GXReplyData(); byte[] data; data = Client.SNRMRequest(); if (data != null) { if (Trace) { Console.WriteLine("Send SNRM request." + GXCommon.ToHex(data, true)); } ReadDLMSPacket(data, reply); if (Trace) { Console.WriteLine("Parsing UA reply." + reply.ToString()); } //Has server accepted client. Client.ParseUAResponse(reply.Data); Console.WriteLine("Parsing UA reply succeeded."); } //Generate AARQ request. //Split requests to multiple packets if needed. //If password is used all data might not fit to one packet. foreach (byte[] it in Client.AARQRequest()) { if (Trace) { Console.WriteLine("Send AARQ request", GXCommon.ToHex(it, true)); } reply.Clear(); ReadDLMSPacket(it, reply); } if (Trace) { Console.WriteLine("Parsing AARE reply" + reply.ToString()); } //Parse reply. Client.ParseAAREResponse(reply.Data); reply.Clear(); //Get challenge Is HSL authentication is used. if (Client.IsAuthenticationRequired) { foreach (byte[] it in Client.GetApplicationAssociationRequest()) { reply.Clear(); ReadDLMSPacket(it, reply); } Client.ParseApplicationAssociationResponse(reply.Data); } Console.WriteLine("Parsing AARE reply succeeded."); }