Skip to content

Read values from remote devices

pichler edited this page Jun 28, 2015 · 2 revisions

Read single values

BACnet devices provide infomation using objects. Each object has a certain number of properties that can be read. To read a single property, both the object identifier and the property identifier must passed to a ReadPropertyRequest, that will be sent to the remote device afterwards. Each BACnet device at least has to provide a Device object. In bacnet4J, the device object of a remote device can be retrieved using the getObjectIdentifier() method of the remote device. The device object contains a list of all other objects provided by the device, that can be retrieved using the following command:

List<ObjectIdentifier> oids = ((SequenceOf<ObjectIdentifier>) RequestUtils.sendReadPropertyAllowNull(
                localDevice, remoteDevice, remoteDevice.getObjectIdentifier(), PropertyIdentifier.objectList)).getValues();

The command retrieves a sequence of object identifiers from the remote device. The following example shows how to read the present value of an object specified by objectIdentifier:

ConfirmedRequestService request = new ReadPropertyRequest(objectIdentifier, PropertyIdentifier.presentValue);
ReadPropertyAck result = (ReadPropertyAck) localDevice.send(remoteDevice, request);

The result object of the read property request above contains the value, which can be retrieved using the getValue() method. In the first code line, a new request is built with a specific object and property to read (in this case the present value property). Afterwards, the request is sent to a remote device. This method allows to read a single value, but BACnet also provides functionality to read multiple values at once.

Read multiple values

To read multiple values, the objects and properties to read must be collected in a PropertyReferences class instance. The instance stores all objects and related properties to read. A single static command in the RequestUtils class allows to read all properties at once afterwards. The decision to make a single request (transfering a lot of data at once) or multiple requests with smaller payloads is made by the method readProperties() internally. The following example shows how to handle the read request:

PropertyReferences references = new PropertyReferences();

for (ObjectIdentifier objectIdentifier : oids) {
	references.add(objectIdentifier, PropertyIdentifier.presentValue);
}
    	
PropertyValues values = RequestUtils.readProperties(localDevice, remoteDevice, references, null);

for (ObjectIdentifier objectIdentifier : oids) {
	System.out.println(values.getString(objectIdentifier, PropertyIdentifier.presentValue));
}

In the example above, the object identifiers are stored in a list instance named oids. In the first for-each loop, all objects are added to the PropertyReferences instance. The property to read from each object is present value in the example. Afterwards, the read request is performed returning a PropertyValues instance. The second for-each loop prints all values to System.out, just to show how to access the values.

Back to bacnet4J page