Help in understanding output from Modbus node

I have read and re-read all the posts here about Modbus nodes and their output, but I am truly stumped as to what I am seeing.

In CAS Modbus scanner, I get:

while in Node-RED (taken a few minutes later, and trying to read the same registers), I get:

Originally I thought Node-RED was outputting 16-bit integers or something, but I do not think so. For what it's worth, the values I see on the controller that I am trying to read are approx 1143 and approx 101, so CAS Modbus scanner is clearly seeing the correct values, but Node-RED is apparently not.

Node red is outputing 16 bit registers, but the debug is showing each byte as decimal. So the first number has ms byte 4 and ls byte 119, which is 4*256 + 119 = 1143 which is exactly what you expected. To generate the values in a function node you can do

let v1 = msg.payload[0] << 8 + msg.payload[1]

however I think there as some javascript functions that will do that for you more concisely and also at least one contrib nodes but I don't remember what they are.

Thank you Colin! This is exactly the help I needed.

This return from the modbus node is a a nodejs buffer - this is quite typical of many PLC type nodes.

A buffer is ideal for data where the endianness of the data may or may not be the same on the receiver (node-red in this case)

additionally, nodejs buffer has many built in functions that permit easy conversion from byte to int16 / uint16 / float / int32 etc (in both big endian and little endian)

If you are not much of a coder I wrote a contrib node that does most of the heavy lifting.


[{"id":"a1be6544.008218","type":"buffer-parser","z":"553814a2.1248ec","name":"","data":"payload","dataType":"msg","specification":"{\"options\":{\"resultType\":\"object\",\"singleResult\":true},\"items\":[{\"name\":\"MB30121\",\"type\":\"int16\",\"offset\":0,\"length\":1},{\"name\":\"MB30122\",\"type\":\"int16\",\"offset\":2,\"length\":1}]}","specificationType":"json","x":970,"y":240,"wires":[["3c8ca47e.9ffb7c"]]},{"id":"a59c2b8.5d451d8","type":"inject","z":"553814a2.1248ec","name":"Fake Modbus DATA [4,119,4,118]","topic":"","payload":"[4,119,4,118]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":910,"y":200,"wires":[["a1be6544.008218"]]},{"id":"3c8ca47e.9ffb7c","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","x":990,"y":280,"wires":[]}]

Thank you Steve. That node does the trick nicely. In fact, I am trying to read quite a few registers (not just registers 121 and 122). So I figured I could change the code in your Node to add register 123, for example:
Node JS integer conversion
but it does not work (gives me SyntaxError: Unexpected token { in JSON at position 351 and when I go back to the node editor, it lost the formatting, i.e. looks like this { "options": { "resultType": "object", "singleResult": true }, "items": [ {

so I have obviously wandered above my knowledge grade. I can still make it work with the array[2] node you provided.

The real end game is to get a whole slew of values from the device (shown in bold below), which I believe will be possible with your node if I can get the syntax correct.
|122|Probe Sensor mV value
|123|Process Variable
|124|Dew point value
|125|Probe Temperature
|126|CO measured value
|127|Probe Resistance value
|128|Probe Response Time
|129|Probe Temperature at last Probe Test
|130|Main Control Output Value
|131|Secondary Control Output Value
|132|PID Out Value - % Output|
|133|Main Control Ouput Display value
|134|Secondary Control Output Display Value
|135|Device Status Mode
|136|Type of Operative setpoint
|137|Operative Setpoint value
|138|Remote Setpoint value
|139|Not Used
|140|Not Used
|141|Not Used
|142|CO Factor
|143|H2 Factor
|144|%O2 process variable

The syntax error is you have a trailing comma after the new item making the json invalid.

The specification has many options - read the built in help on the info side bar & check out the built in examples (on the import examples dialog (CTRL+I) )

for example, if you wanted to send the data to mqtt, you could set the option singleResult to false & add a topic to each item & the node will generate separate messages that you can pipe directly into an MQTT out node.

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