Checking number encoding?

Hi folks,
I have been messing with Modbus coms and have it working now.
However I am using buffer objects to change between number formats and realised I don't know how to check what format a number in node red actually is.

Obviously if I inject or read from somewhere a value that is not an integer then it must be a float, 32bit I assume.

However where a type isn't implicit, from how would I determine what the underlying structure is?

For example I have a node package for Victron equipment installed, some of those nodes return values that are always whole numbers but I suspect it is not safe to assume that those will be doubles whilst the majority of values are clearly floats.

I am used to PLC's and structured languages that require you to know, or define, how a value is to be represented and I am confused, probably missing something, as to how this stuff is handled in Node Red.

I would really appreciate someone telling me what fundamental nugget of information have manage to overlook this time :wink:


In JavaScript all normal numbers are floats. An integer is just a float that happens to be a whole number.

That is 64 bit float.

'O'... Well I wasn't expecting that...

So checking bounds will be easy I guess, just a min and max, huge ones at that.

Can I assume then that the make buffer node is always excepting a float64 and then constructing a buffer in the format that matches its input spec... I had it backwards in my head I think.

Assuming that is correct the reciprocal process must construct a 'value' from the underlying buffer structure, assuming the spec, and then cast that as a float64...?

Is that right or am I missing something?

This kind of stuff always worries me...
It could look OK and then break for some case I didn't even know needed testing if I get it wrong.

My plan is to always read and write floats or doubles on the PLC, because its simpler that way, well mostly.
There will need to be some error checking and of course all the Modbus data is in actuality int16/uint16 but to be honest I think it is easier to think of it as byte pares rather than values.

The memory in the PLC will be set up in blocks of defined types, and read that way as multiple values.
Generally I define floats, 32bit and ints16bit but usually leave the space to redefine values in a block containing ints as doubles. Doing it that way prevents inadvertently using the same word as part of a float and also an int... When bad things can occur !!

For this project the RPI/Node Red will be working as a DSP and using the PLC as semi intelligent remote IO so keeping all the reads/writes the same size, per symbol, in bytes makes sense.

At least I think it dose...

BTW how do I do a bitwise comparison of a word, or buffer UI guess, in node red?
The flexModbus nodes do not support coils so I will have to map everything to bits in a word in the PLC.
When updating that, I would like to read it first and `then just flip the bit/s I am interested in, which avoids the need to keep track of everything for every write on the Node Red end.
I know what I need/want, just not how to go about it in Node Red

Thanks for the clarification thus far,

What values do you want to check, and where are you doing it? If you have a number in JavaScript you don't need to check it as it is not possible for it to be out of range.

It is expecting a JavaScript number, so yes.

I haven't looked at the code, but imagine it is converting the byte sequence direct to a js number.

I am sure Google will answer that quicker than I can.

I suggest converting that to individual Boolean values in node red rather than bits in words. That will be more efficient in terms of processor usage. Either named variables or an array of values if appropriate.

Thanks Colin,
I didnt find much googling bitwise comparison but I was / am probly using the wrong terminology, and/or not recognisinthe answers for the same reason.
I will have another go.

An array of bools's I likeI hadnt considdered that, not sure how I will do the conversion but a simple loop in a function will work if ther is nothing built in to do the job.

Checking value bounds.... Its because I am writing back to the PLC. That has to be controlled or it will fail at some poit when I least need it too.
I anm stuck with the comunication standards and value storage constreignts imposed by the hardware.

The code will ultimatly be controlling four 6 kW DC links that bridge the ACsystems of a couple of hyrroelectric systems. It will read instrumentation data from various devices using modbus TCP and Modbus RTU do some basic calculation and arror checking and then update the control values in the three PLC's hat physically manage the equipment.

Obviously there will need to be safe dfaults and coms traffic checks to ensure that a code/quipmwnt failure isnt driving things ino an undesirabl state.

I know I am being a little detail focused but this is a bit more critical than my lounge lights doing to the wrong brightness !


I thought the buffer parser node would unpack bits for you.

Google for
JavaScript bitwise operators
Though if I am right about the parser you won't need them.

I will check out packing / unpacking with the buffer nodes, not sure why that didnt occur to me.
As for the google search I cant remember exactly wat I looked for but the first result with your suggested search got me th info I needed.
Interestingly bitwise operations only work on 32 bits so anything processed is convrted first and then converted back as an implicit part of the operation.

Thanks again.

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