niagara框架中串口的打开方式
来源:互联网 发布:剑网三纯阳脸型数据 编辑:程序博客网 时间:2024/05/12 18:59
一直好奇niagara框架中串口是如何打开的,最近通过看源码,大致明白了,现记录如下
首先从BModbusAsyncNetwork这个类看起,在niagara help中查找没有源码,不过没关系,利用反编译看,重点代码如下
//BModbusAsyncNetwork是一个BIServicepublic 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-init try { 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的动态添加
0 0
- niagara框架中串口的打开方式
- 打开投影机的2中方式:串口、单板机
- niagara编程中需要注意的几点问题
- 在niagara中开发driver遇到的问题
- Arduino串口的正确打开方式
- niagara编译的那些事
- android中打开串口打印
- VC中如何打开Com10及以上的串口
- VC中如何打开Com10及以上的串口
- VC中如何打开Com10及以上的串口
- VC中如何打开Com10及以上的串口
- VC中如何打开Com10及以上的串口
- UE4串口打开方式(4.13)
- web方式cgi程序操作串口,不能打开串口
- Eclipse中设置文件的打开方式
- Html中select的onchange打开方式
- 串口名导致的串口打开失败
- 打开Solaris的串口输出
- apache 反向代理
- Wireless Witch: How to Test Your Wireless Performance
- kali2016.2安装Nessus记录
- CSS3动画下拉菜单(当transition遇到display的坑)
- 无法使用索引的select语句(转)
- niagara框架中串口的打开方式
- EditText中将光标位置移到文字后面显示‘并且设置光标颜色和所输入字体颜色一样
- Java-Servlet服务端初试
- socket 通信流程和三次握手
- 课程设计(银行叫号机系统)
- 前端面试题大集合
- java 泛型方法 返回泛型结果
- LinerLayout 添加分割线
- react dangerouslySetInnerHTML