Modbus - interpreting data

I have a need to get readings out of a YSI water quality meter that supports Modbus. I’ve found an Arduino Modbus TCP library (MgsModbus v0.1.1) that I’m hoping will easily port to the Photon. It claims to offer Modbus master (client) capabilities.

What I’ve learned is that the analyzer’s readings are communicated as 4 bytes of data representing an IEEE-754 32 bit floating point number.

I’m curious if anyone has any experience converting 4 bytes of data into a floating point number within the Photon and then I’ll plan to convert it to a string to post to Grovestreams. Or, could I be luck enough that the Photon uses IEEE-754 floating point numbers.

Yes, I need to look further into MsgModbus to see how it’s returning the data.

Does anyone have any experience with Modbus with a Particle device as a master?

Hi @ctmorrison

The ARM inside of Photon does use IEEE-754 to represent floating-point numbers; for 32-bits you want type float. You will need to convert from an array of bytes to a 32-bit floating-point number. Here is a stackoverflow article on how to do that:

You may need to adjust the order (but probably not) such that the first byte you receive might go in the array in location 0 for normal or 3 for reversed. The order then goes up or down as you write more bytes. More than likely you will just need to write them in order 0,1,2,3, but it depends on what the meter sends.

1 Like

Hi @bko,

A quick read indicates this goes from float to byte, not the reverse that I need. However, I’ll keep reading to see if I can figure out how to do what I need. Thanks so much for the quick response!

Well…I guess I’d just use the same technique, but load the bytes into the array and then read the float that’s in the union. duh…

Thanks!!!

1 Like

@ctmorrison If you get this working I would love to try to code out.

I have a solar charge controller that communicates via Modbus that I would like to read its data output stream.

So far I have not tried this but I would like to.