BACSharp.Types.BacNetTag.GetBytes C# (CSharp) Method

GetBytes() public method

public GetBytes ( ) : byte[]
return byte[]
        public byte[] GetBytes()
        {
            byte[] res = _longTag ? new byte[2] : new byte[1];

            for (int i = 0; i < 8; i++)
            {
                if (i < 3)
                {
                    if (((_longTag ? 5 : Length) >> i & 1) == 1)
                        res[0] = (byte) (res[0] | (1 << i));
                }
                if (i > 2 & i < 4)
                {
                    if (Class)
                        res[0] = (byte)(res[0] | (1 << i));
                }
                if (i > 3)
                {
                    if ((Number >> (i - 4) & 1) == 1)
                        res[0] = (byte)(res[0] | (1 << i));
                }
            }
            if (_longTag)
                res[1] = Length;
            return res;
        }

Usage Example

Example #1
0
        public byte[] GetBytes()
        {
            ArrayList res = new ArrayList();
            res.Add((byte)BacNetEnums.BACNET_PDU_TYPE.PDU_TYPE_CONFIRMED_SERVICE_REQUEST);
            res.Add((byte)84);
            res.Add(BacNetDevice.Instance.InvokeId);
            res.Add((byte)BacNetEnums.BACNET_CONFIRMED_SERVICE.SERVICE_CONFIRMED_CREATE_OBJECT);

            BacNetTag openingTag = new BacNetTag { Class = true, Length = 6, Number = 0 };
            res.AddRange(openingTag.GetBytes());

            BacNetTag objectTag = new BacNetTag { Class = true, Length = 4, Number = 1 };
            res.AddRange(objectTag.GetBytes());
            res.AddRange(NewObject.GetObjectBytes());

            BacNetTag closingTag = new BacNetTag { Class = true, Length = 7, Number = 0 };
            res.AddRange(closingTag.GetBytes());

            objectTag.Class = false;
            objectTag.Number = 0;

            openingTag.Number = 1;
            res.AddRange(openingTag.GetBytes());

            openingTag.Number = 2;
            closingTag.Number = 2;

            foreach (BacNetProperty property in NewObject.Properties)
            {
                BacNetTag propertyIdTag = new BacNetTag { Class = true, Length = (byte)property.PropertyId.GetLength(), Number = 0 };
                res.AddRange(propertyIdTag.GetBytes());
                res.AddRange(property.PropertyId.GetBytes());

                res.AddRange(openingTag.GetBytes());

                BacNetTag metaTag = new BacNetTag();
                foreach (var value in property.Values)
                {
                    int type;
                    byte[] valueBytes = ByteConverter.GetPropertyValueBytes(value, out type);
                    metaTag = new BacNetTag { Class = false, Length = (byte)valueBytes.Length, Number = (byte)type, LongTag = true };
                    res.AddRange(metaTag.GetBytes());
                    res.AddRange(valueBytes);
                }

                res.AddRange(closingTag.GetBytes());
            }

            closingTag.Number = 1;
            res.AddRange(closingTag.GetBytes());

            return (byte[])res.ToArray(typeof(byte));
        }
All Usage Examples Of BACSharp.Types.BacNetTag::GetBytes