Modbus <-> MQTT bridge for node-red

4,664 views
Skip to first unread message

nzfarmer

unread,
Feb 12, 2015, 6:55:48 PM2/12/15
to node...@googlegroups.com
As protocols go, Modbus is an older synchronous one that doesn't necessarily lend itself to the inherently asynchronous world of node-red and mqtt messaging.

However, a modbus-in, modbus-out, and modbus-config nodes are high on my list of nice to haves right now.

Anyone know of a project or code in existence? If not I'll roll my own.

thanks

Edward Vielmetti

unread,
Feb 12, 2015, 7:07:38 PM2/12/15
to node...@googlegroups.com
There's some MODBUS code in node.js here:


I'm interested in whatever you come up with.

--
http://nodered.org
---
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.
For more options, visit https://groups.google.com/d/optout.



--

nzfarmer

unread,
Feb 12, 2015, 9:07:17 PM2/12/15
to node...@googlegroups.com
Cool I'll take a look. My initial thoughts were to sit on top of this one:


As for the UI, there is nice mapping of Function code type to input or output.

How we map multiplier register reads is a different question.

Lawrence Griffiths

unread,
Feb 13, 2015, 7:06:34 AM2/13/15
to node...@googlegroups.com
That Git is quite old

You might want to look at these in NPM they both have updates in the last few months and regular downloads

And if your doing just TCP

>>How we map multiplier register reads is a different question
The the modbus-tcp libary returns an array

nzfarmer

unread,
Feb 14, 2015, 2:43:42 AM2/14/15
to node...@googlegroups.com
Great to see some interest and alternatives for the protocol stack.

Would appreciate some use cases and any thoughts on error handling with an async context. 

Naive thoughts for the config node are:

1. a flag to denote input or output.
2.  function code -  limited to read or write mode i.e a drop down for modbus-in would provide:

  • readCoils
  • readDiscreteInputs
  • readHoldingRegisters
  • readInputRegisters
3. the start register
4. number of bytes/words (if applicable)
5. data format (string, int, etc.)
6. name of topic (overwritten by register address if null - payload of course becomes the register value for the input node)

Note: the above parameters will be constrained by the function code.





Lawrence Griffiths

unread,
Feb 14, 2015, 6:10:59 AM2/14/15
to node...@googlegroups.com
All good have few questions

A.  1. a flag to denote input or output. not sure of the why you need to know?
B.  4. number of bytes/words With the modbus interfaces I've used you specify length & the interface does the rest 

nzfarmer

unread,
Feb 14, 2015, 2:30:03 PM2/14/15
to node...@googlegroups.com
A.  1. a flag to denote input or output. not sure of the why you need to know?

Was just looking re use code for the Modbus in and Modbius out nodes. I noticed in node-red that you can add default properties when defining a config node so we can pass a flag and that controls the UI setup to be in modbus-in or modbus-out mode.

B.  4. number of bytes/words With the modbus interfaces I've used you specify length & the interface does the rest
 
Ok cool. I'll check a few more UIs.  My experience with Modbus has been mainly via APIs.  From memory there are a few free clients / simulators about 


Dave C-J

