Coding in C#

Hi,

I have written a program in c# that controls the lights in the house. There is one button that swicthoff all relays and dimmers but the code is not working. can you tell me how i can do it?
Seem that bus.send isn’t working. Have to use bus.sendblocking. The problem is in the routine 'Private void AllesUit_Click. Can you release the code for auotmatic detecting the comport where the usb interface is connected?

this is the code in C# express 2010

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Velleman.Velbus;


namespace Lindemeers
{
    
    public partial class Form_Lindemeers : Form
    {
        public byte Module_Address;
        public byte Module_Datasize;
        public byte Module_Command;
        public byte Module_Data2;
        public byte Module_Data3;
        public byte Module_Data4;
        public byte Module_Data5;
        public Packet packet = new Packet();
        public SerialBus bus = new SerialBus("Com11");

        public Form_Lindemeers()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
               
        }

        private void Buiten_Click(object sender, EventArgs e)
        {
            if (Buiten.BackColor == Color.Red)
            {
                Module_Address = 0x33;
                Module_Datasize = 2;
                Module_Command = 0x02;
                Module_Data2 = 0x08;
                Buiten.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x33;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x08;
                Buiten.BackColor = Color.Red;
            }
        }

        private void Guest_Click(object sender, EventArgs e)
        {
            if (Guest.BackColor == Color.Red)
            {
                Module_Address = 0x30;
                Module_Datasize = 2;
                Module_Command = 0x02;
                Module_Data2= 0x04;
                Guest.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x30;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x04;
                Guest.BackColor = Color.Red;
            }
        }

        private void Kleerkast_Click(object sender, EventArgs e)
        {
            if (Kleerkast.BackColor == Color.Red)
            {
                Module_Address = 0x30;
                Module_Datasize = 2;
                Module_Command = 0x02;
                Module_Data2 = 0x08;
                Kleerkast.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x30;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x08;
                Kleerkast.BackColor = Color.Red;
            }
        }

        private void Master_Click(object sender, EventArgs e)
        {
            if (Master.BackColor == Color.Red)
            {
                Module_Address = 0x30;
                Module_Datasize = 2;
                Module_Command = 0x02;
                Module_Data2 = 0x02;
                Master.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x30;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x02;
                Master.BackColor = Color.Red;
            }
        }

        private void Wc_Click(object sender, EventArgs e)
        {
            if (Wc.BackColor == Color.Red)
            {
                Module_Address = 0x31;
                Module_Datasize = 5;
                Module_Command = 0x03;
                Module_Data2 = 0x02;
                Module_Data3 = 0x0;
                Module_Data4 = 0x0;
                Module_Data5 = 0x0;
                Wc.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x31;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x02;
                Wc.BackColor = Color.Red;
            }
        }

        private void Inkom_Click(object sender, EventArgs e)
        {
            if (Inkom.BackColor == Color.Red)
            {
                Module_Address = 0x31;
                Module_Datasize = 2;
                Module_Command = 0x02;
                Module_Data2 = 0x08;
                Inkom.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x31;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x08;
                Inkom.BackColor = Color.Red;
            }
        }

        private void Vestiare_Click(object sender, EventArgs e)
        {
            if (Vestiare.BackColor == Color.Red)
            {
                Module_Address = 0x31;
                Module_Datasize = 2;
                Module_Command = 0x02;
                Module_Data2 = 0x04;
                Vestiare.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x31;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x04;
                Vestiare.BackColor = Color.Red;
            }
        }

        private void Amplix_Click(object sender, EventArgs e)
        {
            if (Amplix.BackColor == Color.Red)
            {
                Module_Address = 0x32;
                Module_Datasize = 2;
                Module_Command = 0x02;
                Module_Data2 = 0x08;
                Amplix.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x32;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x08;
                Amplix.BackColor = Color.Red;
            }
        }

        private void Vitrine_Click(object sender, EventArgs e)
        {
            if (Vitrine.BackColor == Color.Red)
            {
                Module_Address = 0x33;
                Module_Datasize = 2;
                Module_Command = 0x02;
                Module_Data2 = 0x08;
                Vitrine.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x33;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x08;
                Vitrine.BackColor = Color.Red;
            }
        }

