我正在使用串行Modbus(jamod)从设备读取数据。我能够连接到设备但无法从寄存器中读取数据。
这是我的代码:
public class Serial {
static SerialConnection connection = null;
SerialParameters params;
ModbusSerialTransaction transaction = null;
ReadMultipleRegistersRequest holdingRequest = null;
ReadMultipleRegistersResponse holdingResponse = null;
public void getData() {
try {
ModbusCoupler.getReference().setUnitID(1);
params = new SerialParameters();
params.setPortName("/dev/ttyS0");
params.setBaudRate(9600);
params.setDatabits(8);
params.setEncoding(Modbus.SERIAL_ENCODING_RTU);
params.setParity("None");
params.setStopbits(1);
params.setEcho(false);
params.setReceiveTimeout(500);
connection = new SerialConnection(params);
connection.open();
checkRegister(40257, 2);
connection.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public void registerData(int address, int count) {
holdingRequest = new ReadMultipleRegistersRequest(address, count);
holdingResponse = new ReadMultipleRegistersResponse();
holdingRequest.setUnitID(130); // I tried with 1 and getting same error
holdingRequest.setHeadless();
holdingResponse = (ReadMultipleRegistersResponse) executeRequest(connection, holdingRequest);
System.out.println("Response: " + holdingResponse);
}
private ModbusResponse executeRequest(SerialConnection connection, ModbusRequest request) {
try {
transaction = new ModbusSerialTransaction(connection);
transaction.setRequest(request);
Thread.sleep(500);
transaction.execute(); // Error at this line
return transaction.getResponse();
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
}
我在第transaction.execute()
行收到以下错误:
net.wimpi.modbus.ModbusIOException: I/O exception - failed to read
at net.wimpi.modbus.io.ModbusRTUTransport.readResponse(ModbusRTUTransport.java:163)
at net.wimpi.modbus.io.ModbusSerialTransaction.execute(ModbusSerialTransaction.java:187)
at Serial.executeRequest(Serial.java:279)
at Serial.checkRegister(Serial.java:337)
at Serial.getData(Serial.java:66)
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.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)
任何建议都将受到赞赏。