Example #1
0
        /// <summary>
        /// Read Data
        /// </summary>
        /// <param name="block">Block description to be read</param>
        /// <param name="station">Address of the remote station connected to the common field bus. –1 if not applicable.
        /// </param>
        /// <param name="data">The frame with the requested data.</param>
        /// <param name="retries">Number of retries to get data.</param>
        /// <returns>
        ///   ALRes_Success: Operation accomplished successfully
        ///   ALRes_DatTransferErrr: Data transfer is imposible because of a communication error – loss of
        ///      communication with a station
        ///   ALRes_DisInd: Disconnect indication – connection has been shut down remotely or lost because of
        ///      communication error. Data is unavailable
        /// </returns>
        AL_ReadData_Result IApplicationLayerMaster.ReadData
            (IBlockDescription block, int station, out IReadValue data, byte retries)
        {
            data = null;
            if (!m_protocol.GetICommunicationLayer.Connected)
            {
                return(AL_ReadData_Result.ALRes_DisInd);
            }
            T_ALMessage request = m_Pool.GetEmptyISesDBuffer();
            T_ALMessage response;

            request.PrepareRequest(station, block);
            AL_ReadData_Result res = TxGetResponse(request, out response, retries);

            if (res == AL_ReadData_Result.ALRes_Success)
            {
                response.SetBlockDescription(station, block);
                data = (IReadValue)response;
            }
            else
            if (response != null && !((IEnvelope)response).InPool)
            {
                EventLogMonitor.WriteToEventLogInfo("TxGetResponse has failed and  response != null  && !( (IEnvelope)response ).InPool", 195);
                response.ReturnEmptyEnvelope();
            }
            request.ReturnEmptyEnvelope();
            m_protocol.GetIProtocolParent.RxDataBlock(res == AL_ReadData_Result.ALRes_Success);
            return(res);
        }
        AL_ReadData_Result IApplicationLayerSlave.SendACKRes()
        {
            SBUSbase_message frame = m_Pool.GetEmptyISesDBuffer();

            frame.PrepareResultFrame(0);
            AL_ReadData_Result res = m_ALProtocol.TransmitMessage(frame, FrameType.FT_RES_ANK);

            frame.ReturnEmptyEnvelope();
            m_ALProtocol.GetIProtocolParent.IncStTxACKCounter();
            return(res);
        }
Example #3
0
        /// <summary>
        /// Transits frame and gets response. Response is analyzed to much the sent frame.
        /// </summary>
        /// <param name="cTxmsg">Frame to be sent.</param>
        /// <returns>Result of the operation.</returns>
        /// <param name="cRetries"></param>
        /// <returns>
        ///   ALRes_Success: Operation accomplished successfully
        ///   ALRes_DatTransferErrr: Data transfer is impossible because of a communication error – loss of
        ///      communication with a station
        ///   ALRes_DisInd: Disconnect indication – connection has been shut down remotely or lost because of
        ///      communication error. Data is unavailable
        /// </returns>
        private AL_ReadData_Result TxGetResponse(T_ALMessage cTxmsg, byte cRetries)
        {
            T_ALMessage        rxmsg;
            AL_ReadData_Result res = TxGetResponse(cTxmsg, out rxmsg, cRetries);

            if (rxmsg != null)
            {
                rxmsg.ReturnEmptyEnvelope();
            }
            return(res);
        }
Example #4
0
        /// <summary>
        /// Send values to the data provider
        /// </summary>
        /// <param name="data">
        /// Data to be send. Always null after return. Data buffer must be returned to the pool.
        /// </param>
        /// <param name="retries">Number of retries to wrtie data.</param>
        /// <returns>
        ///   ALRes_Success: Operation accomplished successfully
        ///   ALRes_DatTransferErrr: Data transfer is imposible because of a communication error – loss of
        ///      communication with a station
        ///   ALRes_DisInd: Disconnect indication – connection has been shut down remotely or lost because of
        ///      communication error. Data is unavailable
        /// </returns>
        AL_ReadData_Result IApplicationLayerMaster.WriteData(ref IWriteValue data, byte retries)
        {
            if (!m_protocol.GetICommunicationLayer.Connected)
            {
                return(AL_ReadData_Result.ALRes_DisInd);
            }
            AL_ReadData_Result response = TxGetResponse((T_ALMessage)data, retries);

            data.ReturnEmptyEnvelope();
            data = null;
            m_protocol.GetIProtocolParent.TxDataBlock(response == AL_ReadData_Result.ALRes_Success);
            return(response);
        }
