I have written the following program to understand how to use jamod to access registers. First, I use "Modbus Slave" to simulate a virtual TCP-MODBUS Holding registers. And I use my program with jamod lib to read the holding registers that i just made.
But I got errors below:
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)
Here is my program
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
means the compiler can not find the (constant) value MAX_IP_MESSAGE_LENGTH
.
at net.wimpi.modbus.io.ModbusTCPTransport.prepareStreams(ModbusTCPTransport.java:223)
means that at line 223 of ModbusTCPTransport.java MAX_IP_MESSAGE_LENGTH
is referenced.
The problem is not in your code, but in the compilation of the jamod library. Have you copied the code of jamod to your project or have you included the jamod jar to your classpath? If the first then that is your problem, do the latter (use maven http://mvnrepository.com/artifact/net.wimpi/jamod/1.2).