D
I use PLC as Server and write the codes to hold values of Holding Register (DB10.DBW0) like the Picture attach below. And I use jamodModbus as client to write codes from PC to connect to PLC and read the values from of Holding Register (DB10.DBW0).
The result is: I can connect to PLC sucessful, send the request to PLC but the values out put respond I get from Holding Register (DB10.DBW0) of PLC always zero (00). How can I fix this problem and please tell which code I use to get only values from Holding Register (DB10.DBW0) and change them to Decimal?
<b>Moderator's Note:</b> You cannot attach illustrations to Control.com posts. I think he means the code below.
This is my codes from PC:<pre>
package testmodbus;
import java.lang.*;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import net.wimpi.modbus.Modbus;
import net.wimpi.modbus.ModbusSlaveException;
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 TestModbus {
public static void main(String[] args) throws ModbusSlaveException, SocketTimeoutException{
// TODO code application logic here
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
int ref = 40000; //the reference; offset where to start reading from
int count =0;
int repeat = 2;
/* Variables for storing the parameters */
InetAddress addr = null; // the slave's address
int port = Modbus.DEFAULT_PORT; // DEFAULT_PORT = 502.
// 1. Setup ?":>the parameters
addr = InetAddress.getByName("192.168.0.1"); // ** The address of PLC
// 2. Open the connection
con = new TCPMasterConnection(addr);
con.setPort(port);
con.connect();
System.out.println("--- Message: Line:36 success --- ");
System.out.println("Connect to PLC sucessfull!!");
// ~~~~~~~~~~~~~~~~~~~~ The faulty Read Request ~~~~~~~~~~~~~~~~~~~~
// // 3r. Prepare the READ request
rreq = new ReadInputRegistersRequest(ref, count);
System.out.println("The reqquest values =" + rreq.getHexMessage());
System.out.println("--- Message: Line:42 success --- ");
//// // 4r. Prepare the READ transaction
trans = new ModbusTCPTransaction(con);
trans.setRequest(rreq);
/ System.out.println("--- Message: Line:46 success --- ");
//// // 5r. Execute the READ transaction
int k = 0;
do {
trans.execute();
rres = (ReadInputRegistersResponse) trans.getResponse();
System.out.println("The reqquest values =" + rreq.getHexMessage());
System.out.println("getHexMessage of resspond = "+ rres.getHexMessage());
k++;
}while (k<repeat);
// 6. Close the connection
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} </pre>
And this is the out put I get from Console:
run:
--- Message: Line:36 success ---
Connect to PLC sucessfull!!
The reqquest values =00 00 00 00 00 06 00 04 9c 40 00 00
--- Message: Line:42 success ---
--- Message: Line:46 success ---
The reqquest values =00 01 00 00 00 06 00 04 9c 40 00 00
getHexMessage of resspond = 00 01 00 00 00 03 00 04 00
The reqquest values =00 02 00 00 00 06 00 04 9c 40 00 00
getHexMessage of resspond = 00 02 00 00 00 03 00 04 00 // always get 00 values form PLC with whatever address I try to read from PLC.
BUILD SUCCESSFUL (total time: 0 seconds)
The result is: I can connect to PLC sucessful, send the request to PLC but the values out put respond I get from Holding Register (DB10.DBW0) of PLC always zero (00). How can I fix this problem and please tell which code I use to get only values from Holding Register (DB10.DBW0) and change them to Decimal?
<b>Moderator's Note:</b> You cannot attach illustrations to Control.com posts. I think he means the code below.
This is my codes from PC:<pre>
package testmodbus;
import java.lang.*;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import net.wimpi.modbus.Modbus;
import net.wimpi.modbus.ModbusSlaveException;
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 TestModbus {
public static void main(String[] args) throws ModbusSlaveException, SocketTimeoutException{
// TODO code application logic here
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
int ref = 40000; //the reference; offset where to start reading from
int count =0;
int repeat = 2;
/* Variables for storing the parameters */
InetAddress addr = null; // the slave's address
int port = Modbus.DEFAULT_PORT; // DEFAULT_PORT = 502.
// 1. Setup ?":>the parameters
addr = InetAddress.getByName("192.168.0.1"); // ** The address of PLC
// 2. Open the connection
con = new TCPMasterConnection(addr);
con.setPort(port);
con.connect();
System.out.println("--- Message: Line:36 success --- ");
System.out.println("Connect to PLC sucessfull!!");
// ~~~~~~~~~~~~~~~~~~~~ The faulty Read Request ~~~~~~~~~~~~~~~~~~~~
// // 3r. Prepare the READ request
rreq = new ReadInputRegistersRequest(ref, count);
System.out.println("The reqquest values =" + rreq.getHexMessage());
System.out.println("--- Message: Line:42 success --- ");
//// // 4r. Prepare the READ transaction
trans = new ModbusTCPTransaction(con);
trans.setRequest(rreq);
/ System.out.println("--- Message: Line:46 success --- ");
//// // 5r. Execute the READ transaction
int k = 0;
do {
trans.execute();
rres = (ReadInputRegistersResponse) trans.getResponse();
System.out.println("The reqquest values =" + rreq.getHexMessage());
System.out.println("getHexMessage of resspond = "+ rres.getHexMessage());
k++;
}while (k<repeat);
// 6. Close the connection
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} </pre>
And this is the out put I get from Console:
run:
--- Message: Line:36 success ---
Connect to PLC sucessfull!!
The reqquest values =00 00 00 00 00 06 00 04 9c 40 00 00
--- Message: Line:42 success ---
--- Message: Line:46 success ---
The reqquest values =00 01 00 00 00 06 00 04 9c 40 00 00
getHexMessage of resspond = 00 01 00 00 00 03 00 04 00
The reqquest values =00 02 00 00 00 06 00 04 9c 40 00 00
getHexMessage of resspond = 00 02 00 00 00 03 00 04 00 // always get 00 values form PLC with whatever address I try to read from PLC.
BUILD SUCCESSFUL (total time: 0 seconds)