niagara框架中串口的打开方式
一直好奇niagara框架中串口是如何打开的,最近通过看源码,大致明白了,现记录如下
首先从BModbusAsyncNetwork这个类看起,在niagara help中查找没有源码,不过没关系,利用反编译看,重点代码如下
//BModbusAsyncNetwork是一个BIService
public void serviceStarted() throws Exception {
//必须看父类的实现super.serviceStarted();getNameSubscriber().subscribe(getSerialPortConfig());getSerialPortConfig().setSerialHelperParent(this);...}
public void startComm() throws Exception {if (getSerialPortConfig().getPortName().equals("none")) {configFail("No port selected for Modbus communication.");} else {try {
//还是要看父类的实现super.startComm();configOk();} catch (Exception e) {configFail(new StringBuffer("Could not enable Modbus serial communication (").append(e).append(')').toString());throw e;}}...}
//对父类抽象方法的重写
protected Comm makeComm() {return new ModbusAsyncSerialComm(this);}
回溯父类,其中BBasicNetwork比较重要(有niagara源码)
/*** Builds the communication handler (Comm).*/public void serviceStarted()throws Exception{buildComm();}
/*** Initializes the basic network with a new communication handler (Comm)* for both the transmit and receive drivers. After creating* a new Comm, this method calls initComm(Comm comm)* with the new Comm to allow subclasses to perform any initialization* (i.e. adding custom UnsolicitedMessageListeners for handling unsolicited received * messages). */private void buildComm()throws Exception{comm = makeComm();initComm(comm);}
/*** This method starts the Communication handler* (Comm) if the network is not down/fault/out-of-service* and the current Comm is not null.*/public void startComm()throws Exception{if((!isDisabled()) && (!isFatalFault()) && (comm != null)){if (getLog().isTraceOn()) getLog().trace(getName() + " *** Starting Communication Handler ***");comm.start();if (getLog().isTraceOn()) getLog().trace(getName() + " *** Started Communication Handler ***");}}
/*** Start the basic network. This starts the Communication handler* (Comm) if the network is not down/fault/out-of-service.*/public void started()throws Exception{ super.started();try{startComm();}catch (Exception e){getLog().error("Could not start communication handler", e);}}
接下来要看comm.start(),comm是Comm对象(有niagara源码)其start()里主要调用了started(),对于BModbusAsyncNetwork,comm是ModbusAsyncSerialComm ,
protected boolean started() throws Exception {try {BISerialService bISerialService = (BISerialService) Sys.getService(BISerialService.TYPE);this.serialPort = ((BModbusAsyncNetwork) getNetwork()).getSerialPortConfig().open(getNetwork().getName());this.serialPort.enableReceiveTimeout(bISerialService.getMinTimeout());this.in = this.serialPort.getInputStream();this.out = this.serialPort.getOutputStream();...
getSerialPortConfig()返回的是BSerialHelper对象(有niagara源码),其open()方法为
/*** Opens the serial port and sets the port parameters.* Returns the serial port as a BISerialPort.* * @param String owner - The name of the owner to set for the serial port.*/public BISerialPort open(String owner)throws Exception{ BISerialService platSvc = (BISerialService) Sys.getService(BISerialService.TYPE);((BComponent)platSvc).lease(); // create a subscription to force the platform service to lazy-inittry{port = platSvc.openPort(getPortName(), owner);}
可以看到是通过BISerialService来打开串口的,遇到接口就只能猜测一下了。。。还好,最终发现在我的电脑上是BSerialPortPlatformServiceWin64,它的父类BSerialPortPlatformService实现了该接口,openPort()的核心代码如下
public BISerialPort openPort(String paramString1, String paramString2)throws PortNotFoundException, PortDeniedException{...Object localObject = (BSerialPort)get(paramString1);...try{((BSerialPort)localObject).openPort();return new BSerialPortHandle((BSerialPort)localObject);}...}
get(paramString1)这个看上去比较奇怪,其实它是BComplex的方法(获得slot),这些BSerialPort是如何添加的能,这就要看它的抽象方法loadPortIdentifiers()了,这里看BSerialPortPlatformServiceWin64的实现
protected String[] loadPortIdentifiers()throws Exception
{ ... Enumeration localEnumeration = CommPortIdentifier.getPortIdentifiers();while (localEnumeration.hasMoreElements()){CommPortIdentifier localCommPortIdentifier1 = (CommPortIdentifier)localEnumeration.nextElement();if (localCommPortIdentifier1.getPortType() == 1){localVector.add(localCommPortIdentifier1);}}...BSerialPortWin64 localBSerialPortWin64 = new BSerialPortWin64(arrayOfCommPortIdentifier[j], j);add(SlotPath.escape(arrayOfString[j]), localBSerialPortWin64, 3);
}
是不是很熟悉了,RxTx库哈哈,add()实现slot的动态添加
niagara框架中串口的打开方式相关推荐
- 【Python】NumPy 中 ravel() 正确打开方式
[Python]NumPy 中 ravel() 正确打开方式 最近在看书时,对numpy的ravel()的有些疑惑,书上这样写到"如果结果中的值在原始数组中是连续的,则ravel 不会生成底 ...
- python中的文件打开方式
r: 以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模式. rb: 以二进制格式打开一个文件用于只读.文件指针将会放在文件的开头.这是默认模式. r+: 打开一个文件用于读写.文件指针将会 ...
- windows中LNK文件打开方式恢复(图形文件打开方式怎么都成CAD打开方式)
windows中LNK文件打开方式恢复(图形文件打开方式怎么都成CAD打开方式) 一大早,有同事说桌面上的文件快捷方式全是CAD图标,打开也是CAD方式打开,我在"文件夹选项"下设 ...
- Spring MVC框架中关于限制请求方式
在Spring MVC框架中,@RequestMapping注解的主要作用是配置请求路径,除此以外,还可以配置请求方式,例如: @RequestMapping(value = "/add-n ...
- TinyFrame框架中的UOW使用方式纠正
我自己的框架中,UOW是可以通过反射提取具体的Repository的,也可以调用Commit方法. 但是正确的应用方式应该是: Using(var uow = new UnitOfWork()) { ...
- 修改eclipse中propertier文件打开方式使用插件
在eclipse中设置properties文件编码格式为UTF-8时显示还是不成功,就使用eclipse editor工具进行修改 打开如图位置 进入后需要等待,加载完成,搜索eclipse edit ...
- Windows7中快捷方式图标打开方式恢复
症状:用户不小心按了不知什么操作后,导致桌面所有快捷方式的图标都变成用某一程序打开,即.lnk的文件都被指定为指定的程序. 原因:文件关联方式更改 解决方法:首先将用户加到管理组,在运行处输:rege ...
- HttpClient在.NET Core中的正确打开方式
问题来源 长期以来,.NET开发者都通过下面的方式发送http请求: using (var httpClient = new HttpClient()) { var response = aw ...
- 巨省显存的重计算技巧在TF、Keras中的正确打开方式
一只小狐狸带你解锁 炼丹术&NLP 秘籍 作者:苏剑林(来自追一科技,人称"苏神") 前言 在前不久的文章<BERT重计算:用22.5%的训练时间节省5倍的显存开销( ...
最新文章
- C++核心编程(三)
- Android开发之LisitView的图文并排效果实现(源代码分享)
- 配置MySQL5.7基于GTID的复制
- 最佳子集aic选择_AutoML的起源:最佳子集选择
- 异步上传文件显示进度条
- 为什么量子计算会对我们产生威胁?
- ajax回调函数提交表单,MVC3中ajax提交表单无法执行success回调函数,怎么办?
- 关于树形插件展示中数据结构转换的算法
- 【数据结构笔记】归并排序(merge_sort)+ 堆排序 -- python2.7
- 【题解】【合并序列(水题)P1628】
- python怎么求中位数_Python求两个有序数组的中位数的几种方法
- MySQL数据库建立数据库和表(命令行方式)
- Tensorflow+Keras+VGG19 猫狗大战分类
- 计算机什么病毒有远程控制功能,怎样知道自己的电脑已被远程控制
- 麦肯锡 “金字塔原理”:职场人结构化思维、表达和解决问题的利器
- 一个算法模型搞定千万种场景,人工智能领域出现一匹黑马
- windows xp 安装好openssh后,不能正常启动,/var/empty must be owned by root and not group or world-writable
- 李立超python视频_《李》字意思读音、组词解释及笔画数 - 新华字典 - 911查询
- Linux:解压bz2文件
- python 宏定义函数_python 宏使用详解
热门文章
- 约瑟夫问题C语言实现——N个人排成一队循环报数,报M的倍数的出列,最后剩下的的是原来队列的第几位
- 穴位按摩---自我保健---1
- Spring源码构建项目,导入eclipse后,缺失spring-cglib-repack-3.2.4.jar和spring-objenesis-repack-2.4.jar的解决办法
- 雨听 | 英语学习笔记(十)~作文范文:怎样提高学生体能?
- 黑盒测试简介和常用方法
- java 阿里云 发送短信功能实现
- 语音识别 --- 音频信号提取
- 第九章 C#界面设计 主菜单
- 1C.小a与星际探索(C++)
- 计算机一级execl怎么学,【计算机一级 excel】大学计算机一级excle