我编写了以下程序,以了解如何使用jamod访问寄存器。首先,我使用“ Modbus从站”来模拟虚拟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();
}
}
}
最佳答案
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
。
在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)。