Closed aniluoh closed 3 years ago
Writing a float value 286.075 when reading, it should return the same value.
When reading it, it shows the value 287.20
/*
import com.ghgande.j2mod.modbus.Modbus; import com.ghgande.j2mod.modbus.io.ModbusSerialTransaction; import com.ghgande.j2mod.modbus.msg.WriteMultipleRegistersRequest; import com.ghgande.j2mod.modbus.msg.WriteMultipleRegistersResponse; import com.ghgande.j2mod.modbus.msg.WriteSingleRegisterRequest; import com.ghgande.j2mod.modbus.msg.WriteSingleRegisterResponse; import com.ghgande.j2mod.modbus.net.SerialConnection; import com.ghgande.j2mod.modbus.procimg.Register; import com.ghgande.j2mod.modbus.procimg.SimpleRegister; import com.ghgande.j2mod.modbus.util.ModbusUtil; import com.ghgande.j2mod.modbus.util.SerialParameters; import java.util.logging.Level; import java.util.logging.Logger;
@author Matteo */ public class Register5253Test {
public static void main(String[] args) { String comPort = "COM9"; int slave_Address = 5; //the unit identifier we will be talking to SerialParameters parameters; SerialConnection serialConnection = null; ModbusUtil modbusUtil = null; WriteMultipleRegistersRequest writeMultipleRegistersRequest; WriteMultipleRegistersResponse writeMultipleRegistersResponse; ModbusSerialTransaction trans; try { parameters = new SerialParameters(); parameters.setPortName(comPort); parameters.setBaudRate(9600); parameters.setDatabits(8); parameters.setStopbits(1); parameters.setParity("None"); parameters.setEncoding(Modbus.SERIAL_ENCODING_RTU); parameters.setEcho(false); serialConnection = new SerialConnection(parameters); serialConnection.open(); System.out.println("__Connection SuccessFul ____"); } catch (Exception ex) { Logger.getLogger(App.class .getName()).log(Level.SEVERE, null, ex); } float fl = (float) 286.075;
byte[] byteArr = modbusUtil.floatToRegisters(fl); byte[] byteArr1 = {byteArr[0], byteArr[1]}; byte[] byteArr2 = {byteArr[2], byteArr[3]}; try { trans = new ModbusSerialTransaction(serialConnection); SimpleRegister myregister = new SimpleRegister(byteArr[0], byteArr[1]); SimpleRegister myregister2 = new SimpleRegister(byteArr[2], byteArr[3]); writeMultipleRegistersRequest = new WriteMultipleRegistersRequest(); writeMultipleRegistersRequest.setUnitID(slave_Address); writeMultipleRegistersRequest.setHeadless(); writeMultipleRegistersRequest.setReference(52); //register number myregister.setValue(byteArr1); myregister2.setValue(byteArr2); Register[] register = {myregister, myregister2}; writeMultipleRegistersRequest.setRegisters(register); trans.setRequest(writeMultipleRegistersRequest); trans.execute(); } catch (com.ghgande.j2mod.modbus.ModbusException exception) { System.out.println("Exception Occured ----" + exception); } serialConnection.close();
} }
Sorry! for inconvenience, my device was faulty.
Expected Behavior
Writing a float value 286.075 when reading, it should return the same value.
Actual Behavior
When reading it, it shows the value 287.20
Steps to Reproduce the Problem
/*
import com.ghgande.j2mod.modbus.Modbus; import com.ghgande.j2mod.modbus.io.ModbusSerialTransaction; import com.ghgande.j2mod.modbus.msg.WriteMultipleRegistersRequest; import com.ghgande.j2mod.modbus.msg.WriteMultipleRegistersResponse; import com.ghgande.j2mod.modbus.msg.WriteSingleRegisterRequest; import com.ghgande.j2mod.modbus.msg.WriteSingleRegisterResponse; import com.ghgande.j2mod.modbus.net.SerialConnection; import com.ghgande.j2mod.modbus.procimg.Register; import com.ghgande.j2mod.modbus.procimg.SimpleRegister; import com.ghgande.j2mod.modbus.util.ModbusUtil; import com.ghgande.j2mod.modbus.util.SerialParameters; import java.util.logging.Level; import java.util.logging.Logger;
/*
@author Matteo */ public class Register5253Test {
public static void main(String[] args) { String comPort = "COM9"; int slave_Address = 5; //the unit identifier we will be talking to SerialParameters parameters; SerialConnection serialConnection = null; ModbusUtil modbusUtil = null; WriteMultipleRegistersRequest writeMultipleRegistersRequest; WriteMultipleRegistersResponse writeMultipleRegistersResponse; ModbusSerialTransaction trans; try { parameters = new SerialParameters(); parameters.setPortName(comPort); parameters.setBaudRate(9600); parameters.setDatabits(8); parameters.setStopbits(1); parameters.setParity("None"); parameters.setEncoding(Modbus.SERIAL_ENCODING_RTU); parameters.setEcho(false); serialConnection = new SerialConnection(parameters); serialConnection.open(); System.out.println("__Connection SuccessFul ____"); } catch (Exception ex) { Logger.getLogger(App.class .getName()).log(Level.SEVERE, null, ex); } float fl = (float) 286.075;
} }
Specifications