Hi all,
I have a private node i use for converting (mostly PLC) data into usable values for various projects. I would like some fresh eyes and thoughts.
FIRSTLY: about that name - as you read through the below & understand its capabilities - if you can think of a much nicer name - i'm open to suggestions
Foreword, background & reasoning...
- I work with PLCs and industrial devices and much of the type the data I get is a buffer or array of 16bit integer
- Often, I need to convert various parts of this data to BCD, UINT16LE, FLOATLE etc.
- I must have read 50 times (especially MODBUS threads) "how do I get a 32 bit integer" "but its not the right way round" etc etc
- Depending on the equipment, the data is often byteswapped, sometimes it is even necessary to swap32 followed by swap16 to sort the data
- Many times I need to get flags (bits/booleans) from a WORD but also need the whole word as an integer too
- sometimes...
- I need a simple array of values created from the data
- I need a object with named properties (for easy access)
- I need individual
topic
/payload
s generated (for easy sending to MQTT) - other times i just want the data as a buffer in the right order
Inputs...
- data - a buffer (of bytes) or an array (of 16bit integer)
- specification - an object specifying what you want to do with it
Options...
- The specification can instruct the node to
- byteswap 16, 32, 64. Not only once but you can actually tell it to swap32, then swap64, then swap16 (and so on) if thats what is required
- extract bits, booleans, ints, uints, floats, doubles all with BE and LE options
- can re-use any part of the buffer e.g.
- take 2 bytes starting at offset 0 & create a UINT16BE and also take the same 2 bytes starting at offset 0 & create a UINT16LE and also take 4 bytes starting at offset 0 & create a INT32BE and so on
- can get a bit (or bit array) from data starting at a byte offset & bit offset
Outputs 4 formats...
- value - just the values you specified in an array
-
object - a named object contaiing
.vaue
and other parts of your specification to help you correlate the value (provides context to your value) - array - a combination of above 2 (an array of context objects)
-
buffer - no processing other than byteSwap(s). This is useful for ...
- turning an array into a buffer
- simple byteswaps
Outputs options...
- can return a single result with all conversions
- can return multiple results with a topic & payload for each
item
in your specification
Some Samples
Typical PLC Data (array 16bit) converted to various formatted values
The data...
[25185,25699,26213,26727,27241,27755,28013,28783,29297,29811,30325,30839,31353,256,512,768,1024,1280,1536,1792,2048,2304,2560,2816,3072,3597]
specification...
{
"options": {
"byteSwap": [
"swap16"
],
"resultType": "value",
"singleResult": false,
"msgProperty": "data"
},
"items": [
{
"name": "plc1/production/alphabet",
"type": "string",
"offset": 0,
"length": 26
},
{
"name": "plc1/production/status/counts",
"type": "int",
"offset": 4
},
{
"name": "plc1/production/status/sequence",
"type": "bcd",
"offset": 4,
"length": 5
},
{
"name": "plc1/machine/status/runners/temperature",
"type": "int16le",
"offset": 26,
"length": 6
},
{
"name": "plc1/machine/status/runners/speed",
"type": "int16be",
"offset": 26,
"length": 6
},
{
"name": "plc1/machine/status/flags",
"type": "bool",
"offset": 0,
"length": 32
}
]
}
Output...
Using same PLC Data Sample...
different specification...
{
"options": {
"byteSwap": [
"swap16"
],
"resultType": "object",
"singleResult": true,
"msgProperty": "data"
},
"items": [
{
"name": "alphabet",
"type": "string",
"offset": 0,
"length": 26
},
{
"name": "single byte pos 4",
"type": "int",
"offset": 4
},
{
"name": "bcd equiv",
"type": "bcd",
"offset": 4,
"length": 5
},
{
"name": "Array[6] of int16le",
"type": "int16le",
"offset": 26,
"length": 6
},
{
"name": "Array[6] of int16be",
"type": "int16be",
"offset": 26,
"length": 6
},
{
"name": "32 bools",
"type": "bool",
"offset": 0,
"length": 32
},
{
"name": "Array[4] of 16bits",
"type": "16bit",
"offset": 0,
"length": 4
}
]
}
Output...
The built in help
continued
Wrap up
Please feel free to chip in.
- A nicer name suggestion?
- better option names?
- additional functionality?
- better help info?
final notes
- i realise a UI editor would improve this (an when I get time I may add that) but for now, I have made it so the specification can be typed in or passed in (via msg, flow, global etc) - at some point in the future, these can/will be mapped to a UI editor