Recently I was writing code to read Int64 numbers from Modbus devices in dotnet C#. The data coming from Modbus was all in array of bytes. But how to decode it and transform that to some meaningful numbers?
That's how a sample response from Modbus looks like
The data reading of Float value is 400.5
and another one
this time it's Int64 value 347238
The architecture was built on top of Azure IoT and I wanted to have it as an Edge Module. However at the end it was nothing more than a piece of c# code inside of the framework. I started with iot-edge-modbus, and that was a good beginning. It was able to decode the message and read the UInt16 values.
Le't quickly decompose the sample message
First few bytes are addresses, etc - more on that here. We are interested values starting at 9th byte which is in this example value 8 - this information of how many bytes the data itself has. In Modbus message you may have multiple numbers. So here we have 8 for Int64, however it may be i.e. 16 then it would contain two Int64 values each of 8 bytes.
Then starting at 10th byte we have the actual data and then 8 bytes that would be that part
Ok so how to make an Int64 number out of it which we know already is 347238?
Let's first read them into variables to make it easier.
And then as simple as that in C#. Note the reverse order of bytes.
If you have other types like UInt16, UInt32, UInt64, Int16, Int32, Float and also IEEE Float then the functions are here. Naturally other types will use different number of bytes.
Comments
Post a Comment