Reputation: 891
I have a unit supports the ModBus protocol, using 'Jamod' trying to connect to the unit and read the register values, gets an error code-2,
unit config:
The unit supports the Modbus protocol over the RS-485 and Ethernet interfaces. On the RS-485 interface it has a configurable, Modbus address on the Modbus network; by default this is set to 99. The unit will also respond to the broadcast address of 0.
The RS-485 interface operates at a baud rate of 9600 Baud by default with 8 bits and even parity. It is configurable to 1200, 2400, 4800, 9600, 19200, 38400, 57600 or 15200 Baud.
The Ethernet interface uses a RJ45 connector. This interface supports TCP/IP Ethernet connections on port 502. The slave address is 0.
The unit returns data using the Modbus Read Input Registers function code 4. It also allows the reading and writing of configuration parameters using the Modbus holding register access functions 3 and 16. Also supported is a subset of the Modbus diagnostics function code 8.
please give directions to connect to the unit and read, thanks
*******************Sample Code***********************
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.ReadInputRegistersRequest;
import net.wimpi.modbus.msg.ReadInputRegistersResponse;
import net.wimpi.modbus.net.TCPMasterConnection;
public class modbus_conn {
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
/* Variables for storing the parameters */
InetAddress addr = null; // the slave's address
int port = 502; // the default port
//int coil = 1; // one of the coils (D0 1 for this address) to switch ON/OFF
//Setup the parameters
addr = InetAddress.getByName("127.192.6.31"); // ** The address assigned to the module **
//Open the connection
con = new TCPMasterConnection(addr);
con.setPort(port);
con.connect();
//Prepare the READ request
int k = 30001; // register address starting from 30001
rreq = new ReadInputRegistersRequest(k, 2); // Reading 8 bytes
//Prepare the READ transaction
trans = new ModbusTCPTransaction(con);
trans.setRequest(rreq);
//Execute the READ transaction
trans.execute();
rres = (ReadInputRegistersResponse) trans.getResponse();
System.out.println("Hex Value of register " + "= " + rres.getHexMessage());
//Close the connection
con.close();
}
catch (Exception ex) {
System.out.println("Error");
ex.printStackTrace();
}
}
}
error:
Error
net.wimpi.modbus.ModbusSlaveException: Error Code = 2
at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:207)
at modbusConn.Control_ADAM.main(modbus_conn.java:48)
Upvotes: 0
Views: 4089
Reputation: 21
Error code 2 states that there is an ILLEGAL DATA ADDRESS. In your case 30001 is the register address and your are reading 2 bytes. to over come this problem use the hexadecimal address of register, see the manual if you don't know the hexadecimal address.(Works for me :-))
And giving the increment no too high will also give you error code 3, take care of that too.
Upvotes: 2