采用PLC作为服务器保存数值,然后用jamodmodbus客户端写代码从PC连接到PLC,读取PLC寄存器值。值始终为零
我用PLC作为服务器和写代码读取寄存器值(DB10。data files)如图片贴在下面。我用jamodmodbus客户端写代码从PC连接到PLC寄存器(DB10。data files)。
结果是:我可以连接到PLC的成功,将请求发送到PLC的输出响应值,但我从寄存器(DB10。data files)PLC总是零(00)。我怎样才能解决这个问题,请告诉我使用代码从寄存器读取值(DB10。data files)和改变他们的小数?
这是我个人电脑上的代码:
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();
}
}
}
这是我从控制台得到的输出 代码:
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)
回答一:如果客户端/主请求数据来自于保持寄存器,则在请求消息和响应消息中出现功能代码03。
如果你的例子是十六进制查询或响应
的reqquest值= 00 00 00 00 00 06 00 04 40 00 00 9C
信息:路线:42成功---
信息:路线:46成功---
的reqquest值= 00 01 00 00 00 06 00 04 40 00 00 9C
对resspond = 00 01 00 00 00 03 00 04 00 gethexmessage
的reqquest值= 00 02 00 00 00 06 00 04 40 00 00 9C
然后在任何消息中我都没有看到03个函数代码。
您确定在保持寄存器中的数据吗?
你发送(fc03)设置保持寄存器?
|