简体   繁体   English

在Android上获取CommPortIdentifier的实例

[英]Obtaining an instance of CommPortIdentifier on Android

I'm using Jamod 1.2 to establish Modbus TCP connection between an Android device and PLC. 我正在使用Jamod 1.2在Android设备和PLC之间建立Modbus TCP连接。 Everything was fine until I was asked to migrate to Modbus RTU (connecting via USB). 一切都很好,直到要求我迁移到Modbus RTU(通过USB连接)为止。 Jamod has classes to work with Modbus RTU but I've encountered a specific problem. Jamod具有可用于Modbus RTU的类,但是我遇到了一个特定的问题。 To establish Modbus RTU connection one have to do these simple manipulations: 要建立Modbus RTU连接,必须执行以下简单操作:

SerialParameters params = new SerialParameters();
params.setPortName(portname);
...
SerialConnection connection = new SerialConnection(params);
connection.open();

But the connection doesn't open due to the following obstacle: 但是由于以下障碍,连接无法打开:

public class SerialConnection implements SerialPortEventListener {
    ...
    public void open() throws Exception {
            try {
                this.m_PortIdentifier = CommPortIdentifier.getPortIdentifier(this.m_Parameters.getPortName());
            } catch (NoSuchPortException var2) {
                if(Modbus.debug) {
                    System.out.println(var2.getMessage());
                }

                throw new Exception(var2.getMessage());
            }
    ...
    }
...
}

Here the lib is trying to get an instance of CommPortIdentifier with a static method CommPortIdentifier.getPortIdentifier(String portname). 此处的lib尝试使用静态方法CommPortIdentifier.getPortIdentifier(String portname)获取CommPortIdentifier的实例。 Going deeper: 更深入:

public class CommPortIdentifier {
    public static CommPortIdentifier getPortIdentifier(String var0) throws NoSuchPortException {
        SecurityManager var1 = System.getSecurityManager();
        if(var1 != null) {
            var1.checkDelete(propfilename);
        }
    ...
    }
...
}

And here is the root of the evil - System.getSecurityManager(). 这是邪恶的根源-System.getSecurityManager()。 It always returns null in Android. 在Android中始终返回null。 official doc 官方文件

Finally, the question : is there a way to generate CommPortIdentifier on Android? 最后, 问题是:有没有办法在Android上生成CommPortIdentifier? Or may be someone can think of another solution to the problem? 还是有人可以想到该问题的另一种解决方案?

PS I would like to avoid replacing Jamod with something else or writing my own Modbus RTU wrapper because a lot of code depend on the lib. PS我想避免用其他东西替换Jamod或编写我自己的Modbus RTU包装器,因为很多代码都依赖于lib。 Unless there is no other option. 除非没有其他选择。

Ended up writing my own Modbus RTU wrapper for Android. 最后编写了自己的Android Modbus RTU包装器。 Its basic implementation can be found here: https://github.com/dh-28/ModbusRtuConnect 它的基本实现可以在这里找到: https : //github.com/dh-28/ModbusRtuConnect

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM