关于BACnet

BACnet 是一个为嵌入式系统实现的 BACnet (楼宇自动化和控制网络)专门协议。 BACnet 本身就是一个标准的路由协议设计用于处理通信自动化的建筑,如空调系统和其他暖通设备。

Android如何使用BACnet

使用bacnet4j这个库bacnet4j
使用Yabe这个模拟器进行测试

BACnet读取测试

public class ReadTest01 {public static void main(String[] args) throws Exception {String localAdd = "192.168.31.91"; //PC
//        int devId = 82155;int devId = 2146306;int myId = 99;if (args != null && args.length == 1) {localAdd = args[0];myId = 28;}LocalDevice d = null;try {//创建网络对象System.out.println("add="+localAdd);IpNetwork ipNetwork = new IpNetworkBuilder().withLocalBindAddress(localAdd)//本机的ip.withSubnet("255.255.255.0", 24)  //掩码和长度,如果不知道本机的掩码和长度的话,可以使用后面代码的工具类获取.withPort(47808) //Yabe默认的UDP端口.withReuseAddress(true).build();//创建虚拟的本地设备,deviceNumber随意d = new LocalDevice(myId, new DefaultTransport(ipNetwork));d.initialize();d.startRemoteDeviceDiscovery();//            d.getEventHandler().addListener();//             d.getRemoteDevices(); //查询所有设备RemoteDevice rd = d.getRemoteDeviceBlocking(devId);//获取远程设备,instanceNumber 是设备的device idSystem.out.println("modelName=" + rd.getDeviceProperty( PropertyIdentifier.modelName));System.out.println("analogInput2= " +RequestUtils.readProperty(d, rd, new ObjectIdentifier(ObjectType.analogInput, 0), 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();}}}}

BACnet写入代码测试

public class WriteTest01 {static final Logger LOG = Logger.getLogger(WriteTest01.class);public static void main(String[] args) throws Exception {LocalDevice d = null;try {IpNetwork ipNetwork = new IpNetworkBuilder().withLocalBindAddress("192.168.31.91").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(2146306).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(80));Thread.sleep(2000);RequestUtils.writeProperty(d, rd, new ObjectIdentifier(ObjectType.characterstringValue, 3), PropertyIdentifier.outOfService, Boolean.TRUE);Thread.sleep(1000);RequestUtils.writePresentValue(d, rd, new ObjectIdentifier(ObjectType.characterstringValue, 3), new Real(33333));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();}}}}

BACnet安卓使用封装

1.创建单例类来保存BACnet连接和读写操作

如果连接不存在则会创建新的连接

public class BACnetManager {private BACnetCommunication baCnetCommunication;private final static List<BACnetCommunication> communicationObjects = new LinkedList<BACnetCommunication>();public static BACnetManager getInstance() {return SingletonHolder.INSTANCE;}private static class SingletonHolder {private static final BACnetManager INSTANCE = new BACnetManager();private SingletonHolder() {}}private BACnetManager(){}public void init(int devId) {baCnetCommunication = getBACnetCommunication(devId);}private BACnetCommunication getBACnetCommunication(int devId) {Iterator<BACnetCommunication> iterator = communicationObjects.iterator();BACnetCommunication baCnetCommunication;while (iterator.hasNext()) {baCnetCommunication = iterator.next();if (baCnetCommunication.getDevId() == devId) {if (baCnetCommunication.isInitSuccess()) {return baCnetCommunication;} else {iterator.remove();break;}}}baCnetCommunication = new BACnetCommunication(devId, 47808);communicationObjects.add(baCnetCommunication);return baCnetCommunication;}public List<ObjectIdentifier> getObjectList(int remoteDeviceId){return baCnetCommunication.getObjectList(remoteDeviceId);}public void read(int remoteDeviceId, ObjectIdentifier objectIdentifier, PropertyIdentifier propertyIdentifier,BACnetDataListener baCnetDataListener) {BACnetDataObject baCnetDataObject = new BACnetDataObject(remoteDeviceId,objectIdentifier,propertyIdentifier,true,baCnetDataListener);baCnetCommunication.readBACnet(baCnetDataObject);}public void write(int remoteDeviceId,ObjectIdentifier objectIdentifier,Encodable present_value) {BACnetDataObject baCnetDataObject = new BACnetDataObject(remoteDeviceId,objectIdentifier,false,present_value);baCnetCommunication.writeBACnet(baCnetDataObject);}public void destroy() {baCnetCommunication.destroy();}
}

2.创建通信代理类

