第一次接触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>

注意:1.Yabe和代码在同一台电脑少,跨网段的话暂时不知道该怎样解决。

2.运行代码的时候,必须先关闭Yabe的客户端(explorer),否则代码会提示地址被占用了

ReadTest01.java

package com.fei;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 ReadTest01 {/*** Yabe在本地电脑上启动* @param args* @throws Exception */public static void main(String[] args) throws Exception {LocalDevice d = null;try {//创建网络对象IpNetwork ipNetwork = new IpNetworkBuilder().withLocalBindAddress("192.168.1.114")//本机的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(3);//获取远程设备,instanceNumber 是设备的device idSystem.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);//获取指定的presentValuePropertyValues pvs = RequestUtils.readOidPresentValues(d, rd,Arrays.asList(oid,oid1,oid2), new ReadListener(){@Overridepublic 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));//获取指定的presentValuePropertyValues 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();}}}
}

执行结果

SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.
modelName=Room_FC_2014
analogInput2= 12.0
device 3
analog-input 0
analog-input 1
analog-input 2
analog-value 0
analog-value 1
analog-value 2
analog-value 3
characterstring-value 1
characterstring-value 2
characterstring-value 3
binary-value 0
binary-value 1
multi-state-value 0
multi-state-value 1
========
progress=0.3333333333333333
deviceId=3
oid=analog-input 0
pid=present-value
UnsignedInteger=null
value=27.8  getClass =class com.serotonin.bacnet4j.type.primitive.Real
========
progress=0.6666666666666666
deviceId=3
oid=analog-input 1
pid=present-value
UnsignedInteger=null
value=39.7  getClass =class com.serotonin.bacnet4j.type.primitive.Real
========
progress=1.0
deviceId=3
oid=analog-input 2
pid=present-value
UnsignedInteger=null
value=12.0  getClass =class com.serotonin.bacnet4j.type.primitive.Real
analogInput:0 == 27.8
analogInput:1 == 39.5

WriteTest01.java

package com.fei;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;public class WriteTest01 {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为trueRequestUtils.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();}}}
}

执行结果

SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.
analogValue0= 77.0

注意:修改的话,必须是设备端运行被修改的才行,否则会报异常,拒绝写。可以使用Yabe的客户端(expoler)进行测试确认哪个只能被修改.

注意:代码里的ip或掩码都是代码所在的电脑的ip,可以使用一下代码获取本地的ip或掩码