        private void Bureel_Click(object sender, EventArgs e)
        {
            if (Bureel.BackColor == Color.Red)
            {
                Module_Address = 0x30;
                Module_Datasize = 2;
                Module_Command = 0x02;
                Module_Data2 = 0x01;
                Bureel.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x30;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x01;
                Bureel.BackColor = Color.Red;
            }
        }

        private void Garage_Click(object sender, EventArgs e)
        {
            if (Garage.BackColor == Color.Red)
            {
                Module_Address = 0x33;
                Module_Datasize = 2;
                Module_Command = 0x02;
                Module_Data2 = 0x02;
                Garage.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x33;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x02;
                Garage.BackColor = Color.Red;
            }
        }

        private void Badkamer_Click(object sender, EventArgs e)
        {
            if (Badkamer.BackColor == Color.Red)
            {
                Module_Address = 0x33;
                Module_Datasize = 5;
                Module_Command = 0x03;
                Module_Data2 = 0x04;
                Module_Data3 = 0x0;
                Module_Data4 = 0x0;
                Module_Data5 = 0x0;
                Badkamer.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x33;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x04;
                Badkamer.BackColor = Color.Red;
            }
        }

        private void Keukenkasten_Click(object sender, EventArgs e)
        {
            if (Keukenkasten.BackColor == Color.Red)
            {
                Module_Address = 0x32;
                Module_Datasize = 2;
                Module_Command = 0x02;
                Module_Data2 = 0x01;
                Keukenkasten.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x32;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x01;
                Keukenkasten.BackColor = Color.Red;
            }
        }

        private void Keuken_Click(object sender, EventArgs e)
        {
            if (Keuken.BackColor == Color.Red)
            {
                Module_Address = 0x32;
                Module_Datasize = 2;
                Module_Command = 0x02;
                Module_Data2 = 0x04;
                Keuken.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x32;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x04;
                Keuken.BackColor = Color.Red;
            }
        }

        private void Berging_Click(object sender, EventArgs e)
        {
            if (Berging.BackColor == Color.Red)
            {
                Module_Address = 0x33;
                Module_Datasize = 2;
                Module_Command = 0x02;
                Module_Data2 = 0x01;
                Berging.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x33;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x01;
                Berging.BackColor = Color.Red;
            }
        }

        private void Keukentafel_Click(object sender, EventArgs e)
        {
            if (Keukentafel.BackColor == Color.Red)
            {
                Module_Address = 0x32;
                Module_Datasize = 2;
                Module_Command = 0x02;
                Module_Data2 = 0x02;
                Keukentafel.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x32;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x02;
                Keukentafel.BackColor = Color.Red;
            }
        }

        private void Tuin_Click(object sender, EventArgs e)
        {
 //           if (Tuin.BackColor == Color.Red)
 //           {
 //               Module_Address = 0x30;
 //               Module_Datasize = 2;
 //               Module_Command = 0x02;
 //               Module_Data2 = 0x10;
 //               Tuin.BackColor = Color.Green;
 //           }
 //           else
 //           {
 //               Module_Address = 0x30;
 //               Module_Datasize = 2;
 //               Module_Command = 0x01;
 //               Module_Data2 = 0x010;
 //               Tuin.BackColor = Color.Red;
 //           }
        }

