A VelBus server-mode tool

did a quick check by making a countup and countdown routine. the outcome is the same. seems to do somehow with sending my packet to the dimmer.
this is the code:

   private void button1_Click(object sender, EventArgs e)
    {
        tellerup = tellerup + 1;
        if (tellerup > 100)
        {
            tellerup = 100;
        }
        label17.Text = Convert.ToString(tellerup);
        packet.Address = 0x40;
        packet.Priority = PacketPriority.High;
        packet.Rtr = false;
        packet.DataSize = 5;
        packet.Command = 0x07;
        packet[1] = 0x01;
        packet[2] = Convert.ToByte(tellerup);
        packet[3] = 0x00;
        packet[4] = 0x00;
        bus.Send(packet);

    }

    private void button2_Click(object sender, EventArgs e)
    {
        tellerdn = tellerdn - 1;
        if (tellerdn < 1)
        {
            tellerdn = 0;
        }
        label17.Text = Convert.ToString(tellerdn);
        packet.Address = 0x40;
        packet.Priority = PacketPriority.High;
        packet.Rtr = false;
        packet.DataSize = 5;
        packet.Command = 0x07;
        packet[1] = 0x01;
        packet[2] = Convert.ToByte(tellerdn);
        packet[3] = 0x00;
        packet[4] = 0x00;
        bus.Send(packet);
    }

maybe someone at velleman can give some help or a tip. Feel free to give your help too.

My suggestion is to use rs232 COM analyzing software (serial analyzers) to inspect the serial data that’s being sent and received. Or you could replace your bus class quickly with a class that connects as a synchronous socket client (like 10 lines of code) and connect it to the socket server in this thread. Then you can do the same with VelbusLink and inspect the traffic from there.

Are you sure the packet is actually being send? Have you tried putting a MessageBox.Show(…) right before or after the bus.Send() command or a breakpoint in the button voids? Then you can be pretty sure that the code is actually executed and the problem lies somewhere behind your software or pc…

Just thinking. : )

i put a loglist on sending a packet and also one on receiving a packet. nothing special to find in the logs. when sending a packet to a relay i get 1 packet in the sentlog. in the receivelog i get 2 packets from the relaymodule.

And do the packets in the log also appear when the relays don’t work because of the dimmers not being zero? then it’s defenitly a problem with the hardware, right? And something for the support team to look at…

Found the solution. The dimmers routine parsed 4 databytes and the relay_on/off routine (in each buttonclick routine)parsed only 2 databytes so leaving databytes 3&4 in the state set in the dimmerroutine. All i need to do was putting those bytes to zero.

You mentioned earlier to adapt my program so i could connect to your velbusserver. Can you give the code to do that because i don’t know how to do this.

Thx

Of course, it’s even somewhat on topic.

The simplest way is to use a synchronous socket client. It’s very easy to implement but as it’s synchronous, it has to send one packet in order to receive one packet. If you only want to send packets, then it’s good enough. But if you want to receive, you shouldn’t use this method as you won’t be able to receive packets arbitrary.

using System.Net.Sockets;
using Velleman.Velbus;

public class socketBus
{
        TcpClient clientSocket;
        NetworkStream serverStream;

        public socketBus()
        {
         clientSocket = new System.Net.Sockets.TcpClient();
        }

        public void connect(string IP, int port)
        {
         clientSocket.Connect(IP, port);
         serverStream = clientSocket.GetStream();
        }

        public void send(Packet pckt)
        {
         byte] outStream = packet.Pack();
         serverStream.Write(outStream, 0, outStream.Length);
         serverStream.Flush();
        }

        public void disconnect()
        {
         serverStream.Dispose();
         clientSocket.Close();
        }
}

A sample usage would simply be:

...
/* connect to the server */
socketBus sBus = new sBus();
sBus.connect("127.0.0.1", 8445);
...
/* send a packet (from anywhere with access to sBus) */
sBus.send(somevelbuspacket);
...
/* disconnect from the server */
sBus.disconnect();
...

The better way is to use an asynchronous socket client. I will post an example this evening.

Cheers

I wrote a usercontrol to connect to a velbus socket server, I use it succesfull with danssaertd’s socket server.
I hope you can use it in your projects.

!!WARNING code is quick and dirty!!

VelbusConnector.cs

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

namespace VelbusServer
{
    public partial class VelbusConnector : UserControl
    {

        IAsyncResult m_asynResult;
        public AsyncCallback pfnCallBack;

        private Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        private PacketParser pp = new PacketParser();

        private byte] receivebuffer = new byte[512];

        public delegate void PacketReceivedHandler(ReceivedPacketArgs packetInfo);
        public event PacketReceivedHandler PacketReceived;

        public VelbusConnector()
        {
            InitializeComponent();
        }

        public void Connect(string server, int port)
        {
            try
            {
                IPHostEntry hostEntry = null;
                hostEntry = Dns.GetHostEntry(server);
                IPAddress resolvedIP = null;
                foreach (IPAddress ip in hostEntry.AddressList)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        resolvedIP = ip;
                    }

                }
                IPEndPoint ipe = new IPEndPoint(resolvedIP, port);

                s.Connect(ipe);

                WaitForData();
            }
            catch
            {
                MessageBox.Show("Kan niet verbinden, is de Server online?");
            }
        }

        public void Disconnect()
        {
            s.Disconnect(false);
            s.Close();
        }

        public void WaitForData()
        {
            if (pfnCallBack == null)
                pfnCallBack = new AsyncCallback(OnDataReceived);
            // now start to listen for any data...
            m_asynResult =
            s.BeginReceive(receivebuffer, 0, receivebuffer.Length, SocketFlags.None, pfnCallBack, null);
        }

        private void OnDataReceived(IAsyncResult res)
        {
            try
            {
                //end receive...
                int iRx = 0;
                iRx = s.EndReceive(res);

                pp.Feed(receivebuffer, iRx); // feed received data to the parser

                Packet packet = pp.Next(); // try to parse a packet

                while (packet != null) // did we succeed in parsing a packet?
                {
                    ReceivedPacketArgs ra = new ReceivedPacketArgs(packet);

                    if (PacketReceived != null)
                        PacketReceived(ra);  // raise event

                    packet = pp.Next(); // try to parse another packet
                }

                WaitForData();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        public class ReceivedPacketArgs : EventArgs
        {
            public ReceivedPacketArgs(Packet packet)
            {
                this.packet = packet;
            }
            public readonly Packet packet;
        }


        public void SendPacket(Packet packet)
        {
            s.Send(packet.Pack());      
        }

    }
}

VelbusConnector.designer.cs

