Code-House / bacnet4j-wrapper

Simple facade for bacnet4j api.
GNU General Public License v3.0
48 stars 20 forks source link

Example code showing how to connect to a Bacnet Device and get/set values ? #9

Closed shaileshsathe closed 4 years ago

shaileshsathe commented 5 years ago

Hi @splatch , Can you provide any sample code/example to show how to connect to a Bacnet Device and getting/setting values of it. I am newbie to Bacnet. I am implementing it in my project. Need your help about the same. Thanks in advance.

splatch commented 5 years ago

There is short example of code in README.md. Two methods which you can use is getPropertyValue and setPropertyValue. Currently initialization code can be found in NetworkProgram.java. Wrapper itself is blocked due to device discovery, but once its done you can operate on their names and not specific (technical) identifiers:

BacNetClient client = new BacNetIpClient("<bind ip>", "<broadcast ip>", <client device id>);
client.start();
Set<Device> devices = client.discoverDevices(500); // given number is timeout in millis
for (Device device : devices) {
    System.out.println(device);

    for (Property property : client.getDeviceProperties(device)) {
        System.out.println(property.getName() + " " + client.getPropertyValue(property));

    }
}

client.stop();

Let me know if this helps. Best, Łukasz

shaileshsathe commented 5 years ago

Thanks @splatch for your quick reply. I have used the above short example you have provided but it gives me compile time error for client.getPropertyValue(property) as following

"method getPropertyValue in interface BacNetClient cannot be applied to given types; required: Property,BacNetToJavaConverter found: Property reason: cannot infer type-variable(s) T (actual and formal argument lists differ in length) where T is a type-variable: T extends Object declared in method getPropertyValue(Property,BacNetToJavaConverter)"

And how to use the setPropertyValue() method having following signature: Public void setPropertyValue(Property prprt, T t, JavaToBacNetConverter jtbnc)

I have imported assembly-1.1.0.jar from your 1.1x release in my project. Let me know if I am doing anything wrong. NetworkProgram.java link is not accessible. It says "Not Found". It will be great, If you can provide me the code that can get and set values for any bacnet device. Thanks in advance

shaileshsathe commented 5 years ago

Hi @splatch, Any help about above query will be really appreciated.

Thanks

splatch commented 5 years ago

@shaileshsathe Sorry for missing it out, indeed README is a bit outdated. The getPropertyValue signature has been changed and now requires the second argument to handle property value, try this: client.getPropertyValue(property, new BypassBacnetConverter()); Above will just return you a device answer with no conversion at all.

Converter comes from org.code_house.bacnet4j.wrapper.api.BypassBacnetConverter.

shaileshsathe commented 5 years ago

Thanks @splatch for your reply. I have used client.getPropertyValue(property, new BypassBacnetConverter());.Now its not giving any compile time error but after running the code it gives Exception at this line : for (Property property : client.getDeviceProperties(device)) { Following is the Exception occurred:

Exception in thread "main" org.code_house.bacnet4j.wrapper.api.BacNetClientException: Unable to get device properties
    at org.code_house.bacnet4j.wrapper.ip.BacNetIpClient.getDeviceProperties(BacNetIpClient.java:169)
    at bacnetjavatest.BacnetJavaTest.main(BacnetJavaTest.java:38)
Caused by: com.serotonin.bacnet4j.exception.AbortAPDUException: Abort(server=true, originalInvokeId=3, abortReason=segmentation-not-supported)
    at com.serotonin.bacnet4j.transport.ServiceFutureImpl.result(ServiceFutureImpl.java:87)
    at com.serotonin.bacnet4j.transport.ServiceFutureImpl.get(ServiceFutureImpl.java:64)
    at org.code_house.bacnet4j.wrapper.ip.BacNetIpClient.getDeviceProperties(BacNetIpClient.java:153)
    ... 1 more

It says the abort reason as segmentation-not-supported. Can you help me to remove this error ? Thanks in advance.

splatch commented 5 years ago

This error indicates that other device can't send response within single ethernet packet. Usually it is 1500 bytes . Value comes from network MTU setting. You can try to read properties one by one by constructing them based on device catalog card. Main difference is constructing properties manually instead of getting them from device. Check a Property constructor for that. This is how this library is used in openhab binding: https://github.com/openhab/org.openhab.binding.bacnet/blob/master/src/main/java/org/openhab/binding/bacnet/internal/BacNetBinding.java#L253

Cheers, Łukasz

splatch commented 4 years ago

I'm closing this issue as there was no message back, please do reopen if you still need a support for your case.