由jamod提供的Modbus / TCP

时间:2014-02-14 08:49:54

标签: java modbus

我编写了以下程序来了解如何使用jamod访问寄存器。首先,我使用“Modbus Slave”来模拟虚拟TCP-MODBUS保持寄存器。我使用我的程序和jamod lib来读取我刚刚创建的保存寄存器。

但我得到以下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
        MAX_IP_MESSAGE_LENGTH cannot be resolved or is not a field
        at net.wimpi.modbus.io.ModbusTCPTransport.prepareStreams(ModbusTCPTransport.java:223)
        at net.wimpi.modbus.io.ModbusTCPTransport.setSocket(ModbusTCPTransport.java:79)
        at net.wimpi.modbus.io.ModbusTCPTransport.<init>(ModbusTCPTransport.java:59)
        at net.wimpi.modbus.net.TCPMasterConnection.prepareTransport(TCPMasterConnection.java:104)
        at net.wimpi.modbus.net.TCPMasterConnection.connect(TCPMasterConnection.java:67)
        at test_modbus.main(test_modbus.java:36)

这是我的程序

import java.io.*;
import java.lang.*;
import java.net.InetAddress;
import net.wimpi.modbus.Modbus;
import net.wimpi.modbus.io.ModbusTCPTransaction;
import net.wimpi.modbus.msg.WriteCoilRequest;
import net.wimpi.modbus.msg.ReadInputRegistersRequest;
import net.wimpi.modbus.msg.ReadInputRegistersResponse ;
import net.wimpi.modbus.net.TCPMasterConnection;

public class test_modbus {

    public static void main(String args[]) {
        try {
            /* The important instances of the class */
            TCPMasterConnection con = null; // the connection
            ModbusTCPTransaction trans = null; // the transaction
            ReadInputRegistersRequest rreq = null; // the read request
            ReadInputRegistersResponse  rres = null; // the read response
            WriteCoilRequest req = null; // the write request


            /* Variables for storing the parameters */
            InetAddress addr = null; // the slave's address
            int port = 502; // the default port


            // 1. Setup the parameters
            addr = InetAddress.getByName("127.0.0.1"); // ** The address
                                                            // assigned to the
                                                            // module **

            // 2. Open the connection
            con = new TCPMasterConnection(addr);
            con.setPort(port);
            con.connect();
            System.out.println( "--- Message: Line:36 success --- " );
            // ~~~~~~~~~~~~~~~~~~~~ The faulty Read Request ~~~~~~~~~~~~~~~~~~~~
            // 3r. Prepare the READ request
            int k = 4000; 
            rreq = new ReadInputRegistersRequest(k, 2); // Reading 8 bytes (of
                                                        // what??)

            // 4r. Prepare the READ transaction
            trans = new ModbusTCPTransaction(con);
            trans.setRequest(rreq);
            System.out.println( "--- Message: Line:46 success --- " );
            // 5r. Execute the READ transaction
            trans.execute();
            rres = (ReadInputRegistersResponse) trans.getResponse();
            System.out.println("Hex Value of register " + "= "
                    + rres.getHexMessage());

            // ~~~~~~~~~~~~~~~~~~~~ The functional Write Request
            // ~~~~~~~~~~~~~~~~~~~~
            // 3w. Prepare the request
            //req = new WriteCoilRequest(coil, true); // Switching ON the "DO 1"
                                                    // (= address 17)

            // 4w. Prepare the transaction
            trans = new ModbusTCPTransaction(con);
            trans.setRequest(req);

            // 5w. Execute the transaction repeat times
            trans.execute();

            // 6. Close the connection
            con.close();

        } catch (Exception ex) {
            System.out.println("Error");
            ex.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

Exception in thread "main" java.lang.Error: Unresolved compilation problem: MAX_IP_MESSAGE_LENGTH cannot be resolved or is not a field

表示编译器找不到(常量)值MAX_IP_MESSAGE_LENGTH

at net.wimpi.modbus.io.ModbusTCPTransport.prepareStreams(ModbusTCPTransport.java:223)

表示在ModbusTCPTransport.java的第223行引用MAX_IP_MESSAGE_LENGTH

问题不在您的代码中,而是在编译jamod库中。你有没有将jamod的代码复制到你的项目中,或者你把jamod jar包含在你的类路径中?如果第一个那就是你的问题,那就做后者(使用maven http://mvnrepository.com/artifact/net.wimpi/jamod/1.2)。