[code]namespace VelbusServer
{
partial class VelbusConnector
{
///


/// Required designer variable.
///

private System.ComponentModel.IContainer components = null;

    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        s.Dispose();
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Component Designer generated code

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(14, 13);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(88, 13);
        this.label1.TabIndex = 0;
        this.label1.Text = "VelbusConnector";
        // 
        // VelbusConnector
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Controls.Add(this.label1);
        this.Name = "VelbusConnector";
        this.Size = new System.Drawing.Size(122, 39);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Label label1;
}

}
[/code]

Thank you Cantryn! That’s almost exactly what I did.
Pretty smart of you to make a UserControl of it, I wouldn’t have thought of that.
I might do that with my own class too.

@Stis: I obviously don’t have to post mine anymore. : )

The idea was to make it reuseable.
I’ve got usercontrols for lights (relais), dimmers and temperature sensors with custom properties (address, relaisnumber, … and the velbusconnector control)
This way I can just drop them on a form, set their properties and presto, a working application.

@ danssaertd

here is my program code:

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

namespace Lindemeers
{
public partial class Form_lindemeers : Form
{
public bool pir_enabled;
public bool week_changed = false;

    public int inputpir;
    public int dag_in_jaar;
    public int old_dag_in_jaar;
    public int index_zon;
    public int index_temp;
    public int index_temp_bureel;
    public int index_temp_badkamer;
    public int index_jaar;
    public int kolum1;
    public int kolum2;
    public int aantal_schakelmomenten;
    public int aantal_schakelmomenten_bureel;
    public int aantal_schakelmomenten_badkamer;
    public int j = 0;
    public int k = 0;
    public int l = 0;
    public int waarde1;
    public int waarde2;
    public int waarde3;
            
    public decimal waarde13;
    public decimal waarde14;
    public decimal waarde15;

    public double waarde4;
    public double waarde5;
    public double waarde6;

    public string str_row_zon;
    public string str_row_temp;
    public string str_row_jaar;
    public string opkomstuur;
    public string onderganguur;
    public string kolumnaam1;
    public string kolumnaam2;
    public string stis;
    public string temp_regime_leefruimte;
    public string temp_regime_bureel;
    public string temp_regime_badkamer;
    public string input_week = "1";

