RS-485 modbus library


@peergum you’re a genius man. The solutions so intuitive too. I was too focused on the library. Thanks alot!


If that works, I’ll probably add the option to pass parity (e.g. setParity method).


Yep, your solution worked great. :smile:
I also noticed that you added the set slave ID functionality. I was thinking about daisy chaining a few of these devices together but I am confused about how slave IDs work for multiple devices. I’ve only been working with one device at a time so that is easy to address when calling the constructor. However, having multiple slave devices seems more complicated. How do I set which device to have which slaveID? After that’s established, I am guessing I can easily call your setSlave function to switch slaves. One last thing is, I’m not sure how you would wire these devices up either, since all the connections are just A and B.


you have to configure the IDs on the devices themselves. Then you code will address each device by using corresponding ID. There’s no automatic configuration/discovery, although you can scan for devices using successive IDs, if you know a common register they use (e.g. serial number), so you have to know in advance what IDs are used on your bus.

As far as chaining them, you should check some modbus doc online, but note you will have to use terminations on each end of your chain. Note that having more than one device on the bus is generally a source of noise and require more care. Also any device you connect to the bus as to use short wires. In other words, do short "T"s, not long "Y"s :wink:


I updated the library on github and particle, both with a speed improvement and an optional additional parity parameter to the setSpeed() method.

I also added a simple example on how to use the lib.


Thanks for all the help! I think I’ll have to scan through Slave ID’s and see if that works for me. However, forgive me if I’m wrong, wouldn’t putting two identical devices on the same modbus line have conflicting slave ID’s (since they come pre configured with it).

In terms of your library, is there a reason why you went with the custom pre/post transmission functionality for 485 chips as opposed to the other Particle IDE library (found at which simply has an enableTXPin function which handles the pre/post transmission stuff. Sorry for all the questions.


You’ll have to connect them one by one and configure their ID with a program, unless they have a panel to configure them.

As mentioned earlier, I wanted to be able to act on the TX and ^RX pins of the MAX separately, so I could lower both simultaneously and put the MAX in sleep mode, which you can’t do if you wire them together. I use the pre- and post- transmission function to act on the two pins. If saving battery is not a string requirement for you you could use the other library I guess.


I like your library because I would like the ability to set the slave ID which your library offers. The only issue I’m having right now is receiving messages back, they’re all timing out (error E2). When I write to a single register, it seems to work as the Slave will do the command but the return value will always be a timeout. Any suggestions on why this may be the case?

The only thing my pre and post transmission functions do is digitalWrite max485 enable pin high and low respectively. Is this incorrect?


I suggest you enable debugging and monitor on serial usb. That should give you some clues. Also add something like delay(10) in your idle function. It will wait 10ms before checking received bytes. If that works you can always tweak later to a shorter delay. If you want to try longer timeouts change the value in the modbusmaster.h file.


Thank you for your suggestions and I tried them all but did not work. However, after looking at the differences between the library I used before and your one, I noticed that previously the pin setup was done for me (as in pinMode) and all I had to do to fix the issue was pinMode(enablepin, OUTPUT). How silly of me and sorry for the bother. Again, thanks for the library, very useful :smiley:


@DriftingShadows, @peergum,
Thanks for the discussion and library. I am sure it will help me getting modbus working on my project.
Do you have any example sketches showing good practices using this library to respond to commands, or to copy registers to structures/variables and vice versa, etc?

My slave device has EEPROM registers defined for some config variables and I need to be able to update them by Modbus as well as through a web app. I also have a couple of sensor readings that I want to make available on modbus.

FWIW, I am using serial4 (pins C2, C3) rather than serial1, with DE,RE tied together to C4.