        private void AllesUit_Click(object sender, EventArgs e)
        {
            int teller = 0;
            for(teller = 0; teller <= 3; teller ++)
            {
                bus.Open();
                packet.Address = 0x30 + teller;
                packet.Priority = PacketPriority.Low;
                packet.DataSize = 2;
                packet.Command = 0x01;
                packet[1] = 0x0f;
                bus.SendBlocking(packet);
                bus.Close();
               
            }

           
//            bus.Open();
//            packet.Address = 0x31;
//            packet.Priority = PacketPriority.High;
//            packet.DataSize = 2;
//            packet.Command = 0x01;
//            packet[1] = 0x0F;
//            bus.SendBlocking(packet);
//            bus.Close();

//            bus.Open();
//            packet.Address = 0x32;
//            packet.Priority = PacketPriority.High;
//            packet.DataSize = 2;
//            packet.Command = 0x01;
//            packet[1] = 0xF;
//            bus.SendBlocking(packet);
//            bus.Close();

//            bus.Open();
//            packet.Address = 0x33;
//            packet.Priority = PacketPriority.High;
//            packet.DataSize = 2;
//            packet.Command = 0x01;
//            packet[1] = 0xF;
//            bus.SendBlocking(packet);
//            bus.Close();

            bus.Open();
            packet.Address = 0x40;
            packet.Priority = PacketPriority.High;
            packet.DataSize = 5;
            packet.Command = 0x07;
            packet[1] = 0x01;
            packet[2] = 0x00;
            packet[3] = 0xff;
            packet[4] = 0xff;
            bus.SendBlocking(packet);
            bus.Close();

            bus.Open();
            packet.Address = 0x41;
            packet.Priority = PacketPriority.High;
            packet.DataSize = 5;
            packet.Command = 0x07;
            packet[1] = 0x01;
            packet[2] = 0x00;
            packet[3] = 0xFF;
            packet[4] = 0xFF;
            bus.SendBlocking(packet);
            bus.Close();

            bus.Open();
            packet.Address = 0x50;
            packet.Priority = PacketPriority.High;
            packet.DataSize = 5;
            packet.Command = 0x07;
            packet[1] = 0x01;
            packet[2] = 0x00;
            packet[3] = 0xFF;
            packet[4] = 0xFF;
            bus.SendBlocking(packet);
            bus.Close();
    }

        private void Verzend_Click(object sender, EventArgs e)
        {
            SerialBus bus = new SerialBus("Com11");
            bus.Open();
            Packet packet = new Packet();
            packet.Address = Module_Address;
            packet.Priority = PacketPriority.High;
            packet.DataSize = Module_Datasize;
            packet.Command = Module_Command;
            if (packet.DataSize == 2)
            {
                packet[1] = Module_Data2;
            }
            if (packet.DataSize == 5)
            {
                packet[1] = Module_Data2;
                packet[2] = Module_Data3;
                packet[3] = Module_Data4;
                packet[4] = Module_Data5;
            }
            bus.SendBlocking(packet);
            bus.Close();
        }

        private void TrackBarLiving_Scroll_1(object sender, EventArgs e)
        {
            int waarde;
            waarde = TrackBarLiving.Value;
            Module_Address = 0x41;
            Module_Datasize = 5;
            Module_Command = 0x07;
            Module_Data2 = 0x01;
            Module_Data3 = Convert.ToByte(waarde);
            Module_Data4 = 0x00;
            Module_Data5 = 0x00;
        }

        private void Zolder_Click(object sender, EventArgs e)
        {
            if (Zolder.BackColor == Color.Red)
            {
                Module_Address = 0x31;
                Module_Datasize = 5;
                Module_Command = 0x03;
                Module_Data2 = 0x01;
                Module_Data3 = 0x0;
                Module_Data4 = 0x0;
                Module_Data5 = 0x0;
                Zolder.BackColor = Color.Green;
            }
            else
            {
                Module_Address = 0x31;
                Module_Datasize = 2;
                Module_Command = 0x01;
                Module_Data2 = 0x01;
                Zolder.BackColor = Color.Red;
            }
        }

        private void TrackBarEetplaats_Scroll(object sender, EventArgs e)
        {
            int waarde;
            waarde = TrackBarEetplaats.Value;
            Module_Address = 0x40;
            Module_Datasize = 5;
            Module_Command = 0x07;
            Module_Data2 = 0x01;
            Module_Data3 = Convert.ToByte(waarde);
            Module_Data4 = 0x00;
            Module_Data5 = 0x00;
        }

