ModBus TCP Read 32-bit registers

@eduardotodero I usually create a new Uint16Array with the 16-bit values and then create a Float64Array with the Uint16Array's buffer:

var ui16a = new Uint16Array([1,2,3,4]); // your 16-bit integers here
var fl64a = new Float64Array(ui16a.buffer);
console.log(fl64a[0]);

Or, if you need more flexibility, get familiar with DataView, specifically you would end up using: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getFloat64

Edit: missed if you were talking about 64-bit integers or floating-point numbers -- in any case, you can use typed arrays: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays to do what you want. You may want to create a Int64Array, for example. With these, you can convert an entire block of a certain type in one shot.

5 Likes