2022.01.21 | along | 32821次围观
第一次接触BACnet ip,开发语言使用java,网上搜了下,都是推荐使用BACnet4j,但是找不到完整的demo,折腾了一段时间,勉强跑通了自己写的demo,读取到的设备模拟器上的数据。
1.下载Yabe设备模拟器
链接: https://pan.baidu.com/s/1OK1uq-tfU-XoOH10h4Otag 提取码: 4eu4
安装后
打开太阳的图标
这是设备模拟器,Yabe还提供了一个客户端,打开放大镜图标
可以在client端看到相关的数据信息。
2.下载BACnet4j
https://github.com/infiniteautomation/BACnet4J
下载需要的版本,本博文的代码使用的是5.0.2版本,然后本地安装 mvn install -Dmaven.test.skip=true
3.写demo代码
创建maven项目,pom.xml引入BACnet4j(上面下载安装的),比如
<dependencies> <!-- https://github.com/infiniteautomation/BACnet4J --> <dependency> <groupId>com.infiniteautomation</groupId> <artifactId>bacnet4j</artifactId> <version>5.0.2</version> </dependency> </dependencies>
package utils.bacnet; import java.util.Arrays; import java.util.List; import com.serotonin.bacnet4j.LocalDevice; import com.serotonin.bacnet4j.RemoteDevice; import com.serotonin.bacnet4j.npdu.ip.IpNetwork; import com.serotonin.bacnet4j.npdu.ip.IpNetworkBuilder; import com.serotonin.bacnet4j.transport.DefaultTransport; import com.serotonin.bacnet4j.type.Encodable; import com.serotonin.bacnet4j.type.enumerated.ObjectType; import com.serotonin.bacnet4j.type.enumerated.PropertyIdentifier; import com.serotonin.bacnet4j.type.primitive.ObjectIdentifier; import com.serotonin.bacnet4j.type.primitive.UnsignedInteger; import com.serotonin.bacnet4j.util.PropertyValues; import com.serotonin.bacnet4j.util.ReadListener; import com.serotonin.bacnet4j.util.RequestUtils; /** * 启动Yabe的天气模拟 * @author Jfei * */ public class ReadTest1 { /** * 同一网段读取 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { LocalDevice d = null; try { //创建网络对象 IpNetwork ipNetwork = new IpNetworkBuilder() .withLocalBindAddress("192.168.1.245")//本机的ip .withSubnet("255.255.255.0", 24) .withPort(47808) //Yabe默认的UDP端口 .withReuseAddress(true) .build(); //创建虚拟的本地设备,deviceNumber随意 d = new LocalDevice(123, new DefaultTransport(ipNetwork)); d.initialize(); d.startRemoteDeviceDiscovery(); RemoteDevice rd = d.getRemoteDeviceBlocking(2457706);//获取远程设备,instanceNumber 是设备的device id System.out.println("modelName=" + rd.getDeviceProperty( PropertyIdentifier.modelName)); System.out.println("analogInput2= " +RequestUtils.readProperty(d, rd, new ObjectIdentifier(ObjectType.analogInput, 2), PropertyIdentifier.presentValue, null)); List<ObjectIdentifier> objectList = RequestUtils.getObjectList(d, rd).getValues(); //打印所有的Object 名称 for(ObjectIdentifier o : objectList){ System.out.println(o); } ObjectIdentifier oid = new ObjectIdentifier(ObjectType.analogInput, 0); ObjectIdentifier oid1 = new ObjectIdentifier(ObjectType.analogInput, 1); ObjectIdentifier oid2 = new ObjectIdentifier(ObjectType.analogInput, 2); //获取指定的presentValue PropertyValues pvs = RequestUtils.readOidPresentValues(d, rd,Arrays.asList(oid,oid1,oid2), new ReadListener(){ @Override public boolean progress(double progress, int deviceId, ObjectIdentifier oid, PropertyIdentifier pid, UnsignedInteger pin, Encodable value) { System.out.println("========"); System.out.println("progress=" + progress); System.out.println("deviceId=" + deviceId); System.out.println("oid="+oid.toString()); System.out.println("pid="+pid.toString()); System.out.println("UnsignedInteger="+pin); System.out.println("value="+value.toString() + " getClass =" +value.getClass()); return false; } }); Thread.sleep(3000); System.out.println("analogInput:0 == " + pvs.get(oid, PropertyIdentifier.presentValue)); //获取指定的presentValue PropertyValues pvs2 = RequestUtils.readOidPresentValues(d, rd,Arrays.asList(oid,oid1,oid2),null); System.out.println("analogInput:1 == " + pvs2.get(oid1, PropertyIdentifier.presentValue)); d.terminate(); } catch (Exception e) { e.printStackTrace(); if(d != null){ d.terminate(); } } } }
package utils.bacnet; import java.util.Arrays; import java.util.List; import com.serotonin.bacnet4j.LocalDevice; import com.serotonin.bacnet4j.RemoteDevice; import com.serotonin.bacnet4j.npdu.ip.IpNetwork; import com.serotonin.bacnet4j.npdu.ip.IpNetworkBuilder; import com.serotonin.bacnet4j.npdu.ip.IpNetworkUtils; import com.serotonin.bacnet4j.service.acknowledgement.ReadPropertyAck; import com.serotonin.bacnet4j.service.confirmed.ReadPropertyRequest; import com.serotonin.bacnet4j.transport.DefaultTransport; import com.serotonin.bacnet4j.type.Encodable; import com.serotonin.bacnet4j.type.constructed.Address; import com.serotonin.bacnet4j.type.enumerated.ObjectType; import com.serotonin.bacnet4j.type.enumerated.PropertyIdentifier; import com.serotonin.bacnet4j.type.primitive.ObjectIdentifier; import com.serotonin.bacnet4j.type.primitive.UnsignedInteger; import com.serotonin.bacnet4j.util.PropertyValues; import com.serotonin.bacnet4j.util.ReadListener; import com.serotonin.bacnet4j.util.RequestUtils; /** * 启动Yabe的天气模拟 * @author Jfei * */ public class ReadTest2 { /** * 跨网段读取 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { LocalDevice d = null; try { //创建网络对象 IpNetwork ipNetwork = new IpNetworkBuilder() .withLocalBindAddress("192.168.1.245") .withSubnet("255.255.255.0", 24) .withPort(47808) .withReuseAddress(true) .build(); d = new LocalDevice(201, new DefaultTransport(ipNetwork)); d.initialize(); Address address = new Address(IpNetworkUtils.toOctetString("111.111.51.71:77")); ObjectIdentifier AI = new ObjectIdentifier(ObjectType.analogValue, 3); ReadPropertyAck ack = (ReadPropertyAck) d.send(address, new ReadPropertyRequest(AI, PropertyIdentifier.presentValue, null)).get(); //System.out.println(ack.getValue()); d.terminate(); d.startRemoteDeviceDiscovery(); RemoteDevice rd = d.getRemoteDeviceBlocking(2457706);//获取远程设备,instanceNumber 是设备的device id System.out.println("modelName=" + rd.getDeviceProperty( PropertyIdentifier.modelName)); System.out.println("analogInput2= " +RequestUtils.readProperty(d, rd, new ObjectIdentifier(ObjectType.analogInput, 2), PropertyIdentifier.presentValue, null)); List<ObjectIdentifier> objectList = RequestUtils.getObjectList(d, rd).getValues(); //打印所有的Object 名称 for(ObjectIdentifier o : objectList){ System.out.println(o); } ObjectIdentifier oid = new ObjectIdentifier(ObjectType.analogInput, 0); ObjectIdentifier oid1 = new ObjectIdentifier(ObjectType.analogInput, 1); ObjectIdentifier oid2 = new ObjectIdentifier(ObjectType.analogInput, 2); //获取指定的presentValue PropertyValues pvs = RequestUtils.readOidPresentValues(d, rd,Arrays.asList(oid,oid1,oid2), new ReadListener(){ @Override public boolean progress(double progress, int deviceId, ObjectIdentifier oid, PropertyIdentifier pid, UnsignedInteger pin, Encodable value) { System.out.println("========"); System.out.println("progress=" + progress); System.out.println("deviceId=" + deviceId); System.out.println("oid="+oid.toString()); System.out.println("pid="+pid.toString()); System.out.println("UnsignedInteger="+pin); System.out.println("value="+value.toString() + " getClass =" +value.getClass()); return false; } }); Thread.sleep(3000); System.out.println("analogInput:0 == " + pvs.get(oid, PropertyIdentifier.presentValue)); //获取指定的presentValue PropertyValues pvs2 = RequestUtils.readOidPresentValues(d, rd,Arrays.asList(oid,oid1,oid2),null); System.out.println("analogInput:1 == " + pvs2.get(oid1, PropertyIdentifier.presentValue)); d.terminate(); } catch (Exception e) { e.printStackTrace(); if(d != null){ d.terminate(); } } } }
package utils.bacnet; import com.serotonin.bacnet4j.LocalDevice; import com.serotonin.bacnet4j.RemoteDevice; import com.serotonin.bacnet4j.npdu.ip.IpNetwork; import com.serotonin.bacnet4j.npdu.ip.IpNetworkBuilder; import com.serotonin.bacnet4j.transport.DefaultTransport; import com.serotonin.bacnet4j.type.enumerated.ObjectType; import com.serotonin.bacnet4j.type.enumerated.PropertyIdentifier; import com.serotonin.bacnet4j.type.primitive.Boolean; import com.serotonin.bacnet4j.type.primitive.ObjectIdentifier; import com.serotonin.bacnet4j.type.primitive.Real; import com.serotonin.bacnet4j.util.RequestUtils; /** * 写入修改数据 * @author nbuu * */ public class WriteTest1 { public static void main(String[] args) throws Exception { LocalDevice d = null; try { IpNetwork ipNetwork = new IpNetworkBuilder() .withLocalBindAddress("192.168.1.114") .withSubnet("255.255.255.0", 24) .withPort(47808) .withReuseAddress(true) .build(); d = new LocalDevice(123, new DefaultTransport(ipNetwork)); d.initialize(); d.startRemoteDeviceDiscovery(); RemoteDevice rd = d.getRemoteDevice(3).get();//获取远程设备 //必须先修改out of service为true RequestUtils.writeProperty(d, rd, new ObjectIdentifier(ObjectType.analogValue, 0),PropertyIdentifier.outOfService, Boolean.TRUE); Thread.sleep(1000); //修改属性值 RequestUtils.writePresentValue(d, rd, new ObjectIdentifier(ObjectType.analogValue, 0), new Real(77)); Thread.sleep(2000); System.out.println("analogValue0= " +RequestUtils.readProperty(d, rd, new ObjectIdentifier(ObjectType.analogValue, 0), PropertyIdentifier.presentValue, null)); Thread.sleep(1000); d.terminate(); } catch (Exception e) { e.printStackTrace(); if(d != null){ d.terminate(); } } } }
参考
https://blog.csdn.net/dream_broken/article/details/106646604
https://blog.csdn.net/dream_broken/article/details/106659096
版权声明
本文仅代表作者观点,不代表xx立场。
本文系作者授权xxx发表,未经许可,不得转载。
发表评论