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

**URL:** https://discourse.nodered.org/t/does-anyone-know-how-to-do-modbus-tcp-to-javascript-conversions/22902
**Category:** General
**Created:** [10 March 2020 03:41 UTC](https://discourse.nodered.org/t/does-anyone-know-how-to-do-modbus-tcp-to-javascript-conversions/22902 "2020-03-10T03:41:20Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)
#### Post date: [10 March 2020 03:41 UTC](https://discourse.nodered.org/t/does-anyone-know-how-to-do-modbus-tcp-to-javascript-conversions/22902/1 "2020-03-10T03:41:20Z")

</div>

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 ?  
 ![11111](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/e/7ed6d53dda6cc2fa6bacbf81d34652943befdac6.jpeg)

following code I used for **32 bit float - little endian byte swapped** and its working fine.  
 ![1111222](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/e/ae032005ae1f4294f3865c37a50acc815adeffbf.jpeg)

thanks in advance.

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [10 March 2020 05:44 UTC](https://discourse.nodered.org/t/does-anyone-know-how-to-do-modbus-tcp-to-javascript-conversions/22902/2 "2020-03-10T05:44:29Z")

</div>

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

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

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.

[https://nodejs.org/dist/latest-v10.x/docs/api/buffer.html](https://nodejs.org/dist/latest-v10.x/docs/api/buffer.html)

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.

> **[node-red-contrib-binary](https://flows.nodered.org/node/node-red-contrib-binary)**
>
> A Node-RED node to parse and serialize binary data

---

<div class="post-metadata">

### Author: ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)
#### Post date: [10 March 2020 08:42 UTC](https://discourse.nodered.org/t/does-anyone-know-how-to-do-modbus-tcp-to-javascript-conversions/22902/3 "2020-03-10T08:42:22Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)
#### Post date: [10 March 2020 08:43 UTC](https://discourse.nodered.org/t/does-anyone-know-how-to-do-modbus-tcp-to-javascript-conversions/22902/4 "2020-03-10T08:43:10Z")

</div>

@rueben for your information.

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [10 March 2020 08:50 UTC](https://discourse.nodered.org/t/does-anyone-know-how-to-do-modbus-tcp-to-javascript-conversions/22902/5 "2020-03-10T08:50:24Z")

</div>

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. 😅

---

<div class="post-metadata">

### Author: ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)
#### Post date: [10 March 2020 09:12 UTC](https://discourse.nodered.org/t/does-anyone-know-how-to-do-modbus-tcp-to-javascript-conversions/22902/6 "2020-03-10T09:12:55Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [10 March 2020 09:26 UTC](https://discourse.nodered.org/t/does-anyone-know-how-to-do-modbus-tcp-to-javascript-conversions/22902/7 "2020-03-10T09:26:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)
#### Post date: [10 March 2020 09:33 UTC](https://discourse.nodered.org/t/does-anyone-know-how-to-do-modbus-tcp-to-javascript-conversions/22902/8 "2020-03-10T09:33:38Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [10 March 2020 09:41 UTC](https://discourse.nodered.org/t/does-anyone-know-how-to-do-modbus-tcp-to-javascript-conversions/22902/9 "2020-03-10T09:41:17Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)
#### Post date: [10 March 2020 09:43 UTC](https://discourse.nodered.org/t/does-anyone-know-how-to-do-modbus-tcp-to-javascript-conversions/22902/10 "2020-03-10T09:43:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [11 March 2020 05:50 UTC](https://discourse.nodered.org/t/does-anyone-know-how-to-do-modbus-tcp-to-javascript-conversions/22902/11 "2020-03-11T05:50:03Z")

</div>

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. 🤔

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [10 May 2020 05:50 UTC](https://discourse.nodered.org/t/does-anyone-know-how-to-do-modbus-tcp-to-javascript-conversions/22902/12 "2020-05-10T05:50:11Z")

</div>

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