문제

I have this sample in java with 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");
    }

And I can't run it because I have this error message:

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

It's a sample which was downloaded from the sourcfoge. Can someone please give me a little help or some intruduction what did I do wrong? Or can you help me choose which library is the best for Java(Android) modbus communication?

도움이 되었습니까?

해결책

You should use the JAMOD ! and use this tutorial: example and this slave server: server

i hope it helps!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top