public class BACnetCommunication implements Observer {private final BACnetConnection baCnetConnection;private final Container actionContainer;private final Container resultContainer;private final Container errorContainer;private final int devId;private final int port;private boolean isInitSuccess = false;public BACnetCommunication(int devId, int port) {this.actionContainer = new Container();this.resultContainer = new Container();this.errorContainer = new Container();this.devId = devId;this.port = port;this.baCnetConnection = new BACnetConnection(actionContainer,resultContainer,errorContainer,devId,port);baCnetConnection.addObserver(this);Thread thread = new Thread(baCnetConnection);thread.start();}public int getDevId() {return devId;}public int getPort() {return port;}public boolean isInitSuccess() {return isInitSuccess;}public void destroy(){baCnetConnection.closeDevice();}public void readBACnet(BACnetDataObject baCnetDataObject){actionContainer.push(baCnetDataObject);}public void writeBACnet(BACnetDataObject baCnetDataObject){actionContainer.push(baCnetDataObject);}public List<ObjectIdentifier> getObjectList(int remoteDeviceId){return baCnetConnection.getObjectList(remoteDeviceId);}@Overridepublic void update(Observable observable, Object data) {if (data instanceof Boolean) {boolean terminated = ((Boolean) data).booleanValue();this.isInitSuccess = terminated;}}
}

3.创建连接类来处理BACnet连接读写操作逻辑

actionContainer, resultContainer, errorContainer是三个队列分别保存读写操作,又返回的读写操作,错误操作,BACnet连接错误时会重试5次

public class BACnetConnection extends Observable implements Runnable{private static final String TAG = "BACnetConnection";private static final int RETRY_COUNT = 5;//重连次数private final Container actionContainer, resultContainer, errorContainer;private final int devId;private final int port;private LocalDevice localDevice;private boolean isInitSuccess;public BACnetConnection(Container actionContainer, Container resultContainer, Container errorContainer, int devId, int port) {this.actionContainer = actionContainer;this.resultContainer = resultContainer;this.errorContainer = errorContainer;this.devId = devId;this.port = port;}@Overridepublic void run() {BACnetDataObject object;int errorCount = 1;while (errorCount < RETRY_COUNT) {  //if (!isInitSuccess) {initBACnet();if (isInitSuccess) {errorCount = 1;} else {errorCount++;}} else {object = actionContainer.pop();if (!object.isUnProcessable()) {RemoteDevice remoteDevice = findDevice(object.getRemoteDeviceId());if (!object.isRead()) {try {RequestUtils.writePresentValue(localDevice, remoteDevice, object.getObjectIdentifier(), object.getValue());resultContainer.push(object);} catch (Exception e) {e.printStackTrace();object.increaseErrors();actionContainer.push(object);closeDevice();}} else {try {Encodable presentValue = RequestUtils.readProperty(localDevice, remoteDevice, object.getObjectIdentifier(), object.getPropertyIdentifier(), null);object.setValue(presentValue);notifySuccess(object);} catch (Exception e) {e.printStackTrace();object.increaseErrors();notifyFail(object,e.getMessage());actionContainer.push(object);closeDevice();}}} else {errorContainer.push(object);}}}setInitSuccess(false);}private RemoteDevice findDevice(int id) {if (localDevice != null) {try {RemoteDevice rd = localDevice.getRemoteDeviceBlocking(id);if (rd != null) {return rd;}} catch (Exception e) {e.printStackTrace();}}return null;}private void initBACnet(){System.out.println("initBACnet");try {IpNetwork ipNetwork = new IpNetworkBuilder().withSubnet("255.255.255.0", 24).withPort(port).withReuseAddress(true).build();localDevice = new LocalDevice(devId, new DefaultTransport(ipNetwork));localDevice.initialize();localDevice.startRemoteDeviceDiscovery();isInitSuccess = true;} catch (Exception e) {e.printStackTrace();isInitSuccess = false;if (localDevice != null) {localDevice.terminate();}}}private void setInitSuccess(boolean success) {if (success != this.isInitSuccess) {this.setChanged();}this.isInitSuccess = success;this.notifyObservers((Boolean) success);}private void notifySuccess(BACnetDataObject baCnetDataObject){if(baCnetDataObject != null && baCnetDataObject.getBaCnetDataListener() != null){baCnetDataObject.getBaCnetDataListener().onSuccess(baCnetDataObject);}}private void notifyFail(BACnetDataObject baCnetDataObject,String message){if(baCnetDataObject != null && baCnetDataObject.getBaCnetDataListener() != null){baCnetDataObject.getBaCnetDataListener().onFail(message);}}public void closeDevice(){isInitSuccess = false;if (localDevice != null) {localDevice.terminate();}localDevice = null;}public List<ObjectIdentifier> getObjectList(int remoteDeviceId){RemoteDevice dev = findDevice(remoteDeviceId);List<ObjectIdentifier> objectList = new ArrayList<>();if(localDevice == null || !localDevice.isInitialized() || dev == null){return objectList;}try {objectList = RequestUtils.getObjectList(localDevice, dev).getValues();} catch (BACnetException e) {e.printStackTrace();}return objectList;}
}

BACnet安卓使用实现

通过BACnetManager.getInstance().getObjectList(2146306);来获取所有对象2146306为设备ID

读取具体的属性值

