Exemple #1
0
        ///<summary>
        /// Get packet from received data.
        ///</summary>
        ///<param name="buff">
        /// Received data. </param>
        ///<returns>
        ///Reply if any.
        ///</returns>
        private byte[] GetPacket(byte[] buff)
        {
            GXByteBuffer receivedFrame = new GXByteBuffer(buff);
            // Save position where data was. This is used if data is not sent to
            // this server.
            ushort offset = Reply.Data.Size;

            GXDLMS.GetData(Settings, receivedFrame, Reply);
            // If all data is not received yet.
            if (!Reply.IsComplete)
            {
                return(null);
            }
            byte[] data;
            // If client sends keepalive or get next frame request.
            if ((Reply.MoreData & RequestTypes.Frame) != 0)
            {
                if (Frames != null && Frames.Length > FrameIndex)
                {
                    data = Frames[FrameIndex++];
                    return(data);
                }
                FrameIndex = 0;
                data       = GXDLMS.SplitToHdlcFrames(Settings, Settings.KeepAlive(), null)[0];
                return(data);
            }

            // Check is data sent to this server.
            if (ConnectedServerAddress == 0 || ConnectedClientAddress == 0)
            {
                // Check is data send to this server.
                if (!IsTarget(Settings.ServerAddress, Settings.ClientAddress))
                {
                    Reply.Data.Size  = offset;
                    Reply.IsComplete = false;
                }
                ConnectedServerAddress = Settings.ServerAddress;
                ConnectedClientAddress = Settings.ClientAddress;
            }
            else if (Settings.ServerAddress != ConnectedServerAddress ||
                     Settings.ClientAddress != ConnectedClientAddress)
            {
                Reply.Data.Size  = offset;
                Reply.IsComplete = false;
            }


            // Clear received data.
            receivedFrame.Clear();
            ServerReply.Data = Reply.Data;
            FrameIndex       = 0;
            return(null);
        }
Exemple #2
0
 ///<summary>
 /// Removes the HDLC frame from the packet, and returns COSEM data only.
 ///</summary>
 ///<param name="reply">
 /// The received data from the device.
 ///</param>
 ///<param name="data">
 /// Information from the received data.
 ///</param>
 ///<returns>
 /// Is frame complete.
 ///</returns>
 public bool GetData(GXByteBuffer reply, GXReplyData data)
 {
     return(GXDLMS.GetData(Settings, reply, data));
 }
 ///<summary>
 /// Removes the HDLC frame from the packet, and returns COSEM data only.
 ///</summary>
 ///<param name="reply">
 /// The received data from the device.
 ///</param>
 ///<param name="data">
 /// Information from the received data.
 ///</param>
 ///<returns>
 /// Is frame complete.
 ///</returns>
 public bool GetData(GXByteBuffer reply, GXReplyData data, GXReplyData notify)
 {
     return(GXDLMS.GetData(Settings, reply, data, notify, null));
 }
Exemple #4
0
 ///<summary>
 /// Removes the HDLC frame from the packet, and returns COSEM data only.
 ///</summary>
 ///<param name="reply">
 /// The received data from the device.
 ///</param>
 ///<param name="data">
 /// Information from the received data.
 ///</param>
 ///<returns>
 /// Is frame complete.
 ///</returns>
 public bool GetData(byte[] reply, GXReplyData data)
 {
     return(GXDLMS.GetData(Settings, new GXByteBuffer(reply), data));
 }
Exemple #5
0
        ///<summary>
        /// Handles client request.
        /// </summary>
        ///<param name="buff">
        /// Received data from the client. </param>
        ///<returns>
        ///Response to the request. Response is null if request packet is not complete.
        ///</returns>
        public virtual byte[] HandleRequest(byte[] buff, GXDLMSConnectionEventArgs connectionInfo)
        {
            if (buff == null || buff.Length == 0)
            {
                return(null);
            }
            if (!Initialized)
            {
                throw new Exception("Server not Initialized.");
            }
            try
            {
                receivedData.Set(buff);
                bool first = Settings.ServerAddress == 0 && Settings.ClientAddress == 0;
                GXDLMS.GetData(Settings, receivedData, info);
                //If all data is not received yet.
                if (!info.IsComplete)
                {
                    return(null);
                }
                receivedData.Clear();
                if (first)
                {
                    // Check is data send to this server.
                    if (!IsTarget(Settings.ServerAddress,
                                  Settings.ClientAddress))
                    {
                        info.Clear();
                        return(null);
                    }
                }

                //If client want next frame.
                if ((info.MoreData & RequestTypes.Frame) == RequestTypes.Frame)
                {
                    return(GXDLMS.GetHdlcFrame(Settings, Settings.ReceiverReady(), replyData));
                }
                //Update command if transaction and next frame is asked.
                if (info.Command == Command.None)
                {
                    if (transaction != null)
                    {
                        info.Command = transaction.command;
                    }
                }
                //Check inactivity time out.
                if (hdlcSetup != null)
                {
                    if (info.Command == Command.Snrm)
                    {
                        dataReceived = DateTime.Now;
                    }
                    else
                    {
                        int elapsed = (int)(DateTime.Now - dataReceived).TotalSeconds;
                        //If inactivity time out is elapsed.
                        if (elapsed >= hdlcSetup.InactivityTimeout)
                        {
                            Reset();
                            dataReceived = DateTime.MinValue;
                            return(null);
                        }
                        dataReceived = DateTime.Now;
                    }
                }

                byte[] reply = HandleCommand(info.Command, info.Data, connectionInfo);
                info.Clear();
                return(reply);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                if (info.Command != Command.None)
                {
                    return(ReportError(info.Command, ErrorCode.HardwareFault));
                }
                else
                {
                    Reset();
                    if (Settings.Connected)
                    {
                        Settings.Connected = false;
                        Disconnected(connectionInfo);
                    }
                    return(null);
                }
            }
        }