java对接bacnet ip协议(跨网段方式)

1、环境准备

复制代码
#maven环境
    <repositories>
        <repository>
            <id>ias-releases</id>
            <url>https://maven.mangoautomation.net/repository/ias-release/</url>
        </repository>
    </repositories>

<dependencies>
        <dependency>
            <groupId>com.infiniteautomation</groupId>
            <artifactId>bacnet4j</artifactId>
            <version>6.0.0</version>
        </dependency>
</dependencies>

2、实现

java 复制代码
IpNetwork ipNetwork = new IpNetworkBuilder()
                    .withLocalBindAddress("0.0.0.0") 
                    .withSubnet("255.255.252.0", 22)
                    .withPort(port) // <== 修改为你设备的端口
                    .build();
            ipNetwork.enableBBMD(); //跨网段必须开启BBMD
            //创建虚拟的本地设备,deviceNumber随意 初始化本地设备
            localDevice = new LocalDevice(123456, new DefaultTransport(ipNetwork)).withClock(new WarpClock()).initialize();
            ipNetwork.registerAsForeignDevice(new InetSocketAddress("ba设备所在地址ip", "ba设备所在地址的端口"), 10000000);
            //搜寻网段内远程设备
            localDevice.startRemoteDeviceDiscovery();

            //获取远程设备,instanceNumber是远程设备ID
            RemoteDevice remoteDevice = localDevice.getRemoteDeviceBlocking(reqVO.getDeviceId());

            //获取远程设备的标识符对象
            List<ObjectIdentifier> objectList = RequestUtils.getObjectList(localDevice, remoteDevice).getValues();

            List<ObjectIdentifier> biList = new ArrayList<>();
            List<ObjectIdentifier> aiList = new ArrayList<>();

            System.out.println("<===================对象标识符的对象类型,实例数(下标)===================>");

            //Object所有标识符 binaryInput
            for (ObjectIdentifier oi : objectList) {

//                System.out.println(oi.getObjectType().toString() + "," + oi.getInstanceNumber());

                //binary-input
                if (oi.getObjectType().equals(ObjectType.binaryInput)) {
                    biList.add(new ObjectIdentifier(ObjectType.binaryInput, oi.getInstanceNumber()));
                }
                //analog-input
                if (oi.getObjectType().equals(ObjectType.analogInput)) {
                    aiList.add(new ObjectIdentifier(ObjectType.analogInput, oi.getInstanceNumber()));
                }
            }
            System.out.println("<==================================================================>");

            System.out.println("取值开始!!!================>");

            final PropertyReferences refs = new PropertyReferences();
            if (biList != null && biList.size() > 0) {
                for (final ObjectIdentifier oid : biList) {
                    if (reqVO.getInstNums() != null && !reqVO.getInstNums().contains(oid.getInstanceNumber())) continue;
                    refs.add(oid, PropertyIdentifier.objectName);
                    refs.add(oid, PropertyIdentifier.presentValue);
                    //  refs.add(oid, PropertyIdentifier.description);
                }
                PropertyValues pvAll = RequestUtils.readProperties(localDevice, remoteDevice, refs, false, null);

                for (ObjectIdentifier oi : biList) {

                System.out.println("instanceNumber:" + oi.getInstanceNumber());
                System.out.println(oi.getObjectType() + " " + oi.getInstanceNumber() + " Name: " + pvAll.get(oi, PropertyIdentifier.objectName));
                System.out.println(oi.getObjectType() + " " + oi.getInstanceNumber() + " PresentValue: " + pvAll.get(oi, PropertyIdentifier.presentValue));
                }
            } else if (aiList != null && aiList.size() > 0) {
                for (final ObjectIdentifier oid : aiList) {
                    if (reqVO.getInstNums() != null && !reqVO.getInstNums().contains(oid.getInstanceNumber())) continue;
                    refs.add(oid, PropertyIdentifier.objectName);
                    refs.add(oid, PropertyIdentifier.presentValue);
                    //  refs.add(oid, PropertyIdentifier.description);
                }
                PropertyValues pvAll = RequestUtils.readProperties(localDevice, remoteDevice, refs, false, null);

                for (ObjectIdentifier oi : aiList) {

                System.out.println("instanceNumber:" + oi.getInstanceNumber());
                System.out.println(oi.getObjectType() + " " + oi.getInstanceNumber() + " Name: " + pvAll.get(oi, PropertyIdentifier.objectName));
                System.out.println(oi.getObjectType() + " " + oi.getInstanceNumber() + " PresentValue: " + pvAll.get(oi, PropertyIdentifier.presentValue));
                }
            }

            System.out.println("================>取值结束!!!");

3、可以是用可视化工具Bacnet Scane可视化工具

bacnet scane可视化下载地址

图中含义:搜索ip地址为172.16.33.250下的47809端口的ba设备。

相关推荐
L***d6701 小时前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
凌波粒1 小时前
Springboot基础教程(3)--自动装配原理/静态资源处理/欢迎页
java·spring boot·后端
likuolei1 小时前
XSL-FO 软件
java·开发语言·前端·数据库
凌波粒1 小时前
SpringBoot基础教程(2)--yaml/配置文件注入/数据校验/多环境配置
java·spring boot·后端·spring
S***26751 小时前
Spring Boot环境配置
java·spring boot·后端
6***83051 小时前
什么是Spring Boot 应用开发?
java·spring boot·后端
毕设源码柳学姐1 小时前
计算机毕设 java 智慧社区服务系统 SSM 框架社区生活平台 Java 开发的便民服务与互动系统
java·开发语言·生活
U***l8321 小时前
【postgresql】分区表管理
java·数据库·postgresql
倚肆1 小时前
MyBatis-Plus Mapper 接口方法详解
java·mybatis
n***78681 小时前
SpringBoot详解
java·spring boot·后端