I'm using Jamod 1.2 to establish Modbus TCP connection between an Android device and PLC. Everything was fine until I was asked to migrate to Modbus RTU (connecting via USB). Jamod has classes to work with Modbus RTU but I've encountered a specific problem. To establish Modbus RTU connection one have to do these simple manipulations:
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). 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(). It always returns null in Android. official doc
Finally, the question: is there a way to generate CommPortIdentifier on Android? Or may be someone can think of another solution to the problem?
P.S. 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. Unless there is no other option.
Ended up writing my own Modbus RTU wrapper for Android. Its basic implementation can be found here: https://github.com/dh-28/ModbusRtuConnect