Example #1
0
        public byte[] ToBytes()
        {
            var cemiBytes = new byte[apdu.Length + 10];

            cemiBytes[0] = message_code;

            cemiBytes[1] = aditional_info_length;

            cemiBytes[2] = control_field_1;
            cemiBytes[3] = control_field_2;

            cemiBytes[4] = 0x00;
            cemiBytes[5] = 0x00;

            var dst_address = KnxHelper.GetAddress(destination_address);

            cemiBytes[6] = dst_address[0];
            cemiBytes[7] = dst_address[1];

            cemiBytes[8] = (byte)apdu.Length;

            cemiBytes[9] = 0x00;
            if (!_isstatus)
            {
                cemiBytes[10] = 0x80;
            }

            cemiBytes[10] |= (byte)(apdu[0] & 0x3f);
            for (var i = 1; i < apdu.Length; i++)
            {
                cemiBytes[10 + i] = apdu[i];
            }

            return(cemiBytes);
        }
Example #2
0
        public static KnxCEMI FromBytes(bool threeLevelGroupAssigning, byte[] cemiBytes)
        {
            var cemi = new KnxCEMI();

            cemi.message_code          = cemiBytes[0];
            cemi.aditional_info_length = cemiBytes[1];

            if (cemi.aditional_info_length > 0)
            {
                cemi.aditional_info = new byte[cemi.aditional_info_length];
                Array.Copy(cemiBytes, 2, cemi.aditional_info, 0, cemi.aditional_info_length);
            }

            cemi.control_field_1 = cemiBytes[2 + cemi.aditional_info_length];
            cemi.control_field_2 = cemiBytes[3 + cemi.aditional_info_length];
            cemi.source_address  = KnxHelper.GetIndividualAddress(new[] { cemiBytes[4 + cemi.aditional_info_length], cemiBytes[5 + cemi.aditional_info_length] });

            cemi.destination_address =
                KnxHelper.GetKnxDestinationAddressType(cemi.control_field_2).Equals(KnxHelper.KnxDestinationAddressType.INDIVIDUAL)
                    ? KnxHelper.GetIndividualAddress(new[] { cemiBytes[6 + cemi.aditional_info_length], cemiBytes[7 + cemi.aditional_info_length] })
                    : KnxHelper.GetGroupAddress(new[] { cemiBytes[6 + cemi.aditional_info_length], cemiBytes[7 + cemi.aditional_info_length] }, threeLevelGroupAssigning);

            var data_length = Math.Min(cemiBytes[8 + cemi.aditional_info_length], cemiBytes.Length - 10); // AR

            if (data_length > 0)
            {
                cemi.apdu = new byte[data_length];
                Array.Copy(cemiBytes, 10 + cemi.aditional_info_length, cemi.apdu, 0, data_length);
            }

            return(cemi);
        }
Example #3
0
        public static KnxCEMI CreateStatusCEMI(byte messageCode, string destinationAddress)
        {
            KnxCEMI cemi = new KnxCEMI()
            {
                message_code          = messageCode != 0x00 ? messageCode : (byte)0x11,
                aditional_info_length = 0,
                control_field_1       = 0xAC,
                control_field_2       = KnxHelper.IsAddressIndividual(destinationAddress) ? (byte)0x50 : (byte)0xF0,
                destination_address   = destinationAddress,
                apdu      = new byte[] { 0x00 },
                _isstatus = true
            };

            return(cemi);
        }
Example #4
0
        public static KnxCEMI CreateActionCEMI(byte messageCode, string destinationAddress, byte[] asdu)
        {
            KnxCEMI cemi = new KnxCEMI()
            {
                message_code          = messageCode != 0x00 ? messageCode : (byte)0x11,
                aditional_info_length = 0,
                control_field_1       = 0xAC,
                control_field_2       = KnxHelper.IsAddressIndividual(destinationAddress) ? (byte)0x50 : (byte)0xF0,
                destination_address   = destinationAddress,
                apdu = new byte[asdu.Length]
            };

            cemi.apdu[0] = (byte)(0x80 | (asdu[0] & 0x3f));
            for (var i = 1; i < asdu.Length; i++)
            {
                cemi.apdu[i] = asdu[i];
            }

            return(cemi);
        }