我在 java 中有这个带有 modbus4j.jar 的示例:
public class Modbus {
public static void main(String[] args) throws Exception {
ModbusFactory factory = new ModbusFactory();
IpParameters params = new IpParameters();
params.setHost("127.0.0.1");
params.setPort(502);
params.setEncapsulated(true);
ModbusMaster master = factory.createTcpMaster(params, false);
// master.setRetries(4);
master.setTimeout(2000);
master.setRetries(0);
long start = System.currentTimeMillis();
try {
master.init();
for (int i = 0; i < 3; i++) {
System.out.println(master.getValue(127, RegisterRange.HOLDING_REGISTER, 1220,
DataType.TWO_BYTE_INT_UNSIGNED));
}
}
finally {
master.destroy();
}
System.out.println("Took: " + (System.currentTimeMillis() - start) + "ms");
}
而且我无法运行它,因为我有以下错误消息:
Exception in thread "main" java.lang.NoClassDefFoundError: com/serotonin/messaging/MessagingExceptionHandler
at com.serotonin.modbus4j.ModbusFactory.createTcpMaster(ModbusFactory.java:27)
at modbus.Modbus.main(Modbus.java:20)
Caused by: java.lang.ClassNotFoundException: com.serotonin.messaging.MessagingExceptionHandler
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 2 more
Java Result: 1
这是一个从源代码下载的示例。有人可以给我一点帮助或一些介绍我做错了什么吗?或者你能帮我选择哪个库最适合 Java(Android) modbus 通信吗?