How to configure this Modbus example to use Serial3 instead of serial

Hi,

I'm new to arduino programming, using arduino MAX, but i'm not sure to see where to change the serial port used for the arduino Modbus library ?

How to configure this Modbus example to use Serial3 instead of serial.

Here is the link for the arduino modbus official library but i don't see where to put Serial3 instead of serial0.

Thanks
Best regards
Mickael

/*
  Modbus RTU Server Kitchen Sink

  This sketch creates a Modbus RTU Server and demostrates
  how to use various Modbus Server APIs.

  Circuit:
   - MKR board
   - MKR 485 shield
     - ISO GND connected to GND of the Modbus RTU server
     - Y connected to A/Y of the Modbus RTU client
     - Z connected to B/Z of the Modbus RTU client
     - Jumper positions
       - FULL set to OFF
       - Z \/\/ Y set to OFF

  created 18 July 2018
  by Sandeep Mistry
*/

#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>

const int numCoils = 10;
const int numDiscreteInputs = 10;
const int numHoldingRegisters = 10;
const int numInputRegisters = 10;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println("Modbus RTU Server Kitchen Sink");

  // start the Modbus RTU server, with (slave) id 42
  if (!ModbusRTUServer.begin(42, 9600)) {
    Serial.println("Failed to start Modbus RTU Server!");
    while (1);
  }

  // configure coils at address 0x00
  ModbusRTUServer.configureCoils(0x00, numCoils);

  // configure discrete inputs at address 0x00
  ModbusRTUServer.configureDiscreteInputs(0x00, numDiscreteInputs);

  // configure holding registers at address 0x00
  ModbusRTUServer.configureHoldingRegisters(0x00, numHoldingRegisters);

  // configure input registers at address 0x00
  ModbusRTUServer.configureInputRegisters(0x00, numInputRegisters);
}

void loop() {
  // poll for Modbus RTU requests
  ModbusRTUServer.poll();

  // map the coil values to the discrete input values
  for (int i = 0; i < numCoils; i++) {
    int coilValue = ModbusRTUServer.coilRead(i);

    ModbusRTUServer.discreteInputWrite(i, coilValue);
  }

  // map the holiding register values to the input register values
  for (int i = 0; i < numHoldingRegisters; i++) {
    long holdingRegisterValue = ModbusRTUServer.holdingRegisterRead(i);

    ModbusRTUServer.inputRegisterWrite(i, holdingRegisterValue);
  }
}

Unfortunately, there is currently no way to do this from your sketch. The developers are aware that this functionality is needed in the ArduinoModbus library and there are a couple of outstanding proposals to do this:

For now, the easiest solution is to modify the ArduinoRS485 library. Here are the instructions:
File > Examples > ArduinoRS485 > RS485Passthrough

Sketch > Show Sketch Folder. This will open the folder containing the RS485Passthrough example sketch, which will allow you to easily find the ArduinoRS485 library's source code files.

Navigate up two folder levels to the ArduinoRS485 folder.

Open the src subfolder.

Open the file RS485.cpp in a text editor.

Change the last line of the file from:

RS485Class RS485(SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS845_DEFAULT_DE_PIN, RS845_DEFAULT_RE_PIN);

to:

RS485Class RS485(Serial3, 14, RS845_DEFAULT_DE_PIN, RS845_DEFAULT_RE_PIN);

Save the file.

This is untested, but it should work. If not, let me know and I'll do further investigation.

I should note that I assumed you meant Arduino Mega when you said "arduino MAX", since I've never heard of a board called Arduino MAX, and didn't find anything about it in a quick Google search. So I set the TX pin for Serial3 to pin 14 in my instructions. If you are using another board with a different TX pin for Serial3 then you'd need to modify the instructions accordingly

Hi,
Thanks a lot, yes it's arduino MEGA, sorry.

Thanks, i seems little complicated for me so i will see if i can continue tu use serial0 for now.

Best regards

You're welcome.

If you end up needing to switch to Serial3 later and there is anything in my instructions that doesn't make sense, feel free to come back here with some specific questions and I'll see if I can help out.

I don't have much experience with RS-485 and Modbus yet but I do have a MKR 485 Shield as well as some of the common Chinese modules so I've been meaning to do some experimentation to gain familiarity with it and those Arduino libraries.