unread,
Feb 14, 2015, 2:44:36 PM2/14/15
to node...@googlegroups.com
The config node should (IMHO) be about the common bits ie. the shared connection, underlying hardware ports, etc... anything in or out specific should be in the in or out nodes themselves ... but I may be missing something. (don't know Modbus :-)

nzfarmer

unread,
Feb 14, 2015, 3:37:42 PM2/14/15
to node...@googlegroups.com
Good point.  Some form elements will be specific to each node.  But then there is question of the actual registers.  Should these be cofig nodes?  hmmm.

nzfarmer

unread,
Feb 20, 2015, 3:56:46 PM2/20/15
to node...@googlegroups.com
This is where I've got to on the spec.  Am searching from some time to be able to start on this as we are close to the release of our new service.

Any one interested in crowd funding / sponsoring the work let me know.

Latest spec below - just a brain dump really so feel free to add comments.  It struck me that modbus-in can work in either slave or master mode, the only difference is whether it polls its own registers from a listening slave, or an external slave. 

Overview: 

- Create 3 nodes:  modbus-in; modbus-out; modbus-config.  
- Use modbus node: https://www.npmjs.com/package/modbus for protocol stack
- This uses the node libmodbus libraries:  https://github.com/tuxnsk/nodejs_libmodbus
- modbus config would have default settings to assist in its form creation and would expose the context and connection/config parameters.

.html for config nodes
- Modbus TCP needs to be unique by IP Address and Port, Max connections (default 1)
- Modbus RTU needs to be unique by slaveId, device and baud
- Specify whether slave or master (modbus-in only)
- Config node needs a name and label identifying type and paramaters.

- both config nodes expose their connection params, name and ctx (connection object), mode (slave / master) - see server.js example

.js for config nodes
Create a modbus slave server; Estabish and listen to connection; or warn.  Make ctx available to owner node.
For slave mode need to be able to bind to address
For master mode, needs to be able to handle disconnects and reconnect

.html form elements for modbus-in  node-red html interface

- Name (required)
- Topic - default function code + register i.e. 40001, or overwritten by mqtt topic definition
- Function Code (limited to read for read/write registers ) required
- Register value (uint 0-9999) required
- Length (uint number of 16bit registers - default 1) required
- Type ( integer , decimal(float), hex, bit ) use radio buttons. default integer?
- Poll interval (milliseconds)
- Inject message even if unchanged (checkbox)?

.js for modbus-in

require modbus
on start and poll
check value of register(s) defined in html - ctx.getReg
if new or changed (and report if unchanged)
format register value as type (use JSON [address,value] or [[addressx,value],[addressx+1,value]] where length >1)
send message

Outputs 1. Topic + Payload (register value + length cast as type)

.html form elements for modbus-out node

- Name (required)
- Function Code (uint limited to write only or read/write registers ) not mandatory
- Register value (uint 0-9999) not mandatory
Note: above paramaters can be overwritten by incoming topic if not set. i.e. 40001
- Type ( integer , decimal(float), hex, bit )

.js logic for modbus-out

listen for on('message',...)
perform any type casting operations and validations - i.e. isInt()
derrive function code and register from topic and/or node params
perform ctx.setBit,ctx.setReg operations etc. based on html form elements
flag any errors

Inputs 1:Topic (i.e. 40001) +  Payload (value)
Outputs 1. Logging of operation




Message has been deleted

Lawrence Griffiths

unread,
Feb 21, 2015, 1:55:37 PM2/21/15
to node...@googlegroups.com
Andrew, a quick response

What is your hardware platform?
If it's a Raspberry PI then for RTU//ASCII you will need to a TTL serial to 4 wire RS422 and 2 wire RS485 converter.

In the past I've used an cheap Modbus TCP to RTU/ASCII device this one is 80 GBP 

That way it becomes a lot simpler you just need to support Modbus TCP.
The converter can be set to RUT master or slave.

As to crowd funding I've considered that before for NodeRed.
What there doesn't seem to be is a Kickstarter for ad hoc projects.
Would be happy to make a small contribution to TCP support.

Also with Mobus TCP you get access to a lot of Protocol converters
RTA make some good converters these give supply NZ  
Example TCP Client to BACnet client

Lawrence

nzfarmer

unread,
Feb 21, 2015, 5:27:43 PM2/21/15
to node...@googlegroups.com
Thanks Lawrence

Let's exclude RTU from scope for now.

Have created Github project and will canvas out source for market for quote.

See:

Mika Karaila

unread,
Feb 25, 2015, 7:41:43 AM2/25/15
to node...@googlegroups.com
I have already working implementation from the last year. I will make presentation how to connect MODBUS and OPC UA.
Presentation will be 18.3.2015, look: http://xxi.automaatioseura.fi/
I will provide examples in April...

Lawrence Griffiths

unread,
Feb 25, 2015, 9:26:25 AM2/25/15
to node...@googlegroups.com
Mika good news on both front's.
I've seen one company implement some basic OPC UA features in a node function.
Assume you've created NR nodes for both Modbus and OPC UA ?

Lawrence

Mika Karaila

unread,
Feb 25, 2015, 11:55:41 AM2/25/15
to node...@googlegroups.com

Yes and some other, it is just so easy to reuse them. ..
There are additional tricks that I found.

--
http://nodered.org
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/1M9U-srsgss/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+u...@googlegroups.com.

nzfarmer

unread,
Feb 25, 2015, 2:47:51 PM2/25/15
to node...@googlegroups.com
That's great Mika

Any chance of posting your code somewhere now so that we can decide what approach to take?

Mika Karaila

unread,
Feb 26, 2015, 11:24:30 AM2/26/15
to node...@googlegroups.com
I´m actually on winter vacation this week, I will check this next week.
Perhaps I can upload modbus implementation + some instructions to github.
BUT I would like to refactor it. I would like to use more topic than special fields in the msg...

Br,
Mika

On Wed, Feb 25, 2015 at 9:47 PM, nzfarmer <southwebn...@gmail.com> wrote:
That's great Mika

Any chance of posting your code somewhere now so that we can decide what approach to take?

--

Mika Karaila

unread,
Mar 3, 2015, 4:10:56 PM3/3/15
to node...@googlegroups.com

Almost done, one problem pop out as I extended and refactored code.
I am using jsmodbus and I had to modify it...
I will check fi it is possible to leave something out and use original code.
Br,
Mika

nzfarmer

unread,
Mar 16, 2015, 5:53:39 PM3/16/15
to node...@googlegroups.com
Hi Mika

Any update on this?

Thanks

Mika Karaila

unread,
Mar 17, 2015, 1:25:57 AM3/17/15
to node...@googlegroups.com
I sent you zip last night, did not you receive that ?

--

nzfarmer

unread,
Mar 17, 2015, 3:41:23 AM3/17/15
to node...@googlegroups.com
Hi Mika

Thanks for that!

No not yet? I'll send another email address to you privately.

Bluefox Fox

unread,
May 18, 2015, 3:40:30 PM5/18/15
to node...@googlegroups.com
Hello there,
What is the status? Stopped or will be developed?

Thank you,
Bluefox

вторник, 17 марта 2015 г., 8:41:23 UTC+1 пользователь nzfarmer написал:

Mika Karaila

unread,
May 19, 2015, 4:25:33 AM5/19/15
to node...@googlegroups.com
Working, but only on evenings. I would like to solve function codes 15 & 16. I just do not have much time to work with this now. After a week or two I will look again...
Reply all
Reply to author
Forward
0 new messages