        private void TrackBarTV_Scroll(object sender, EventArgs e)
        {
            int waarde;
            waarde = TrackBarTV.Value;
            Module_Address = 0x50;
            Module_Datasize = 5;
            Module_Command = 0x07;
            Module_Data2 = 0x01;
            Module_Data3 = Convert.ToByte(waarde);
            Module_Data4 = 0x00;
            Module_Data5 = 0x00;
        }
    }
}

Thanks for any reply

Everything else works?
Are you using the correct COM port?

Yes i am. I can control everything. Only when i use the “Alles Uit” button only the modules with address 30,40 and 50 are responding to the command sent.
Also the bus.sent event isn’t working. I have to use the bus.sentblocking so there is no 50ms delay between packets. How can i put a 50ms delay in my “Alles Uit” event?
Can you release the code for automatic detecting the comport where the USB interface is connected to ( like you use in Velbuslink 7)?

The ‘Switch Relay Off’ command (0x01) has high priority

    bus.Open();
    packet.Address = 0x30 + teller;
    packet.Priority = PacketPriority.High; // <- fix this
    packet.DataSize = 2;
    packet.Command = 0x01;
    packet[1] = 0x0f;
    bus.SendBlocking(packet);
    bus.Close();

The reason Send() does not work for you is because you close the connection before the packet can be sent (packets are queued and sent asynchronously by a separate thread, with a 50ms delay between each packet).

You should fix your application so that you connect once at the start of the application, and disconnect once when your application finishes. Calling Open() and Close() for every packet is very inefficiënt.

Another fix you could do (which i don’t recommend) is putting the thread to sleep until the packet has been sent.

// using System.Threading;

    bus.Send(packet); // put the packet in the queue
    Thread.Sleep(100); // wait 100ms to make sure the queue is processed
    bus.Close(); // disconnect

VelbusLink is written in Delphi 7, it uses some advanced Windows API calls to detect USB interfaces and filter out the ones with the correct VID/PID combination. Perhaps this can also be done using .NET, but it probably won’t be simple.

Thanks for the quick reply.
I will modify my program to open the at the start and close when the program ends. For detecting the comport i will google a bit around.

Its working like a charm.
Feel free to use this c# code.

LOL, that was easy :confused: Here’s how you enumerate COM ports for the Velbus (fixed + virtual) in C#:

// using System.Management;
// + Project->Add Reference..->System.Management.dll

ManagementObjectSearcher searcher = new ManagementObjectSearcher(
        "Select * from Win32_SerialPort");

ManagementObjectCollection results = searcher.Get();

foreach (ManagementObject USBDevice in results)
{
    string deviceId = USBDevice.Properties"PNPDeviceID"].Value.ToString();

    if (deviceId.Contains("VID_10CF&PID_0B1B") || deviceId.Contains("ACPI"))
    {
        // If you want the complete port name like:
        //   Communications Port (COM1)
        //   VMB1USB Velbus USB interface (COM4)
        //
        // Then use the property "Name"
        //
        // USBDevice.Properties"Name"].Value.ToString();

        // If you want simple port names that you can use like:
        //   COM1
        //   COM4
        //
        // Then use the property "DeviceId"
        //
        // USBDevice.Properties"DeviceId"].Value.ToString();
    }
}

My own console example using the “Name” property gave me:

Which is correct :slight_smile:

How can i get at the start of my program the status of my relaysmodules. I want to know if there are already relays ‘On’ or ‘Off’ before i switch them on or off. I tried to send a ‘FA’ command to my modules at the start of my program but gets no reply.
This is the piece of code:

for (int i = 0x30; i <= 0x50; i++)
{
// loop over all possible addresses
// and send out my scan packet
packet.Address =(byte) i;
packet.Rtr = true;
packet.Priority = PacketPriority.Low;
packet.DataSize = 2;
packet.Command = 0xFA;
packet[1] = 0x0F;
bus.Send(packet);
}

Any hints?

...
packet.Rtr := false; // false is also the default value
...

RTR is not used for 99% of packets

i have solved already the problem. Still adding controls to the form. When central heating program part is finnished i will put the code here or is there a place where i can put the sourcecode of
my program to share with others?

Best thing would be to put it online on some free file sharing website or personal webspace, since i imagine it’s quite a long bit of code