2/23/2016 12:46 PM | |
Posts: 3 Rating:
|
I can not read the true values form Holding Register (DB10.DBW0) of PLC. Need help. 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 sucessfull, send the request to PLC but the vulues out put respond I get from Holding Register (DB10.DBW0) of PLC alway 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?
This is my codes from PC: 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(); }
}
} 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 ------------------------------------------------------------------------------------------ |
Last edited by: kyloivn at: 2/23/2016 12:48:32 PMLast edited by: Min_Moderator at: 2/23/2016 2:07:35 PMNew subject after splitting |
|
2/24/2016 3:30 AM | |
Posts: 3 Rating:
|
Any one here can help me, please!! |
2/24/2016 9:47 AM | |
Joined: 12/16/2012 Last visit: 3/16/2025 Posts: 655 Rating:
|
Hi! Have you checked on the PLC side, what's in the ERROR and STATUS registers and if the DR becomes 1 when you read? As a first step, you could also make sure that the PLC communicates with a client by using a modbus tester, e.g. this: http://www.simplymodbus.ca/TCPclient.htm If that works, then you can start with debugging your PC code. |
2/25/2016 12:27 PM | |
Posts: 3 Rating:
|
I can connect to PLC, read and write everything I want now. Thanks everyone. Have a good day!! |
This contribution was helpful to
2 thankful Users |
Follow us on