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