我试图使用Modbus / TCP从设备的保持寄存器(Weidmüller制造的PowerMonitor)读取值。我使用java和一个名为“jamod”的modbus库。我的代码:
public class Master1 {
public static void main(String[] args) {
try {
TCPMasterConnection con = null; // the connection
ModbusTCPTransaction trans = null; // the transaction
ReadMultipleRegistersRequest req = null; // the request
ReadMultipleRegistersResponse res = null; // the response
byte[] ip = { (byte) 127, (byte) 0, (byte) 0, (byte) 1 }; // the
// powermonitor
// actually
// has a
// different
// IP
InetAddress addr = InetAddress.getByAddress(ip); // the slave's
// adress
int port = 502; // Modbus.DEFAULT_PORT;
int ref = 4; // the reference; offset where to start reading from,
// =n-1
int count = 4; // the number of Reg's to read
int repeat = 100; // a loop for repeating the transaction
// 2. Open the connection
con = new TCPMasterConnection(addr);
con.setPort(port);
con.connect();
// 3. Prepare the request
req = new ReadMultipleRegistersRequest(ref, count);
// 4. Prepare the transaction
trans = new ModbusTCPTransaction(con);
trans.setRequest(req);
// 5. Execute the transaction repeat times
int k = 0;
do {
trans.execute();
res = (ReadMultipleRegistersResponse) trans.getResponse();
for (int i = 0; i < count; i++) {
System.out.println("Status register " + (ref + i + 1)
+ ": " + res.getRegisterValue(i));
}
k++;
} while (k < repeat);
// 6. Close the connection
con.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
在eclipse中运行此代码(使用实际的IP),我在控制台上获取此代码:
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.read(Unknown Source)
at net.wimpi.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:171)
at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:193)
at Master1.main(Master1.java:44)
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.read(Unknown Source)
at net.wimpi.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:171)
at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:193)
at Master1.main(Master1.java:44)
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.read(Unknown Source)
at net.wimpi.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:171)
at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:193)
at Master1.main(Master1.java:44)
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.read(Unknown Source)
at net.wimpi.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:171)
at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:193)
at Master1.main(Master1.java:44)
net.wimpi.modbus.ModbusIOException: Executing transaction failed (tried 3 times)
at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:197)
at Master1.main(Master1.java:44)
当我在本地主机(如上所述)使用EasyModbusTCP服务器模拟器运行此程序时,没有问题,并且我将值存储在保持寄存器中。 那我做错了什么?我真的不知道该怎么做。我希望我问的方式还可以(我今天很新)。非常感谢!