Skip to content

chipkin/BACnetClientExampleJava

Repository files navigation

BACnetClientExampleJava

A beginner-focused BACnet/IP client tutorial in Java using the CAS BACnet Stack v4.5.6.2626

This example shows how to:

  • Send Who-Is
  • Send ReadProperty
  • Send SubscribeCOV
  • Decode all sent and received BACnet packets as XML using BACnetStack_DecodeAsXML
  • Parse XML to print beginner-friendly summaries (I-Am and property values)

This project intentionally does not use stack Hook APIs.

How This Example Is Structured

  • CASBACnetStackAdapter.java
    • Loads the native CAS BACnet library.
  • ICASBACnetStackLibrary.java
    • JNA function signatures and callback signatures used by this tutorial.
  • SimpleBACnetClientExample.java
    • Main tutorial client flow, UDP networking, callbacks, command loop.
  • XmlMessageParser.java
    • Parses decoded XML and prints summarized BACnet events.
  • BACnetConstants.java
    • Minimal constants used by this tutorial.

Prerequisites

  • Java 8+
  • Maven
  • CAS BACnet Stack native library available on PATH or java.library.path
    • Example on Windows x64: CASBACnetStack_x64_Release.dll

Build

mvn -q -DskipTests compile

Run

mvn exec:java

Optional args:

mvn exec:java -Dexec.args="<targetIp> <targetDeviceInstance> <readObjectType> <readObjectInstance> <readPropertyId>"

Example:

mvn exec:java -Dexec.args="192.168.2.217 389999 2 2 85"

Commands

Type a command key in the console (most terminals require Enter):

  • W Send Who-Is
  • R Send ReadProperty
  • S Send SubscribeCOV
  • H Show help
  • Q Quit

What New Users Should Notice

  • The stack does BACnet encoding/decoding and state handling.
  • Your application provides network transport through callbacks:
    • ReceiveMessage: read UDP data from the network and pass bytes to stack.
    • SendMessage: stack gives encoded BACnet bytes, your app sends them via UDP.
    • GetSystemTime: stack asks your app for current time.
  • BACnet/IP connection strings are 6 bytes:
    • 4 bytes IPv4 address + 2 bytes UDP port.
  • Who-Is is unconfirmed discovery.
  • ReadProperty and SubscribeCOV are confirmed requests and normally produce ACK/error flows.
  • XML decode is used here for visibility and troubleshooting.

Expected Output Pattern

  • Outbound packet log:
    • [TX] 192.168.x.x:47808
    • <BACnetPacket ...>...</BACnetPacket>
  • Inbound packet log:
    • [RX] 192.168.x.x:47808
    • <BACnetPacket ...>...</BACnetPacket>
  • Parsed summaries:
    • I-Am discovered: deviceId=..., connectionString=...
    • Property value: <object>:<instance>.<property> = <value>

Notes

  • This tutorial keeps the API surface small to make first-time BACnet + CAS stack learning easier.
  • If your XML shape differs between stack versions, raw XML still prints, and parser fallbacks try to extract key fields safely.

Example output

CAS BACnet Stack Client Example (Java) v1.0.0
Stack version: 4.5.6.2626
UDP local bind: 0:0:0:0:0:0:0:0:47808
Target connection string: 192.168.2.217:47808
Target device instance (for tutorial context): 389999
ReadProperty default: object=2:2, property=85

Commands:
  W - Send Who-Is
  R - Send ReadProperty
  S - Send SubscribeCOV
  H - Show help
  Q - Quit
Input tip: type a command key and press Enter.

w

Command W: sending Who-Is (unconfirmed discovery).
  Destination: broadcast via 192.168.2.217:47808 (actual UDP destination 255.255.255.255:47808)
  BACnet network: 0 (local network)
[TX] 255.255.255.255:47808
<!-- CAS BACnet Stack v4.5.6.2626 --><BACnetPacket networkType='IP'><BVLL function='originalBroadcastNPDU' /><NPDU control='0x00' version='1' /><UnconfirmedRequestPDU serviceChoice='whoIs'><WhoIsRequest /></UnconfirmedRequestPDU></BACnetPacket>

[RX] 192.168.3.76:47808
<!-- CAS BACnet Stack v4.5.6.2626 --><BACnetPacket networkType='IP'><BVLL function='originalBroadcastNPDU' /><NPDU control='0x00' version='1' /><UnconfirmedRequestPDU serviceChoice='whoIs'><WhoIsRequest /></UnconfirmedRequestPDU></BACnetPacket>

[RX] 192.168.3.76:47808
<!-- CAS BACnet Stack v4.5.6.2626 --><BACnetPacket networkType='IP'><BVLL function='originalBroadcastNPDU' /><NPDU control='0x00' version='1' /><UnconfirmedRequestPDU serviceChoice='iAm'><IAmRequest><IAmDeviceIdentifier datatype='12' objectInstance='389002' objectType='8'>device, 389002</IAmDeviceIdentifier><MaxAPDULengthAccepted datatype='2' value='1476'>1476</MaxAPDULengthAccepted><SegmentationSupported datatype='9' value='3'>noSegmentation</SegmentationSupported><VendorId datatype='2' value='389'>389</VendorId></IAmRequest></UnconfirmedRequestPDU></BACnetPacket>

I-Am discovered: deviceId=389002, connectionString=192.168.3.76:47808

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages