Does anyone know how to do Modbus tcp to Javascript conversions?

Hey all,

I just wanna read floating point data from modbus tcp. Therefore I wanna convert modbus tcp all formats to Javascript using node red function node. Currently I can read only 32 bit float - little endian byte swapped only. Method I will mention below. How to do the rest ?

following code I used for 32 bit float - little endian byte swapped and its working fine.

thanks in advance.

There's no need to create loads of typed arrays. :slightly_smiling_face:

I think the Modbus reader node also has a NodeJS Buffer in its output message. :thinking:

You can use the Buffer's helper functions to read all kinds of different values from it, including numbers in little and big endian byte order.

In your case you would just need to call readFloatLE(offset) on that buffer.

If you are working with predefined data structures, you can also use the following node and decode it in one go into a nice JS object.

1 Like

thanks for the reply @kuema. But still im tryna figuring out how to do the task using this. Can I directly import modbus data to this binary node and do the conversion?

@rueben for your information.

Doing it in a function node is just fine. It was just another solution if you try to decode more complex structures.

Both solutions rely on the usage of Buffers, so you need to investigate which output data the Modbus nodes can provide. Use a debug node in "complete message" mode for that.

We used it to retrieve data from Siemens Sentron devices, but that's been a while ago. But AFAIR we used the raw buffer data for that. :sweat_smile:

oh okay thanks. does it have those little endian byte swap also ? I need basically those 4 formats. :slight_smile:

I don't quite understand what you mean by byte swaps. The endianess already determines the byte order. :thinking:
Which bytes are swapped?

If I'm not wrong, its like this,
Modbus has 4 basic types;
Big endian,
Little endian,
Big endian byte swap,
Little endian byte swap.

As I have shown in the 1st post.

Yes, I read that. But that doesn't clarify the technical background which bytes to swap. :sweat_smile:

1 Like

okay. its swapped like this.
Big Endian (AB CD), byte swap (BA DC)
little endian (DC BA), byte swap (CD AB)

That's weird, I've never seen that.

So the byte order of individual values (in your case 2 byte floats) is the same with LE/BE+swap, and BE/LE+swap respectively.

So you could just use the standard buffer methods like readFloatLE(offset), but read the buffer in the swap case from the end, instead of the beginning by using the offset. :thinking:

Don't know if that will work for other data types as well, I don't know your raw data.

1 Like

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