How to keep 3 decimal places ?

I’m a new user of openEMS.
How to keep 3 decimal places after collecting the value of meter through FC4ReadInputRegistersTask method?
SourceMeterValue Of ModbusSlave: 10.783915 (ReadInputRegister, 32Bit, Float)
image

I’m reading from Modbus register 40102 with the following code:

				/*
				 * Read Input Register, two Modbus Registers or 32 bit.
				 */
 				new FC4ReadInputRegistersTask(102, Priority.HIGH,
 						m(MyModbusDevice.ChannelId.ACTIVE_FC4_102, 
								new FloatDoublewordElement(102), ElementToChannelConverter.SCALE_FACTOR_MINUS_2),

The values always seems to be a integer(0 or 1) while Modbus binding is reading proper values from this adress. Replace SCALE_FACTOR_MINUS_2 with the SCALE_FACTOR_3,the value was 10783. Log and Influxdb was that(a integer number).

I don’t konw how to change a float of keep 3 decimal places, pls help me.

Thank you for your reply.

Hi Paul,

we very often rely on Integer values in OpenEMS, as it provides certain benefits over Float/Double values, e.g. when checking for equality. In Grafana you can then simply configure the data type e.g. as Milliwatt [mW] etc. to get proper rounding in the charts.

In the Modbus mapping there are two different places where type definitions happen:

  1. new FloatDoublewordElement(102) in defineModbusProtocol() tells the Modbus Bridge, that the byte-stream coming via Modbus should be interpreted as a Float32 (IEEE 754)

  2. In the definition of MyModbusDevice.ChannelId.ACTIVE_FC4_102 you set the actual OpenemsType of the Channel. I am quite certain that you defined the Channel as Doc.of(OpenemsType.INTEGER). Try Doc.of(OpenemsType.FLOAT) here to define the Channel as a Float-Channel.

The ElementToChannelConverter converts from Modbus-Element-Type (1.) to Channel-Type (2.). The different SCALE_FACTOR_* settings are applied before rounding and converting Float values to Integer in your case.

Does hat help?

Regards,
Stefan

1 Like

Hi,Stefan
Great!thanks a lot your response.

1 Like