我使用Modbuspal模拟器作为从站,使用Jamod tcp主代码作为主站。以下是我的Jamod tcp主代码,用于从寄存器中读取数据:
import java.net.InetAddress;
import net.wimpi.modbus.io.ModbusTCPTransaction;
import net.wimpi.modbus.msg.ReadInputDiscretesRequest;
import net.wimpi.modbus.msg.ReadInputDiscretesResponse;
import net.wimpi.modbus.net.TCPMasterConnection;
public class SlaveTest {
Logger LOG = LoggerFactory.getLogger(SlaveTest.class);
TCPMasterConnection connection;
ModbusTCPTransaction transaction = null; //the transaction
ReadInputDiscretesRequest request = null; //the request
ReadInputDiscretesResponse response = null; //the response
public void test() {
connection = null; //the connection
InetAddress addr = null; //the slave's address
int port = 502;
int ref = 1, count = 1;
try {
addr = InetAddress.getByName("127.0.0.1");
connection = new TCPMasterConnection(addr);
connection.setPort(port);
connection.connect();
request = new ReadInputDiscretesRequest(ref, count);
transaction = new ModbusTCPTransaction(connection);
transaction.setRequest(request);
transaction.execute();
response = (ReadInputDiscretesResponse) transaction.getResponse();
System.out.println("Digital Inputs Status=" + response.getDiscretes().toString());
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Modbuspal奴隶:
当我执行我的代码时,建立了连接,但是我收到以下错误:
net.wimpi.modbus.ModbusIOException: Executing transaction failed (tried 3 times)
at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:197)
at modbus.slave.SlaveTest.test(SlaveTest.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:408)
at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:279)
at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:252)
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:177)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:68)
at org.apache.camel.component.bean.BeanProducer.process(BeanProducer.java:38)
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:141)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:165)
at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:73)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
错误是行" transaction.execute();"的触发器。请帮忙。
先谢谢。
答案 0 :(得分:1)
您需要为主人提供单位ID。所以,请尝试以下方法:
request = new ReadInputDiscretesRequest(ref, count);
request.setUnitID(ID)
并且ID是您的从属单元ID,应该是一个数字