Example #5
0
        /// <summary>
        /// Transits frame and gets response. Response is analyzed to much the sent frame.
        /// </summary>
        /// <param name="Txmsg">Frame to be sent.</param>
        /// <param name="Rxmsg">Received frame</param>
        /// <param name="cRetries">Number of retries to get frame from remote unit.</param>
        /// <returns>
        ///   ALRes_Success: Operation accomplished successfully
        ///   ALRes_DatTransferErrr: Data transfer is impossible because of a communication error – loss of
        ///      communication with a station
        ///   ALRes_DisInd: Disconnect indication – connection has been shut down remotely or lost because of
        ///      communication error. Data is unavailable
        /// </returns>
        private AL_ReadData_Result TxGetResponse(T_ALMessage Txmsg, out T_ALMessage Rxmsg, byte cRetries)
        {
            Rxmsg = null;
            TGRState currState = TGRState.TxReq;

            if (Txmsg == null)
            {
                AssemblyTraceEvent.Tracer.TraceEvent(TraceEventType.Information, 83, "ApplicationLayerMaster.TxGetResponse - Transmitted message cannot be null.");
                return(AL_ReadData_Result.ALRes_DatTransferErrr);
            }
            while (true)
            {
                switch (currState)
                {
                case TGRState.TxReq:
                    Timer.WaitTimeout(m_protocol.GetProtocolParameters.InterframeGapSpan, InterfrarmeStopwatch);
                    switch (m_protocol.TransmitMessage(Txmsg))
                    {
                    case AL_ReadData_Result.ALRes_Success:
                        currState = TGRState.RxRes;
                        break;

                    case AL_ReadData_Result.ALRes_DisInd:
                        return(AL_ReadData_Result.ALRes_DisInd);

                    case AL_ReadData_Result.ALRes_DatTransferErrr:
                        return(AL_ReadData_Result.ALRes_DatTransferErrr);
                    }
                    break;

                case TGRState.RxRes:
                    if (Txmsg == null)
                    {
                        AssemblyTraceEvent.Tracer.TraceEvent(TraceEventType.Information, 105, "ApplicationLayerMaster.TxGetResponse - Before: m_protocol.GetMessage( out Rxmsg, Txmsg ): Transmitted message cannot be null.");
                        return(AL_ReadData_Result.ALRes_DatTransferErrr);
                    }
                    AL_ReadData_Result res = m_protocol.GetMessage(out Rxmsg, Txmsg);
                    InterfrarmeStopwatch.Reset();
                    InterfrarmeStopwatch.Start();
                    switch (res)
                    {
                    case AL_ReadData_Result.ALRes_Success:
                        InterfrarmeStopwatch.Reset();
                        InterfrarmeStopwatch.Start();
                        ProtocolALMessage.CheckResponseResult lastCheckResult = Rxmsg.CheckResponseFrame(Txmsg);
                        switch (lastCheckResult)
                        {
                        case ProtocolALMessage.CheckResponseResult.CR_OK:
                            m_Statistic.IncStRxFrameCounter();
                            return(AL_ReadData_Result.ALRes_Success);

                        case ProtocolALMessage.CheckResponseResult.CR_SynchError:
                            m_Statistic.IncStRxSynchError();
                            break;

                        case ProtocolALMessage.CheckResponseResult.CR_Incomplete:
                            m_Statistic.IncStRxFragmentedCounter();
                            break;

                        case ProtocolALMessage.CheckResponseResult.CR_CRCError:
                            m_Statistic.IncStRxCRCErrorCounter();
                            break;

                        case ProtocolALMessage.CheckResponseResult.CR_Invalid:
                            m_Statistic.IncStRxInvalid();
                            break;

                        case ProtocolALMessage.CheckResponseResult.CR_NAK:
                            m_Statistic.IncStRxNAKCounter();
                            break;
                        }
                        if (!((IEnvelope)Rxmsg).InPool)
                        {
                            Rxmsg.ReturnEmptyEnvelope();
                        }
                        Rxmsg     = null;
                        currState = TGRState.Retray;
                        break;

                    //return AL_ReadData_Result.ALRes_DatTransferErrr;
                    case AL_ReadData_Result.ALRes_DisInd:
                        if (Rxmsg != null)
                        {
                            if (!((IEnvelope)Rxmsg).InPool)
                            {
                                Rxmsg.ReturnEmptyEnvelope();
                            }
                            Rxmsg = null;
                        }
                        return(AL_ReadData_Result.ALRes_DisInd);

                    default:
                        currState = TGRState.Retray;
                        break;
                    }
                    break;

                case TGRState.Retray:
                    if (Rxmsg != null)
                    {
                        if (!((IEnvelope)Rxmsg).InPool)
                        {
                            Rxmsg.ReturnEmptyEnvelope();
                        }
                        Rxmsg = null;
                    }
                    if (cRetries == 0)
                    {
                        return(AL_ReadData_Result.ALRes_DatTransferErrr);
                    }
                    cRetries--;
                    currState = TGRState.TxReq;
                    break;
                }
            }//while (true)
        }
        }//ReadData

        #endregion
        #region IApplicationLayerSniffer
        /// <summary>
        /// Interface of the passive reader station, i.e. responsible for reading master station commands and slave station responses
        /// and gathering the data from coupled pairs.
        /// </summary>
        /// <param name="block">Data block description.</param>
        /// <param name="data">Message containing obtained data.</param>
        /// <returns>
        ///   ALRes_Success:
        ///     Operation accomplished successfully
        ///   ALRes_DatTransferErrr: Data transfer is imposible because of a communication error – loss of
        ///      communication with a station
        ///   ALRes_DisInd: Disconnect indication – connection has been shut down remotely or lost because of
        ///      communication error. Data is unavailable
        /// </returns>
        AL_ReadData_Result IApplicationLayerSniffer.ReadData(out IBlockDescription block, out IReadValue data)
        {
            bool               resetanswer = false;
            SBUSbase_message   SBUS_Frame1, SBUS_Frame2;
            AL_ReadData_Result res = GetFrame(out SBUS_Frame1, null, true, ref resetanswer);

            //otrzymalismy polecenie zapisu lub odczytu
            if (res == AL_ReadData_Result.ALRes_Success)
            {
                do
                {
                    resetanswer = false;
                    //TODO it is protected and must be used as an inherited member SBUS_Frame1.CalculateExpectedResulLength();
                    switch (SBUS_Frame1.cmd)
                    {
                    case SBUSbase_message.ProtocolCmd.coRS0:
                    case SBUSbase_message.ProtocolCmd.coRS1:
                    case SBUSbase_message.ProtocolCmd.coRS2:
                    case SBUSbase_message.ProtocolCmd.coRS3:
                    case SBUSbase_message.ProtocolCmd.coRS4:
                    case SBUSbase_message.ProtocolCmd.coRS5:
                    case SBUSbase_message.ProtocolCmd.coRS6:
                    case SBUSbase_message.ProtocolCmd.coRS7:
                    case SBUSbase_message.ProtocolCmd.coRR:
                    case SBUSbase_message.ProtocolCmd.coRF:
                        #region read
                        if (GetFrame(out SBUS_Frame2, SBUS_Frame1, false, ref resetanswer) == AL_ReadData_Result.ALRes_Success)
                        {
                            if (!resetanswer)
                            {
                                // odczytywanie danych - wiec pierwsza przechwycona ramka  zawiera opis bloku,
                                // a druga zawiera wlasciwe dane
                                block = SBUS_Frame1;
                                data  = SBUS_Frame2;
                                m_ALProtocol.GetIProtocolParent.RxDataBlock(true);
                                return(AL_ReadData_Result.ALRes_Success);
                            }
                            else
                            {
                                SBUS_Frame1 = SBUS_Frame2;
                            }
                        }
                        m_ALProtocol.GetIProtocolParent.RxDataBlock(false);
                        #endregion
                        break;

                    case SBUSbase_message.ProtocolCmd.coWR:
                    case SBUSbase_message.ProtocolCmd.coWF:
                        #region write
                        if (GetFrame(out SBUS_Frame2, SBUS_Frame1, false, ref resetanswer) == AL_ReadData_Result.ALRes_Success)
                        {
                            if (!resetanswer)
                            {
                                // zapisywanie danych - wiec pierwsza przechwycona ramka zawiera opis bloku+dane,
                                // a druga zawiera potwierdzenie czy jest OK
                                //jesli w ostatnim IF bylo OK - tzn - ze dostalismy ACK:
                                //wiec :
                                block = SBUS_Frame1;
                                SBUS_Frame2.PrepareDataResponse(block);
                                for (int idx = 0; idx < block.length; idx++)
                                {
                                    ((IResponseValue)(SBUS_Frame2))[idx] = ((IReadCMDValue)SBUS_Frame1)[idx];
                                }
                                data = SBUS_Frame2;
                                m_ALProtocol.GetIProtocolParent.TxDataBlock(true);
                                return(AL_ReadData_Result.ALRes_Success);
                            }
                            else
                            {
                                SBUS_Frame1 = SBUS_Frame2;
                            }
                        }
                        m_ALProtocol.GetIProtocolParent.TxDataBlock(false);
                        #endregion
                        break;

                    default:
                        break;
                    }
                } while (resetanswer);
            }
            block = null;
            data  = null;
            return(AL_ReadData_Result.ALRes_DatTransferErrr);
        }