上一篇博客中https://blog.csdn.net/dream_broken/article/details/106646604,代码和设备模拟器Yabe是同一台电脑上,现在试试代码和设备模拟器不在同一台电脑上,但是在同网段内,因为bacnet ip是通过udp在同网段内进行广播的。现在进行测试下。

Yabe运行在192.168.0.200上,代码在192.168.0.123上。测试环境window,开发工具eclipse.

注意:必须同网段,必须同网段,必须同网段。不明白为什么同网段要求的,自行百度了解UDP协议

代码

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.0.123")//本机的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();}}}
}

执行结果就不贴了,能正常读取到192.168.0.200上Yabe的数据。更改设备端数据也是一样可以的,就不贴代码了,参考上一篇博文。

如果代码无法读取到数据,检查:

1.服务器端(Yabe模拟器)和客户端(代码所在的电脑),IP是否属于同一个网段

2.检查代码的withLocalBindAddress("192.168.1.150")(代码所在电脑的ip)和withSubnet("255.255.255.0", 24)是否填写正确。IP和subnet可以通过java代码获取(代码在最后贴出供参考)

3.服务端。netstat -aon|findstr "47808",看是否通过端口47808进行UDP广播了。

4.检查客户端47808是否有被占用的情况,netstat -aon|findstr "47808"

5.客户端电脑上启动Yabe的客户端,检查47808是不是只有一个服务器设备

6.执行代码时,如果客户端电脑上有运行Yabe客户端,为了避免干扰,则关闭Yabe客户端。然后运行代码

java获取IP,subnet的范例

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代码例子属性读写同网段跨主机02相关推荐

  1. BACnet/IP之BACnet4j学习java代码例子属性读写01

    第一次接触BACnet ip,开发语言使用java,网上搜了下,都是推荐使用BACnet4j,但是找不到完整的demo,折腾了一段时间,勉强跑通了自己写的demo,读取到的设备模拟器上的数据. 1.下 ...

  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. Bacnet IP协议和Java实现

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

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

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

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

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

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

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

最新文章

  1. request和response一览
  2. 短视频Gif快手-有点意思 | 手摸手产品研究院
  3. python项目策划书_跟着销售学python系列(1)--实践项目骨架(1)
  4. bzoj1110: [POI2007]砝码Odw
  5. A Common Framework for Interactive Texture Transfer(CVPR 2018)学习笔记
  6. linear在HTML的作用,CSS3里的linear-gradient()函数
  7. Linux下动态库的创建与更新
  8. [PHP] - Laravel - CSRF token禁用方法与排除验证csrf_token的url设置
  9. Google 浏览器(2011)书签同步
  10. NYOJ题目1170-最大的数
  11. 实验室计算机主机关,计算机综合实验室管理办法
  12. Pandas中DataFrame基本函数整理(全)
  13. laravel 微信授权登录
  14. 提高spark任务稳定性的解决方案及Blacklist 机制说明解释
  15. Windows 钩子,基本的dll注入
  16. 数字政务是推动公共服务和社会治理精细化、智能化的重要载体
  17. python图片直接保存到远端_Python在远程服务器中的实现挂代码-发送定期天气预报到邮箱+每天一句话(小白教程),远端,定时,至,每日...
  18. java poi 设置时间空间_java - POI - 如何将单元格值设置为Date并应用默认的Excel日期格式?...
  19. H5 IOS input光标错位的问题
  20. Hyperledger Fabric 1.2.1启用CouchDB作为状态数据库

热门文章

  1. mysql 1032 update_解决mysql 1032 主从错误
  2. S5PV210系列 (裸机十五)之 iNand
  3. realme Q2Pro和红米x10哪个好
  4. 盖高辛氏衰,天下归之
  5. Oracle 12c统一审计
  6. 2022-10-17 环境映射
  7. 如何搭建职业教育智慧课堂?
  8. 等保2.0的自动代码审计及开源治理解决方案
  9. 如何用Python进行数据分析
  10. 上岸快手,我选择一条不一样的路