Exemple #1
0
        /// <summary>
        /// Converts <see cref="Medium_T "/> to equivalent internal frame type <see cref="SbusCode"/> for write operations.
        /// </summary>
        /// <param name="type">The external type.</param>
        /// <returns>The internal frame type <see cref="SbusCode"/> used to write values.</returns>
        internal static SbusCode DataType2SbusCode4Write(Medium_T type)
        {
            SbusCode code = 0;

            switch (type)
            {
            case Medium_T.Counter:
                code = SbusCode.coWriteCounter;
                break;

            case Medium_T.Flag:
                code = SbusCode.coWriteFlag;
                break;

            case Medium_T.Output:
                code = SbusCode.coWriteOutput;
                break;

            case Medium_T.Register:
                code = SbusCode.coWriteRegister;
                break;

            case Medium_T.Timer:
                code = SbusCode.coWriteTimer;
                break;

            case Medium_T.Text:
                code = SbusCode.coWriteText;
                break;
            }
            return(code);
        }
Exemple #2
0
        private static SbusCode GetSbusCode(Medium_T dataType)
        {
            SbusCode _ret = default(SbusCode);

            switch (dataType)
            {
            case Medium_T.Flag:
                _ret = SbusCode.coWriteFlag;
                break;

            case Medium_T.Output:
                _ret = SbusCode.coWriteOutput;
                break;

            case Medium_T.Register:
                _ret = SbusCode.coWriteRegister;
                break;

            case Medium_T.Timer:
                _ret = SbusCode.coWriteTimer;
                break;

            case Medium_T.Counter:
                _ret = SbusCode.coWriteCounter;
                break;

            case Medium_T.Text:
                _ret = SbusCode.coWriteText;
                break;

            default:
                Assert.Fail("Unexpected data type");
                break;
            }
            return(_ret);
        }
Exemple #3
0
        /// <summary>
        /// /// Converts <see cref="Medium_T "/> to equivalent internal frame type <see cref="SbusCode"/> for read operations
        /// </summary>
        /// <param name="type">The external type.</param>
        /// <returns>The internal frame type <see cref="SbusCode"/> used to read values.</returns>
        internal static SbusCode DataType2SbusCode4Read(Medium_T type)
        {
            SbusCode code = 0;

            switch (type)
            {
            case Medium_T.Counter:
                code = SbusCode.coReadCounter;
                break;

            case Medium_T.Flag:
                code = SbusCode.coReadFlag;
                break;

            case Medium_T.Output:
                code = SbusCode.coReadOutput;
                break;

            case Medium_T.Input:
                code = SbusCode.coReadInput;
                break;

            case Medium_T.Register:
                code = SbusCode.coReadRegister;
                break;

            case Medium_T.Timer:
                code = SbusCode.coReadTimer;
                break;

            case Medium_T.Text:
                code = SbusCode.coReadText;
                break;
            }
            return(code);
        }
Exemple #4
0
 /// <summary>
 /// Prepares the frame header.
 /// </summary>
 /// <param name="station">The station.</param>
 /// <param name="block">The block description.</param>
 /// <param name="code">The code.</param>
 protected override void PrepareFrameHeader(int station, IBlockDescription block, SbusCode code)
 {
     base.PrepareFrameHeader(station, block, code);
     FSPart = FrameSynchronisationChar;
 }
Exemple #5
0
 protected override void PrepareFrameHeader(int station, IBlockDescription block, SbusCode code)
 {
     base.PrepareFrameHeader(station, block, code);
     this.FRAMELENGTH = userDataLength + CRCLength;
     this.SetStandardVersion();
     this.SetStandardProtocolType();
     this.SEQUENCE_NUMBER = sequencenumber++;
 }
Exemple #6
0
        internal RequestSimulator(int length, object value, UInt32 frameLengt, byte stationAddr, Medium_T dataType, UInt16 addressIOFRTC)
        {
            SbusCode code = GetSbusCode(dataType);

            this.AddRange(FrameBitConverter.GetBytes((Int32)(frameLengt + 2))); //<frame>+<CRC>
            this.AddRange(_header);
            Add((byte)AttributeCharacter.Telegram);
            Add(stationAddr);
            Add((byte)code);
            Add((byte)(frameLengt - this.Count - 1 - 1));
            AddRange(FrameBitConverter.GetBytes(addressIOFRTC));
            switch (code)
            {
            case SbusCode.coWriteCounter:
            case SbusCode.coWriteRegister:
            case SbusCode.coWriteTimer:
                Assert.IsTrue(value.GetType() == typeof(Int32), "Wrong Sbus code");
                byte[] _valueBytes = FrameBitConverter.GetBytes((Int32)value);
                for (int _ix = 0; _ix < length; _ix++)
                {
                    AddRange(_valueBytes);
                }
                break;

            case SbusCode.coWriteFlag:
            case SbusCode.coWriteOutput:
                Assert.IsTrue(value.GetType() == typeof(bool), "Wrong Sbus code");
                Add((byte)length);
                byte _val = (bool)value ? (byte)0xFF : (byte)0x0;
                for (int _ix = 0; _ix < (length / 8 + 1); _ix++)
                {
                    Add(_val);
                }
                break;

            case SbusCode.coWriteText:
                Assert.IsTrue((value == null) || (value.GetType() == typeof(string)), "Wrong Sbus code");
                AddRange(FrameBitConverter.GetBytes((UInt16)0));
                string _valString            = (string)value;
                int    _expectedStringLength = (int)(frameLengt - this.Count);
                if (_valString == null)
                {
                    _valString = String.Empty;
                }
                if (_valString.Length < _expectedStringLength)
                {
                    _valString = _valString.PadRight(_expectedStringLength);
                }
                else if (_valString.Length > _expectedStringLength)
                {
                    _valString = _valString.Substring(0, _expectedStringLength);
                }
                this.AddRange(FrameBitConverter.GetBytes(_valString, length));
                break;

            default:
                Assert.Fail("Error in code");
                break;
            }
            Assert.AreEqual <uint>(frameLengt, (uint)this.Count, "Wrong length of the simulated frame");
        }
        /// <summary>
        /// Prepares the frame header.
        /// </summary>
        /// <param name="station">The station address.</param>
        /// <param name="block">The block description.</param>
        /// <param name="code">The type of the SBUS frame.</param>
        protected virtual void PrepareFrameHeader(int station, IBlockDescription block, SbusCode code)
        {
            userDataLength      = userBuffLength;
            this.FrameAttribute = AttributeCharacter.Telegram;
            this.offset         = stationAddressPos;
            WriteByte((byte)station);
            this.DataType = code;
            offset        = addressStartPos;
            WriteInt16((short)block.startAddress); //<text-number> or <address-RTC> or <address-IOF>
            switch (code)
            {
            case SbusCode.coReadText:
                WriteInt16(0); // <char-position>
                break;

            default:
                break;
            }
            this.SetBlockDescription(station, block); //It is used usaly while receiving.
            userDataLength = FrameRequestLength((ushort)block.length);
        }