/// <summary>
 /// Checks if the frame is exactly what we are looking for.
 /// </summary>
 /// <param name="txmsg">Sent frame as request  we expect answer to this frame.</param>
 /// <returns>
 /// CR_OK: OK
 /// CR_CRCError: CRC Error
 /// CR_NAK: Negative acknowledge.
 /// CR_SynchError: Synchronization error.
 /// CR_Incomplete: Incomplete frame.
 /// CR_Invalid: Invalid frame
 /// CR_OtherStation: Answer is from unexpected station.
 /// </returns>
 public override CheckResponseResult CheckResponseFrame(ProtocolALMessage txmsg)
 {
     if (rxMsgCurrError != CheckResponseResult.CR_OK)
     {
         TraceEvent(System.Diagnostics.TraceEventType.Verbose, 64,
                    "CheckResponseFrame has found error that  occured during receive, expected: " + rxMsgCurrError.ToString());
         return(rxMsgCurrError);
     }
     if (Part != FramePart.FrameEnd)
     {
         TraceEvent(System.Diagnostics.TraceEventType.Verbose, 70,
                    "CheckResponseFrame frame is incompleted ");
         return(CheckResponseResult.CR_Incomplete);
     }
     if (this.FrameAttribute == AttributeCharacter.ResponseACK_NAK && NakCode != 0)
     {
         TraceEvent(System.Diagnostics.TraceEventType.Verbose, 39, String.Format("Received NAC response with code: {0}", NakCode));
         return(CheckResponseResult.CR_NAK);
     }
     if (this.expectedFrameAttribute != this.FrameAttribute)
     {
         TraceEvent(System.Diagnostics.TraceEventType.Verbose, 76,
                    "CheckResponseFrame: CR_Invalid, expected: " + this.expectedFrameAttribute.ToString() + " received: " + this.FrameAttribute.ToString());
         return(CheckResponseResult.CR_Invalid);
     }
     return(CheckResponseResult.CR_OK);
 }//CheckResponseFrame
        public override ProtocolALMessage.CheckResponseResult CheckResponseFrame(ProtocolALMessage tMes)
        {
            ModBusMessage TXModBusMessage = (ModBusMessage)tMes;

            if (this.TransactionIdentifier != TXModBusMessage.TransactionIdentifier)
            {
                return(CheckResponseResult.CR_SynchError);
            }
            if (this.ProtocolIdentifierOffset != 0)
            {
                return(CheckResponseResult.CR_Invalid);
            }
            return(base.CheckResponseFrame(tMes));
        }
        public override ProtocolALMessage.CheckResponseResult CheckResponseFrame(ProtocolALMessage txmsg)
        {
            string wholeresponse = System.Text.ASCIIEncoding.ASCII.GetString(GetManagedBuffer()).Trim(
                new char[] { '\n', '\r', '\0', ' ', '\t' });

            responses = wholeresponse.Split(new char[] { '\n' });
            for (int idx = 0; idx < responses.Length; idx++)
            {
                responses[idx] = responses[idx].Trim();
            }
            if (((IBlockDescription)txmsg).length > responses.Length)
            {
                return(CheckResponseResult.CR_Incomplete);
            }
            if (((IBlockDescription)txmsg).length < responses.Length)
            {
                return(CheckResponseResult.CR_Invalid);
            }
            return(CheckResponseResult.CR_OK);
        }
