示例#1
0
        public async Task <bool> SendStatusReq()
        {
            byte[] packet = new byte[64]; //Create packet which will be fixed 64 bytes long

            //Report Header
            packet[0] = 1;    // Report ID fixed 1;
            packet[1] = 0x13; // First Packet with start and end
            packet[2] = 9;    // Data length


            //Transfer Header
            packet[3]  = 0;    // Version fixed 0
            packet[4]  = 8;    // Header length
            packet[5]  = 0;    // Body length
            packet[6]  = 1;    // Body length
            packet[7]  = 0x0F; // Protocol ID
            packet[8]  = 0x01; // Service Identifier
            packet[9]  = 0;    // Manufacturer Code
            packet[10] = 0;    // Manufacturer Code

            // Body
            packet[11] = 3; // Bus Status

            await DeviceKnx.InitializeAsync();

            await DeviceKnx.WriteAsync(packet);

            DeviceKnx.Close();

            return(true);
        }
示例#2
0
        //TODO Adresse der Schnitstelle schreibt man im Speicher an Adresse 279

        public async Task Connect()
        {
            byte[] packet = new byte[64]; //Create packet which will be fixed 64 bytes long

            //Report Header
            packet[0] = 1;    //Report Id (fixed)
            packet[1] = 0x13; // First Packet with start and end
            packet[2] = 9;    // Data length

            //Transfer Header
            packet[3]  = 0;    // Version fixed 0
            packet[4]  = 8;    // Header length
            packet[5]  = 0;    // Body length
            packet[6]  = 1;    // Body length
            packet[7]  = 0x0F; // Protocol ID
            packet[8]  = 0x01; // Service Identifier
            packet[9]  = 0;    // Manufacturer Code
            packet[10] = 0;    // Manufacturer Code

            // Body
            packet[11] = 1; // Supported Emis

            bool isInited = DeviceKnx.IsInitialized;

            await DeviceKnx.InitializeAsync();

            CancellationTokenSource source = new CancellationTokenSource(1000);

            TransferResult read = await DeviceKnx.WriteAndReadAsync(packet, source.Token);

            BitArray emis = new BitArray(new byte[] { read.Data[13] });

            byte setEmi = 0;

            if (emis.Get(2))
            {
                Debug.WriteLine("USB: Verwende cEmi");
                CurrentType = ProtocolTypes.cEmi;
            }
            else if (emis.Get(1))
            {
                Debug.WriteLine("USB: Verwende Emi2");
                CurrentType = ProtocolTypes.Emi2;
            }
            else if (emis.Get(0))
            {
                CurrentType = ProtocolTypes.Emi1;
                Debug.WriteLine("USB: Verwende Emi1");
            }
            else
            {
                Debug.WriteLine("USB: unterstützt kein Emi1/Emi2/cEmi");
            }


            packet[6]  = 0x02;
            packet[8]  = 0x03;                        //Device Feature Set
            packet[11] = 0x05;                        //Set Emi Type
            packet[12] = Convert.ToByte(CurrentType); //Selected Emi type

            source = new CancellationTokenSource(1000);
            await DeviceKnx.WriteAsync(packet, source.Token);

            packet[6]  = 0x01;
            packet[8]  = 0x01; //Device Feature Get
            packet[11] = 0x03; //Bus Status
            packet[12] = 0x00;

            source = new CancellationTokenSource(1000);
            read   = await DeviceKnx.WriteAndReadAsync(packet, source.Token);

            int status = read.Data[12];

            if (status == 0)
            {
                throw new Exception("Schnittstelle hat keine Verbindung zum Bus.");
            }


            packet[2]  = 0x0c;
            packet[6]  = 0x04;                        //Body length
            packet[7]  = 0x01;                        //Tunnel
            packet[8]  = Convert.ToByte(CurrentType); //Selected Emi
            packet[11] = 0x4c;                        //Memory Read
            packet[12] = 0x02;                        //Length
            packet[13] = 0x01;                        //Address High
            packet[14] = 0x17;                        //Address Low

            source = new CancellationTokenSource(1000);
            read   = await DeviceKnx.WriteAndReadAsync(packet, source.Token);

            PhysicalAddress = UnicastAddress.FromByteArray(new byte[] { read.Data[15], read.Data[16] });

            if (source.IsCancellationRequested)
            {
                DeviceKnx.Close();
                throw new Exception("Schnittstelle antwortet nicht.");
            }

            IsConnected = true;
            ConnectionChanged?.Invoke(true);

            ProcessSendMessages();
        }