RS-485 modbus library

@peekay123 The Modbus master library you created uses a RS-485 communication (Modbus RTU). I can see you can set the number of the serial port. However, I am wondering where you define the pin of the spark/photon that controls the lines (DE / REneg)? Since RS-485 is a 2-wire balanced protocol you need these for not having multiple players writing at the same time on the line. Can you help me out with this please? Thank you

@GrtVHecke, a similar question was asked a while ago, the answer will still be the same

To “chip select” any of multiple transceifers you can choose any and as many pins you like - I’d say :wink:

@ScruffR @peekay123 There is some difference. What I am trying to say is that the library provide is missing the RE/DEneg functionality for RS-485. Simply a TTL/RS-232 modbus RTU implementation is useless. The whole idea of modes is to have multiple player on the line and therefor you need RS-485.
I just pointed out that this is missing in the library and therefor on no practical use. Modbus RTU is widely used, but RS-485 is required for it. best would be to adapt the library to enable and disable the driver on the line for reading and writing.

@GrtVHecke, you are correct. I had ported that library for another member. If you can find a more suitable library, I would be glad to port it for you :smiley:

@peekay123, that is actually no necessary, basically you need to add the following:

  1. Add the function rs485Read (as shown below)
  2. Make the DE (Driver enabled) and RENeg (Read Enabled negative) configurable DO pins
    When DE = HIGH, the driver is in "Write mode"
    When ReNeg = LOW, the driver is in “Read mode”
  3. At startup call rs485Read( true ) --> to put the device into listening mode (default)
  4. Before calling Serial.write or Serial.writeln call rs485Read( false )
  5. Before calling or Serial.available call rs485Read( true )

The rest of the library seems good (but it was only for TTL/RS-232 mode, which does not allow multi-drop communication and which is the whole point of Modbus).

void rs485Read( bool value )
if ( value )
digitalWrite( DE, HIGH );
digitalWrite( RENeg, LOW );
digitalWrite( RENeg, HIGH );
digitalWrite( DE, HIGH );

I hope this could help

1 Like

@GrtVHecke, I found what may be a more suitable library which works with the RS485. Take a look and tell me what you think. I should be straight forward to adapt to the Core/Photon.

@peekay123 Yes, this seems a suitable library. They use 1 pin to control read/write --> u8txenpin.
This pin is then connected to both the DE (Driver enabled) and RENeg (Read Enabled negative). The only limitation is that you can not take the device offline from the bus, but that is OK, 99,99% of the people (me included) use read as default more.

The big advantage of this library is that it implements a Modbus master as well as a modes slave.

So if possible, yes please port this library since I suppose this is wat everyone is looking for for RS-485 Modbus RTU communication.

1 Like

@GrtVHecke, I’ll try to get to it this weekend. Thanks for the feedback and hopefully you can test the new library :smiley:

1 Like

@peekay123 I will order a RS-485 transceiver at Farnell to be able to test it.

For your information, the dest device to test this with is a Moxa (works great) and I will use a VM to run Windows with ModbusPoll and ModbusSlave (both tools of

1 Like

@peekay123 The RS-485 transceivers are ordered (resistors are already on my desk) :wink:
I will try to test it somewhere during the evenings of net week if you have it
Have to say, I love the Spark community, just got actively involved the last week and I like the collaboration


@GrtVHecke, I have the RS485-slave example and library compiling for the Photon and the Core. I just need to post the code somewhere so you can test it. If the RS-485 stuff works then I can port all the examples and post everything to a repo. Dropbox may be a good start.


I’m interested in the RS-485 slave example and library that you mentioned you were compiling. Is there somewhere I could get that from you? Thanks

I am new to Photon, and want to connect a device with RS485 ModBus with he Photon.
I am interested to have the example and lib for the Photon you mentioned.
Would you mind sending me to use.

@mulderne, @Charlie001, you can find the files here:

The code is untested so let me know how it works! :smile:

Hi !

I’m also very interested in developing a library to connect through modbus… I’ve tried to check this librery (the one you shared @peekay123 but I couldn’t find it!

I also have some doubts concerning to the wiring between MAX485 (to convert to TTL levels) and Particle, do you know where can I find more information on this?

Thank you!

@peekay123 the link might need to be updated to a public folder containing the zipped code :wink:

1 Like

@ibon, try the dropbox again. It should allow you access to the 2 files there.

Could you link this to a public folder? Currently this link points to a private folder on your login.

1 Like

@mulderne, @Ibon, @Charlie001, sorry for the confusion. Here is a public link to the files :slight_smile:

Thanks :slight_smile: