How to make Baudrate adjustable in MODBUS code.

Here is simple code. I have used Modbus library of arduio . How can make slave baud rate adjustable .

current code working fine with 9600. But i want to keep option where user can select his own baudrate 9600,19200, 57600 etc

#include "Arduino.h"
//#include "modbus_library_here"

#define modbus_ID 1                    // modbus slave ID:

#define mux_chan_0_pin 5               // pin for MUX 0:
#define mux_chan_1_pin 4               // pin for MUX 1:
#define mux_chan_2_pin 3               // pin for MUX 2:
#define sensor_25_pin A3               // pin for 'current' sensor number 25:
#define sensor_volts_pin A4            // pin for one and only volatge sensor, scale is ....:
#define temp_sensor_pin A5             // pin for temperature sensor, scale is ....:

int8_t state = 0;                      // must be the state of the Modbus poll result:
uint16_t mb_reg[30];                   // array of uint16 to hold values to be sent out by Modbus:

uint32_t  last_loop = 0;               // current value of loop timer:
const uint32_t period_loop = 1000;     // loop timer preset value, every 1 second:

Modbus slave(modbus_ID, 0, 0);         // this configures the Modbus instance:

void read_sensors() {
  for(uint8_t chan_count = 0; chan_count < 8; chan_count++)
  {
    digitalWrite(mux_chan_0_pin, (chan_count & 0x01) ? HIGH : LOW);        // set MUX channel 0 according to channel count:
    digitalWrite(mux_chan_1_pin, (chan_count & 0x02) ? HIGH : LOW);        // set MUX channel 1 according to channel count:
    digitalWrite(mux_chan_2_pin, (chan_count & 0x04) ? HIGH : LOW);        // set MUX channel 2 according to channel count:

    delay(2);  // a short delay to let the MUX settle before reading analog channel:

    mb_reg[chan_count] = (uint16_t)((analogRead(A1) - 512) / 0.512);       // sample analog A1, remove offset and scale to 250 Amps:
    mb_reg[chan_count + 8] = (uint16_t)((analogRead(A1) - 512) / 0.512);   // sample analog A1, remove offset and scale to 250 Amps:
    mb_reg[chan_count + 16] = (uint16_t)((analogRead(A2) - 512) / 0.512);  // sample analog A2, remove offset and scale to 250 Amps:
  }

  mb_reg[24] = (uint16_t)((analogRead(sensor_25_pin) - 512) / 0.512);      // sample and remove offset and scale to 250 Amps:
  mb_reg[25] = analogRead(sensor_volts_pin);                               // sample as voltage sensor, need scaling data:
  mb_reg[26] = analogRead(temp_sensor_pin);                                // sample as temperature sesnor, needs scaling:
}



void setup() {
  slave.begin(9600);    // which serial will this use:
//  Serial.begin(9600);   // this will be the default async serial port:

  pinMode(mux_chan_0_pin, OUTPUT);  // pin 0 for MUX:
  pinMode(mux_chan_1_pin, OUTPUT);  // pin 1 for MUX:
  pinMode(mux_chan_2_pin, OUTPUT);  // pin 2 for MUX:

  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(sensor_25_pin, INPUT);
  pinMode(sensor_volts_pin, INPUT);
  pinMode(temp_sensor_pin, INPUT);
}


void loop() {
  if (millis() - last_loop >= period_loop)  // ye good old delay without delay:
  {
      read_sensors();
    //  print_result();
      last_loop = millis();                 // reset our loop timer value:
  }

  mb_reg[27] = slave.getInCnt();            // store in count:
  mb_reg[28] = slave.getOutCnt();           // store out count:
  mb_reg[29] = slave.getErrCnt();           // store error count:

  state = slave.poll(mb_reg, 30);           // maybe this actually polls the modbus channel, but who knows:
}

How can make slave baud rate adjustable

Put the baud rates in an array

long baudRates[] = {9600,19200, 57600};

Prompt the user to enter 0 to 2 then

slave.begin(baudRates[userInput]);

Whether i need to define user input?? what is declartion for userinput

Prompt the user on the Serial monitor, then read the number they enter and use it as the array index. This should help Serial input basics

Here i have attached my complete code. The project is about hall sensor measurement using Mux IC. I used MODBUS library. The code seem working fine.

When i used MODSCAN32 to monitor i get error saying.

modbus message timeout

The code run fine for 10 minute without any issues. After that it gives out error. Can someone suggest me how to resolve this

Final_code2.zip (8 KB)