Android使用BACnet协议进行数据读写测试相关推荐

  1. Android POI对Excel进行数据读写

    使用的是POI-3.8 HSSFWorkbook ---- 对2003版本的Excel的支持 XSSFWorkbook ---- 对2007版本以及更高版本的支持 public void upWork ...

  2. DDR3基本的读写测试,适用于verilog语言学习

    近期学习使用Verilog编写DDR3接口读写测试,在编写过程中遇到许多问题,最终功夫不负有心人,实现了DDR3数据写入和数据读取功能.同时在问题排查过程中,也学习到了很多新的东西. 现将我实现DDR ...

  3. RS232(Modbus )通讯协议工业高频读写器|读卡器CK-FR08-B01电脑端测试配置说明

    本测试采用RS232串口转USB转换器链接笔记本测试,主要测(RS232(Modbus )协议工业高频读写器CK-FR08-B01的感应速度与数据的自动上传功能. 1.准备工作(软件+硬件) 软件:M ...

  4. 【Android 高性能音频】AAudio 音频流 缓冲区 简介 ( AAudio 音频流内部缓冲区 | 缓冲区帧容量 | 缓冲区帧大小 | 音频数据读写缓冲区 )

    文章目录 I . AAudio 音频流内部缓冲区 与 音频数据读写缓冲区 概念 II . AAudio 音频流内部缓冲区 缓冲区帧容量 BufferCapacityInFrames 与 缓冲区帧大小 ...

  5. 微信小程序的 websocket 以及 微信开发者工具测试 ws 协议没有数据的 离奇解决方案 记录

    微信小程序的 websocket 在本地web能够使用ws协议去链接websocket,但是小程序不能使用. 一.WSS 协议与 WS 协议 二.业务场景记录 : 使用 ws 协议的 websocek ...

  6. 【Android 逆向】ptrace 函数 ( ptrace 函数族 | 进程附着 | 进程脱离 | 进程数据读写权限 | 进程对应的主线程寄存器读写 | 单步调试 |ptrace 函数族状态转换 )

    文章目录 一.ptrace 函数族 1.进程附着 2.进程脱离 3.进程数据读写权限 4.进程对应的主线程寄存器读写 5.单步调试 6.继续向后执行 二.ptrace 函数族状态转换 一.ptrace ...

  7. matlab画扇区,NFCDemo NFC读写测试 ,自动读取每个扇区 块的值 matlab 238万源代码下载- www.pudn.com...

    文件名称: NFCDemo下载 收藏√  [ 5  4  3  2  1 ] 开发工具: Java 文件大小: 66 KB 上传时间: 2013-12-13 下载次数: 24 提 供 者: wuze ...

  8. A40I工控主板(SBC-X40I)USB接口读写测试

    SBC-X40I产品特性 采用Allwinner公司Cortex-A7四核A40i处理器,运行最高速度为1.2GHZ: 支持Mali-400MP2 GPU,支持OpenGL ES 2.0 / Open ...

  9. BACnet协议读取与发送

    BACnet协议读取与发送 注意 我的提问: 更新 开发环境 BACnet相关基础知识 BACnet格式 BACnet代码 BACnet设备查找 BACnet设备读取 BACnet写入操作 AND其他 ...

最新文章

  1. 高一升学计算机,(有答案)2016年上学期高一年级对口升学第一次月考计算机应用试题资料讲解(9页)-原创力文档...
  2. Java实战equals()与hashCode()
  3. 牛客2018校招 1. 拼多多 大整数相乘
  4. LeetCode-剑指 Offer 27. 二叉树的镜像
  5. Node中同步与异步的方式读取文件
  6. excel分段排序_EXCEL基础篇第六章(日期和时间的使用方法)
  7. php 文件保存函数,PHP文件函数
  8. 《管理系统中计算机应用》上机题,《管理系统中计算机应用》上机试题
  9. Mblog 开源Java多人博客系统
  10. 《黄聪:手机移动站SEO优化教程》4、如何实现手机移动网站和PC站点的自主适配...
  11. 如何将CAD图纸在线转换成JPG图片格式
  12. 小米max刷鸿蒙,用了小米Max2,这简直是浪费我一天一夜的时间!
  13. YDUI Touch +mescroll上拉加载测试
  14. 写给很累的你:面对苦难,停止内耗
  15. 【数据库】三级模式两级映射详解
  16. c++学习笔记-------《c++自学通》第六章 基本类
  17. 计算机专业人才培养评价意见,谈高职计算机专业人才培养综合评价.pdf
  18. php md5 file算法原理,MD5算法原理与实现
  19. 淘淘商城---8.10
  20. 软件构造Lab6总结

热门文章

  1. 解决usb2.0UnknownDevice问题
  2. primitive type java_Java的原始类型(Primitive Type)
  3. 北理 嵩天老师 Python程序设计 课后作业易错题总结
  4. 双语播放器关键词研究
  5. JAVA正则表达式乘号_正则表达式基本语法
  6. MSP430 判断 旋转编码开关转动方向
  7. Fedora常用软件安装简介
  8. 和平精英改名服务器维护中,和平精英改名系统维护怎么回事?重新开放时间介绍...
  9. 手机平板电脑自适应_6 月国内手机市场分析报告 | 华为平板电脑将搭载八核麒麟芯片...
  10. EditText过滤表情符号