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.");
}