/// <summary>
 /// Read data block from the device.
 /// </summary>
 /// <param name="data">data to send</param>
 /// <param name="text">Progress text.</param>
 /// <param name="multiplier"></param>
 /// <returns>Received data.</returns>
 internal void ReadDataBlock(byte[] data, string text, int multiplier, GXReplyData reply)
 {
     if (parent.InactivityMode == InactivityMode.ReopenActive && media is GXSerial && DateTime.Now.Subtract(connectionStartTime).TotalSeconds > 40)
     {
         parent.Disconnect();
         parent.InitializeConnection();
     }
     GXLogWriter.WriteLog(text, data);
     ReadDLMSPacket(data, reply);
     if (OnDataReceived != null)
     {
         OnDataReceived(this, reply);
     }
     if (reply.IsMoreData)
     {
         if (reply.TotalCount != 1)
         {
             NotifyProgress(text, 1, multiplier * reply.TotalCount);
         }
         while (reply.IsMoreData)
         {
             data = client.ReceiverReady(reply.MoreData);
             if ((reply.MoreData & RequestTypes.Frame) != 0)
             {
                 GXLogWriter.WriteLog("Get next frame: ", data);
             }
             else
             {
                 GXLogWriter.WriteLog("Get Next Data block: ", data);
             }
             ReadDLMSPacket(data, reply);
             if (OnDataReceived != null)
             {
                 OnDataReceived(this, reply);
             }
             if (reply.TotalCount != 1)
             {
                 NotifyProgress(text, reply.Count, multiplier * reply.TotalCount);
             }
         }
     }
 }
        /// <summary>
        /// Read data block from the device.
        /// </summary>
        /// <param name="data">data to send</param>
        /// <param name="text">Progress text.</param>
        /// <param name="multiplier"></param>
        /// <returns>Received data.</returns>
        internal byte[] ReadDataBlock(byte[] data, string text, double multiplier)
        {
            if (Parent.InactivityMode == InactivityMode.ReopenActive && Media is GXSerial && DateTime.Now.Subtract(ConnectionStartTime).TotalSeconds > 40)
            {
                Parent.Disconnect();
                Parent.InitializeConnection();
            }
            GXLogWriter.WriteLog(text, data);
            byte[]       reply    = ReadDLMSPacket(data);
            byte[]       allData  = null;
            RequestTypes moredata = m_Cosem.GetDataFromPacket(reply, ref allData);

            if (OnDataReceived != null)
            {
                OnDataReceived(this, (byte[])allData);
            }
            if ((moredata & (RequestTypes.Frame | RequestTypes.DataBlock)) != 0)
            {
                int maxProgress = m_Cosem.GetMaxProgressStatus(allData);
                NotifyProgress(text, 1, maxProgress);
                while (moredata != 0)
                {
                    while ((moredata & RequestTypes.Frame) != 0)
                    {
                        data = m_Cosem.ReceiverReady(RequestTypes.Frame);
                        GXLogWriter.WriteLog("Get next frame: ", (byte[])data);
                        reply = ReadDLMSPacket(data);
                        RequestTypes tmp = m_Cosem.GetDataFromPacket(reply, ref allData);
                        if (OnDataReceived != null)
                        {
                            OnDataReceived(this, (byte[])allData);
                        }
                        //If this was last frame.
                        if ((tmp & RequestTypes.Frame) == 0)
                        {
                            moredata &= ~RequestTypes.Frame;
                            break;
                        }
                        int current = m_Cosem.GetCurrentProgressStatus(allData);
                        //TODO: System.Diagnostics.Debug.Assert(!(current == maxProgress && moredata != RequestTypes.None));
                        NotifyProgress(text, (int)(multiplier * current), maxProgress);
                    }
                    if (Parent.InactivityMode == InactivityMode.ReopenActive && Media is GXSerial && DateTime.Now.Subtract(ConnectionStartTime).TotalSeconds > 40)
                    {
                        Parent.Disconnect();
                        Parent.InitializeConnection();
                    }
                    if ((moredata & RequestTypes.DataBlock) != 0)
                    {
                        //Send Receiver Ready.
                        data = m_Cosem.ReceiverReady(RequestTypes.DataBlock);
                        GXLogWriter.WriteLog("Get Next Data block: ", (byte[])data);
                        reply    = ReadDLMSPacket(data);
                        moredata = m_Cosem.GetDataFromPacket(reply, ref allData);
                        if (OnDataReceived != null)
                        {
                            OnDataReceived(this, (byte[])allData);
                        }
                        int current = m_Cosem.GetCurrentProgressStatus(allData);
                        //TODO: System.Diagnostics.Debug.Assert(!(current == maxProgress && moredata != RequestTypes.None));
                        NotifyProgress(text, (int)(multiplier * current), maxProgress);
                    }
                }
            }
            return(allData);
        }