How to get each value from modbus read node

2,483 views
Skip to first unread message

chris....@gmail.com

unread,
Sep 27, 2017, 4:13:59 AM9/27/17
to Node-RED
I use modbus read node to read register(ID=1,FC=3, Address=0, Quantity=10) and try to get each value through function node. However, it debug shows "Function tried to send a message of type number". However to solve it.

flow as follows.

[{"id":"fcbc3779.b529b8","type":"modbus-read","z":"6f3fbd9e.4f4b9c","name":"","showStatusActivities":false,"showErrors":false,"unitid":"1","dataType":"HoldingRegister","adr":"0","quantity":"10","rate":"1","rateUnit":"s","server":"d631304d.f42a4","x":377,"y":414.1600036621094,"wires":[["c9fb2a0f.12a118","601cbf63.33b9cc"],[]]},{"id":"6f3d0382.d932ac","type":"debug","z":"6f3fbd9e.4f4b9c","name":"","active":true,"console":"false","complete":"payload","x":830,"y":400,"wires":[]},{"id":"c9fb2a0f.12a118","type":"function","z":"6f3fbd9e.4f4b9c","name":"Get Array value","func":"var m = [];\n\nfor (var i=0;i<10;i++)\n{\n    m[i] = msg.payload[i];\n}\nreturn m;","outputs":"10","noerr":0,"x":623.7899780273437,"y":413.3399963378906,"wires":[["6f3d0382.d932ac"],["bedd1abf.66a488"],[],[],[],[],[],[],[],[]]},{"id":"bedd1abf.66a488","type":"debug","z":"6f3fbd9e.4f4b9c","name":"","active":true,"console":"false","complete":"payload","x":830,"y":440,"wires":[]},{"id":"601cbf63.33b9cc","type":"debug","z":"6f3fbd9e.4f4b9c","name":"","active":true,"console":"false","complete":"payload","x":550,"y":540,"wires":[]},{"id":"d631304d.f42a4","type":"modbus-client","z":"","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"}]
Auto Generated Inline Image 1

Nick O'Leary

unread,
Sep 27, 2017, 4:19:59 AM9/27/17
to Node-RED Mailing List
Function nodes must return message objects, not plain values.

For example:

var m = [];

for (var i=0;i<10;i++)
{
    m[i] = { payload: msg.payload[i] };
}
return m;

Regards
Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/d820f87a-45a3-44ee-9853-b82c27a8c1e7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

chris....@gmail.com

unread,
Sep 27, 2017, 9:46:54 AM9/27/17
to Node-RED
Thanks Nick. It's working now.

BR,
Chris

在 2017年9月27日星期三 UTC+8下午4:19:59,Nick O'Leary写道:
Function nodes must return message objects, not plain values.

For example:

var m = [];

for (var i=0;i<10;i++)
{
    m[i] = { payload: msg.payload[i] };
}
return m;

Regards
Nick

On 27 September 2017 at 09:13, <chris....@gmail.com> wrote:
I use modbus read node to read register(ID=1,FC=3, Address=0, Quantity=10) and try to get each value through function node. However, it debug shows "Function tried to send a message of type number". However to solve it.

flow as follows.

[{"id":"fcbc3779.b529b8","type":"modbus-read","z":"6f3fbd9e.4f4b9c","name":"","showStatusActivities":false,"showErrors":false,"unitid":"1","dataType":"HoldingRegister","adr":"0","quantity":"10","rate":"1","rateUnit":"s","server":"d631304d.f42a4","x":377,"y":414.1600036621094,"wires":[["c9fb2a0f.12a118","601cbf63.33b9cc"],[]]},{"id":"6f3d0382.d932ac","type":"debug","z":"6f3fbd9e.4f4b9c","name":"","active":true,"console":"false","complete":"payload","x":830,"y":400,"wires":[]},{"id":"c9fb2a0f.12a118","type":"function","z":"6f3fbd9e.4f4b9c","name":"Get Array value","func":"var m = [];\n\nfor (var i=0;i<10;i++)\n{\n    m[i] = msg.payload[i];\n}\nreturn m;","outputs":"10","noerr":0,"x":623.7899780273437,"y":413.3399963378906,"wires":[["6f3d0382.d932ac"],["bedd1abf.66a488"],[],[],[],[],[],[],[],[]]},{"id":"bedd1abf.66a488","type":"debug","z":"6f3fbd9e.4f4b9c","name":"","active":true,"console":"false","complete":"payload","x":830,"y":440,"wires":[]},{"id":"601cbf63.33b9cc","type":"debug","z":"6f3fbd9e.4f4b9c","name":"","active":true,"console":"false","complete":"payload","x":550,"y":540,"wires":[]},{"id":"d631304d.f42a4","type":"modbus-client","z":"","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"}]

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

steve rickus

unread,
Sep 27, 2017, 9:50:30 AM9/27/17
to Node-RED
This mistake seems to be happening with more frequency -- yet the function node info clearly says:

The function is expected to return a message object (or multiple message objects), 
but can choose to return nothing in order to halt a flow.

It seems that the main disconnect is that "message object" just sounds like a generic data term, and many newcomers are unfamiliar with Javascript objects and terminology.

Not that I have the answer, but perhaps the corollary to this needs to be more strongly stated as well?

N.B. Returning any javascript primitive (string, number, boolean) will cause an error in your flow!
Instead, that value would typically be set as the payload of the returned msg object.

Alternately, could the function node itself detect the returned type and warn the user? I'm thinking of two places this could possibly happen...

* can the ACE editor validation recognize that the returned var is a primitive type, and show a warning?
* could the runtime sandbox check the typeof the returned object, and either warn the user, or just set the payload to that value?

I'm sure if this was easy to do, it would have already been implemented... I'm just interested in the discussion of how to make it more bullet-proof.
--
Steve

Nick O'Leary

unread,
Sep 27, 2017, 10:35:59 AM9/27/17
to Node-RED Mailing List
Steve,

the Function already checks the types of messages return. For example: return "hello" will result in an error being logged:
"Function tried to send a message of type string"

The ACE editor doesn't do type checking, so no, there's no easy check to be done there.

No doubt the node help text could be expanded a bit to make it clearer.

Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
Reply all
Reply to author
Forward
0 new messages