Thanks for Jamod libraries...
I have a unit supports the ModBus protocol, using Jamod trying to connect to the unit and read the register values, gets error code -2, please give directions to connect to the unit and read, thanks
Modbus details:
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.
******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 WriteCoilRequest req = null; //the write request /* Variables for storing the parameters */ InetAddress addr = null; // the slave's address int port = 502; // the default port int coil = 17; // one of the coils (D0 1 for this address) to switch ON/OFF //1. Setup the parameters addr = InetAddress.getByName("10.0.6.31"); // ** The address assigned to the module ** //2. Open the connection con = new TCPMasterConnection(addr); con.setPort(port); con.connect();
//~~~~~~~~~~~~~~~~~~~~ The faulty Read Request ~~~~~~~~~~~~~~~~~~~~
//3r. Prepare the READ request
int k = 40001; // The address of the "Vin 0" channel (correct??)
rreq = new ReadInputRegistersRequest(k, 2); // Reading 8 bytes (of what??)
//4r. Prepare the READ transaction trans = new ModbusTCPTransaction(con); trans.setRequest(rreq); //5r. Execute the READ transaction trans.execute(); rres = (ReadInputRegistersResponse) trans.getResponse(); System.out.println("Hex Value of register " + "= " + rres.getHexMessage()); //6. 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(Control_ADAM.java:48)
****************
Error in connecting unit using modbus protocol