Convert to float (32 bits) IEEE754

Hello community,
I'm having some trouble reading Modbus TCP addresses from an oxygen and methane sensor on an MSA device. The MSA device manual contains Modbus addresses in a specific format that I need to convert to see the actual value. Details of the format can be found here:

I'm using ModbusTester as a quick tool to visualize data. In ModbusTester, I have the option to change the format, which does give me the measured value (which appears on the device 20.8). So, how would I do it in Ignition?

I'm trying this code, but it doesn't update the value when the Modbus tags change. It's as if it only performs the calculation once and doesn't refresh when the Modbus tag values ​​change.

def transform(self, value, quality, timestamp):
	
	try:
        
        	high = self.custom.key1 & 0xFFFF   # Tag 107
        	low  = self.custom.key2 & 0xFFFF   # Tag 106

        
        # Change 32 bits
        	combined = (high << 16) | low

        # read IEEE754 float (32 bits)
        	sign = -1 if (combined & 0x80000000) else 1
        	exponent = ((combined >> 23) & 0xFF) - 127
        	mantissa = combined & 0x7FFFFF

        
        	if exponent == -127:
            		result = sign * (mantissa / float(1 << 23)) * (2 ** (-126))
        	else:
            		result = sign * (1 + mantissa / float(1 << 23)) * (2 ** exponent)

        	return round(result, 3)

    	except:
        
        	return None

You don't need to do this combination yourself, just create a tag with an address (OPC Item Path) like [Device]HRF107 or and the driver will read 2 registers and combine them into a floating point.

2 Likes

For future referencing:

Also note that the left column in ModbusTester is zero-based, where IA's driver defaults to one-based. (Change in the device advanced settings.) When documentation shows addresses in hexadecimal, that is almost always zero-based.

Thanks for the clarification of the prefixes that should be added. But is there a problem with RS323 to TCP?
In OPC Item Path, it is displayed like this. (Analog8 = 106)

RS232 to TCP

Thank you and please excuse my lack of knowledge on this part of the topic.

You appear to be using the modbus address mapping configuration. Don't do that, just enter the address directly. e.g ns=1;s=[FieldServerMD_TCP]1.HRF106.

Note that you do not have the ability to browse registers on modbus devices using Ignition's driver. Anything you are seeing currently is driven by the address mapping config.

Edit: for pturmel.

  • Cough *

Unless you use an alternate driver....

/shameless plug

1 Like

I thank everyone for their cooperation. The problem is resolved by following their instructions.

You should not use an address mapping prefix that matches a manual addressing prefix, as that will prevent any manual addressing with that prefix.

It is best to not use the address mapping feature at all.

2 Likes