我已经购买了具有Modbus RTU(RS485)接口的露点传感器:
现在我需要获取第一个值(Modbus寄存器= 1001,Modbus addres = 1000,字节数= 4)以插入DDBB表。
我选择了j2mod进行连接并读取如下值:
ModbusSerialMaster serialMaster = null;
int unitID = 1;
int startingRegister = 1001;
int registerCount = 1;
Register[] slaveResponse = new Register[registerCount];
try {
SerialParameters parameters = new SerialParameters();
parameters.setPortName("COM4");
parameters.setBaudRate(19200);
parameters.setDatabits(8);
parameters.setParity(AbstractSerialConnection.EVEN_PARITY);
parameters.setStopbits(AbstractSerialConnection.ONE_STOP_BIT);
parameters.setEncoding(Modbus.SERIAL_ENCODING_RTU);
parameters.setEcho(false);
serialMaster = new ModbusSerialMaster(parameters);
serialMaster.connect();
System.out.println("CONNECTED!");
slaveResponse = serialMaster.readMultipleRegisters(unitID, startingRegister, registerCount);
for (int i = 0; i < slaveResponse.length; i++)
{
System.out.println("reg" + i + " = " + slaveResponse[i]);
}
serialMaster.disconnect();
System.out.println("DISCONNECTED!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
没有发现建立连接的问题,但是当我调用readMultipleRegisters时,由于无法获得响应而导致错误。
我做错了什么?