    public string ] uur = {" "," "," "," "," "," "};
    public string ] temp = {" "," "," "," "," "," "};
    public string] uur_badkamer = { " ", " ", " ", " ", " ", " " };
    public string] temp_badkamer = { " ", " ", " ", " ", " ", " " };
    public string] uur_bureel = { " ", " "};
    public string] temp_bureel = { " ", " "};

    public Packet packet = new Packet();
    public SerialBus bus = new SerialBus("Com5");

    public Form_lindemeers()
    {
        InitializeComponent();
        bus.Open();
        bus.PacketReceived += new BusPacketIoEventHandler(PacketReceived);
        label4.Text = " ";
        label7.Text = input_week;

        // Loop over all relaymodules
        // and send out my scan packet
        for (int i = 0x30; i <= 0x3F; i++)
        {
            packet.Address = i;
            packet.Priority = PacketPriority.Low;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0xFA;
            packet[1] = 0x0F;
            bus.Send(packet);
        }

        // Loop over all dimmermodules
        // and send out my scan packet
        for (int i = 0x40; i <= 0x5F; i++)
        {
            packet.Address = i;
            packet.Priority = PacketPriority.Low;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0xFA;
            packet[1] = 1;
            bus.Send(packet);
        }

        // Send out my scan packet to all the VMBTS modules
        for (int i = 0x61; i <= 0x6F; i++)
        {
            packet.Address = i;
            packet.Priority = PacketPriority.Low;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0xFA;
            packet[1] = 1;
            bus.Send(packet);

            packet.Address = i;
            packet.Priority = PacketPriority.Low;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0xE5;
            packet[1] = 0x00;
            packet[2] = 0x00;
            bus.Send(packet);
        }
        // Reading data from database 'Lindemeers'
        this.zonsopenondergangTableAdapter.Fill(this.lindemeersDataSet.zonsopenondergang);
        this.TempTableAdapter.Fill(this.lindemeersDataSet.Temp);
        this.temp_bureelTableAdapter.Fill(this.lindemeersDataSet.Temp_bureel);
        this.temp_badkamerTableAdapter.Fill(this.lindemeersDataSet.Temp_badkamer);
        this.schoolvakantieTableAdapter.Fill(this.lindemeersDataSet.Schoolvakantie);
        this.feestdagenTableAdapter.Fill(this.lindemeersDataSet.Feestdagen);
    }

    public void PacketReceived(object source, BusPacketIoEventArgs args)
    {
        // look if relay is on or off
        if (args.Packet.Address == 0x30 && args.Packet.Command == 0xFB && args.Packet[1] == 1 && args.Packet[3] == 1)
        {
            if (Bureel.InvokeRequired)
                Bureel.BeginInvoke(new MethodInvoker(delegate() { Bureel.BackColor = Color.Green; }));
            else
                Bureel.BackColor = Color.Green;
        }
        if (args.Packet.Address == 0x30 && args.Packet.Command == 0xFB && args.Packet[1] == 1 && args.Packet[3] == 0)
        {
            if (Bureel.InvokeRequired)
                Bureel.BeginInvoke(new MethodInvoker(delegate() { Bureel.BackColor = Color.Red; }));
            else
                Bureel.BackColor = Color.Red;
        }
        if (args.Packet.Address == 0x30 && args.Packet.Command == 0xFB && args.Packet[1] == 2 && args.Packet[3] == 2)
        {
            if (Master.InvokeRequired)
                Master.BeginInvoke(new MethodInvoker(delegate() { Master.BackColor = Color.Green; }));
            else
                Master.BackColor = Color.Green;
        }
        if (args.Packet.Address == 0x30 && args.Packet.Command == 0xFB && args.Packet[1] == 2 && args.Packet[3] == 0)
        {
            if (Master.InvokeRequired)
                Master.BeginInvoke(new MethodInvoker(delegate() { Master.BackColor = Color.Red; }));
            else
                Master.BackColor = Color.Red;
        }
        if (args.Packet.Address == 0x30 && args.Packet.Command == 0xFB && args.Packet[1] == 4 && args.Packet[3] == 4)
        {
            if (Guest.InvokeRequired)
                Guest.BeginInvoke(new MethodInvoker(delegate() { Guest.BackColor = Color.Green; }));
            else
                Guest.BackColor = Color.Green;
        }
        if (args.Packet.Address == 0x30 && args.Packet.Command == 0xFB && args.Packet[1] == 4 && args.Packet[3] == 0)
        {
            if (Guest.InvokeRequired)
                Guest.BeginInvoke(new MethodInvoker(delegate() { Guest.BackColor = Color.Red; }));
            else
                Guest.BackColor = Color.Red;
        }
        if (args.Packet.Address == 0x30 && args.Packet.Command == 0xFB && args.Packet[1] == 8 && args.Packet[3] == 8)
        {
            if (Kleerkast.InvokeRequired)
                Kleerkast.BeginInvoke(new MethodInvoker(delegate() { Kleerkast.BackColor = Color.Green; }));
            else
                Kleerkast.BackColor = Color.Green;
        }
        if (args.Packet.Address == 0x30 && args.Packet.Command == 0xFB && args.Packet[1] == 8 && args.Packet[3] == 0)
        {
            if (Kleerkast.InvokeRequired)
                Kleerkast.BeginInvoke(new MethodInvoker(delegate() { Kleerkast.BackColor = Color.Red; }));
            else
                Kleerkast.BackColor = Color.Red;
        }

        if (args.Packet.Address == 0x31 && args.Packet.Command == 0xFB && args.Packet[1] == 1 && args.Packet[3] == 1)
        {
            if (Zolder.InvokeRequired)
                Zolder.BeginInvoke(new MethodInvoker(delegate() { Zolder.BackColor = Color.Green; }));
            else
                Zolder.BackColor = Color.Green;
        }
        if (args.Packet.Address == 0x31 && args.Packet.Command == 0xFB && args.Packet[1] == 1 && args.Packet[3] == 0)
        {
            if (Zolder.InvokeRequired)
                Zolder.BeginInvoke(new MethodInvoker(delegate() { Zolder.BackColor = Color.Red; }));
            else
                Zolder.BackColor = Color.Red;
        }
        if (args.Packet.Address == 0x31 && args.Packet.Command == 0xFB && args.Packet[1] == 2 && args.Packet[3] == 2)
        {
            if (Wc.InvokeRequired)
                Wc.BeginInvoke(new MethodInvoker(delegate() { Wc.BackColor = Color.Green; }));
            else
                Wc.BackColor = Color.Green;
        }
        if (args.Packet.Address == 0x31 && args.Packet.Command == 0xFB && args.Packet[1] == 2 && args.Packet[3] == 0)
        {
            if (Wc.InvokeRequired)
                Wc.BeginInvoke(new MethodInvoker(delegate() { Wc.BackColor = Color.Red; }));
            else
                Wc.BackColor = Color.Red;
        }
        if (args.Packet.Address == 0x31 && args.Packet.Command == 0xFB && args.Packet[1] == 4 && args.Packet[3] == 4)
        {
            if (Vestiare.InvokeRequired)
                Vestiare.BeginInvoke(new MethodInvoker(delegate() { Vestiare.BackColor = Color.Green; }));
            else
                Vestiare.BackColor = Color.Green;
        }
        if (args.Packet.Address == 0x31 && args.Packet.Command == 0xFB && args.Packet[1] == 4 && args.Packet[3] == 0)
        {
            if (Vestiare.InvokeRequired)
                Vestiare.BeginInvoke(new MethodInvoker(delegate() { Vestiare.BackColor = Color.Red; }));
            else
                Vestiare.BackColor = Color.Red;
        }
        if (args.Packet.Address == 0x31 && args.Packet.Command == 0xFB && args.Packet[1] == 8 && args.Packet[3] == 8)
        {
            if (Inkom.InvokeRequired)
                Inkom.BeginInvoke(new MethodInvoker(delegate() { Inkom.BackColor = Color.Green; }));
            else
                Inkom.BackColor = Color.Green;
        }
        if (args.Packet.Address == 0x31 && args.Packet.Command == 0xFB && args.Packet[1] == 8 && args.Packet[3] == 0)
        {
            if (Inkom.InvokeRequired)
                Inkom.BeginInvoke(new MethodInvoker(delegate() { Inkom.BackColor = Color.Red; }));
            else
                Inkom.BackColor = Color.Red;
        }

        if (args.Packet.Address == 0x32 && args.Packet.Command == 0xFB && args.Packet[1] == 1 && args.Packet[3] == 1)
        {
            if (Keukenkasten.InvokeRequired)
                Keukenkasten.BeginInvoke(new MethodInvoker(delegate() {Keukenkasten.BackColor = Color.Green;}));
            else
                Keukenkasten.BackColor = Color.Green;
        }
        if (args.Packet.Address == 0x32 && args.Packet.Command == 0xFB && args.Packet[1] == 1 && args.Packet[3] == 0)
        {
            if (Keukenkasten.InvokeRequired)
                Keukenkasten.BeginInvoke(new MethodInvoker(delegate() { Keukenkasten.BackColor = Color.Red; }));
            else
                Keukenkasten.BackColor = Color.Red;
        }
        if (args.Packet.Address == 0x32 && args.Packet.Command == 0xFB && args.Packet[1] == 2 && args.Packet[3] == 2)
        {

// WriteLog_rcv(String.Format(“Packet received from address {0,0}”, args.Packet.Address,args.Packet.Command));
if (Keukentafel.InvokeRequired)
Keukentafel.BeginInvoke(new MethodInvoker(delegate() { Keukentafel.BackColor = Color.Green; }));
else
Keukentafel.BackColor = Color.Green;
}
if (args.Packet.Address == 0x32 && args.Packet.Command == 0xFB && args.Packet[1] == 2 && args.Packet[3] == 0)
{
// WriteLog_rcv(String.Format(“Packet received from address {0,0}”, args.Packet.Address, args.Packet.Command));
if (Keukentafel.InvokeRequired)
Keukentafel.BeginInvoke(new MethodInvoker(delegate() { Keukentafel.BackColor = Color.Red; }));
else
Keukentafel.BackColor = Color.Red;
}
if (args.Packet.Address == 0x32 && args.Packet.Command == 0xFB && args.Packet[1] == 4 && args.Packet[3] == 4)
{
if (Keuken.InvokeRequired)
Keuken.BeginInvoke(new MethodInvoker(delegate() { Keuken.BackColor = Color.Green; }));
else
Keuken.BackColor = Color.Green;
}
if (args.Packet.Address == 0x32 && args.Packet.Command == 0xFB && args.Packet[1] == 4 && args.Packet[3] == 0)
{
if (Keuken.InvokeRequired)
Keuken.BeginInvoke(new MethodInvoker(delegate() { Keuken.BackColor = Color.Red; }));
else
Keuken.BackColor = Color.Red;
}
if (args.Packet.Address == 0x32 && args.Packet.Command == 0xFB && args.Packet[1] == 8 && args.Packet[3] == 8)
{
if (Amplix.InvokeRequired)
Amplix.BeginInvoke(new MethodInvoker(delegate() { Amplix.BackColor = Color.Green; }));
else
Amplix.BackColor = Color.Green;
}
if (args.Packet.Address == 0x32 && args.Packet.Command == 0xFB && args.Packet[1] == 8 && args.Packet[3] == 0)
{
if (Amplix.InvokeRequired)
Amplix.BeginInvoke(new MethodInvoker(delegate() { Amplix.BackColor = Color.Red; }));
else
Amplix.BackColor = Color.Red;
}

        if (args.Packet.Address == 0x33 && args.Packet.Command == 0xFB && args.Packet[1] == 1 && args.Packet[3] == 1)
        {
            if (Berging.InvokeRequired)
                Berging.BeginInvoke(new MethodInvoker(delegate() { Berging.BackColor = Color.Green; }));
            else
                Berging.BackColor = Color.Green;
        }
        if (args.Packet.Address == 0x33 && args.Packet.Command == 0xFB && args.Packet[1] == 1 && args.Packet[3] == 0)
        {
            if (Berging.InvokeRequired)
                Berging.BeginInvoke(new MethodInvoker(delegate() { Berging.BackColor = Color.Red; }));
            else
                Berging.BackColor = Color.Red;
        }
        if (args.Packet.Address == 0x33 && args.Packet.Command == 0xFB && args.Packet[1] == 2 && args.Packet[3] == 2)
        {
            if (Garage.InvokeRequired)
                Garage.BeginInvoke(new MethodInvoker(delegate() { Garage.BackColor = Color.Green; }));
            else
                Garage.BackColor = Color.Green;
        }
        if (args.Packet.Address == 0x33 && args.Packet.Command == 0xFB && args.Packet[1] == 2 && args.Packet[3] == 0)
        {
            if (Garage.InvokeRequired)
                Garage.BeginInvoke(new MethodInvoker(delegate() { Garage.BackColor = Color.Red; }));
            else
                Garage.BackColor = Color.Red;
        }
        if (args.Packet.Address == 0x33 && args.Packet.Command == 0xFB && args.Packet[1] == 4 && args.Packet[3] == 4)
        {
            if (Badkamer.InvokeRequired)
                Badkamer.BeginInvoke(new MethodInvoker(delegate() {Badkamer.BackColor = Color.Green;}));
            else
                Badkamer.BackColor = Color.Green;
        }
        if (args.Packet.Address == 0x33 && args.Packet.Command == 0xFB && args.Packet[1] == 4 && args.Packet[3] == 0)
        {
            if (Badkamer.InvokeRequired)
                Badkamer.BeginInvoke(new MethodInvoker(delegate() { Badkamer.BackColor = Color.Red; }));
            else
                Badkamer.BackColor = Color.Red;
        }
        if (args.Packet.Address == 0x33 && args.Packet.Command == 0xFB && args.Packet[1] == 8 && args.Packet[3] == 8)
        {
            if (Buiten.InvokeRequired)
                Buiten.BeginInvoke(new MethodInvoker(delegate() { Buiten.BackColor = Color.Green; }));
            else
                Buiten.BackColor = Color.Green;
        }
        if (args.Packet.Address == 0x33 && args.Packet.Command == 0xFB && args.Packet[1] == 8 && args.Packet[3] == 0)
        {
            if (Buiten.InvokeRequired)
                Buiten.BeginInvoke(new MethodInvoker(delegate() { Buiten.BackColor = Color.Red; }));
            else
                Buiten.BackColor = Color.Red;
        }

        if (args.Packet.Address == 0x34 && args.Packet.Command == 0xFB && args.Packet[1] == 1 && args.Packet[3] == 1)
        {
            if (label10.InvokeRequired)
                label10.BeginInvoke(new MethodInvoker(delegate() { label10.BackColor = Color.Green; }));
        }
        if (args.Packet.Address == 0x34 && args.Packet.Command == 0xFB && args.Packet[1] == 1 && args.Packet[3] == 0)
        {
            if (label10.InvokeRequired)
                label10.BeginInvoke(new MethodInvoker(delegate() { label10.BackColor = Color.Red; }));
            else
                Guest.BackColor = Color.Red;
        }

        // Get value of the dimmer and put it into variables waarde1,2 & 3
        if (args.Packet.Address == 0x40 && args.Packet.Command == 0xEE)
        {
            if (numericUpDown1.InvokeRequired)
                numericUpDown1.BeginInvoke(new MethodInvoker(delegate() { label15.Text = Convert.ToString(args.Packet[2]); }));
        }
        if (args.Packet.Address == 0x41 && args.Packet.Command == 0xEE)
        {
            if (numericUpDown2.InvokeRequired)
                numericUpDown2.BeginInvoke(new MethodInvoker(delegate() { label14.Text = Convert.ToString(args.Packet[2]); }));
        }
        if (args.Packet.Address == 0x50 && args.Packet.Command == 0xEE)
        {
            if (numericUpDown3.InvokeRequired)
                numericUpDown3.BeginInvoke(new MethodInvoker(delegate() { label16.Text = Convert.ToString(args.Packet[2]); }));
        }
        if (args.Packet.Address == 0x61 && args.Packet.Command == 0xEA)
        {
            waarde13 = Convert.ToDecimal(args.Packet[5]);
            if (numericUpDownTemp_leefruimte.InvokeRequired)
                numericUpDownTemp_leefruimte.BeginInvoke(new MethodInvoker(delegate() {numericUpDownTemp_leefruimte.Value = waarde13/2;}));
        }
        if (args.Packet.Address == 0x62 && args.Packet.Command == 0xEA)
        {
            waarde14 = Convert.ToDecimal(args.Packet[5]);
            if (numericUpDown_bureel.InvokeRequired)
                numericUpDown_bureel.BeginInvoke(new MethodInvoker(delegate() {numericUpDown_bureel.Value = waarde14/2;}));
        }
        if (args.Packet.Address == 0x63 && args.Packet.Command == 0xEA)
        {
            waarde15 = Convert.ToDecimal(args.Packet[5]);
            if (numericUpDown_temp_badkamer.InvokeRequired)
                numericUpDown_temp_badkamer.BeginInvoke(new MethodInvoker(delegate() {numericUpDown_temp_badkamer.Value = waarde15/2;}));
        }

        // Get value from VMBTS and put it in waarde(4,5,6)
        if (args.Packet.Address == 0x61 && args.Packet.Command == 0xE6)
        {
            waarde4 = (((args.Packet[1] * 256) + args.Packet[2]) / 32) * 0.0625;
        }
        if (args.Packet.Address == 0x62 && args.Packet.Command == 0xE6)
        {
            waarde5 = (((args.Packet[1] * 256) + args.Packet[2]) / 32) * 0.0625;
        }
        if (args.Packet.Address == 0x63 && args.Packet.Command == 0xE6)
        {
            waarde6 = (((args.Packet[1] * 256) + args.Packet[2]) / 32) * 0.0625;
        }

        //Looking for input on VMB8pd
        if (args.Packet.Address == 0x0C && args.Packet.Command == 0x00)
        {
            inputpir = args.Packet[1];
        }
    }

    private void Bureel_Click(object sender, EventArgs e)
    {
        if (Bureel.BackColor == Color.Red)
        {
            packet.Address = 0x30;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x02;
            packet[1] = 0x01;
            packet[2] = 0x00;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x30;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x01;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }

    private void Master_Click(object sender, EventArgs e)
    {
        if (Master.BackColor == Color.Red)
        {
            packet.Address = 0x30;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x02;
            packet[1] = 0x02;
            packet[2] = 0x00;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x30;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x02;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }

    private void Guest_Click(object sender, EventArgs e)
    {
        if (Guest.BackColor == Color.Red)
        {
            packet.Address = 0x30;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x02;
            packet[1] = 0x04;
            packet[2] = 0x00;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x30;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x04;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }

    private void Kleerkast_Click(object sender, EventArgs e)
    {
        if (Kleerkast.BackColor == Color.Red)
        {
            packet.Address = 0x30;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x02;
            packet[1] = 0x08;
            packet[2] = 0x00;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x30;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x08;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }

    private void Zolder_Click(object sender, EventArgs e)
    {
        if (Zolder.BackColor == Color.Red)
        {
            packet.Address = 0x31;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 5;
            packet.Command = 0x03;
            packet[1] = 0x01;
            packet[2] = 0x0;
            packet[3] = 0x0;
            packet[4] = 0x0;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x31;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x01;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }

    private void Wc_Click(object sender, EventArgs e)
    {
        if (Wc.BackColor == Color.Red)
        {
            packet.Address = 0x31;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 5;
            packet.Command = 0x03;
            packet[1] = 0x02;
            packet[2] = 0x0;
            packet[3] = 0x0;
            packet[4] = 0x0;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x31;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x02;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }

    private void Vestiare_Click(object sender, EventArgs e)
    {
        if (Vestiare.BackColor == Color.Red)
        {
            packet.Address = 0x31;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x02;
            packet[1] = 0x04;
            packet[2] = 0x00;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x31;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x04;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }

    private void Inkom_Click(object sender, EventArgs e)
    {
        if (Inkom.BackColor == Color.Red)
        {
            packet.Address = 0x31;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x02;
            packet[1] = 0x08;
            packet[2] = 0x00;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x31;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x08;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }

    private void Keukenkasten_Click(object sender, EventArgs e)
    {
        if (Keukenkasten.BackColor == Color.Red)
        {
            packet.Address = 0x32;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x02;
            packet[1] = 0x01;
            packet[2] = 0x00;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x32;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x01;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }

    private void Keukentafel_Click(object sender, EventArgs e)
    {
        if (Keukentafel.BackColor == Color.Red)
        {
            packet.Address = 0x32;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x02;
            packet[1] = 0x02;
            packet[2] = 0x00;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x32;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x02;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }

    private void Keuken_Click(object sender, EventArgs e)
    {
        if (Keuken.BackColor == Color.Red)
        {
            packet.Address = 0x32;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x02;
            packet[1] = 0x04;
            packet[2] = 0x00;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x32;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x04;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }
    private void Amplix_Click(object sender, EventArgs e)
    {
        if (Amplix.BackColor == Color.Red)
        {
            packet.Address = 0x32;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x02;
            packet[1] = 0x08;
            packet[2] = 0x00;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x32;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x08;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }

    private void Berging_Click(object sender, EventArgs e)
    {
        if (Berging.BackColor == Color.Red)
        {
            packet.Address = 0x33;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x02;
            packet[1] = 0x01;
            packet[2] = 0x00;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x33;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x01;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }

    private void Garage_Click(object sender, EventArgs e)
    {
        if (Garage.BackColor == Color.Red)
        {
            packet.Address = 0x33;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x02;
            packet[1] = 0x02;
            packet[2] = 0x00;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x33;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x02;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }

    private void Badkamer_Click(object sender, EventArgs e)
    {
        if (Badkamer.BackColor == Color.Red)
        {
            packet.Address = 0x33;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 5;
            packet.Command = 0x03;
            packet[1] = 0x04;
            packet[2] = 0x0;
            packet[3] = 0x0;
            packet[4] = 0x0;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x33;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x04;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }

    private void Vitrine_Click(object sender, EventArgs e)
    {

// if (Vitrine.BackColor == Color.Red)
// {
// packet.Address = 0x34;
// packet.Priority = PacketPriority.High;
// packet.Rtr = false;
// packet.DataSize = 2;
// packet.Command = 0x02;
// packet[1] = 0x01;
// packet[2] = 0x00;
// bus.Send(packet);
// }
// else
// {
// packet.Address = 0x34;
// packet.Priority = PacketPriority.High;
// packet.Rtr = false;
// packet.DataSize = 2;
// packet.Command = 0x01;
// packet[1] = 0x01;
// packet[2] = 0x00;
// bus.Send(packet);
// }
}

    private void Buiten_Click(object sender, EventArgs e)
    {
        if (Buiten.BackColor == Color.Red)
        {
            packet.Address = 0x33;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x02;
            packet[1] = 0x08;
            packet[2] = 0x00;
            bus.Send(packet);
        }
        else
        {
            packet.Address = 0x33;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x08;
            packet[2] = 0x00;
            bus.Send(packet);
        }
    }
    private void Pergola_Click(object sender, EventArgs e)
    {

// if (Pergola.BackColor == Color.Red)
// {
// packet.Address = 0x34;
// packet.Priority = PacketPriority.High;
// packet.Rtr = false;
// packet.DataSize = 2;
// packet.Command = 0x02;
// packet[1] = 0x02;
// packet[2] = 0x00;
// bus.Send(packet);
// }
// else
// {
// packet.Address = 0x34;
// packet.Priority = PacketPriority.High;
// packet.Rtr = false;
// packet.DataSize = 2;
// packet.Command = 0x01;
// packet[1] = 0x02;
// packet[2] = 0x00;
// bus.Send(packet);
// }
}

    private void AllesUit_Click(object sender, EventArgs e)
    {
        //Switch all relaymodules off
        for (int i = 0x30; i <= 0x3f; i++)
        {
            packet.Address = i;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x0F;
            packet[2] = 0x00;
            packet[3] = 0;
            packet[4] = 0;
            bus.Send(packet);
        }

        // Switch all dimmers off
        for (int i = 0x40; i <= 0x5F; i++)
        {
            packet.Address = i;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 5;
            packet.Command = 0x07;
            packet[1] = 0x01;
            packet[2] = 0x00;
            packet[3] = 0x00;
            packet[4] = 0x00;
            bus.Send(packet);
        }
    }

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        packet.Address = 0x40;
        packet.Priority = PacketPriority.High;
        packet.Rtr = false;
        packet.DataSize = 5;
        packet.Command = 0x07;
        packet[1] = 0x01;
        packet[2] = Convert.ToByte(numericUpDown1.Value);
        packet[3] = 0xFF;
        packet[4] = 0xFF;
        bus.Send(packet);
        label15.Text = Convert.ToString(numericUpDown1.Value);
    }

    private void numericUpDown2_ValueChanged(object sender, EventArgs e)
    {
        packet.Address = 0x41;
        packet.Priority = PacketPriority.High;
        packet.Rtr = false;
        packet.DataSize = 5;
        packet.Command = 0x07;
        packet[1] = 0x01;
        packet[2] = Convert.ToByte(numericUpDown2.Value);
        packet[3] = 0xFF;
        packet[4] = 0xFF;
        bus.Send(packet);
        label14.Text = Convert.ToString(numericUpDown2.Value);
    }

    private void numericUpDown3_ValueChanged(object sender, EventArgs e)
    {
        packet.Address = 0x50;
        packet.Priority = PacketPriority.High;
        packet.Rtr = false;
        packet.DataSize = 5;
        packet.Command = 0x07;
        packet[1] = 0x01;
        packet[2] = Convert.ToByte(numericUpDown3.Value);
        packet[3] = 0xFF;
        packet[4] = 0xFF;
        bus.Send(packet);
        label16.Text = Convert.ToString(numericUpDown3.Value);
    }

    private void numericUpDownTemp_leefruimte_ValueChanged(object sender, EventArgs e)
    {
        packet.Address = 0x61;
        packet.Priority = PacketPriority.Low;
        packet.Rtr = false;
        packet.DataSize = 3;
        packet.Command = 0xE4;
        packet[1] = 0;
        packet[2] = Convert.ToByte(numericUpDownTemp_leefruimte.Value * 2);
        packet[3] = 0;
        packet[4] = 0;
        bus.Send(packet);
    }
   
    private void numericUpDown_bureel_ValueChanged(object sender, EventArgs e)
    {
        packet.Address = 0x62;
        packet.Priority = PacketPriority.Low;
        packet.Rtr = false;
        packet.DataSize = 3;
        packet.Command = 0xE4;
        packet[1] = 0;
        packet[2] = Convert.ToByte(numericUpDown_bureel.Value * 2);
        packet[3] = 0;
        packet[4] = 0;
        bus.Send(packet);
    }

    private void numericUpDown_temp_badkamer_ValueChanged(object sender, EventArgs e)
    {
        packet.Address = 0x63;
        packet.Priority = PacketPriority.Low;
        packet.Rtr = false;
        packet.DataSize = 3;
        packet.Command = 0xE4;
        packet[1] = 0;
        packet[2] = Convert.ToByte(numericUpDown_temp_badkamer.Value * 2);
        packet[3] = 0;
        packet[4] = 0;
        bus.Send(packet);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        label2.Text = DateTime.Now.ToString();
        dag_in_jaar = DateTime.Now.DayOfYear;

        //controleren  op verandering van dag zodat de database mag gelezen worden
        if (dag_in_jaar != old_dag_in_jaar)
        {
            old_dag_in_jaar = dag_in_jaar;
            index_zon = this.lindemeersDataSet.zonsopenondergang.Count();
            index_temp = this.lindemeersDataSet.Temp.Count();
            index_temp_bureel = this.lindemeersDataSet.Temp_bureel.Count();
            index_temp_badkamer = this.lindemeersDataSet.Temp_badkamer.Count();
            index_jaar = this.lindemeersDataSet.Feestdagen.Count();

            //uur van opkomst en ondergang uit datatabel zonsopenondergang lezen
            for (int teller_zon = 1; teller_zon <= index_zon; teller_zon++)
            {
                foreach (DataRow row1 in lindemeersDataSet.zonsopenondergang)
                {
                    str_row_zon = row1"Id"].ToString();
                    if (str_row_zon == dag_in_jaar.ToString())
                    {
                        opkomstuur = row1"Opkomst"].ToString().Substring(11);
                        onderganguur = row1"Ondergang"].ToString().Substring(11);
                        break;
                    }
                }
            }
            label5.Text = opkomstuur;
            label6.Text = onderganguur;
            // lezen van schakelmomenten voor temperatuurregeling van leefruimte
            for (int teller_temp = 1; teller_temp <= index_temp; teller_temp++)
            {
                foreach (DataRow row2 in lindemeersDataSet.Temp)
                {
                    if (input_week == row2"week"].ToString())
                    {
                        if (DateTime.Now.DayOfWeek.ToString() == row2"dag"].ToString())
                        {
                            int teller1 = Convert.ToInt16(row2"aantal_schakelmomenten"]);
                            aantal_schakelmomenten = teller1;
                            for (int i = 0; i < teller1; i++)
                            {
                                kolum1 = 4 + (2 * i);
                                if (kolum1 == 4)
                                {
                                    kolumnaam1 = "uur_1";
                                    kolumnaam2 = "temperatur_1";
                                    uur[0] = row2[kolumnaam1].ToString();
                                    temp[0] = row2[kolumnaam2].ToString();
                                }
                                if (kolum1 == 6)
                                {
                                    kolumnaam1 = "uur_2";
                                    kolumnaam2 = "temperatuur_2";
                                    uur[1] = row2[kolumnaam1].ToString();
                                    temp[1] = row2[kolumnaam2].ToString();
                                }
                                if (kolum1 == 8)
                                {
                                    kolumnaam1 = "uur_3";
                                    kolumnaam2 = "temperatuur_3";
                                    uur[2] = row2[kolumnaam1].ToString();
                                    temp[2] = row2[kolumnaam2].ToString();
                                }
                                if (kolum1 == 10)
                                {
                                    kolumnaam1 = "uur_4";
                                    kolumnaam2 = "temperatuur_4";
                                    uur[3] = row2[kolumnaam1].ToString();
                                    temp[3] = row2[kolumnaam2].ToString();
                                }
                                if (kolum1 == 12)
                                {
                                    kolumnaam1 = "uur_5";
                                    kolumnaam2 = "temperatuur_5";
                                    uur[4] = row2[kolumnaam1].ToString();
                                    temp[4] = row2[kolumnaam2].ToString();
                                }
                                if (kolum1 == 14)
                                {
                                    kolumnaam1 = "uur_6";
                                    kolumnaam2 = "temperatuur_6";
                                    uur[5] = row2[kolumnaam1].ToString();
                                    temp[5] = row2[kolumnaam2].ToString();
                                }
                                if (i == Convert.ToInt16(row2"aantal_schakelmomenten"]))
                                {
                                    break;
                                }
                            }
                        }
                    }
                    if (teller_temp > index_temp)
                    {
                        break;
                    }
                }
            }

            // lezen van schakelmomenten voor temperatuurregeling van badkamer
            for (int teller_temp_badkamer = 1; teller_temp_badkamer <= index_temp_badkamer; teller_temp_badkamer++)
            {
                foreach (DataRow row3 in lindemeersDataSet.Temp_badkamer)
                {
                    if (input_week == row3"week_badkamer"].ToString())
                    {
                        if (DateTime.Now.DayOfWeek.ToString() == row3"dag_badkamer"].ToString())
                        {
                            int teller1 = Convert.ToInt16(row3"aantal_schakelmomenten_badkamer"]);
                            aantal_schakelmomenten_badkamer = teller1;
                            for (int i = 0; i < teller1; i++)
                            {
                                kolum1 = 4 + (2 * i);
                                if (kolum1 == 4)
                                {
                                    kolumnaam1 = "uur_1_badkamer";
                                    kolumnaam2 = "regime_1_badkamer";
                                    uur_badkamer[0] = row3[kolumnaam1].ToString();
                                    temp_badkamer[0] = row3[kolumnaam2].ToString();
                                }
                                if (kolum1 == 6)
                                {
                                    kolumnaam1 = "uur_2_badkamer";
                                    kolumnaam2 = "regime_2_badkamer";
                                    uur_badkamer[1] = row3[kolumnaam1].ToString();
                                    temp_badkamer[1] = row3[kolumnaam2].ToString();
                                }
                                if (kolum1 == 8)
                                {
                                    kolumnaam1 = "uur_3_badkamer";
                                    kolumnaam2 = "regime_3_badkamer";
                                    uur_badkamer[2] = row3[kolumnaam1].ToString();
                                    temp_badkamer[2] = row3[kolumnaam2].ToString();
                                }
                                if (kolum1 == 10)
                                {
                                    kolumnaam1 = "uur_4_badkamer";
                                    kolumnaam2 = "regime_4_badkamer";
                                    uur_badkamer[3] = row3[kolumnaam1].ToString();
                                    temp_badkamer[3] = row3[kolumnaam2].ToString();
                                }
                                 if (i == Convert.ToInt16(row3"aantal_schakelmomenten_badkamer"]))
                                {
                                    break;
                                }
                            }
                        }
                    }
                    if (teller_temp_badkamer > index_temp)
                    {
                        break;
                    }
                }
            }

            // lezen van schakelmomenten voor temperatuurregeling van bureel
            for (int teller_temp_bureel = 1; teller_temp_bureel <= index_temp_bureel; teller_temp_bureel++)
            {
                foreach (DataRow row4 in lindemeersDataSet.Temp_bureel)
                {
                        if (DateTime.Now.DayOfWeek.ToString() == row4"dag_bureel"].ToString())
                        {
                            int teller1 = Convert.ToInt16(row4"aantal_schakelmomenten_bureel"]);
                            aantal_schakelmomenten_bureel = teller1;
                            for (int i = 0; i < teller1; i++)
                            {
                                kolum1 = 3 + (2 * i);
                                if (kolum1 == 3)
                                {
                                    kolumnaam1 = "uur_1_bureel";
                                    kolumnaam2 = "regime_1_bureel";
                                    uur_bureel[0] = row4[kolumnaam1].ToString();
                                    temp_bureel[0] = row4[kolumnaam2].ToString();
                                }
                                if (kolum1 == 5)
                                {
                                    kolumnaam1 = "uur_2_bureel";
                                    kolumnaam2 = "regime_2_bureel";
                                    uur_bureel[1] = row4[kolumnaam1].ToString();
                                    temp_bureel[1] = row4[kolumnaam2].ToString();
                                }
                                if (i == Convert.ToInt16(row4"aantal_schakelmomenten_bureel"]))
                                {
                                    break;
                                }
                            }
                        }
                    }
                    if (teller_temp_bureel > index_temp_bureel)
                    {
                        break;
                    }
            }
        }
        stis = DateTime.Now.ToLongTimeString();
        label1.Text = stis;

        //Disabelen van bewegingsmelder tussen zonsop- en zonsondergang
        if (stis == opkomstuur)
        {
            pir_enabled = false;
            packet.Address = 0x33;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 2;
            packet.Command = 0x01;
            packet[1] = 0x08;
            bus.Send(packet);
        }
        else if (stis == onderganguur)
        {
            pir_enabled = true;
        }
        if (pir_enabled == true)
        {
            label3.Text = "Enabled";
        }
        else
        {
            label3.Text = "Disabled";
        }
        if (pir_enabled == true && inputpir == 0x10)
        {
            packet.Address = 0x33;
            packet.Priority = PacketPriority.High;
            packet.Rtr = false;
            packet.DataSize = 5;
            packet.Command = 0x03;
            packet[1] = 0x08;
            packet[2] = 0x00;
            packet[3] = 0x00;
            packet[4] = 0x00;
            bus.Send(packet);
        }

        label4.Text = Convert.ToString(waarde4);
        label8.Text = Convert.ToString(waarde5);
        label9.Text = Convert.ToString(waarde6);

        // testen of er tussen dag- en nachttemperatuur moet geschakeld worden in leefruimte
        if (uur[j]== DateTime.Now.ToLongTimeString())
        {
            if (temp[j] == "Day")
            {
                packet.Address = 0x61;
                packet.Priority = PacketPriority.Low;
                packet.Rtr = false;
                packet.DataSize = 3;
                packet.Command = 0xDC;
                packet[1] = 0x00;
                packet[2] = 0x00;
                bus.Send(packet);
            }
            if (temp[j] == "Night")
            {
                packet.Address = 0x61;
                packet.DataSize = 3;
                packet.Priority = PacketPriority.Low;
                packet.Rtr = false;
                packet.Command = 0xDD;
                packet[1] = 0x00;
                packet[2] = 0x00;
                bus.Send(packet);
            }
            j = j+1;
            if (j > aantal_schakelmomenten-1)
            {
                j = 0;
            }
        }

        // testen of er tussen dag- en nachttemperatuur moet geschakeld worden in bureel
        if (uur_bureel[k] == DateTime.Now.ToLongTimeString())
        {
            if (temp_bureel[k] == "Day")
            {
                packet.Address = 0x62;
                packet.Priority = PacketPriority.Low;
                packet.Rtr = false;
                packet.DataSize = 3;
                packet.Command = 0xDC;
                packet[1] = 0x00;
                packet[2] = 0x00;
                bus.Send(packet);
            }
            if (temp_bureel[k] == "Night")
            {
                packet.Address = 0x62;
                packet.Priority = PacketPriority.Low;
                packet.Rtr = false;
                packet.DataSize = 3;
                packet.Command = 0xDD;
                packet[1] = 0x00;
                packet[2] = 0x00;
                bus.Send(packet);
            }
            k = k + 1;
            if (k > aantal_schakelmomenten_bureel - 1)
            {
                k = 0;
            }
        }

        // testen of er tussen dag- en nachttemperatuur moet geschakeld worden in badkamer
        if (uur_badkamer[l] == DateTime.Now.ToLongTimeString())
        {
            if (temp_badkamer[l] == "Comfort")
            {
                packet.Address = 0x63;
                packet.Priority = PacketPriority.Low;
                packet.Rtr = false;
                packet.DataSize = 3;
                packet.Command = 0xDB;
                packet[1] = 0x00;
                packet[2] = 0x00;
                bus.Send(packet);
            }
             if (temp_badkamer[l] == "Night")
            {
                packet.Address = 0x63;
                packet.Priority = PacketPriority.Low;
                packet.Rtr = false;
                packet.DataSize = 3;
                packet.Command = 0xDD;
                packet[1] = 0x00;
                packet[2] = 0x00;
                bus.Send(packet);
            }
            l = l + 1;
        }
        if (l > aantal_schakelmomenten_badkamer - 1)
        {
            l = 0;
        }

        if (DateTime.Now.DayOfWeek.ToString() == "Sunday" && DateTime.Now.ToLongTimeString() == "23:59:59" && input_week == "1" && !week_changed)
        {
            input_week = "2";
            label7.Text = "Week 2";
            week_changed = true;
        }
        if (DateTime.Now.DayOfWeek.ToString() == "Sunday" &&DateTime.Now.ToLongTimeString() == "23:59:59" && input_week == "2" && week_changed)
        {
            input_week = "1";
            label7.Text = "Week 1";
            week_changed = false;
        }
    }

    void MainForm_Formclosed(object sender, FormClosedEventArgs e)
    {
        // Be sure to clean up
        bus.Close();
    }

 }

}

