gpt4 book ai didi

java - 在 Android 上获取 CommPortIdentifier 实例

转载 作者:行者123 更新时间:2023-11-29 02:41:13 25 4
gpt4 key购买 nike

我正在使用 Jamod 1.2 在 Android 设备和 PLC 之间建立 Modbus TCP 连接。一切都很好,直到我被要求迁移到 Modbus RTU(通过 USB 连接)。 Jamod 有与 Modbus RTU 一起工作的类,但我遇到了一个特定的问题。要建立 Modbus RTU 连接,必须执行以下简单操作:

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

但是由于以下障碍,连接没有打开:

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());
}
...
}
...
}

这里的库尝试使用静态方法 CommPortIdentifier.getPortIdentifier(String portname) 获取 CommPortIdentifier 的实例。更深入:

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

这就是罪恶的根源 - System.getSecurityManager()。它在 Android 中总是返回 null。 official doc

最后,问题:有没有办法在 Android 上生成 CommPortIdentifier?或者可能有人能想到另一种解决问题的方法?

P.S. 我想避免用其他东西替换 Jamod 或编写我自己的 Modbus RTU 包装器,因为很多代码都依赖于库。除非别无选择。

最佳答案

最终为 Android 编写了我自己的 Modbus RTU 包装器。它的基本实现可以在这里找到:https://github.com/dh-28/ModbusRtuConnect

关于java - 在 Android 上获取 CommPortIdentifier 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43975287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com