コード例 #1
0
        /// <summary>
        /// This function gets message from the remote unit.
        /// </summary>
        /// <param name="Rxmsg">Received message</param>
        /// <param name="Txmsg">Transmited message, information about this frmae could be necessary to properly init received frame.
        /// </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>
        protected override AL_ReadData_Result GetMessage(out ModBusMessage Rxmsg, ModBusMessage Txmsg)
        {
            Rxmsg = null;
            try
            {
                InterCharStopwatch.Reset();
                InterCharStopwatch.Start();
                if (!CheckCharTimeout(GetProtocolParameters.ResponseTimeOutSpan, InterCharStopwatch))
                {
                    GetIProtocolParent.IncStRxNoResponseCounter();
                    return(AL_ReadData_Result.ALRes_DatTransferErrr);
                }
                GetIProtocolParent.TimeMaxResponseDelayAdd(InterCharStopwatch.ElapsedMilliseconds);
                Rxmsg = m_Pool.GetEmptyISesDBuffer();
                Rxmsg.userDataLength = Rxmsg.userBuffLength;
                Rxmsg.offset         = 0;
                bool first = true;
                do
                {
                    byte lastChar;
                    InterCharStopwatch.Reset();
                    InterCharStopwatch.Start();
                    switch (GetICommunicationLayer.GetChar(out lastChar))
                    {
                    case TGetCharRes.Success:
                        if (!Rxmsg.WriteByte(lastChar))
                        {
                            return(AL_ReadData_Result.ALRes_DatTransferErrr);
                        }
                        if (first)
                        {
                            first = false;
                        }
                        else
                        {
                            GetIProtocolParent.TimeCharGapAdd(CAS.Lib.RTLib.Processes.Timer.ToUSeconds(InterCharStopwatch.Elapsed));
                        }
                        break;

                    case TGetCharRes.DisInd:
                        return(AL_ReadData_Result.ALRes_DisInd);
                    }
                }while (CheckCharTimeout(((ModBus_ProtocolParameters)GetProtocolParameters).TimeoutCharacterSpan, InterCharStopwatch));
                Rxmsg.userDataLength = Rxmsg.offset;
                Rxmsg.offset         = 0;
                if (CheckCharTimeout(((ModBus_ProtocolParameters)GetProtocolParameters).TimeoutCharacterSpan, InterCharStopwatch))
                {
                    Flush(((ModBus_ProtocolParameters)GetProtocolParameters).InterframeGapSpan);
                    Rxmsg.ReturnEmptyEnvelope();
                    Rxmsg = null;
                    GetIProtocolParent.IncStRxFragmentedCounter();
                    return(AL_ReadData_Result.ALRes_DatTransferErrr);
                }
                return(AL_ReadData_Result.ALRes_Success);
            }
            catch (DisconnectException) { return(AL_ReadData_Result.ALRes_DisInd); }
        }
コード例 #2
0
        /// <summary>
        /// This function gets message from the remote unit.
        /// </summary>
        /// <param name="receiveMessage">Received message</param>
        /// <param name="transmitMessage">Transited message, information about this frame could be necessary to properly initialize received frame.
        /// </param>
        /// <returns>
        ///   ALRes_Success: Operation accomplished successfully
        ///   ALRes_DatTransferErrr: Data transfer is imposable 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>
        protected override AL_ReadData_Result GetMessage(out ModBusMessage receiveMessage, ModBusMessage transmitMessage)
        {
            receiveMessage = null;
            try
            {
                InterCharStopwatch.Reset();
                InterCharStopwatch.Start();
                if (!CheckCharTimeout(GetProtocolParameters.ResponseTimeOutSpan, InterCharStopwatch))
                {
                    GetIProtocolParent.IncStRxNoResponseCounter();
                    return(AL_ReadData_Result.ALRes_DatTransferErrr);
                }
                GetIProtocolParent.TimeMaxResponseDelayAdd(InterCharStopwatch.ElapsedMilliseconds);
                receiveMessage = m_Pool.GetEmptyISesDBuffer();
                receiveMessage.userDataLength = receiveMessage.userBuffLength;
                receiveMessage.offset         = 0;
                bool first = true;
                do
                {
                    byte lastChar;
                    InterCharStopwatch.Reset();
                    InterCharStopwatch.Start();
                    switch (GetICommunicationLayer.GetChar(out lastChar))
                    {
                    case TGetCharRes.Success:
                        if (!receiveMessage.WriteByte(lastChar))
                        {
                            AssemblyTraceEvent.Tracer.TraceEvent(TraceEventType.Warning, 77, "ModBusProtocol.GetMessage: cannot write character received from the Communication Layer");
                            return(AL_ReadData_Result.ALRes_DatTransferErrr);
                        }
                        if (first)
                        {
                            first = false;
                        }
                        else
                        {
                            GetIProtocolParent.TimeCharGapAdd(CAS.Lib.RTLib.Processes.Timer.ToUSeconds(InterCharStopwatch.Elapsed));
                        }
                        break;

                    case TGetCharRes.DisInd:
                        return(AL_ReadData_Result.ALRes_DisInd);
                    }
                }while (CheckCharTimeout(((ModBus_ProtocolParameters)GetProtocolParameters).Timeout15Span, InterCharStopwatch));
                receiveMessage.userDataLength = receiveMessage.offset;
                receiveMessage.offset         = 0;
                if (CheckCharTimeout(((ModBus_ProtocolParameters)GetProtocolParameters).Timeout35Span, InterCharStopwatch))
                {
                    Flush(((ModBus_ProtocolParameters)GetProtocolParameters).Timeout35Span);
                    receiveMessage.ReturnEmptyEnvelope();
                    receiveMessage = null;
                    GetIProtocolParent.IncStRxFragmentedCounter();
                    return(AL_ReadData_Result.ALRes_DatTransferErrr);
                }
                return(AL_ReadData_Result.ALRes_Success);
            }
            catch (DisconnectException)
            {
                return(AL_ReadData_Result.ALRes_DisInd);
            }
        }