错误:modbus4j Java Createmaster

时间:2013-08-30 19:09:47

标签: java modbus

我在带有modbus4j.jar的java中有这个示例:

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

这是从sourcfoge下载的样本。 有人可以给我一些帮助或一些介绍我做错了什么? 或者你能帮我选择最适合Java(Android)modbus通信的库吗?

1 个答案:

答案 0 :(得分:0)

您应该使用JAMOD! 并使用本教程: example 和这个从服务器: server

我希望它有所帮助!