Esempio n. 4
0
 public override ProtocolALMessage.CheckResponseResult CheckResponseFrame(ProtocolALMessage txmsg)
 {
     return(CheckResponseResult.CR_OK);
 }
        public override CheckResponseResult CheckResponseFrame(ProtocolALMessage TransmitedMessage)
        {
            MBUS_message transmitedMbusMessage = (MBUS_message)TransmitedMessage;

            MBUSTrace(TraceEventType.Verbose, 266, "CheckResponseFrame:" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt") + this.ToString());
            switch (expectedframetype)
            {
            case MBusFrameTypes.LongFrame:
                #region MBusFrameTypes.LongFrame
                crc.clear();
                // wyslana ramka typu short - powinnismy otrzymac odpowiedz dluga ramke typu UserData
                if (this.userDataLength < 9)
                {
                    return(CheckResponseResult.CR_Incomplete);
                }
                // sprawdzamy start 68h
                if (this.startbyte != MBUSConstans.StartLong)
                {
                    return(CheckResponseResult.CR_Invalid);
                }
                //sprawdzamy dlugosc
                if (this[LongFrame_Length1Pos] != this[LongFrame_Length2Pos])
                {
                    return(CheckResponseResult.CR_Invalid);
                }
                int UserApplicationDataLength = this[LongFrame_Length1Pos] - 3;
                //sprawdzamy drugi start 68h
                if (this[LongFrame_MiddleStartPos] != (byte)MBUSConstans.StartLong)
                {
                    return(CheckResponseResult.CR_Invalid);
                }
                //sprawdzamy code (function) field (C)
                if (!(this[LongFrame_ControlFieldPos] == (byte)MBUSControlCodes.RSP_UD ||
                      this[LongFrame_ControlFieldPos] == (byte)MBUSControlCodes.RSP_UD_ACDSet ||
                      this[LongFrame_ControlFieldPos] == (byte)MBUSControlCodes.RSP_UD_ACDSet_DFCSet ||
                      this[LongFrame_ControlFieldPos] == (byte)MBUSControlCodes.RSP_UD_DFCSet
                      ))
                {
                    return(CheckResponseResult.CR_Invalid);
                }
                crc.CRC_Calc(this[LongFrame_ControlFieldPos]);
                //sprawdzamy addesss field (A)
                if (this.LongFrameStation != transmitedMbusMessage.ShortFrameStation && transmitedMbusMessage.ShortFrameStation != 254)
                {
                    //we accept any address if request was sent to bradcast address (254)
                    return(CheckResponseResult.CR_OtherStation);
                }
                crc.CRC_Calc(this.LongFrameStation);
                //sprawdzamy ControlInformation Field (CI)
                //MZNI: co zrobic z ControlInformation Field
                crc.CRC_Calc(this[LongFrame_ControlInformationFieldPos]);

                // teraz przegladamy userdata
                offset = LongFrame_StartUserDataPos;
                for (int idx = 0; idx < UserApplicationDataLength; idx++)
                // tutaj czytamy ramke - ale wazne jest ze nie mozemy przekroczycdlugosci calej ramki - 2 (na CRC +bajt stopu)
                {
                    byte userbyte = ReadByte();
                    crc.CRC_Calc(userbyte);
                    if (offset == userDataLength - 1)
                    {
                        return(CheckResponseResult.CR_Incomplete);
                    }
                }
                //teraz sprawdzamy sume kontrolna i stop byte na koncu:
                byte currentCRC = ReadByte();
                byte lastByte   = ReadByte();
                //crc:
                if (crc.CurrentCRC != currentCRC)
                {
                    return(CheckResponseResult.CR_CRCError);
                }
                //czy na koncu jest bajt stopu:
                if (lastByte != (byte)MBUSConstans.Stop)
                {
                    return(CheckResponseResult.CR_Invalid);
                }
                // teraz robimy przetworzenie danych w application layer i zwracamy wynik
                return(MbusApplicationLayerMessageAnalyser.AnalyseFrame(this, LongFrame_StartUserDataPos,
                                                                        transmitedMbusMessage.ExpectedDataAnalysisMode, myTrace));

                #endregion //MBusFrameTypes.LongFrame
            case MBusFrameTypes.ShortFrame:
            case MBusFrameTypes.ControlFrame:
            default:
                myAssert.Assert(false, 311);
                break;
            }
            return(CheckResponseResult.CR_Invalid);
        } //CheckResponseFrame
Esempio n. 6
0
        public override CheckResponseResult CheckResponseFrame(ProtocolALMessage tMes)
        {
            ModBusMessage txmsg = (ModBusMessage)tMes;

            this.registers32AreUsed = txmsg.Registers32AreUsed;
            //out
            Modbus_Exceptions exception = Modbus_Exceptions.OK;

            if (this.userDataLength < MinimumFrameLength)
            {
                return(CheckResponseResult.CR_Incomplete);
            }
            switch (this.dataType)
            {
            case Modbus_Functions.READ_COILS:                         //0x01
            case Modbus_Functions.READ_DISCRETE_INPUT:                //0x02
            case Modbus_Functions.READ_HOLDING_REGISTERS:             //0x03
            case Modbus_Functions.READ_INPUT_REGISTER:                //0x04
            case Modbus_Functions.READ_HOLDING_REGISTERS_8BIT_CONTROL:
            case Modbus_Functions.READ_MEMORYBANK_CONTROL:
                if (this[quantityPos] != this.DataLength - DifferenceBetwwenFrameLengthAndReadObjectQuantity)
                {
                    return(CheckResponseResult.CR_Incomplete);
                }
                break;

            case Modbus_Functions.WRITE_SINGLE_COIL:                  //0x05
            case Modbus_Functions.WRITE_SINGLE_REGISTER:              //0x06
                if (this.DataLength != WriteSingleOrMultipleFrameLength)
                {
                    return(CheckResponseResult.CR_Incomplete);
                }
                break;

            case Modbus_Functions.WRITE_SINGLE_HOLDING_REGISTERS_8BIT_CONTROL:
                if (this.DataLength != WriteSingleOrMultipleFrameLength - 1) // we substract 1 because response for writing single register is shorter han standard response
                {
                    return(CheckResponseResult.CR_Incomplete);
                }
                break;

            case Modbus_Functions.WRITE_MULTIPLE_REGISTERS:
                if (this.DataLength != WriteSingleOrMultipleFrameLength)
                {
                    return(CheckResponseResult.CR_Incomplete);
                }
                break;

            case Modbus_Functions.WRITE_MEMORYBANK_CONTROL:
                if (this.DataLength != WriteSingleOrMultipleFrameLength + 1) // we add 1 because of bank field
                {
                    return(CheckResponseResult.CR_Incomplete);
                }
                break;

            default:
                break;
            }
            if (!this.PerformCRCCheck())
            {
                return(CheckResponseResult.CR_CRCError);
            }
            this.userDataLength -= CRCLengthInBytes;
            CheckResponseResult res = CheckResponseResult.CR_OK;

            offset = 0;
            if ((uint)(this.dataType) <= 0x7F)
            {
                exception = Modbus_Exceptions.OK;
                if ((txmsg.station != station) || (txmsg.dataType != dataType))
                {
                    return(CheckResponseResult.CR_SynchError);
                }
                switch (this.dataType)
                {
                case Modbus_Functions.READ_DISCRETE_INPUT:               //0x02
                case Modbus_Functions.READ_COILS:                        //0x01
                    offset = dataPos;
                    if (this[quantityPos] != txmsg.reqQuantity / 8 + 1)
                    {
                        return(CheckResponseResult.CR_SynchError);
                    }
                    RegisterInFrame = false;
                    break;

                case Modbus_Functions.READ_HOLDING_REGISTERS: //0x03
                case Modbus_Functions.READ_INPUT_REGISTER:    //0x04
                case Modbus_Functions.READ_MEMORYBANK_CONTROL:
                    if (this[quantityPos] != txmsg.reqQuantity * 2)
                    {
                        //             ^
                        //w tym polu porównywana jest liczba bajtow, ktora jest dwa razy wieksza od liczby rejsetrzow:
                        //Modbus_Application_Protocol_V1_1a.pdf page 15
                        return(CheckResponseResult.CR_SynchError);
                    }
                    offset          = dataPos;
                    RegisterInFrame = true;
                    break;

                case Modbus_Functions.READ_HOLDING_REGISTERS_8BIT_CONTROL:
                    if (this[quantityPos] != txmsg.reqQuantity)
                    {
                        //             ^
                        //w tym polu porównywana jest liczba bajtow, ktora jest dwa razy wieksza od liczby rejsetrzow:
                        //Modbus_Application_Protocol_V1_1a.pdf page 15
                        return(CheckResponseResult.CR_SynchError);
                    }
                    offset          = dataPos;
                    RegisterInFrame = true;
                    break;

                case Modbus_Functions.WRITE_SINGLE_COIL:                 //0x05
                    if ((this[coilWritePos + 1] != 0x0) || ((this[coilWritePos] != 0x00) & (this[coilWritePos] != 0xFF)))
                    {
                        res = CheckResponseResult.CR_Invalid;
                    }
                    if (this.DataLength != WriteSingleOrMultipleFrameLength - CRCLengthInBytes)
                    {
                        res = CheckResponseResult.CR_Invalid;
                    }
                    break;

                case Modbus_Functions.WRITE_SINGLE_REGISTER: //0x06
                    if (this.DataLength != WriteSingleOrMultipleFrameLength - CRCLengthInBytes)
                    {
                        res = CheckResponseResult.CR_Invalid;
                    }
                    break;

                case Modbus_Functions.WRITE_MULTIPLE_REGISTERS: //0x10
                    if (this.DataLength != WriteSingleOrMultipleFrameLength - CRCLengthInBytes)
                    {
                        res = CheckResponseResult.CR_Invalid;
                    }
                    if (this.address != txmsg.address || this.reqQuantity != txmsg.reqQuantity)
                    {
                        res = CheckResponseResult.CR_Invalid;
                    }
                    break;

                case Modbus_Functions.WRITE_SINGLE_HOLDING_REGISTERS_8BIT_CONTROL:
                    if (this.DataLength != WriteSingleOrMultipleFrameLength - CRCLengthInBytes - 1)//writing sigle 8bit is shorter than standard write resp
                    {
                        res = CheckResponseResult.CR_Invalid;
                    }
                    break;

                case Modbus_Functions.WRITE_MEMORYBANK_CONTROL:
                    if (this.DataLength != WriteSingleOrMultipleFrameLength - CRCLengthInBytes + 1)
                    {
                        res = CheckResponseResult.CR_Invalid;
                    }
                    break;

                default:
                    res = CheckResponseResult.CR_Invalid;
                    break;
                }
            }
            else
            {
                exception = (Modbus_Exceptions)this[exceptionPos];
                TraceEvent(TraceEventType.Warning, 328, "Modbus Message: the following exception has occurred: " + exception.ToString());
                res = CheckResponseResult.CR_NAK;
            }
            return(res);
        }//CheckResponseFrame