Data Conversion

Hello,

I am working on IoT kit using Hilscher NetIOT Connect Gateway, I have a P&F Ultrasonic sensor, I am getting process data 14 bit in two bytes (attachment Received data). I want to convert it into 16bit uint starting 2-bit offset of the first byte, bit read starting from bit 2 to bit 14. So the value range from 0 to 4000mm.

image

Thanks in advance.

Br,
Devendra

How are you receiving the data from the device? It would be much easier if the two bytes were in one message.

Hi,

Data receiving in two separate messages, using OPC UA client data comming. below screenshot.

Now I want to convert this 11 & 233 into a UINT16.
image

below is the P&F ultrasonic sensor process data information.

Br,
Devendra

Hi, firstly, you should (for the sake of consistency in your data) retrieve these as a 16bit value OR an array of 2 bytes - so that they are read from the PLC in the same scan (and therefore consistent)

Does the OPCUA server permit you to setup 16bit tags? Do you have permissions to adjust the OPCUA server config? (do so if you can)

Once you have 2 bytes, it is easy to convert to UINT16 in a function.
var u16 = (msg.payload[1] << 8) + msg.payload[0]; //NOTE: might need to byteswap

once you have a 16 bit value, mask out bits 0 & 1 by shifting right twice...
u16 = (u16 >> 2);

Hello,

Thank you so much for your support, now I am getting proper Sensor value in mm.

.

Br,
Devendra

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.