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();
 }
Esempio n. 2
0
 void ReadDevice(GXDLMSDevice dev)
 {
     try
     {
         dev.Comm.OnBeforeRead += new ReadEventHandler(OnBeforeRead);
         dev.Comm.OnAfterRead += new ReadEventHandler(OnAfterRead);
         dev.KeepAliveStop();
         int cnt = dev.Objects.Count;
         if (cnt == 0)
         {
             RefreshDevice(dev, true);
             //Do not try to refresh and read values at the same time.
             return;
         }
         int pos = 0;
         foreach (GXDLMSObject it in dev.Objects)
         {
             OnProgress(dev, "Reading " + it.LogicalName + "...", ++pos, cnt);
             dev.Comm.Read(this, it, 0);
             DLMSItemOnChange(it, false, 0, null);
         }
     }
     catch (Exception Ex)
     {
         throw Ex;
     }
     finally
     {
         dev.Comm.OnBeforeRead -= new ReadEventHandler(OnBeforeRead);
         dev.Comm.OnAfterRead -= new ReadEventHandler(OnAfterRead);
         dev.KeepAliveStart();
     }
 }
Esempio n. 3
0
 void RefreshDevice(GXDLMSDevice dev, bool bRefresh)
 {
     try
     {
         if (bRefresh)
         {
             dev.KeepAliveStop();
         }
         TreeNode deviceNode = (TreeNode)ObjectTreeItems[dev];
         if (bRefresh)
         {
             OnProgress(dev, "Refresh device", 0, 1);
             UpdateTransaction(true);
             RemoveObject(dev);
             while (dev.Objects.Count != 0)
             {
                 RemoveObject(dev.Objects[0]);
             }
             GXManufacturer m = Manufacturers.FindByIdentification(dev.Manufacturer);
             dev.ObisCodes = m.ObisCodes;
             //Walk through object tree.
             dev.UpdateObjects();
             //Read registers units and scalers.
             int cnt = dev.Objects.Count;
             GXLogWriter.WriteLog("--- Reading scalers and units. ---");
             for (int pos = 0; pos != cnt; ++pos)
             {
                 GXDLMSObject it = dev.Objects[pos];
                 GXObisCode obj = m.ObisCodes.FindByLN(it.ObjectType, it.LogicalName, null);
                 if (obj != null)
                 {
                     UpdateFromObisCode(it, obj);
                 }
                 else
                 {
                     continue;
                 }
             }
             GXLogWriter.WriteLog("--- Reading scalers and units end. ---");
             /* TODO:
             if (!m.UseLogicalNameReferencing)
             {
                 GXLogWriter.WriteLog("--- Reading Access rights. ---");
                 try
                 {
                     foreach (GXDLMSAssociationShortName sn in dev.Objects.GetObjects(ObjectType.AssociationShortName))
                     {
                         dev.Comm.Read(sn, 3);
                     }
                 }
                 catch (Exception ex)
                 {
                     GXLogWriter.WriteLog(ex.Message);
                 }
                 GXLogWriter.WriteLog("--- Reading Access rights end. ---");
             }
              * */
             this.OnProgress(dev, "Reading scalers and units.", cnt, cnt);
             GroupItems(GroupsMnu.Checked);
         }
     }
     catch (ThreadAbortException)
     {
         //User has cancel action. Do nothing.
     }
     catch (Exception Ex)
     {
         throw Ex;
     }
     finally
     {
         if (bRefresh)
         {
             dev.KeepAliveStart();
             UpdateTransaction(false);
         }
     }
 }
        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
            {
                media.Open();
            }
            try
            {
                GXReplyData reply = new GXReplyData();
                byte[]      data;
                client.Limits.WindowSizeRX = parent.WindowSizeRX;
                client.Limits.WindowSizeTX = parent.WindowSizeTX;
                client.Limits.MaxInfoRX    = parent.MaxInfoRX;
                client.Limits.MaxInfoTX    = parent.MaxInfoTX;
                client.MaxReceivePDUSize   = parent.PduSize;
                client.Priority            = parent.Priority;
                client.ServiceClass        = parent.ServiceClass;

                data = SNRMRequest();
                if (data != null)
                {
                    ReadDataBlock(data, "Send SNRM request.", reply);
                    GXLogWriter.WriteLog("Parsing UA reply succeeded.");
                    //Has server accepted client.
                    ParseUAResponse(reply.Data);
                }
                //Generate AARQ request.
                //Split requests to multiple packets if needed.
                //If password is used all data might not fit to one packet.
                reply.Clear();
                ReadDataBlock(AARQRequest(), "Send AARQ request.", reply);
                try
                {
                    //Parse reply.
                    ParseAAREResponse(reply.Data);
                    GXLogWriter.WriteLog("Parsing AARE reply succeeded.");
                }
                catch (Exception Ex)
                {
                    reply.Clear();
                    ReadDLMSPacket(DisconnectRequest(), 1, reply);
                    throw Ex;
                }
                //If authentication is required.
                if (client.Authentication > Authentication.Low)
                {
                    foreach (byte[] it in client.GetApplicationAssociationRequest())
                    {
                        GXLogWriter.WriteLog("Authenticating", it);
                        reply.Clear();
                        ReadDLMSPacket(it, reply);
                    }
                    client.ParseApplicationAssociationResponse(reply.Data);
                }
            }
            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;
            }
            parent.KeepAliveStart();
        }