jvelbus Wiki
Status: Beta
Brought to you by:
kdw0001
The code fragment below shows the most basic steps to be taken to start using Velbus from your Java code.
public class JVelbusTest { private SerialBus bus; private boolean running; public JVelbusTest(){ //Listening for Velbus responses is done is a separate thread. running = true; Worker worker = new Worker(); Thread thread = new Thread(worker); thread.start(); //Start scanning the Velbus. performScan(); } public void performScan(){ try{ //Create a packet containing the command to send to Velbus. //These commands can be found in the module specification. //The command below performs a bus scan. Packet packet = new Packet(); packet.setPacketPrioriy(Packet.LOW_PRIORITY); packet.setRtr(true); for(short i = 0x01 ; i <= 0xFF ; i++){ packet.setAddress(i); bus.send(packet); } System.out.println("Scan completed."); }catch(ArgumentOutOfRangeException ex){ ex.printStackTrace(); } } public static void main(String[] args){ new JVelbusTest(); } private class Worker implements Runnable, PacketSentListener, PacketReceivedListener{ @Override public void packetReceivedEvent(PacketReceivedEvent event) { Packet packet = event.getPacket(); System.out.println("R=" + packet.toString()); } @Override public void packetSentEvent(PacketSentEvent event) { Packet packet = event.getPacket(); System.out.println("S=" + packet.toString()); } @Override public void run() { //Initiate communication to Velbus using the specified COM-port bus = new SerialBus("COM16"); //Register event listeners for both packet received and //packet send events (from the application point of view). bus.addPacketReceivedListener(this); bus.addPacketSentListener(this); //Start communication with the bus. bus.open(); //We are just waiting for the bus to reply. while(running){ try{ Thread.sleep(500); }catch(InterruptedException ex){} } //Unregister registered listeners. bus.removePacketReceivedListener(this); bus.removePacketSentListener(this); //IMPORTANT : always shutdown bus communication. //Not doing so results in a port in use exception during the //bus initialization!! bus.close(); } } }
Please take in mind JVelbus only provides access at the lowest level to the Velbus. No methods are being provided to send, e.g., a switch on output X command on address XX.
The COM-port modifier used in the example is operating system dependent. The example above specifies a Microsoft Windows port identifier.