package com.nanhe.common.util;import java.lang.management.ManagementFactory;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Set;import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.Query;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;public class IpUtil {private static Logger LOG = LoggerFactory.getLogger(IpUtil.class);/*** 获取本机IP地址* @return* @throws SocketException*/public static String getIpAddress() throws SocketException {String ipString = null;Inet4Address inet4Address  = getInet4Address();if(inet4Address != null){/*NetworkInterface networkInterface = NetworkInterface.getByInetAddress(inet4Address);for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {ipString = address.getAddress().getHostAddress();}*/ipString = inet4Address.getHostAddress();}LOG.info("本机IP地址={}" , ipString);return ipString;}/*** 获取tomcat容器的http端口* @return* @throws MalformedObjectNameException */public static String getTomcatHttpPort() throws MalformedObjectNameException{MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"),Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));String port = objectNames.iterator().next().getKeyProperty("port");return port;}/*** 获取网络前缀长度,* 如果长度为8,则表示掩码是255.0.0.0,* 如果长度为16,则表示掩码是255.255.0.0,* 如果长度为24,则表示掩码是255.255.255.0,* @return* @throws UnknownHostException * @throws SocketException */public static int getNetworkPrefixLength() throws UnknownHostException, SocketException{int networkPrefixLength = 0;Inet4Address inet4Address  = getInet4Address();if(inet4Address != null){NetworkInterface networkInterface = NetworkInterface.getByInetAddress(inet4Address);for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {if(address.getAddress() instanceof Inet4Address){networkPrefixLength =  address.getNetworkPrefixLength();}}}LOG.info("本机网络前缀长度networkPrefixLength={}" , networkPrefixLength);return networkPrefixLength;}/*** 获取网络掩码255.0.0.0,255.0.0.0,255.0.0.0,* @return* @throws UnknownHostException * @throws SocketException */public static String getSubnet() throws UnknownHostException, SocketException{String subnet = null;int prefix = getNetworkPrefixLength();if(prefix > 0){if(prefix == 8){subnet = "255.0.0.0";}else if(prefix == 16){subnet = "255.255.0.0";}else if(prefix == 24){subnet = "255.255.255.0";}else if(prefix == 32){subnet = "255.255.255.255";}}return subnet;}private static Inet4Address getInet4Address() throws SocketException{Inet4Address inet4Address = null;Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();InetAddress ip = null;while (allNetInterfaces.hasMoreElements()) {NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();//用于排除回送接口,非虚拟网卡,未在使用中的网络接口.if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {continue;} else {Enumeration<InetAddress> addresses = netInterface.getInetAddresses();while (addresses.hasMoreElements()) {ip = addresses.nextElement();if (ip != null && ip instanceof Inet4Address) {inet4Address = (Inet4Address)ip;break;}}if(inet4Address != null){break;}}}return inet4Address;}public static void main(String[] args) throws SocketException, UnknownHostException {System.out.println(getIpAddress());System.out.println(getNetworkPrefixLength());System.out.println(getSubnet());}}

BACnet/IP之BACnet4j学习java代码例子属性读写01相关推荐

  1. BACnet/IP之BACnet4j学习java代码例子属性读写同网段跨主机02

    上一篇博客中https://blog.csdn.net/dream_broken/article/details/106646604,代码和设备模拟器Yabe是同一台电脑上,现在试试代码和设备模拟器不 ...

  2. BACnet/IP之BACnet4j学习VTS创建虚拟设备及点位测试03

    在前两篇文章中,我们使用的虚拟设备软件是Yabe,模拟天气数据,无法自定义自己的点位数据,这章就学习下使用VTS来自己创建虚拟设备,创建定义点位. 1.下载VTS 链接: https://pan.ba ...

  3. jsch连接mysql_求用jsch网络工具包通过ssh连接远程oracle数据库并发送sql操作语句(数据库在unix上)java代码例子...

    求用jsch网络工具包通过ssh连接远程oracle数据库(数据库在unix上)java代码例子:为何jsch发送:sqlplususer/pwd@service此命令,却没有结果返回啊.下面是代码: ...

  4. java中蛇的属性有哪些_学习Java类的属性

    学习Java类的属性-武汉北大青鸟 Public.private.protected显示了三种类中的属性和服务的类型,public是可以随意访问的.private是外界不能访问的(显示了数据的封装性) ...

  5. java代码例子_Java与C++两大语言比较

    Java Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征.Java语言作为静态面向对象编程 ...

  6. java css网页布局实例_java代码例子

    JAVA 类名.方法名(这里面写的是什么)能不能写个代要是类名直接调用的方法,那这个方法就是静态的(static)方法,是不用new出新对象实例就可以直接调用的方法.看下面例子: class A{ p ...

  7. JVM原理(Java代码编译和执行的整个过程+JVM内存管理及垃圾回收机制)

    转载注明出处: http://blog.csdn.net/cutesource/article/details/5904501 JVM工作原理和特点主要是指操作系统装入JVM是通过jdk中Java.e ...

  8. Bacnet IP协议和Java实现

    Bacnet IP 楼宇自控BACnet/IP协议网关用于楼宇自控系统.楼宇自动化.楼宇信息系统,暖通HAVC行业实现联网,需要需要满足BACNet协议.PLC协议.Modbus协议.OPC UA协议 ...

  9. 【BACnet/IP协议-基于Bacnet4j读采集器点位数据 (实测)】

    一.模拟BACnet/IP协议设备 下载Yabe模拟器 链接:https://pan.baidu.com/s/1lvHhQYLPEYvPn8cjZIfLUg 提取码:xccl 默认下一步安装 安装后打 ...

最新文章

  1. 盘点52个全球人工智能和机器学习重要会议
  2. c#算两个火星坐标的距离(高德or百度)
  3. 计组第一章(唐朔飞)——计算机系统概述章节总结
  4. android软件的data使用方法,实例讲解Android中SQLiteDatabase使用方法
  5. MQ保证消息的可靠性传输
  6. cks32和stm32_cks子,间谍,局部Mo子和短管
  7. Windows下如何如何将项目上传至GitHub?
  8. 2012年回忆录及2013年目标设立
  9. matlab测量液体液位,基于MATLAB三容水箱液位控制系统.doc
  10. ROS学习笔记基础1(Ubuntu16.04安装ROS和依赖包)
  11. Berkeley DB Java Edition
  12. java ssm基于springboot的设备巡检系统
  13. 安卓dj专业打碟机软件_Mac平台上的专业DJ打碟软件
  14. linux备份mysql部分表数据,mysqldump导出表的部分数据库
  15. bzoj 2876: [Noi2012]骑行川藏 拉格朗日数乘
  16. CANopen总线的协议详解
  17. 实现抖音霓虹灯效果---OpenCV-Python开发指南(55)
  18. qq申诉网站无法接到服务器,为什么我qq申诉不成功 - 卡饭网
  19. 微信小程序开发之路(十三)正式开工--设计ER图与数据库的创建
  20. [系统]制作老毛桃U盘WinPE

热门文章

  1. Quartus current license file doesn't support EP4CE6E22C6 device解决方案
  2. WebBrowser或IE物件分析網頁表格/自動登入網頁
  3. 电气制图的首选?CAD还是EPLAN?
  4. “互联网+”与我:今年有哪些新板眼
  5. java包名命名规范
  6. cuMemcpyHtoDAsync failed: invalid argument
  7. REXROTH力士乐减压阀3DR16P5-5X/100Y/00M
  8. 国信办发布"新规":规范互联网账号名称和头像乱象问题
  9. SQL入门之第二十讲——SQL日期函数介绍
  10. pip/conda/venv/virtualenv区别详解