コード例 #1
0
 internal ResponseSimulator(int length, object value)
 {
     this.AddRange(_header);
     Add((byte)AttributeCharacter.ResponseData);
     if (value.GetType() == typeof(bool))
     {
         byte _val = FrameBitConverter.GetBytes((bool)value);
         for (int _ix = 0; _ix < ((length - 1) / 8 + 1); _ix++)
         {
             Add(_val);
         }
     }
     else if (value.GetType() == typeof(string))
     {
         this.AddRange(FrameBitConverter.GetBytes((string)value, length));
     }
     else
     {
         byte[] _valueBytes = FrameBitConverter.GetBytes((int)value);
         for (int _ix = 0; _ix < length; _ix++)
         {
             AddRange(_valueBytes);
         }
     }
 }
コード例 #2
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");
        }