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设备。

相关推荐
ytadpole9 小时前
揭秘设计模式:命令模式-告别混乱,打造优雅可扩展的代码
java·设计模式
用户37215742613510 小时前
Java 教程:轻松实现 Excel 与 CSV 互转 (含批量转换)
java
叫我阿柒啊10 小时前
Java全栈开发实战:从基础到微服务的深度解析
java·微服务·kafka·vue3·springboot·jwt·前端开发
EasyControl移动设备管理10 小时前
更智能的零售终端设备管理:合规、安全与高效
物联网·零售·远程管理·mdm(移动设备管理)·租赁行业数字化
凯尔萨厮10 小时前
Java学习笔记三(封装)
java·笔记·学习
霸道流氓气质10 小时前
Java开发中常用CollectionUtils方式,以及Spring中CollectionUtils常用方法示例
java·spring
失散1310 小时前
分布式专题——5 大厂Redis高并发缓存架构实战与性能优化
java·redis·分布式·缓存·架构
通达的K10 小时前
Java实战项目演示代码及流的使用
java·开发语言·windows
David爱编程10 小时前
深入 Java synchronized 底层:字节码解析与 MonitorEnter 原理全揭秘
java·后端
索迪迈科技10 小时前
Protobuf 新版“调试表示为什么有链接?为什么会打码?我该怎么改代码?
java·log4j·apache