How to swap TX-RX pins in RS-485 serial

I am using the following board

That make life a lot easier. No need to follow my earlier link.

You just need to use the following (based on https://github.com/CMB27/ModbusRTUSlave/blob/main/examples/ModbusRTUSlaveExample/ModbusRTUSlaveExample.ino)

#define MODBUS_SERIAL Serial2
...
...
ModbusRTUSlave modbus(MODBUS_SERIAL, dePin);

I'm already doing this, but it's still not communicating.... see

line15,

lines 18/19/20,

line 84,

lines 86/87,

lines 140,141,142 and

line 324

[code]
/* GEX-12D Version II
 *  Firmware version: V0.8 Date: Aug 26, 2025
 *  a) Estabelecer IOs digitais e analógicos e testá-los
 *  b) Estabelecer os IOs digitais que serão usados com interrupção - Nota: Não há necessidade de usar macro digitalPinToInterrupt, pois todos os pinos suportam interrupção (usar attachInterrupt)
 *  c) Testes iniciais de movimentação do motor Me2 do disjuntor - ok
 *  d) Início dos testes de movimentação da gaveta
 *  e) teste com a estrutura "while" para ver se facilita o controle da gaveta
 *  f) A partir da V0.7 testes para comunicação com placa CPU - Protocolo Modbbus RTU (CPU -> Master & driver -> Client)
 *  g) Versão V0.7 - início dos testes com comunicação RS-485 - Protocolo Modbus RTU - Slave

*/
#include <DipSwitch.h>
#include <ModbusRTUSlave.h>                                       // Incluido na versão V0.7
//
#define MODBUS_SERIAL Serial2
//
// You can change the baud, config, and unit id values if you like.
#define MODBUS_BAUD 9600                                                          // Era 38400
#define MODBUS_CONFIG SERIAL_8N1
#define MODBUS_UNIT_ID 1
//
// Define the digital and analog pin connected to the board
#define debugBt     13
#define fdcCbOn     14
#define fdcCbOff    15
#define fdcCbEnb    16
#define fdcInterLk  17
#define fdcDoor     18
#define fdcINS      19
#define fdcEXT      20
#define fdcTST      21

#define btCbOn      8
#define btCbOff     9
#define btTST       10
#define btINS       11
#define btEXT       12
#define ledCbOn     36                        // RED LED
#define ledCbOff    35                        // GREEN LED
#define ledINS      34
#define ledTST      37
#define ledEXT      38
#define debugLed    39

#define enbM2       3
#define phM2        4
#define inlM1       5
#define inhM1       6

#define isM1        A8        // 30 PE0
#define ipropoiM2   A9        // 31 PE1    

int dipSwitchPins [] = {22,23,24,25,26,27,28,29};
DipSwitch myDipSwitch (8, dipSwitchPins);
//
// ------------------ Variables -------------------------------------------------------------------------------------------------------------------------------------------------------------
int verFirmware = 8;          // Version v0.8
int flashNum = 3;
int address = 0;
bool flagDebugBt = false;

const int16_t dePin = 46;                         // Enalbe to TX - signal name: ser-enb (habilita tx rs-485)

volatile bool flagCbOnFdcInt = false;             // For variables used inside function interrupt must declare "volatile"
bool flagCbOnFdc = false;
volatile bool flagCbOffFdcInt = false;
bool flagCbOffFdc = false;
volatile bool flagCbEnbFdc = false;               // x
volatile bool flagInterFdc = false;               // x
volatile bool flagDoorFdc = false;                // x
bool flagINSfdc = false;
bool flagEXTfdc = false;
bool flagTSTfdc = false;
volatile bool flagINSfdcInt = false;
volatile bool flagEXTfdcInt = false;
volatile bool flagTSTfdcInt = false;

volatile bool flagCbOnBt = false;
volatile bool flagCbOffBt = false;
volatile bool flagINSbt = false;
volatile bool flagTSTbt = false;
volatile bool flagEXTbt = false;

ModbusRTUSlave modbus(MODBUS_SERIAL, dePin);
                                                    
const uint8_t numCoils = 2;
bool coils[numCoils];                               // const uint8_t numDiscreteInputs = 5; (Caso use discreteinputs)
                                                    // bool discreteInputs[numDiscreteInputs];
//
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void setup() {
    pinMode (debugBt, INPUT_PULLUP);        // On board debug button
    pinMode(fdcCbOn, INPUT_PULLUP);         // Alternative for internal pull-up - ON Circuit Breaker Limit Switch
    pinMode(fdcCbOff, INPUT_PULLUP);        // Alternative for internal pull-up - OFF Circuit Breaker Linit Switch
    pinMode(fdcCbEnb, INPUT_PULLUP);        // Alternative for internal pull-up - Enable Circuit Breaker Limit Switch
    pinMode(fdcInterLk, INPUT_PULLUP);      // Alternative for internal pull-up - Drawer Interlock Limit Switch 
    pinMode(fdcDoor, INPUT_PULLUP);         // Alternative for internal pull-up - Door Open Limit Switch
    pinMode(fdcINS, INPUT_PULLUP);          // Alternative for internal pull-up - Drawer in position INSERTED
    pinMode(fdcEXT, INPUT_PULLUP);          // Alternative for internal pull-up - Drawer in position EXTRACTED
    pinMode(fdcTST, INPUT_PULLUP);          // Alternative for internal pull-up - Drawer in position TEST
    pinMode(btCbOn, INPUT_PULLUP);          // Alternative for internal pull-up - ON Button for Circuit Breaker
    pinMode(btCbOff, INPUT_PULLUP);         // Alternative for internal pull-up - OFF Button for Circuit Breaker
    pinMode(btTST, INPUT_PULLUP);           // Alternative for internal pull-up - TEST position Button
    pinMode(btINS, INPUT_PULLUP);           // Alternative for internal pull-up - INSERT position Button
    pinMode(btEXT, INPUT_PULLUP);           // Alternative for internal pull-up - EXTRACT position Button
//    
    pinMode (debugLed, OUTPUT);             // LED onboard for Debug purpose
    pinMode (ledCbOn, OUTPUT);              // Red Lamp inside Circuit Breaber Button
    pinMode (ledCbOff, OUTPUT);             // Green Lamp inside Circuit Breaber Button
    pinMode (ledINS, OUTPUT);               // Red Lamp inside Insert Button
    pinMode (ledTST, OUTPUT);               // Yellow Lamp inside Test Button
    pinMode (ledEXT, OUTPUT);               // Green Lamp inside Extract Button
//
    pinMode (enbM2, OUTPUT);                // Driver for M2 motor - Circuit Breaker
    pinMode (phM2, OUTPUT);                 // Driver for M2 motor - Circuit Breaker
    pinMode (inlM1, OUTPUT);                // Driver for M1 motor - Drawer
    pinMode (inhM1, OUTPUT);                // Driver for M1 motor - Drawer
//
    analogReference (EXTERNAL);             // ??
    digitalWrite (inlM1, LOW);
    digitalWrite (inhM1, LOW);
                                            //   address = myDipSwitch.read();
//
    attachInterrupt(fdcCbOn, CBONLS, FALLING);
    attachInterrupt(fdcCbOff, CBOFFLS, FALLING);
    attachInterrupt(fdcCbEnb, CBENBLS, FALLING);
    attachInterrupt(fdcInterLk, INTERLOCKLS, FALLING);
    attachInterrupt(fdcDoor, DOORLS, FALLING);
    attachInterrupt(fdcINS, INSERTLS, FALLING);
    attachInterrupt(fdcEXT, EXTRACTLS, FALLING);
    attachInterrupt(fdcTST, TESTLS, FALLING);
//
    attachInterrupt(btCbOn, CBONBT, FALLING);
    attachInterrupt(btCbOff, CBOFFBT, FALLING);
    attachInterrupt(btTST, TSTBT, FALLING);
    attachInterrupt(btINS, INSBT, FALLING);
    attachInterrupt(btEXT, EXTBT, FALLING);
//
                                                                      // modbus.configureDiscreteInputs(discreteInputs, numDiscreteInputs);
    modbus.configureCoils(coils, numCoils);
    MODBUS_SERIAL.begin(MODBUS_BAUD, MODBUS_CONFIG);
    modbus.begin(MODBUS_UNIT_ID, MODBUS_BAUD, MODBUS_CONFIG);
//
 for (int i = 0; i < flashNum; i++) {                   // Initial blink
          digitalWrite (ledCbOn, HIGH);                   
          delay (200);                                   
          digitalWrite (ledCbOn, LOW);                    
          delay (200);
         }
         for (int i = 0; i < flashNum; i++) {           // 
          digitalWrite (ledCbOff, HIGH);                   
          delay (200);                                   
          digitalWrite (ledCbOff, LOW);                    
          delay (200);
         }
//        
        for (int i = 0; i < flashNum; i++) {            // 
          digitalWrite (ledINS, HIGH);                   
          delay (200);                                   
          digitalWrite (ledINS, LOW);                    
          delay (200);
         }
        for (int i = 0; i < flashNum; i++) {            // 
          digitalWrite (ledTST, HIGH);                   
          delay (200);                                   
          digitalWrite (ledTST, LOW);                    
          delay (200);
         }
         for (int i = 0; i < flashNum; i++) {           // 
          digitalWrite (ledEXT, HIGH);                   
          delay (200);                                   
          digitalWrite (ledEXT, LOW);                    
          delay (200);
         }
//----------------------------------------------------------------------------------------------------------------------------------------------------------
    for (int i = 0; i < verFirmware; i++) {             // BLINK LED version number
      digitalWrite (debugLed, HIGH);                    // 
      delay (200);                                      // 
      digitalWrite (debugLed, LOW);                     // 
      delay (200);                                      // 
      }
      flagCbOnFdc = digitalRead (fdcCbOn);
    if (flagCbOnFdc == false) {
      digitalWrite (ledCbOn,HIGH);
    }
    flagCbOffFdc = digitalRead (fdcCbOff);
    if (flagCbOffFdc == false) {
      digitalWrite (ledCbOff,HIGH);
    }
//
    flagINSfdc = digitalRead (fdcINS);
    if (flagINSfdc == false) {
      digitalWrite (ledINS,HIGH);
    }
    flagEXTfdc = digitalRead (fdcEXT);
    if (flagEXTfdc == false) {
      digitalWrite (ledEXT,HIGH);
    }
    flagTSTfdc = digitalRead (fdcTST);
    if (flagTSTfdc == false) {
      digitalWrite (ledTST,HIGH);
    }
} // END SETUP ROUTINE
//===========================================================================================================================================================================================
void loop() {
        flagDebugBt = digitalRead (debugBt);            // Debug LED test
           if (flagDebugBt == false) {
             BLINKLED ();                                                                                                                
         }
//---------- Rotina para ligar disjuntor ----------------------------------------------------------------------------------------------------------------------------------------------------
           flagCbOnFdc = digitalRead (fdcCbOn); 
            if (flagCbOnBt == true && flagCbOnFdc == true) {          // Se botão para ligar disjuntor e não tenho acionado o fim de curso de disjuntor ligado - permite ligar
              BLINKCBON ();                                           // Pisca Lamp botão On disjuntor
                 digitalWrite (ledCbOff, LOW);                       // desliga Lamp disjuntor desligado
                 digitalWrite (enbM2, LOW);                           // Liga motor do disjuntor M2 para sentido CCW
                 digitalWrite (phM2, HIGH);                           // Liga motor do disjuntor M2 para sentido CCW       
             }
            if (flagCbOnFdcInt == true && flagCbOnBt == true) {       // Se estou em rotina de ligar disjuntor e foi acionado o fim de curso de disjuntor ligado, então
              flagCbOnBt = false;                                     // reseta flag do botão
               digitalWrite (ledCbOn, HIGH);                           // e liga Lamp disjuntor ligado 
                 digitalWrite (enbM2, LOW);                           // e desliga motor M2
                 digitalWrite (phM2, LOW); 
                 delay (500);                                         // dá um tempo de 1/2s
                  digitalWrite (enbM2, HIGH);                         // Reverte sentido do motor e liga CW
                  digitalWrite (phM2, LOW);
                  delay (2000);                                       // por tempo de 2s
                    digitalWrite (enbM2, LOW);                        // desliga motor do disjuntor M2
                    digitalWrite (phM2, LOW);
                     flagCbOnFdcInt = false;                          // Reseta flag do fim de curso de disjuntor ligado    
             }
//---------------- Rotina para desligar disjuntor ------------------------------------------------------------------------------------------------------------------------------------------
           flagDoorFdc = digitalRead (fdcDoor);
             if (flagCbOffBt == true && flagDoorFdc == false) {       // Se botão para desligar disjuntor e não tenho acionado o fim de curso de disjuntor desligado - permite desligar
              BLINKCBOFF ();                                          // Pisca Lamp botão Off disjuntor 
                digitalWrite (ledCbOn, LOW);                          // e desliga Lamp disjuntor ligado
                digitalWrite (enbM2, HIGH);                           // Liga sentido do motor e liga CW
                digitalWrite (phM2, LOW);
             }
             if (flagCbOffFdcInt == true && flagCbOffBt == true) {    // Se estou em rotina de ligar disjuntor e foi acionado o fim de curso de disjuntor ligado, então
              flagCbOffBt = false;                                    // reseta flag do botão
              digitalWrite (ledCbOff, HIGH);                          // e liga Lamp disjuntor desligado
                 digitalWrite (enbM2, LOW);                           // e desliga motor M2 
                 digitalWrite (phM2, LOW); 
                 delay (500);                                         // dá um tempo de 1/2s
                  digitalWrite (enbM2, LOW);                          // Reverte sentido do motor e liga CCW
                  digitalWrite (phM2, HIGH);
                  delay (2000);                                       // por tempo de 2s
                    digitalWrite (enbM2, LOW);                        // desliga motor do disjuntor M2
                    digitalWrite (phM2, LOW);
                     flagCbOffFdcInt = false;                         // Reseta flag do fim de curso de disjuntor desligado    
             }
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//---------------- rotina para gaveta ir da posição extraída para posição de teste --------------------------------------------------------------------------------------------------------
            while (flagTSTbt == true && flagEXTfdc == false) {        // Enqanto o botão para enviar gaveta para posição de teste estiver acionado, e posição atual é a extraida (A)
              if (flagCbOffFdc == false && flagDoorFdc == false){     // Se não tenho porta aberta nem disjuntor ligado
                  BLINKTST ();                                        // Pisca a Lamp amarela até atingir posição de teste
                  digitalWrite (ledINS, LOW);                         // Desliga LED Vermelho
                  digitalWrite (ledEXT, LOW);                         // desliga LED Verde
                  digitalWrite (inlM1, LOW);                          // Liga motor CW para ir para posição de extraida para posição de teste 
                  digitalWrite (inhM1, HIGH);                         // Liga motor CW para ir para posição de extraida para posição de teste 
              }
              if (flagTSTfdcInt == true) {                            // Se atingiu o FDC de posição de teste
                  digitalWrite (ledTST, HIGH);                        // Liga LED Amarelo
                  digitalWrite (inlM1, LOW);                          // Desliga motor M1
                  digitalWrite (inhM1, LOW);                          // Desliga motor M1
                  flagTSTfdcInt = false;                              // Reseta flag do FDC de posição de teste
                  flagTSTbt = false;                                  // Reseta flag do botão de posição de teste
              }
    }
//---------------- rotina para gaveta ir da posição de teste para posição de inserida --------------------------------------------------------------------------------------------------------
            while (flagINSbt == true && flagTSTfdc == true) {         // Enqanto o botão para enviar gaveta para posição de inserida estiver acionado, e posição atual é a posição de teste (B)
              if (flagCbOffFdc == false && flagDoorFdc == false){     // Se não tenho porta aberta nem disjuntor ligado
                  BLINKINS ();                                        // Pisca a Lamp vermelha até atingir posição de Inserir
                  digitalWrite (ledTST, LOW);                         // Liga LED Amarelo
                  digitalWrite (ledEXT, LOW);                         // Liga LED Verde
                  digitalWrite (inlM1, LOW);                          // Liga motor CW para ir para posição de teste para posição de inserida 
                  digitalWrite (inhM1, HIGH);                         // Liga motor CW para ir para posição de teste para posição de inserida
              }
              if (flagINSfdcInt == true && flagEXTbt == false) {      // Se atingiu o FDC de posição de teste
                    digitalWrite (ledINS, HIGH);                      // Liga LED vermelho
                    digitalWrite (inlM1, LOW);                        // Desliga motor M1
                    digitalWrite (inhM1, LOW);                        // Desliga motor M1
                      flagINSfdcInt = false;                          // Reseta flag auxiliar do FDC da posição inserida
                      flagINSbt = false;                              // Reseta flag do botão de posição de inserida
              } 
            }                     
//---------------- rotina para a gaveta ir da posição inserida para a posição extraida -------------------------------------------------------------------------------------------------------
          while (flagEXTbt && flagINSfdc == true) {                   // Enqanto o botão para enviar gaveta para posição de extraida estiver acionado, e a posição atual é a posição inserida (C)
              if (flagCbOffFdc == false && flagDoorFdc == false){     // Se não tenho porta aberta nem disjuntor ligado
                  BLINKEXT ();                                        // Pisca a Lamp verde até atingir posição de Inserir
                  digitalWrite (ledTST, LOW);                         // Desliga LED Amarelo
                  digitalWrite (ledINS, LOW);                         // Desliga LED Vermelho
                  digitalWrite (inlM1, HIGH);                         // Liga motor CCW para ir para posição de inserida para posição de extraida 
                  digitalWrite (inhM1, HIGH);                         // Liga motor CCW para ir para posição de inserida para posição de extraida
              }
             if (flagEXTfdcInt == true) {                             // Se atingiu o FDC de posição de teste
                    digitalWrite (ledEXT, HIGH);                      // Liga LED verde
                    digitalWrite (inlM1, LOW);                        // Desliga motor M1
                    digitalWrite (inhM1, LOW);                        // desliga motor M1
                      flagEXTfdcInt = false;                          // Reseta flag auxiliar de FDC de posição extraida
                      flagEXTbt = false;                              // Reseta flag do botão de posição de extraida
              }            
          }
//---------------- rotina para a gaveta ir da posição extraída para a posição inserida -------------------------------------------------------------------------------------------------------
          while (flagINSbt && flagEXTfdc == true) {                   // Enqanto o botão para enviar gaveta para posição de inserida estiver acionado, e a posição atual é a posição extraida (D)
              if (flagCbOffFdc == false && flagDoorFdc == false){     // Se não tenho porta aberta nem disjuntor ligado
                  BLINKINS ();                                        // Pisca a Lamp vermelha até atingir posição de Inserir
                  digitalWrite (ledTST, LOW);                         // Desliga LED Amarelo
                  digitalWrite (ledEXT, LOW);                         // Desliga LED Verde
                  digitalWrite (inlM1, LOW);                          // Liga motor CW para ir para posição de extraida para posição de inserida 
                  digitalWrite (inhM1, HIGH);                         // Liga motor CW para ir para posição de extraida para posição de inserida
              }
             if (flagINSfdcInt == true) {                             // Se atingiu o FDC de posição de inserida
                    digitalWrite (ledINS, HIGH);                      // Liga LED vermelho
                    digitalWrite (inlM1, LOW);                        // Desliga motor M1
                    digitalWrite (inhM1, LOW);                        // Desliga motor M1
                      flagINSfdcInt = false;                          // Reseta flag de FDC por interrupção de posição inserida
                      flagINSbt = false;                              // Restea flag do botão de posição de inserir
              }            
          }
//
// ------------------------ Rotina de teste de comunicação modbus ----------------------------------------------------------------------------------------------------------------------------
            bool flagStatus1;
            modbus.poll();
            digitalWrite(flagStatus1, coils[1]);
            digitalWrite(flagStatus2, coils[2]);



          if (flagStatus1 == true){
            digitalWrite (debugLed, HIGH);
            flagStatus1 = false;
          }
         if (flagStatus2 == true) {
            digitalWrite (debugLed, LOW);
            flagStatus2 = false;
          }

}   // end main loop
//============================================================================================================================================================================================
void CBONLS(){                                                        // Se atingiu o fim de curso de disjuntor ligado
    flagCbOnFdcInt = true;
    digitalWrite (enbM2, LOW);                                        // desliga motor M2 
    digitalWrite (phM2, LOW); 
}
void CBOFFLS() {                                                      // Se atingiu o fim de curso de disjuntor desligado
    flagCbOffFdcInt = true;
     digitalWrite (enbM2, LOW);                                       // desliga motor M2 
    digitalWrite (phM2, LOW);   
}
void CBENBLS() {                                                      // Se atingiu o fim de curso de disjuntor em posição para habilitar monobra na gaveta (talvez não precise ser por interrupção)
}
void INTERLOCKLS() {                                                  // Se atingiu o fim de curso de posição Intertrava (Cadeado) para habilitar monobra na gaveta (talvez não precise ser por interrupção)
}
void DOORLS() {                                                       // Se atingiu o fim de curso de posição da porta para habilitar monobra na gaveta (talvez não precise ser por interrupção)
}
void INSERTLS(){                                                      // Se atingiu o fim de curso de posição gaveta inserida
    flagINSfdcInt = true;
    digitalWrite (inlM1, LOW);                                        // Desliga motor M1
    digitalWrite (inhM1, LOW);                                                
}
void EXTRACTLS () {                                                   // Se atingiu o fim de curso de posição gaveta extraída
    flagEXTfdcInt = true;
    digitalWrite (inlM1, LOW);                                        // Desliga motor M1
    digitalWrite (inhM1, LOW);                                                
}
void TESTLS() {                                                       // Se atingiu o fim de curso de posição gaveta em teste
    flagTSTfdcInt = true;
    digitalWrite (inlM1, LOW);                                        // Desliga motor M1
    digitalWrite (inhM1, LOW);                                                
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void CBONBT() {                                                       // Se botão para ligar disjuntor for pressionado
    flagCbOnBt = true;
}
void CBOFFBT() {                                                      // Se botão para desligar disjuntor for pressionado
    flagCbOffBt = true;
}
void TSTBT() {                                                        // Se botão para ligar motor da gaveta para ir para posição de teste for pressionado
    flagTSTbt = true;
}
void INSBT() {                                                        // Se botão para ligar motor da gaveta para ir para posição inserida for pressionado
    flagINSbt = true;
}
void EXTBT() {                                                        // Se botão para ligar motor da gaveta para ir para posição extraída for pressionado
    flagEXTbt = true;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void BLINKLED () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (debugLed, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (debugLed, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKCBON () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledCbOn, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledCbOn, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKCBOFF () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledCbOff, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledCbOff, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKTST () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledTST, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledTST, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKINS () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledINS, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledINS, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKEXT () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledEXT, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledEXT, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
// END OF PROGRAM
[/code]

I don't know why the line numbers didn't appear in the print...

Isn't it Serial (USART0) with CoreX?

I don't know (I did not look at that). But jjcohen can easily test all 4 serial ports.

But we did not know.

I think OP should explain the whole project to get support here. There are already two topics about the same case. Not well defined devices that have hardwired non changeable connections and no code at all. So who decided the connections? Why this entity didn't provide code?

[code]
/* GEX-12D Version II
 *  Firmware version: V0.8 Date: Aug 26, 2025
 *  a) Estabelecer IOs digitais e analógicos e testá-los
 *  b) Estabelecer os IOs digitais que serão usados com interrupção - Nota: Não há necessidade de usar macro digitalPinToInterrupt, pois todos os pinos suportam interrupção (usar attachInterrupt)
 *  c) Testes iniciais de movimentação do motor Me2 do disjuntor - ok
 *  d) Início dos testes de movimentação da gaveta
 *  e) teste com a estrutura "while" para ver se facilita o controle da gaveta
 *  f) A partir da V0.7 testes para comunicação com placa CPU - Protocolo Modbbus RTU (CPU -> Master & driver -> Client)
 *  g) Versão V0.7 - início dos testes com comunicação RS-485 - Protocolo Modbus RTU - Slave

*/
#include <DipSwitch.h>
#include <ModbusRTUSlave.h>                                       // Incluido na versão V0.7
//
#define MODBUS_SERIAL Serial2
//
// You can change the baud, config, and unit id values if you like.
#define MODBUS_BAUD 9600                                                          // Era 38400
#define MODBUS_CONFIG SERIAL_8N1
#define MODBUS_UNIT_ID 1
//
// Define the digital and analog pin connected to the board
#define debugBt     13
#define fdcCbOn     14
#define fdcCbOff    15
#define fdcCbEnb    16
#define fdcInterLk  17
#define fdcDoor     18
#define fdcINS      19
#define fdcEXT      20
#define fdcTST      21

#define btCbOn      8
#define btCbOff     9
#define btTST       10
#define btINS       11
#define btEXT       12
#define ledCbOn     36                        // RED LED
#define ledCbOff    35                        // GREEN LED
#define ledINS      34
#define ledTST      37
#define ledEXT      38
#define debugLed    39

#define enbM2       3
#define phM2        4
#define inlM1       5
#define inhM1       6

#define isM1        A8        // 30 PE0
#define ipropoiM2   A9        // 31 PE1    

int dipSwitchPins [] = {22,23,24,25,26,27,28,29};
DipSwitch myDipSwitch (8, dipSwitchPins);
//
// ------------------ Variables -------------------------------------------------------------------------------------------------------------------------------------------------------------
int verFirmware = 8;          // Version v0.8
int flashNum = 3;
int address = 0;
bool flagDebugBt = false;

const int16_t dePin = 46;                         // Enalbe to TX - signal name: ser-enb (habilita tx rs-485)

volatile bool flagCbOnFdcInt = false;             // For variables used inside function interrupt must declare "volatile"
bool flagCbOnFdc = false;
volatile bool flagCbOffFdcInt = false;
bool flagCbOffFdc = false;
volatile bool flagCbEnbFdc = false;               // x
volatile bool flagInterFdc = false;               // x
volatile bool flagDoorFdc = false;                // x
bool flagINSfdc = false;
bool flagEXTfdc = false;
bool flagTSTfdc = false;
volatile bool flagINSfdcInt = false;
volatile bool flagEXTfdcInt = false;
volatile bool flagTSTfdcInt = false;

volatile bool flagCbOnBt = false;
volatile bool flagCbOffBt = false;
volatile bool flagINSbt = false;
volatile bool flagTSTbt = false;
volatile bool flagEXTbt = false;

ModbusRTUSlave modbus(MODBUS_SERIAL, dePin);
                                                    
const uint8_t numCoils = 2;
bool coils[numCoils];                               // const uint8_t numDiscreteInputs = 5; (Caso use discreteinputs)
                                                    // bool discreteInputs[numDiscreteInputs];
//
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void setup() {
    pinMode (debugBt, INPUT_PULLUP);        // On board debug button
    pinMode(fdcCbOn, INPUT_PULLUP);         // Alternative for internal pull-up - ON Circuit Breaker Limit Switch
    pinMode(fdcCbOff, INPUT_PULLUP);        // Alternative for internal pull-up - OFF Circuit Breaker Linit Switch
    pinMode(fdcCbEnb, INPUT_PULLUP);        // Alternative for internal pull-up - Enable Circuit Breaker Limit Switch
    pinMode(fdcInterLk, INPUT_PULLUP);      // Alternative for internal pull-up - Drawer Interlock Limit Switch 
    pinMode(fdcDoor, INPUT_PULLUP);         // Alternative for internal pull-up - Door Open Limit Switch
    pinMode(fdcINS, INPUT_PULLUP);          // Alternative for internal pull-up - Drawer in position INSERTED
    pinMode(fdcEXT, INPUT_PULLUP);          // Alternative for internal pull-up - Drawer in position EXTRACTED
    pinMode(fdcTST, INPUT_PULLUP);          // Alternative for internal pull-up - Drawer in position TEST
    pinMode(btCbOn, INPUT_PULLUP);          // Alternative for internal pull-up - ON Button for Circuit Breaker
    pinMode(btCbOff, INPUT_PULLUP);         // Alternative for internal pull-up - OFF Button for Circuit Breaker
    pinMode(btTST, INPUT_PULLUP);           // Alternative for internal pull-up - TEST position Button
    pinMode(btINS, INPUT_PULLUP);           // Alternative for internal pull-up - INSERT position Button
    pinMode(btEXT, INPUT_PULLUP);           // Alternative for internal pull-up - EXTRACT position Button
//    
    pinMode (debugLed, OUTPUT);             // LED onboard for Debug purpose
    pinMode (ledCbOn, OUTPUT);              // Red Lamp inside Circuit Breaber Button
    pinMode (ledCbOff, OUTPUT);             // Green Lamp inside Circuit Breaber Button
    pinMode (ledINS, OUTPUT);               // Red Lamp inside Insert Button
    pinMode (ledTST, OUTPUT);               // Yellow Lamp inside Test Button
    pinMode (ledEXT, OUTPUT);               // Green Lamp inside Extract Button
//
    pinMode (enbM2, OUTPUT);                // Driver for M2 motor - Circuit Breaker
    pinMode (phM2, OUTPUT);                 // Driver for M2 motor - Circuit Breaker
    pinMode (inlM1, OUTPUT);                // Driver for M1 motor - Drawer
    pinMode (inhM1, OUTPUT);                // Driver for M1 motor - Drawer
//
    analogReference (EXTERNAL);             // ??
    digitalWrite (inlM1, LOW);
    digitalWrite (inhM1, LOW);
                                            //   address = myDipSwitch.read();
//
    attachInterrupt(fdcCbOn, CBONLS, FALLING);
    attachInterrupt(fdcCbOff, CBOFFLS, FALLING);
    attachInterrupt(fdcCbEnb, CBENBLS, FALLING);
    attachInterrupt(fdcInterLk, INTERLOCKLS, FALLING);
    attachInterrupt(fdcDoor, DOORLS, FALLING);
    attachInterrupt(fdcINS, INSERTLS, FALLING);
    attachInterrupt(fdcEXT, EXTRACTLS, FALLING);
    attachInterrupt(fdcTST, TESTLS, FALLING);
//
    attachInterrupt(btCbOn, CBONBT, FALLING);
    attachInterrupt(btCbOff, CBOFFBT, FALLING);
    attachInterrupt(btTST, TSTBT, FALLING);
    attachInterrupt(btINS, INSBT, FALLING);
    attachInterrupt(btEXT, EXTBT, FALLING);
//
                                                                      // modbus.configureDiscreteInputs(discreteInputs, numDiscreteInputs);
    modbus.configureCoils(coils, numCoils);
    MODBUS_SERIAL.begin(MODBUS_BAUD, MODBUS_CONFIG);
    modbus.begin(MODBUS_UNIT_ID, MODBUS_BAUD, MODBUS_CONFIG);
//
 for (int i = 0; i < flashNum; i++) {                   // Initial blink
          digitalWrite (ledCbOn, HIGH);                   
          delay (200);                                   
          digitalWrite (ledCbOn, LOW);                    
          delay (200);
         }
         for (int i = 0; i < flashNum; i++) {           // 
          digitalWrite (ledCbOff, HIGH);                   
          delay (200);                                   
          digitalWrite (ledCbOff, LOW);                    
          delay (200);
         }
//        
        for (int i = 0; i < flashNum; i++) {            // 
          digitalWrite (ledINS, HIGH);                   
          delay (200);                                   
          digitalWrite (ledINS, LOW);                    
          delay (200);
         }
        for (int i = 0; i < flashNum; i++) {            // 
          digitalWrite (ledTST, HIGH);                   
          delay (200);                                   
          digitalWrite (ledTST, LOW);                    
          delay (200);
         }
         for (int i = 0; i < flashNum; i++) {           // 
          digitalWrite (ledEXT, HIGH);                   
          delay (200);                                   
          digitalWrite (ledEXT, LOW);                    
          delay (200);
         }
//----------------------------------------------------------------------------------------------------------------------------------------------------------
    for (int i = 0; i < verFirmware; i++) {             // BLINK LED version number
      digitalWrite (debugLed, HIGH);                    // 
      delay (200);                                      // 
      digitalWrite (debugLed, LOW);                     // 
      delay (200);                                      // 
      }
      flagCbOnFdc = digitalRead (fdcCbOn);
    if (flagCbOnFdc == false) {
      digitalWrite (ledCbOn,HIGH);
    }
    flagCbOffFdc = digitalRead (fdcCbOff);
    if (flagCbOffFdc == false) {
      digitalWrite (ledCbOff,HIGH);
    }
//
    flagINSfdc = digitalRead (fdcINS);
    if (flagINSfdc == false) {
      digitalWrite (ledINS,HIGH);
    }
    flagEXTfdc = digitalRead (fdcEXT);
    if (flagEXTfdc == false) {
      digitalWrite (ledEXT,HIGH);
    }
    flagTSTfdc = digitalRead (fdcTST);
    if (flagTSTfdc == false) {
      digitalWrite (ledTST,HIGH);
    }
} // END SETUP ROUTINE
//===========================================================================================================================================================================================
void loop() {
        flagDebugBt = digitalRead (debugBt);            // Debug LED test
           if (flagDebugBt == false) {
             BLINKLED ();                                                                                                                
         }
//---------- Rotina para ligar disjuntor ----------------------------------------------------------------------------------------------------------------------------------------------------
           flagCbOnFdc = digitalRead (fdcCbOn); 
            if (flagCbOnBt == true && flagCbOnFdc == true) {          // Se botão para ligar disjuntor e não tenho acionado o fim de curso de disjuntor ligado - permite ligar
              BLINKCBON ();                                           // Pisca Lamp botão On disjuntor
                 digitalWrite (ledCbOff, LOW);                       // desliga Lamp disjuntor desligado
                 digitalWrite (enbM2, LOW);                           // Liga motor do disjuntor M2 para sentido CCW
                 digitalWrite (phM2, HIGH);                           // Liga motor do disjuntor M2 para sentido CCW       
             }
            if (flagCbOnFdcInt == true && flagCbOnBt == true) {       // Se estou em rotina de ligar disjuntor e foi acionado o fim de curso de disjuntor ligado, então
              flagCbOnBt = false;                                     // reseta flag do botão
               digitalWrite (ledCbOn, HIGH);                           // e liga Lamp disjuntor ligado 
                 digitalWrite (enbM2, LOW);                           // e desliga motor M2
                 digitalWrite (phM2, LOW); 
                 delay (500);                                         // dá um tempo de 1/2s
                  digitalWrite (enbM2, HIGH);                         // Reverte sentido do motor e liga CW
                  digitalWrite (phM2, LOW);
                  delay (2000);                                       // por tempo de 2s
                    digitalWrite (enbM2, LOW);                        // desliga motor do disjuntor M2
                    digitalWrite (phM2, LOW);
                     flagCbOnFdcInt = false;                          // Reseta flag do fim de curso de disjuntor ligado    
             }
//---------------- Rotina para desligar disjuntor ------------------------------------------------------------------------------------------------------------------------------------------
           flagDoorFdc = digitalRead (fdcDoor);
             if (flagCbOffBt == true && flagDoorFdc == false) {       // Se botão para desligar disjuntor e não tenho acionado o fim de curso de disjuntor desligado - permite desligar
              BLINKCBOFF ();                                          // Pisca Lamp botão Off disjuntor 
                digitalWrite (ledCbOn, LOW);                          // e desliga Lamp disjuntor ligado
                digitalWrite (enbM2, HIGH);                           // Liga sentido do motor e liga CW
                digitalWrite (phM2, LOW);
             }
             if (flagCbOffFdcInt == true && flagCbOffBt == true) {    // Se estou em rotina de ligar disjuntor e foi acionado o fim de curso de disjuntor ligado, então
              flagCbOffBt = false;                                    // reseta flag do botão
              digitalWrite (ledCbOff, HIGH);                          // e liga Lamp disjuntor desligado
                 digitalWrite (enbM2, LOW);                           // e desliga motor M2 
                 digitalWrite (phM2, LOW); 
                 delay (500);                                         // dá um tempo de 1/2s
                  digitalWrite (enbM2, LOW);                          // Reverte sentido do motor e liga CCW
                  digitalWrite (phM2, HIGH);
                  delay (2000);                                       // por tempo de 2s
                    digitalWrite (enbM2, LOW);                        // desliga motor do disjuntor M2
                    digitalWrite (phM2, LOW);
                     flagCbOffFdcInt = false;                         // Reseta flag do fim de curso de disjuntor desligado    
             }
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//---------------- rotina para gaveta ir da posição extraída para posição de teste --------------------------------------------------------------------------------------------------------
            while (flagTSTbt == true && flagEXTfdc == false) {        // Enqanto o botão para enviar gaveta para posição de teste estiver acionado, e posição atual é a extraida (A)
              if (flagCbOffFdc == false && flagDoorFdc == false){     // Se não tenho porta aberta nem disjuntor ligado
                  BLINKTST ();                                        // Pisca a Lamp amarela até atingir posição de teste
                  digitalWrite (ledINS, LOW);                         // Desliga LED Vermelho
                  digitalWrite (ledEXT, LOW);                         // desliga LED Verde
                  digitalWrite (inlM1, LOW);                          // Liga motor CW para ir para posição de extraida para posição de teste 
                  digitalWrite (inhM1, HIGH);                         // Liga motor CW para ir para posição de extraida para posição de teste 
              }
              if (flagTSTfdcInt == true) {                            // Se atingiu o FDC de posição de teste
                  digitalWrite (ledTST, HIGH);                        // Liga LED Amarelo
                  digitalWrite (inlM1, LOW);                          // Desliga motor M1
                  digitalWrite (inhM1, LOW);                          // Desliga motor M1
                  flagTSTfdcInt = false;                              // Reseta flag do FDC de posição de teste
                  flagTSTbt = false;                                  // Reseta flag do botão de posição de teste
              }
    }
//---------------- rotina para gaveta ir da posição de teste para posição de inserida --------------------------------------------------------------------------------------------------------
            while (flagINSbt == true && flagTSTfdc == true) {         // Enqanto o botão para enviar gaveta para posição de inserida estiver acionado, e posição atual é a posição de teste (B)
              if (flagCbOffFdc == false && flagDoorFdc == false){     // Se não tenho porta aberta nem disjuntor ligado
                  BLINKINS ();                                        // Pisca a Lamp vermelha até atingir posição de Inserir
                  digitalWrite (ledTST, LOW);                         // Liga LED Amarelo
                  digitalWrite (ledEXT, LOW);                         // Liga LED Verde
                  digitalWrite (inlM1, LOW);                          // Liga motor CW para ir para posição de teste para posição de inserida 
                  digitalWrite (inhM1, HIGH);                         // Liga motor CW para ir para posição de teste para posição de inserida
              }
              if (flagINSfdcInt == true && flagEXTbt == false) {      // Se atingiu o FDC de posição de teste
                    digitalWrite (ledINS, HIGH);                      // Liga LED vermelho
                    digitalWrite (inlM1, LOW);                        // Desliga motor M1
                    digitalWrite (inhM1, LOW);                        // Desliga motor M1
                      flagINSfdcInt = false;                          // Reseta flag auxiliar do FDC da posição inserida
                      flagINSbt = false;                              // Reseta flag do botão de posição de inserida
              } 
            }                     
//---------------- rotina para a gaveta ir da posição inserida para a posição extraida -------------------------------------------------------------------------------------------------------
          while (flagEXTbt && flagINSfdc == true) {                   // Enqanto o botão para enviar gaveta para posição de extraida estiver acionado, e a posição atual é a posição inserida (C)
              if (flagCbOffFdc == false && flagDoorFdc == false){     // Se não tenho porta aberta nem disjuntor ligado
                  BLINKEXT ();                                        // Pisca a Lamp verde até atingir posição de Inserir
                  digitalWrite (ledTST, LOW);                         // Desliga LED Amarelo
                  digitalWrite (ledINS, LOW);                         // Desliga LED Vermelho
                  digitalWrite (inlM1, HIGH);                         // Liga motor CCW para ir para posição de inserida para posição de extraida 
                  digitalWrite (inhM1, HIGH);                         // Liga motor CCW para ir para posição de inserida para posição de extraida
              }
             if (flagEXTfdcInt == true) {                             // Se atingiu o FDC de posição de teste
                    digitalWrite (ledEXT, HIGH);                      // Liga LED verde
                    digitalWrite (inlM1, LOW);                        // Desliga motor M1
                    digitalWrite (inhM1, LOW);                        // desliga motor M1
                      flagEXTfdcInt = false;                          // Reseta flag auxiliar de FDC de posição extraida
                      flagEXTbt = false;                              // Reseta flag do botão de posição de extraida
              }            
          }
//---------------- rotina para a gaveta ir da posição extraída para a posição inserida -------------------------------------------------------------------------------------------------------
          while (flagINSbt && flagEXTfdc == true) {                   // Enqanto o botão para enviar gaveta para posição de inserida estiver acionado, e a posição atual é a posição extraida (D)
              if (flagCbOffFdc == false && flagDoorFdc == false){     // Se não tenho porta aberta nem disjuntor ligado
                  BLINKINS ();                                        // Pisca a Lamp vermelha até atingir posição de Inserir
                  digitalWrite (ledTST, LOW);                         // Desliga LED Amarelo
                  digitalWrite (ledEXT, LOW);                         // Desliga LED Verde
                  digitalWrite (inlM1, LOW);                          // Liga motor CW para ir para posição de extraida para posição de inserida 
                  digitalWrite (inhM1, HIGH);                         // Liga motor CW para ir para posição de extraida para posição de inserida
              }
             if (flagINSfdcInt == true) {                             // Se atingiu o FDC de posição de inserida
                    digitalWrite (ledINS, HIGH);                      // Liga LED vermelho
                    digitalWrite (inlM1, LOW);                        // Desliga motor M1
                    digitalWrite (inhM1, LOW);                        // Desliga motor M1
                      flagINSfdcInt = false;                          // Reseta flag de FDC por interrupção de posição inserida
                      flagINSbt = false;                              // Restea flag do botão de posição de inserir
              }            
          }
//
// ------------------------ Rotina de teste de comunicação modbus ----------------------------------------------------------------------------------------------------------------------------
          bool flagStatus1;
          bool flagStatus2;
            modbus.poll();
            digitalWrite(flagStatus1, coils[1]);
            digitalWrite(flagStatus2, coils[2]);



          if (flagStatus1 == true){
            digitalWrite (debugLed, HIGH);
            flagStatus1 = false;
          }
         if (flagStatus2 == true) {
            digitalWrite (debugLed, LOW);
            flagStatus2 = false;
          }

}   // end main loop
//============================================================================================================================================================================================
void CBONLS(){                                                        // Se atingiu o fim de curso de disjuntor ligado
    flagCbOnFdcInt = true;
    digitalWrite (enbM2, LOW);                                        // desliga motor M2 
    digitalWrite (phM2, LOW); 
}
void CBOFFLS() {                                                      // Se atingiu o fim de curso de disjuntor desligado
    flagCbOffFdcInt = true;
     digitalWrite (enbM2, LOW);                                       // desliga motor M2 
    digitalWrite (phM2, LOW);   
}
void CBENBLS() {                                                      // Se atingiu o fim de curso de disjuntor em posição para habilitar monobra na gaveta (talvez não precise ser por interrupção)
}
void INTERLOCKLS() {                                                  // Se atingiu o fim de curso de posição Intertrava (Cadeado) para habilitar monobra na gaveta (talvez não precise ser por interrupção)
}
void DOORLS() {                                                       // Se atingiu o fim de curso de posição da porta para habilitar monobra na gaveta (talvez não precise ser por interrupção)
}
void INSERTLS(){                                                      // Se atingiu o fim de curso de posição gaveta inserida
    flagINSfdcInt = true;
    digitalWrite (inlM1, LOW);                                        // Desliga motor M1
    digitalWrite (inhM1, LOW);                                                
}
void EXTRACTLS () {                                                   // Se atingiu o fim de curso de posição gaveta extraída
    flagEXTfdcInt = true;
    digitalWrite (inlM1, LOW);                                        // Desliga motor M1
    digitalWrite (inhM1, LOW);                                                
}
void TESTLS() {                                                       // Se atingiu o fim de curso de posição gaveta em teste
    flagTSTfdcInt = true;
    digitalWrite (inlM1, LOW);                                        // Desliga motor M1
    digitalWrite (inhM1, LOW);                                                
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void CBONBT() {                                                       // Se botão para ligar disjuntor for pressionado
    flagCbOnBt = true;
}
void CBOFFBT() {                                                      // Se botão para desligar disjuntor for pressionado
    flagCbOffBt = true;
}
void TSTBT() {                                                        // Se botão para ligar motor da gaveta para ir para posição de teste for pressionado
    flagTSTbt = true;
}
void INSBT() {                                                        // Se botão para ligar motor da gaveta para ir para posição inserida for pressionado
    flagINSbt = true;
}
void EXTBT() {                                                        // Se botão para ligar motor da gaveta para ir para posição extraída for pressionado
    flagEXTbt = true;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void BLINKLED () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (debugLed, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (debugLed, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKCBON () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledCbOn, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledCbOn, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKCBOFF () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledCbOff, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledCbOff, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKTST () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledTST, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledTST, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKINS () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledINS, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledINS, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKEXT () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledEXT, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledEXT, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
// END OF PROGRAM
[/code]

This is the code

Disregard the Portuguese words, as they are not relevant to this problem.
Note that I already set the serial2 to pins D2 and D7 of the Nano Every, which uses the ATMEGA 4809. Perhaps I'm not getting communication because the syntax in the main loop isn't correct. The Modbus protocol is very sensitive to the number and type of variables transmitted.

In the ModbusRTUSlave library example code ( GitHub - CMB27/ModbusRTUSlave: This is an Arduino library that implements the slave/server logic of the Modbus RTU protocol. ), Serial2 also sets pins 0 and 1 as pins for communication, so I'm a little confused now, as the blog mentioned above ( Arduino Nano Every Serial Ports – Kevin's Blog ) says that Serial2 is for pins D7 (RX) and D2(TX)....?

#include <ModbusRTUSlave.h>

#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) || defined(ARDUINO_SAM_DUE)
  // The ATmega328P and ATmega168 are used in the Ardunino UNO and similar boards.
  // The ATmega2560 and ATmega1280 are used in the Arduino Mega and similar.
  #define MODBUS_SERIAL Serial
#elif defined(ARDUINO_NANO_ESP32)
  // On the Arduino Nano ESP32, the HardwareSerial port on pins 0 and 1 is Serial0.
  #define MODBUS_SERIAL Serial0
#elif defined(ARDUINO_ARCH_STM32)
  // On ST Nucleo-64 Boards, the HardwareSerial port on pins 0 and 1 is Serial2.
  #define MODBUS_SERIAL Serial2
#else
  // On the majority of Arduino boards, the HardwareSerial port on pins 0 and 1 is Serial1.
  #define MODBUS_SERIAL Serial1
#endif

Actually, my main question is:
Can I find a way to declare the following pins using any Modbus Slave library?
TX - 44 (TXD0)
RX - 45 (RXD0)
DE - 46 (ENBTX)
For ATMEGA 4809 microcontroller.

Is this possible? How?

The ATmega4809 data sheet is the very best and most reliable source of information. It will explain if pin reassignments are possible, if that is what you are asking.

You have not explained why you think you need to declare TX and RX pin numbers. As others have pointed out, you may not need to do so. Usually, it is just a matter of wiring.

I am using ATMEGA4809 microcontroller pin 44 for TX and pin 45 for RX and pin 46 for DE.

Are you confusing the 48-Pin VQFN/TQFP IC package pin numbers with Arduino IDE pin numbers?

The datasheet was consulted, and therefore, the mentioned pins were selected. The printed circuit board has already been manufactured and the electronic components assembled.
Unfortunately, I've already run out of time to resolve the issue, and therefore I'm already late in delivering the equipment.
It seems that the GitHub library - CMB27/ModbusRTUSlave: This is an Arduino library that implements the slave/server logic of the Modbus RTU protocol doesn't allow me to swap the serial port pins. And even if it does, I'm unable to do so.

Therefore, I'm looking for a Modbus RTU Slave library that allows me to use SoftwareSerial, for example, so I can use the pins I want. By the way, it needs to be compiled for the ATMEGA4809, the microcontroller used in the Arduino Nano Every. If you know how to help me, I'd be grateful.

Hmm, maybe...let me check… one moment

pins 44, 45, and 46 are physical pins of the TQFP-48 package

In some places these pins are mentioned as D0 and D1, and in other places they are mentioned as D2 and D7, which one should I use?

The physical, IC package pin numbers are irrelevant to Arduino C/C++.

See post #9 and carefully study the blog article linked therein.

Use Arduino pins designated D0, D1 with Serial1.print, D2, D7 with Serial2.print, etc.

Modbus library doesn't define your pins.
The core you use and the Serial you pick does..
No reply to post #27?

Hello Kmin
In fact I answered in post #28. I was wrong in this procedure.
Anyway, I am putting here some imgans and the code again.

[code]
/* GEX-12D Version II
 *  Firmware version: V0.8 Date: Aug 26, 2025
 *  a) Estabelecer IOs digitais e analógicos e testá-los
 *  b) Estabelecer os IOs digitais que serão usados com interrupção - Nota: Não há necessidade de usar macro digitalPinToInterrupt, pois todos os pinos suportam interrupção (usar attachInterrupt)
 *  c) Testes iniciais de movimentação do motor Me2 do disjuntor - ok
 *  d) Início dos testes de movimentação da gaveta
 *  e) teste com a estrutura "while" para ver se facilita o controle da gaveta
 *  f) A partir da V0.7 testes para comunicação com placa CPU - Protocolo Modbbus RTU (CPU -> Master & driver -> Client)
 *  g) Versão V0.7 - início dos testes com comunicação RS-485 - Protocolo Modbus RTU - Slave

*/
#include <DipSwitch.h>
#include <ModbusRTUSlave.h>                                       // Incluido na versão V0.7
//
                                                                        //#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) || defined(ARDUINO_SAM_DUE)
                                                                        // The ATmega328P and ATmega168 are used in the Ardunino UNO and similar boards.
                                                                        // The ATmega2560 and ATmega1280 are used in the Arduino Mega and similar.
                                                                        //  #define MODBUS_SERIAL Serial
                                                                        //#elif defined(ARDUINO_NANO_ESP32)
                                                                        // On the Arduino Nano ESP32, the HardwareSerial port on pins 0 and 1 is Serial0.
                                                                        //  #define MODBUS_SERIAL Serial0
                                                                        //#elif defined(ARDUINO_ARCH_STM32)
                                                                        // On ST Nucleo-64 Boards, the HardwareSerial port on pins 0 and 1 is Serial2.
                                                                        //  #define MODBUS_SERIAL Serial2
                                                                        //#else
                                                                        // On the majority of Arduino boards, the HardwareSerial port on pins 0 and 1 is Serial1.
  #define MODBUS_SERIAL Serial1
                                                                        //#endif
//
// You can change the baud, config, and unit id values if you like.
#define MODBUS_BAUD 9600                                                          // Era 38400
#define MODBUS_CONFIG SERIAL_8N1
#define MODBUS_UNIT_ID 1
//
// Define the digital and analog pin connected to the board
#define debugBt     13
#define fdcCbOn     14
#define fdcCbOff    15
#define fdcCbEnb    16
#define fdcInterLk  17
#define fdcDoor     18
#define fdcINS      19
#define fdcEXT      20
#define fdcTST      21

#define btCbOn      8
#define btCbOff     9
#define btTST       10
#define btINS       11
#define btEXT       12
#define ledCbOn     36                        // RED LED
#define ledCbOff    35                        // GREEN LED
#define ledINS      34
#define ledTST      37
#define ledEXT      38
#define debugLed    39

#define enbM2       3
#define phM2        4
#define inlM1       5
#define inhM1       6

#define isM1        A8        // 30 PE0
#define ipropoiM2   A9        // 31 PE1    

int dipSwitchPins [] = {22,23,24,25,26,27,28,29};
DipSwitch myDipSwitch (8, dipSwitchPins);
//
// ------------------ Variables -------------------------------------------------------------------------------------------------------------------------------------------------------------
int verFirmware = 8;          // Version v0.8
int flashNum = 3;
int address = 0;
bool flagDebugBt = false;

volatile bool flagCbOnFdcInt = false;             // For variables used inside function interrupt must declare "volatile"
bool flagCbOnFdc = false;
volatile bool flagCbOffFdcInt = false;
bool flagCbOffFdc = false;
volatile bool flagCbEnbFdc = false;               // x
volatile bool flagInterFdc = false;               // x
volatile bool flagDoorFdc = false;                // x
bool flagINSfdc = false;
bool flagEXTfdc = false;
bool flagTSTfdc = false;
volatile bool flagINSfdcInt = false;
volatile bool flagEXTfdcInt = false;
volatile bool flagTSTfdcInt = false;

volatile bool flagCbOnBt = false;
volatile bool flagCbOffBt = false;
volatile bool flagINSbt = false;
volatile bool flagTSTbt = false;
volatile bool flagEXTbt = false;

const int16_t dePin = 2;                               // Enalbe to TX - signal name: ser-enb (habilita tx rs-485)
ModbusRTUSlave modbus(MODBUS_SERIAL, dePin);

const uint8_t numHoldingRegisters = 6;                                                  
uint16_t holdingRegisters[numHoldingRegisters];
int tempR = 15;
int tempS = 25;
int tempT = 35;
int tempR1 = 45;
int tempS1 = 55;
int tempT1 = 65;


                                                    
//const uint8_t numCoils = 1;
//bool coils[numCoils];                               // const uint8_t numDiscreteInputs = 5; (Caso use discreteinputs)
                                                      // bool discreteInputs[numDiscreteInputs];
//
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void setup() {
    pinMode (debugBt, INPUT_PULLUP);        // On board debug button
    pinMode(fdcCbOn, INPUT_PULLUP);         // Alternative for internal pull-up - ON Circuit Breaker Limit Switch
    pinMode(fdcCbOff, INPUT_PULLUP);        // Alternative for internal pull-up - OFF Circuit Breaker Linit Switch
    pinMode(fdcCbEnb, INPUT_PULLUP);        // Alternative for internal pull-up - Enable Circuit Breaker Limit Switch
    pinMode(fdcInterLk, INPUT_PULLUP);      // Alternative for internal pull-up - Drawer Interlock Limit Switch 
    pinMode(fdcDoor, INPUT_PULLUP);         // Alternative for internal pull-up - Door Open Limit Switch
    pinMode(fdcINS, INPUT_PULLUP);          // Alternative for internal pull-up - Drawer in position INSERTED
    pinMode(fdcEXT, INPUT_PULLUP);          // Alternative for internal pull-up - Drawer in position EXTRACTED
    pinMode(fdcTST, INPUT_PULLUP);          // Alternative for internal pull-up - Drawer in position TEST
    pinMode(btCbOn, INPUT_PULLUP);          // Alternative for internal pull-up - ON Button for Circuit Breaker
    pinMode(btCbOff, INPUT_PULLUP);         // Alternative for internal pull-up - OFF Button for Circuit Breaker
    pinMode(btTST, INPUT_PULLUP);           // Alternative for internal pull-up - TEST position Button
    pinMode(btINS, INPUT_PULLUP);           // Alternative for internal pull-up - INSERT position Button
    pinMode(btEXT, INPUT_PULLUP);           // Alternative for internal pull-up - EXTRACT position Button
//    
    pinMode (debugLed, OUTPUT);             // LED onboard for Debug purpose
    pinMode (ledCbOn, OUTPUT);              // Red Lamp inside Circuit Breaber Button
    pinMode (ledCbOff, OUTPUT);             // Green Lamp inside Circuit Breaber Button
    pinMode (ledINS, OUTPUT);               // Red Lamp inside Insert Button
    pinMode (ledTST, OUTPUT);               // Yellow Lamp inside Test Button
    pinMode (ledEXT, OUTPUT);               // Green Lamp inside Extract Button
//
    pinMode (enbM2, OUTPUT);                // Driver for M2 motor - Circuit Breaker
    pinMode (phM2, OUTPUT);                 // Driver for M2 motor - Circuit Breaker
    pinMode (inlM1, OUTPUT);                // Driver for M1 motor - Drawer
    pinMode (inhM1, OUTPUT);                // Driver for M1 motor - Drawer
//
    analogReference (EXTERNAL);             // ??
    digitalWrite (inlM1, LOW);
    digitalWrite (inhM1, LOW);
                                            //   address = myDipSwitch.read();
//
/*
    attachInterrupt(fdcCbOn, CBONLS, FALLING);
    attachInterrupt(fdcCbOff, CBOFFLS, FALLING);
    attachInterrupt(fdcCbEnb, CBENBLS, FALLING);
    attachInterrupt(fdcInterLk, INTERLOCKLS, FALLING);
    attachInterrupt(fdcDoor, DOORLS, FALLING);
    attachInterrupt(fdcINS, INSERTLS, FALLING);
    attachInterrupt(fdcEXT, EXTRACTLS, FALLING);
    attachInterrupt(fdcTST, TESTLS, FALLING);
//
    attachInterrupt(btCbOn, CBONBT, FALLING);
    attachInterrupt(btCbOff, CBOFFBT, FALLING);
    attachInterrupt(btTST, TSTBT, FALLING);
    attachInterrupt(btINS, INSBT, FALLING);
    attachInterrupt(btEXT, EXTBT, FALLING);
    */
//
                                                                      // modbus.configureDiscreteInputs(discreteInputs, numDiscreteInputs);
                                                                                    //    modbus.configureCoils(coils, numCoils);
      modbus.configureHoldingRegisters(holdingRegisters, numHoldingRegisters);
      MODBUS_SERIAL.begin(MODBUS_BAUD, MODBUS_CONFIG);
      modbus.begin(MODBUS_UNIT_ID, MODBUS_BAUD, MODBUS_CONFIG);
//
 for (int i = 0; i < flashNum; i++) {                   // Initial blink
          digitalWrite (ledCbOn, HIGH);                   
          digitalWrite (dePin, HIGH);
          delay (200);                                   
          digitalWrite (ledCbOn, LOW); 
          digitalWrite (dePin, LOW);                   
          delay (200);
         }
         for (int i = 0; i < flashNum; i++) {           // 
          digitalWrite (ledCbOff, HIGH);                   
          delay (200);                                   
          digitalWrite (ledCbOff, LOW);                    
          delay (200);
         }
//        
        for (int i = 0; i < flashNum; i++) {            // 
          digitalWrite (ledINS, HIGH);                   
          delay (200);                                   
          digitalWrite (ledINS, LOW);                    
          delay (200);
         }
        for (int i = 0; i < flashNum; i++) {            // 
          digitalWrite (ledTST, HIGH);                   
          delay (200);                                   
          digitalWrite (ledTST, LOW);                    
          delay (200);
         }
         for (int i = 0; i < flashNum; i++) {           // 
          digitalWrite (ledEXT, HIGH);                   
          delay (200);                                   
          digitalWrite (ledEXT, LOW);                    
          delay (200);
         }
//----------------------------------------------------------------------------------------------------------------------------------------------------------
    for (int i = 0; i < verFirmware; i++) {             // BLINK LED version number
      digitalWrite (debugLed, HIGH);                    // 
      delay (200);                                      // 
      digitalWrite (debugLed, LOW);                     // 
      delay (200);                                      // 
      }
      flagCbOnFdc = digitalRead (fdcCbOn);
    if (flagCbOnFdc == false) {
      digitalWrite (ledCbOn,HIGH);
    }
    flagCbOffFdc = digitalRead (fdcCbOff);
    if (flagCbOffFdc == false) {
      digitalWrite (ledCbOff,HIGH);
    }
//
    flagINSfdc = digitalRead (fdcINS);
    if (flagINSfdc == false) {
      digitalWrite (ledINS,HIGH);
    }
    flagEXTfdc = digitalRead (fdcEXT);
    if (flagEXTfdc == false) {
      digitalWrite (ledEXT,HIGH);
    }
    flagTSTfdc = digitalRead (fdcTST);
    if (flagTSTfdc == false) {
      digitalWrite (ledTST,HIGH);
    }
} // END SETUP ROUTINE
//===========================================================================================================================================================================================
void loop() {
        flagDebugBt = digitalRead (debugBt);            // Debug LED test
           if (flagDebugBt == false) {
          //   BLINKLED ();                                                                                                                
         }
//---------- Rotina para ligar disjuntor ----------------------------------------------------------------------------------------------------------------------------------------------------
           flagCbOnFdc = digitalRead (fdcCbOn); 
            if (flagCbOnBt == true && flagCbOnFdc == true) {          // Se botão para ligar disjuntor e não tenho acionado o fim de curso de disjuntor ligado - permite ligar
          //    BLINKCBON ();                                           // Pisca Lamp botão On disjuntor
                 digitalWrite (ledCbOff, LOW);                       // desliga Lamp disjuntor desligado
                 digitalWrite (enbM2, LOW);                           // Liga motor do disjuntor M2 para sentido CCW
                 digitalWrite (phM2, HIGH);                           // Liga motor do disjuntor M2 para sentido CCW       
             }
            if (flagCbOnFdcInt == true && flagCbOnBt == true) {       // Se estou em rotina de ligar disjuntor e foi acionado o fim de curso de disjuntor ligado, então
              flagCbOnBt = false;                                     // reseta flag do botão
               digitalWrite (ledCbOn, HIGH);                           // e liga Lamp disjuntor ligado 
                 digitalWrite (enbM2, LOW);                           // e desliga motor M2
                 digitalWrite (phM2, LOW); 
                 delay (500);                                         // dá um tempo de 1/2s
                  digitalWrite (enbM2, HIGH);                         // Reverte sentido do motor e liga CW
                  digitalWrite (phM2, LOW);
                  delay (2000);                                       // por tempo de 2s
                    digitalWrite (enbM2, LOW);                        // desliga motor do disjuntor M2
                    digitalWrite (phM2, LOW);
                     flagCbOnFdcInt = false;                          // Reseta flag do fim de curso de disjuntor ligado    
             }
//---------------- Rotina para desligar disjuntor ------------------------------------------------------------------------------------------------------------------------------------------
           flagDoorFdc = digitalRead (fdcDoor);
             if (flagCbOffBt == true && flagDoorFdc == false) {       // Se botão para desligar disjuntor e não tenho acionado o fim de curso de disjuntor desligado - permite desligar
         //     BLINKCBOFF ();                                          // Pisca Lamp botão Off disjuntor 
                digitalWrite (ledCbOn, LOW);                          // e desliga Lamp disjuntor ligado
                digitalWrite (enbM2, HIGH);                           // Liga sentido do motor e liga CW
                digitalWrite (phM2, LOW);
             }
             if (flagCbOffFdcInt == true && flagCbOffBt == true) {    // Se estou em rotina de ligar disjuntor e foi acionado o fim de curso de disjuntor ligado, então
              flagCbOffBt = false;                                    // reseta flag do botão
              digitalWrite (ledCbOff, HIGH);                          // e liga Lamp disjuntor desligado
                 digitalWrite (enbM2, LOW);                           // e desliga motor M2 
                 digitalWrite (phM2, LOW); 
                 delay (500);                                         // dá um tempo de 1/2s
                  digitalWrite (enbM2, LOW);                          // Reverte sentido do motor e liga CCW
                  digitalWrite (phM2, HIGH);
                  delay (2000);                                       // por tempo de 2s
                    digitalWrite (enbM2, LOW);                        // desliga motor do disjuntor M2
                    digitalWrite (phM2, LOW);
                     flagCbOffFdcInt = false;                         // Reseta flag do fim de curso de disjuntor desligado    
             }
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//---------------- rotina para gaveta ir da posição extraída para posição de teste --------------------------------------------------------------------------------------------------------
            while (flagTSTbt == true && flagEXTfdc == false) {        // Enqanto o botão para enviar gaveta para posição de teste estiver acionado, e posição atual é a extraida (A)
              if (flagCbOffFdc == false && flagDoorFdc == false){     // Se não tenho porta aberta nem disjuntor ligado
          //        BLINKTST ();                                        // Pisca a Lamp amarela até atingir posição de teste
                  digitalWrite (ledINS, LOW);                         // Desliga LED Vermelho
                  digitalWrite (ledEXT, LOW);                         // desliga LED Verde
                  digitalWrite (inlM1, LOW);                          // Liga motor CW para ir para posição de extraida para posição de teste 
                  digitalWrite (inhM1, HIGH);                         // Liga motor CW para ir para posição de extraida para posição de teste 
              }
              if (flagTSTfdcInt == true) {                            // Se atingiu o FDC de posição de teste
                  digitalWrite (ledTST, HIGH);                        // Liga LED Amarelo
                  digitalWrite (inlM1, LOW);                          // Desliga motor M1
                  digitalWrite (inhM1, LOW);                          // Desliga motor M1
                  flagTSTfdcInt = false;                              // Reseta flag do FDC de posição de teste
                  flagTSTbt = false;                                  // Reseta flag do botão de posição de teste
              }
    }
//---------------- rotina para gaveta ir da posição de teste para posição de inserida --------------------------------------------------------------------------------------------------------
            while (flagINSbt == true && flagTSTfdc == true) {         // Enqanto o botão para enviar gaveta para posição de inserida estiver acionado, e posição atual é a posição de teste (B)
              if (flagCbOffFdc == false && flagDoorFdc == false){     // Se não tenho porta aberta nem disjuntor ligado
        //          BLINKINS ();                                        // Pisca a Lamp vermelha até atingir posição de Inserir
                  digitalWrite (ledTST, LOW);                         // Liga LED Amarelo
                  digitalWrite (ledEXT, LOW);                         // Liga LED Verde
                  digitalWrite (inlM1, LOW);                          // Liga motor CW para ir para posição de teste para posição de inserida 
                  digitalWrite (inhM1, HIGH);                         // Liga motor CW para ir para posição de teste para posição de inserida
              }
              if (flagINSfdcInt == true && flagEXTbt == false) {      // Se atingiu o FDC de posição de teste
                    digitalWrite (ledINS, HIGH);                      // Liga LED vermelho
                    digitalWrite (inlM1, LOW);                        // Desliga motor M1
                    digitalWrite (inhM1, LOW);                        // Desliga motor M1
                      flagINSfdcInt = false;                          // Reseta flag auxiliar do FDC da posição inserida
                      flagINSbt = false;                              // Reseta flag do botão de posição de inserida
              } 
            }                     
//---------------- rotina para a gaveta ir da posição inserida para a posição extraida -------------------------------------------------------------------------------------------------------
          while (flagEXTbt && flagINSfdc == true) {                   // Enqanto o botão para enviar gaveta para posição de extraida estiver acionado, e a posição atual é a posição inserida (C)
              if (flagCbOffFdc == false && flagDoorFdc == false){     // Se não tenho porta aberta nem disjuntor ligado
        //          BLINKEXT ();                                        // Pisca a Lamp verde até atingir posição de Inserir
                  digitalWrite (ledTST, LOW);                         // Desliga LED Amarelo
                  digitalWrite (ledINS, LOW);                         // Desliga LED Vermelho
                  digitalWrite (inlM1, HIGH);                         // Liga motor CCW para ir para posição de inserida para posição de extraida 
                  digitalWrite (inhM1, HIGH);                         // Liga motor CCW para ir para posição de inserida para posição de extraida
              }
             if (flagEXTfdcInt == true) {                             // Se atingiu o FDC de posição de teste
                    digitalWrite (ledEXT, HIGH);                      // Liga LED verde
                    digitalWrite (inlM1, LOW);                        // Desliga motor M1
                    digitalWrite (inhM1, LOW);                        // desliga motor M1
                      flagEXTfdcInt = false;                          // Reseta flag auxiliar de FDC de posição extraida
                      flagEXTbt = false;                              // Reseta flag do botão de posição de extraida
              }            
          }
//---------------- rotina para a gaveta ir da posição extraída para a posição inserida -------------------------------------------------------------------------------------------------------
          while (flagINSbt && flagEXTfdc == true) {                   // Enqanto o botão para enviar gaveta para posição de inserida estiver acionado, e a posição atual é a posição extraida (D)
              if (flagCbOffFdc == false && flagDoorFdc == false){     // Se não tenho porta aberta nem disjuntor ligado
         //         BLINKINS ();                                        // Pisca a Lamp vermelha até atingir posição de Inserir
                  digitalWrite (ledTST, LOW);                         // Desliga LED Amarelo
                  digitalWrite (ledEXT, LOW);                         // Desliga LED Verde
                  digitalWrite (inlM1, LOW);                          // Liga motor CW para ir para posição de extraida para posição de inserida 
                  digitalWrite (inhM1, HIGH);                         // Liga motor CW para ir para posição de extraida para posição de inserida
              }
             if (flagINSfdcInt == true) {                             // Se atingiu o FDC de posição de inserida
                    digitalWrite (ledINS, HIGH);                      // Liga LED vermelho
                    digitalWrite (inlM1, LOW);                        // Desliga motor M1
                    digitalWrite (inhM1, LOW);                        // Desliga motor M1
                      flagINSfdcInt = false;                          // Reseta flag de FDC por interrupção de posição inserida
                      flagINSbt = false;                              // Restea flag do botão de posição de inserir
              }            
          }
//
// ------------------------ Rotina de teste de comunicação modbus ----------------------------------------------------------------------------------------------------------------------------
          bool flagStatus1;
          bool flagStatus2;
 
            holdingRegisters[0] = tempR;
            holdingRegisters[1] = tempS;
            holdingRegisters[2] = tempT;
            holdingRegisters[3] = tempR1;
            holdingRegisters[4] = tempS1;
            holdingRegisters[5] = tempT1;

            
            modbus.poll();
                                                                                                 //   digitalWrite(flagStatus1, coils[1]);
                                                                                                //    digitalWrite(flagStatus2, coils[2]);
                                                                                                //    digitalWrite(ledPins[0], coils[1]);

                                                                                                /*
                                                                                                          if (flagStatus1 == true){
                                                                                                            digitalWrite (debugLed, HIGH);
                                                                                                            flagStatus1 = false;
                                                                                                          }
                                                                                                         if (flagStatus2 == true) {
                                                                                                            digitalWrite (debugLed, LOW);
                                                                                                            flagStatus2 = false;
                                                                                                          }
                                                                                                */
}   // end main loop
//============================================================================================================================================================================================
void CBONLS(){                                                        // Se atingiu o fim de curso de disjuntor ligado
    flagCbOnFdcInt = true;
    digitalWrite (enbM2, LOW);                                        // desliga motor M2 
    digitalWrite (phM2, LOW); 
}
void CBOFFLS() {                                                      // Se atingiu o fim de curso de disjuntor desligado
    flagCbOffFdcInt = true;
     digitalWrite (enbM2, LOW);                                       // desliga motor M2 
    digitalWrite (phM2, LOW);   
}
void CBENBLS() {                                                      // Se atingiu o fim de curso de disjuntor em posição para habilitar monobra na gaveta (talvez não precise ser por interrupção)
}
void INTERLOCKLS() {                                                  // Se atingiu o fim de curso de posição Intertrava (Cadeado) para habilitar monobra na gaveta (talvez não precise ser por interrupção)
}
void DOORLS() {                                                       // Se atingiu o fim de curso de posição da porta para habilitar monobra na gaveta (talvez não precise ser por interrupção)
}
void INSERTLS(){                                                      // Se atingiu o fim de curso de posição gaveta inserida
    flagINSfdcInt = true;
    digitalWrite (inlM1, LOW);                                        // Desliga motor M1
    digitalWrite (inhM1, LOW);                                                
}
void EXTRACTLS () {                                                   // Se atingiu o fim de curso de posição gaveta extraída
    flagEXTfdcInt = true;
    digitalWrite (inlM1, LOW);                                        // Desliga motor M1
    digitalWrite (inhM1, LOW);                                                
}
void TESTLS() {                                                       // Se atingiu o fim de curso de posição gaveta em teste
    flagTSTfdcInt = true;
    digitalWrite (inlM1, LOW);                                        // Desliga motor M1
    digitalWrite (inhM1, LOW);                                                
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void CBONBT() {                                                       // Se botão para ligar disjuntor for pressionado
    flagCbOnBt = true;
}
void CBOFFBT() {                                                      // Se botão para desligar disjuntor for pressionado
    flagCbOffBt = true;
}
void TSTBT() {                                                        // Se botão para ligar motor da gaveta para ir para posição de teste for pressionado
    flagTSTbt = true;
}
void INSBT() {                                                        // Se botão para ligar motor da gaveta para ir para posição inserida for pressionado
    flagINSbt = true;
}
void EXTBT() {                                                        // Se botão para ligar motor da gaveta para ir para posição extraída for pressionado
    flagEXTbt = true;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void BLINKLED () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (debugLed, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (debugLed, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKCBON () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledCbOn, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledCbOn, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKCBOFF () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledCbOff, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledCbOff, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKTST () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledTST, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledTST, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKINS () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledINS, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledINS, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
void BLINKEXT () {
      for (int i = 0; i < flashNum; i++) {                  // BLINK LED
        digitalWrite (ledEXT, HIGH);                      // TURN ON LED
        delay (150);                                        // INTERVAL OF 150ms 
        digitalWrite (ledEXT, LOW);                       // TURN OFF LED
        delay (150);                                        // INTERVAL OF 150ms
      }
}
// END OF PROGRAM
[/code]

If you look at pins_arduino.h of MegaCoreX you will find the following

#define PIN_HWSERIAL0_TX                PIN_PA0
#define PIN_HWSERIAL0_RX                PIN_PA1

#define PIN_HWSERIAL1_TX                PIN_PC0
#define PIN_HWSERIAL1_RX                PIN_PC1

#define PIN_HWSERIAL2_TX                PIN_PF0
#define PIN_HWSERIAL2_RX                PIN_PF1

#define PIN_HWSERIAL3_TX                PIN_PB0
#define PIN_HWSERIAL3_RX                PIN_PB1

As mentioned by @kmin in post #25 pins 44/45 are Serial (not Serial1/2/3).

So I would try that if you did not try that yet.

Thanks sterretje.
The ATMEGA4809 pins for RXD0 and TXDO are PA1 and PA0 respectively.

The hardware is tested and OK.
If anyone finds anything wrong with the code, please let me know. I'd really appreciate it.

#include <DipSwitch.h>
#include <ModbusRTUSlave.h>                          // Included in version V0.7
                                                     
#define PIN_HWSERIAL0_TX                PIN_PA0
#define PIN_HWSERIAL0_RX                PIN_PA1
#define MODBUS_SERIAL Serial

#define MODBUS_BAUD 9600                             // Was 38400
#define MODBUS_CONFIG SERIAL_8N1
#define MODBUS_UNIT_ID 1
onst int16_t dePin = 2;                               // Enalbe to TX PA2/Pin46
ModbusRTUSlave modbus(MODBUS_SERIAL, dePin);

const uint8_t numHoldingRegisters = 8;                                                  
uint16_t holdingRegisters[numHoldingRegisters];
int tempR = 15;
int tempS = 25;
int tempT = 35;
int tempR1 = 45;
int tempS1 = 55;
int tempT1 = 65;

inside setup function

      modbus.configureHoldingRegisters(holdingRegisters, numHoldingRegisters);
      MODBUS_SERIAL.begin(MODBUS_BAUD, MODBUS_CONFIG);
      modbus.begin(MODBUS_UNIT_ID, MODBUS_BAUD, MODBUS_CONFIG);

inside loop function

int segundos = millis()/1000;
            holdingRegisters[0] = tempR;
            holdingRegisters[1] = tempS;
            holdingRegisters[2] = tempT;
            holdingRegisters[3] = tempR1;
            holdingRegisters[4] = tempS1;
            holdingRegisters[5] = tempT1;
            holdingRegisters[6] = segundos;

            modbus.poll();

So #define MODBUS_SERIAL Serial which I proposed on post#8 wasn't enough? You need to define the pins manually?

The way you proposed didn't work, and even with the manual definition it doesn't work... I'm honestly very worried! I don't know what else to do, because if the code above is correct, the microcontroller isn't responding... what do you suggest besides jumping off a bridge?

Likely has no effect on your setup.

there's typo on the first line and number of registers doesn't match (7).