What i want to do is to send my packet to your velbus-server. can you adapt my code or show me the code to do that?
i have tried the code from Cantryn but i thing i did something wrong. it won’t work for me.

thx

stis

Stis,

What problems did you have with my control? It’s a little rough around the edges but you should be able to make it work.

You have to make a new user control, paste my code in the appropriate files, change the namespace, build your project and there will be a VelbusConnector control above your controls in VS, you can now put it somewhere in your application and you should be ok.

About your code, you might want to use procedures for distinct actions, like switching a relay. You can use the “Command” and “CommandArgument” properties of your buttons to pass the Address and Relaynumber of the module you are targeting. It wil make your code much shorter and easier to maintain.
The approach I use is making custom or usercontrols for every function. You can than add your own properties, actions and events. This will greatly reduce your code and development time.

To Cantryn

can you give me a example? i’m new to usercontrols and so

thx

stis

Stis,

try this:
In VS, right click your project - Add - User Control…
Name the CS file VelbusConnector.cs.
Copy the contents of my previous post in the corresponding files (VelbusConnector.cs & VelbusConnector.Designer.cs)
Resolve any namespace errors.
Build your project.
You should now have an extra tab in your toolbox “[Your Namespace] Components” with a VelbusConnector control.
Add the control to your form, it will show up as a rectangle with “VelbusConnector” in it, it won’t be visible at runtime.
You can now refer to it in code using the following methods:
Connect(Hostname as string, port as int) - So to make a connection to a host Host1 running a socket server on port 3788 with VelbusConnector velbusconnector1 use: velbusconnector.Connect(“Host1”,3788);
Disconnect() - Speaks for itself
SendPacket(Packet as Velbus.Packet) - Feed it a Velbus packet and it send it…

