Beispiel #1
0
 private bool CheckSnrm(GXDLMSDevice dev, int count, bool reset)
 {
     if (media.IsOpen)
     {
         int[] serverAddresses = Properties.Settings.Default.HdlcServerAddresses.Split(',').Select(int.Parse).ToArray();
         int[] clientAddresses = Properties.Settings.Default.HdlcClientAddresses.Split(',').Select(int.Parse).ToArray();
         BeginInvoke(new ResetProgressEventHandler(OnResetProgress), count * serverAddresses.Length, reset);
         StringBuilder sb          = new StringBuilder();
         bool          serverFound = false;
         foreach (int server in serverAddresses)
         {
             BeginInvoke(new IncreaseProgressEventHandler(OnIncreaseProgress));
             foreach (int client in clientAddresses)
             {
                 BeginInvoke(new AppendTextEventHandler(OnAppendText),
                             "Try with server address " + server + " (0x" + server.ToString("X") + ") and client " + client + " (0x" + client.ToString("X") + ")" + Environment.NewLine);
                 dev.Comm.client.ClientAddress = client;
                 dev.Comm.client.ServerAddress = server;
                 byte[]      data  = dev.Comm.client.SNRMRequest();
                 GXReplyData reply = new GXReplyData();
                 try
                 {
                     //Accept all data.
                     dev.Comm.client.ClientAddress = 0;
                     dev.Comm.client.ServerAddress = 0x7f;
                     dev.WaitTime = -Properties.Settings.Default.HdlcSearchWaitTime;
                     dev.Comm.ReadDataBlock(data, "Send SNRM request.", 0, 0, reply);
                     //Try to establish the connection to the meter.
                     int logical, physical;
                     GXDLMSTranslator.GetLogicalAndPhysicalAddress(reply.SourceAddress, out logical, out physical);
                     dev.Comm.client.ParseUAResponse(reply.Data);
                     if (serverAddresses[0] != reply.SourceAddress)
                     {
                         BeginInvoke(new ChangeAddressEventHandler(OnChangeAddress), reply.SourceAddress, true);
                     }
                     serverFound = true;
                     sb.AppendLine("++++++++++++++++++++++++++++++++++++");
                     reply.Clear();
                     dev.Comm.client.ClientAddress = client;
                     dev.Comm.client.ServerAddress = server;
                     dev.WaitTime = Properties.Settings.Default.HdlcSearchInitialWaitTime;
                     data         = dev.Comm.client.AARQRequest()[0];
                     dev.Comm.ReadDataBlock(data, "Send AARQ request.", 0, 0, reply);
                     try
                     {
                         dev.Comm.client.ParseAAREResponse(reply.Data);
                     }
                     catch (GXDLMSException ex)
                     {
                         sb.AppendLine("Meter returned an exception:");
                         sb.AppendLine(ex.Message);
                         if (ex.Result == AssociationResult.PermanentRejected)
                         {
                             BeginInvoke(new ChangeAddressEventHandler(OnChangeAddress), clientAddresses[0], false);
                         }
                     }
                     catch (Exception ex)
                     {
                         sb.AppendLine("Meter returned an exception:");
                         sb.AppendLine(ex.Message);
                     }
                     if (media is GXSerial)
                     {
                         sb.AppendLine("Used baud rate: " + (media as GXSerial).BaudRate);
                     }
                     sb.AppendLine("Client address: " + reply.TargetAddress + " (0x" + reply.TargetAddress.ToString("X") + ")");
                     sb.AppendLine("Server address: " + reply.SourceAddress + " (0x" + reply.SourceAddress.ToString("X") + ")");
                     sb.Append("Logical address: " + logical + " (0x" + logical.ToString("X") + ") ");
                     sb.AppendLine("Physical address: " + physical + " (0x" + physical.ToString("X") + ")");
                     BeginInvoke(new AppendTextEventHandler(OnAppendText), sb.ToString());
                     try
                     {
                         dev.Comm.ReadDataBlock(dev.Comm.client.DisconnectRequest(true), "Send Disconnect request.", 0, 0, reply);
                     }
                     catch (Exception)
                     {
                         reply.Clear();
                     }
                     return(serverFound);
                 }
                 catch (TimeoutException)
                 {
                     if (serverFound && clientAddresses[0] == reply.TargetAddress)
                     {
                         BeginInvoke(new ChangeAddressEventHandler(OnChangeAddress), clientAddresses[0], false);
                     }
                     reply.Clear();
                 }
                 catch (Exception ex)
                 {
                     //If user has close the media.
                     if (!media.IsOpen)
                     {
                         return(true);
                     }
                     if (serverFound && clientAddresses[0] == reply.TargetAddress)
                     {
                         BeginInvoke(new ChangeAddressEventHandler(OnChangeAddress), clientAddresses[0], false);
                     }
                     int logical, physical;
                     GXDLMSTranslator.GetLogicalAndPhysicalAddress(reply.SourceAddress, out logical, out physical);
                     sb.AppendLine("Meter returned an exception:");
                     sb.AppendLine(ex.Message);
                     sb.AppendLine("Try to change the client address:");
                     if (media is GXSerial)
                     {
                         sb.AppendLine("Used baud rate: " + (media as GXSerial).BaudRate);
                     }
                     sb.AppendLine("Client address: " + reply.TargetAddress + " (0x" + reply.TargetAddress.ToString("X") + ")");
                     sb.AppendLine("Server address: " + reply.SourceAddress + " (0x" + reply.SourceAddress.ToString("X") + ")");
                     sb.Append("Logical address: " + logical + " (0x" + logical.ToString("X") + ") ");
                     sb.AppendLine("Physical address: " + physical + " (0x" + physical.ToString("X") + ")");
                     BeginInvoke(new AppendTextEventHandler(OnAppendText), sb.ToString());
                     reply.Clear();
                     try
                     {
                         dev.Comm.ReadDataBlock(dev.Comm.client.DisconnectRequest(true), "Send Disconnect request.", 0, 0, reply);
                     }
                     catch (Exception)
                     {
                         reply.Clear();
                     }
                     return(serverFound);
                 }
                 if (!media.IsOpen)
                 {
                     break;
                 }
             }
         }
     }
     return(false);
 }