the PacketReceived event is raised when a packet is received over the socket connection.
So to do your packet parsing here.

I hope this gets you on your way.

thanks for the quick reply. will try it and let you know if it works.

thx,

stis

how can i get a reply from velbus via vebusconnector?

Use the PacketReceived event from the VelbusConnector, this event gets raised every time a package is received.

Something like this:

private void myVelbusConnector_PacketReceived(VelbusConnector.ReceivedPacketArgs packetInfo) { //ParsePacket(packetInfo.packet); }

does that mean that i have to use it inside VelbusConnector or can i use it in my program?

It’s an event raised by the velbusconnector so you have to use it in your main program. Just select the velbusconnector in design view and look for the event in properties. Dubbelclick it and it will create the procedure for you in code view.

To Cantryn,

is this the good way to use it?

    private void velbusConnector1_PacketReceived(VelbusConnector.ReceivedPacketArgs packetInfo)
    {
        //ParsePacket(packetInfo.packet);

        WriteLog(String.Format("Packet receivedfrom address {0,0}", packetInfo.packet.Address));
        if (packetInfo.packet.Address == 0x32 && packetInfo.packet.Command == 0xFB)// && packetInfo.packet[1] == 2 && packetInfo.packet[3] == 2)
        {
            if (Keukentafel.InvokeRequired)
                Keukentafel.BeginInvoke(new MethodInvoker(delegate() { Keukentafel.BackColor = Color.Green; }));
            else
                Keukentafel.BackColor = Color.Green;
        }
        if (packetInfo.packet.Address == 0x32 && packetInfo.packet.Command == 0xFB && packetInfo.packet[1] == 2 && packetInfo.packet[3] == 0)
        {
            WriteLog(String.Format("Packet received from address {0,0}", packetInfo.packet.Address, packetInfo.packet.Command));
            if (Keukentafel.InvokeRequired)
                Keukentafel.BeginInvoke(new MethodInvoker(delegate() { Keukentafel.BackColor = Color.Red; }));
            else
                Keukentafel.BackColor = Color.Red;
        }
    }

That looks good at first glance. Does it work?
You can use the [code] tag to make your code more readable on the forum by the way :wink: