How to decode from modbusTCPServer.poll for Read Input or Write to Holding registers activity

How to decode from modbusTCPServer.poll for Read Input or Write to Holding registers activity?
I have written a ModbusTCP Server for my Teensy4.1 that contains 10 Holding Registers and 16Input registers used to access therest of my application board.
I am using QModMaster as the Client App.
Problem is that I can write the code within the Loop() using within it modbusTCPServer.poll()
for the update but this causes my code to cause continuous accesses even though the client
either wrote or read from register(s) only once upon pressing its button.
The good news though is that the expected data accesses seem to be correct.
The write Holding portion of code preceeds the Input Register portion befor the loop ends.

I wish to have just a write cycle upon writing to holding registers or a read cycle upon reading from Read Input registers but as said the code in the loop just continues as the loop dictates
but NOT as I would expect from the .poll for but one activity.
However if I were to use if( modbusTCPServer.poll() > 0) { instead of just having modbusTCPServer,poll() in the loop, it will now do but one cycle for a write or a read.
I need use .poll to recognize whether the current activity initiated by the client is to read or write
the appropriate registers so both functions are not envoked one after the other as seen in my code.
Also I noticed that during a read input registers activity from QModMaster I need to press the Submit button twice (double click) to see fresh data otherwise the data is old. This is an
unacceptable side affect. See the following top level of code.


void loop() {
// listen for incoming clients
EthernetClient client = EthServer.available();
if (client) { //10/03/23
Serial.println("new client");
modbusTCPServer.accept(client);
while (client.connected()) {
if( modbusTCPServer.poll() > 0) {

UpdateWriteHoldingRegs(); 
UpdateReadInputRegs(); 

PAUSE;
PAUSE;

} //if( modbusTCPServer.poll() > 0)
} //while (client.connected())
Serial.println("client disconnected");
} // If (client)

} //void loop()


I can post the functions UpdateWriteHoldingRegs() and UpdateReadInputRegs() upon request.
I really appreciate advise on this matter. Thank you.

Here is the full code I just added.

//void setup() {
//  // put your setup code here, to run once:
//
//}
//
//void loop() {
//  // put your main code here, to run repeatedly:
//
//}


/*
    Industruino Modbus TCP example master(client)addr_sel

  hardware:
  >Industruino D21G (INDIO or PROTO)
  >Ethernet module

  ArduinoModbus TCP Client Toggle = MASTER
  needs IP address of server = slave to send requests

  notes:
  >using new Ethernet library did not work
  >modbus.connected() is slow to fail

  read 8 input registers of slave
  show on LCD
  post the results to a dummy server

  Tom Tobback - March 2020
*/


//////and the SLAVE sketch:

/*
  Industruino Modbus TCP example slave(server)
  hardware:
  >Industruino D21G (INDIO or PROTO)
  >Ethernet module

  ArduinoModbus TCP Server LED = SLAVE
  responds to client = master

  set 8 input registers from 0x00
  changing every second

  Tom Tobback - March 2020
*/
////////////////// RTC MAC ////////////////////////////////////////
#include <Wire.h>  // for RTC MAC
byte mac[6];       // read from RTC

////////////////// ETHERNET //////////////////////////////////////



#include <NativeEthernet.h>  //2/8/22               // use Teensy 4.1   2/8/22
#include <ArduinoModbus.h>
#include <EEPROM.h>  //4/7/22

unsigned long TimeAct, TimePrev, InputResult, HoldingResult, StartingAddr;  //2/10/22

//2/9/22 EthernetServer server(502);
EthernetServer EthServer(502);  //2/9/22
ModbusTCPServer modbusTCPServer;

#define D35_PIN 35  //2/2/22
#define D34_PIN 34  //2/2/22
#define D33_PIN 33  //2/2/22
#define D32_PIN 32  //2/2/22
#define D31_PIN 31  //2/2/22
#define D30_PIN 30  //2/2/22
#define D29_PIN 29  //2/2/22
#define D28_PIN 28  //2/2/22

#define D26_PIN_26  //3/7/22
#define D25_PIN 25  //2/2/22
#define D24_PIN 24  //2/2/22
#define D23_PIN 23  //2/2/22
#define D22_PIN 22  //2/2/22
#define D21_PIN 21  //2/2/22
#define D20_PIN 20  //2/2/22
#define D15_PIN 15  //2/2/22
#define D14_PIN 14  //2/2/22
#define D9_PIN 9    //2/2/22
#define D8_PIN 8    //2/2/22
#define D7_PIN 7    //2/2/22
#define D6_PIN 6    //2/2/22
#define D5_PIN 5    //2/2/22
#define D4_PIN 4    //2/2/22
#define D3_PIN 3    //2/2/22
#define D2_PIN 2    //2/2/22

//// D0 - D7 for IP Octet prog ////
#define D0_PIN 0    //4/7/22
#define D1_PIN 1    //4/7/22
#define D10_PIN 10  //4/7/22
#define D11_PIN 11  //4/7/22
#define D12_PIN 12  //4/7/22
#define D16_PIN 16  //4/7/22
#define D17_PIN 17  //4/7/22
#define D18_PIN 18  //4/7/22

//// CFG for IP/Subnet/Gateway ////
#define D19_PIN_19  //3/7/22
#define D26_PIN 26  //2/2/22
#define D13_PIN_13  //5/24/23

//// Submit button to prog Octet //
#define D27_PIN 27  //2/2/22

//// Mode button for CFG/RUN //////
#define D39_PIN 39  //2/2/22

//// LED Outputs //////////////////
#define D40_PIN 40  //2/2/22
#define D41_PIN 41  //2/2/22


byte ETH_COM = 0;  //12/13/22
byte c = 0;
byte p;
byte addr_sel = 0;   //4/8/22
byte addr_sel_bit2;  //5/24/23
//byte ip_addr[3]; //4/8/22
byte submit = 1;                     //4/8/22 pushbutton off as default
byte presubmit = 1;                  //5/23/23
byte submit_first;                   //5/23/23
byte octet_data[] = { 0, 0, 0, 0 };  //4/8/22
int i;                               //4/7/22
int d;
int reg_idx;
unsigned long last_change1 = millis();
unsigned long last_change2 = millis();
unsigned long modbus_timeout = 1000;
unsigned long last_modbus_connection;
int hr_var, hr_var1;  //2/21/22
int i_var = 1;        //1/29/23
int i_var_old = 0;    //4/11/22
int i_var_break = 0;  //4/11/22
uint16_t vals[] = { 54, 62, 83, 76, 22, 11, 99, 40 };
//uint16_t  IOPin[] = {2, 3, 4, 5, 6, 7, 8, 9, 14, 15, 20, 21, 22, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 0, 1, 10, 11, 12, 16, 17, 18, 19, 26, 27, 39, 40, 41};  //4/7/22  Total: 41 pins
uint16_t IOPin[] = { 2, 3, 4, 5, 6, 7, 8, 9, 14, 15, 20, 21, 22, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 0, 1, 10, 11, 12, 16, 17, 18, 19, 26, 27, 39, 40, 41, 13 };  //5/24/23 Added pin 13 for BITPIN_IDX = 41  Total: 42 pins
uint16_t bitpin_idx;                                                                                                                                                                  //2/19/22

long debounceDelay = 150;   //5/23/23
long lastDebounceTime = 0;  //5/23/23

/**********************Delays  2/17/22 ****************************************/
#define NOP3 "nop\n\t" \
  "nop\n\t" \
  "nop\n\t"
#define NOP4 "nop\n\t" \
  "nop\n\t" \
  "nop\n\t" \
  "nop\n\t"
#define NOP6 "nop\n\t" \
  "nop\n\t" \
  "nop\n\t" \
  "nop\n\t" \
  "nop\n\t" \
  "nop\n\t"

// P1-5 are 100-500 ns pauses, tested with an oscilloscope (2 second
// display persistence) and a Teensy 3.2 compiling with
// Teensyduino/Arduino 1.8.1, "faster" setting
//#if F_CPU == 96000000
#define P1 __asm__(NOP4 NOP4)
#define P2 __asm__(NOP6 NOP6 NOP6)
#define P3 __asm__(NOP6 NOP6 NOP6 NOP6 NOP3)
#define P4 __asm__(NOP6 NOP6 NOP6 NOP6 NOP6 NOP4 NOP4)
#define P5 __asm__(NOP6 NOP6 NOP6 NOP6 NOP6 NOP6 NOP6 NOP4 NOP3)

#define PAUSE P5  //2/17/22
/************************Delays  2/17/22 *******************************/


////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

void setup() {

  for (bitpin_idx = 0; bitpin_idx < 16; bitpin_idx++)  //2/21/22
    pinMode(IOPin[bitpin_idx], INPUT);

  for (bitpin_idx = 16; bitpin_idx < 27; bitpin_idx++)  //3/7/22  11 pins
    pinMode(IOPin[bitpin_idx], OUTPUT);                 // Init all Digital I/O as Outputs//2/21/22

  for (bitpin_idx = 27; bitpin_idx < 39; bitpin_idx++)  //4/8/22   14 pins
    pinMode(IOPin[bitpin_idx], INPUT);                  // Init all Digital I/O as Inputs//4/8/22

  for (bitpin_idx = 39; bitpin_idx < 41; bitpin_idx++)  //4/8/22   2 pins for LED0, LED1
    pinMode(IOPin[bitpin_idx], OUTPUT);                 // Init all Digital I/O as Outputs//4/8/22

  pinMode(IOPin[41], INPUT);  //5/24/23  Added for SOM_CFG_ADDR2 which was missing in code! It's bitpin_idx = 41.


  digitalWriteFast(IOPin[26], LOW); //initialize POLL Flag to 0 10/03/23

  //Initialize serial and wait for port to open:
  SerialUSB.begin(115200);  ///4/7/22
  delay(2000);              ///4/7/22

  SerialUSB.println("Modbus TCP Server = SLAVE");  ///4/7/22

  //2/9/22 readMACfromRTC();             // MAC stored in RTC eeprom
  byte mac[] = { 0x04, 0xe9, 0xe5, 0x0f, 0xb9, 0xcd };  //2/9/22 Define MAC address
  byte ip[] = { 192, 168, 1, 10 };                      // this is server=slave  //// 2/9/22
  byte subnet[] = { 255, 255, 255, 0 };                 //2/9/22  Define subnet mask
  byte gateway[] = { 192, 168, 1, 1 };                  //4/10/22

  //gateway[3] = 1;    //4/10/22

  //  Ethernet.begin(mac, industruino_ip, subnet); //2/9/22


  /********************************  11/18/22 commented out below ******************************************/
  //
  //  /********** 4/12/22 ***************************/
  for (i = 0; i < 4; i++) {
    //12/13/22 SerialUSB.println(ip[i], DEC);
    //12/13/22  if (i < 4)
    SerialUSB.println(i, DEC);
    delay(1);
    ip[i] = EEPROM.read(i);  //4/12/22
    delay(1);
    SerialUSB.println(ip[i], DEC);
    delay(1);
    subnet[i] = EEPROM.read(i + 4);  //4/12/22
    delay(1);
    SerialUSB.println(subnet[i], DEC);
    delay(1);
    gateway[i] = EEPROM.read(i + 8);  //4/12/22
    delay(1);
    SerialUSB.println(gateway[i], DEC);
    delay(1);
  }

  //  /*********************************************/


  //
  //  /******** Exercise in using Serial Monitor ****************************************************/
  //  //  for (i = 0; i < 4; i++)
  //  //    SerialUSB.println(ip[i], DEC);  ///4/7/22
  //  //
  //  //  SerialUSB.println("Write IP Address to EEPROM"); ///4/7/22
  //  //  for (int i = 0; i < 4; i++)
  //  //    EEPROM.write(i, ip[i] + 1);  //4/7/22
  //  //
  //  //  SerialUSB.println("Read IP Address from EEPROM"); ///4/7/22
  //  //  for (int i = 0; i < 4; i++)
  //  //    ip[i] = EEPROM.read(i);  //4/7/22
  //  //
  //  //  for (i = 0; i < 4; i++)
  //  //    SerialUSB.println(ip[i], DEC);  ///4/7/22
  //  /*******************************************************************************************/
  //
  //
  //
  ////// 4/8/22  Read Switches to set up IP/Subnet/Gateway Addr ///////////////////////////
  digitalWrite(IOPin[39], LOW);  // init LED 0 = 0
  digitalWrite(IOPin[40], LOW);  // init LED 1 = 0


  i_var = digitalRead(IOPin[38]);  // CFG/RUN MODE

  //1/29/23 while (i_var == 1) //4/12/22
  while (i_var == 0)  //1/29/23
  {
    i_var = digitalRead(IOPin[38]);  // CFG/RUN MODE

    //     SerialUSB.println("i_var:"); ///4/11/22
    //     SerialUSB.println(i_var, DEC); //debug 4/11/22
    //     SerialUSB.println("i_var_old:"); ///4/11/22
    //     SerialUSB.println(i_var_old, DEC); //debug 4/11/22

    // if (i_var == 1)
    // {




    //5/25/23  addr_sel_bit2 = digitalRead(IOPin[41]); // 5/24/23  Allows for decoding of SOM1 vs SOM0 with addr_sel bit 2.

    //SerialUSB.println(addr_sel, DEC); //4/11/22 Debug

    for (i = 0; i < 4; i++) {


      addr_sel = 0;  //5/24/23
      for (bitpin_idx = 35; bitpin_idx < 37; bitpin_idx++)
        addr_sel = addr_sel + (digitalRead(IOPin[bitpin_idx]) << bitpin_idx - 35);  /// added ( ) around bitpin-35    11/17/22



      do {
        presubmit = digitalRead(IOPin[37]);  //submit pushbutton for octet data  //5/23/23
        // Filter out any noise by setting a time bufer
        if ((millis() - lastDebounceTime) > debounceDelay) {
          if (presubmit == 0) {
            submit = 0;
            lastDebounceTime = millis();
          } else if (presubmit == 1) {
            submit = 1;
            lastDebounceTime = millis();
          }
        }  //if ((millis() - lastDebounceTime) > debounceDelay);


        delay(1);  // switch bounce

        /****** 4/12/22 ************************************************************************/
        if (submit == 1) {    // pushbutton off
          octet_data[i] = 0;  // init octet_data
          for (bitpin_idx = 27; bitpin_idx < 35; bitpin_idx++) {
            octet_data[i] = octet_data[i] + (digitalRead(IOPin[bitpin_idx]) << (bitpin_idx - 27));
            delay(1);
          }  // for (bitpin_idx = 27; bitpin_idx < 35;bitpin_idx++)
        }    // (submit == 1)
        /*************************************************************************************/



        if (submit == 0)  //pushbutton depressed on for SOM0
        {
          addr_sel = 0;  //5/25/23
          for (bitpin_idx = 35; bitpin_idx < 37; bitpin_idx++)
            addr_sel = addr_sel + (digitalRead(IOPin[bitpin_idx]) << bitpin_idx - 35);  /// added ( ) around bitpin-35    11/17/22


          if (addr_sel == 0) {
            SerialUSB.println("addr_sel:");
            SerialUSB.println(addr_sel, DEC);  //12/14/22
            ip[i] = octet_data[i];
            SerialUSB.println("IPAddr: ");  //12/14/22
            SerialUSB.println(ip[i], DEC);  //12/14/22
            EEPROM.write(i, ip[i]);
          } else if (addr_sel == 1) {
            SerialUSB.println("addr_sel:");
            SerialUSB.println(addr_sel, DEC);  //12/14/22
            subnet[i] = octet_data[i];
            SerialUSB.println("Subnet: ");      //12/14/22
            SerialUSB.println(subnet[i], DEC);  //12/14/22
            EEPROM.write(i + 4, subnet[i]);
          } else if (addr_sel == 2) {
            SerialUSB.println("addr_sel:");
            SerialUSB.println(addr_sel, DEC);  //12/14/22
            gateway[i] = octet_data[i];
            SerialUSB.println("Gateway: ");      //12/14/22
            SerialUSB.println(gateway[i], DEC);  //12/14/22
            EEPROM.write(i + 8, gateway[i]);
          }


          //            SerialUSB.println(submit, DEC); //debug 4/11/22
          //            delay(1);
          //            SerialUSB.println(addr_sel, DEC); //debug 4/11/22
          //            delay(1);
          //            SerialUSB.println(i, DEC); //debug 4/11/22
          //            delay(1);
          //            SerialUSB.println(octet_data[i], DEC); //debug 4/11/22
          //            delay(1);
          //            SerialUSB.println(ip[i], DEC); //debug 4/11/22
          //            delay(1);
          //            SerialUSB.println("   "); //debug 4/11/22

          if (i == 0) {                     // was i == 1
            digitalWrite(IOPin[39], HIGH);  //was HIGH   5/24/23
            digitalWrite(IOPin[40], LOW);   // was LOW
          } else if (i == 1) {              // was i == 2
            digitalWrite(IOPin[39], LOW);   // was LOW   5/24/23
            digitalWrite(IOPin[40], HIGH);  // was HIGH
          } else if (i == 2) {              //  was i == 3
            digitalWrite(IOPin[39], HIGH);  // was HIGH  5/24/23
            digitalWrite(IOPin[40], HIGH);  // was HIGH
          }

          else {
            digitalWrite(IOPin[39], LOW);  // was LOW   5/24/23
            digitalWrite(IOPin[40], LOW);  // was LOW
          }                                // else

        }  // if(submit == 0)

        i_var = digitalRead(IOPin[38]);  // CFG/RUN MODE

        //1/29/23 } while (submit == 1 and i_var == 1); // pushbutton off and mode cfg
      } while (submit == 1 and i_var == 0);  // pushbutton off and mode cfg  //1/29/23

      //1/29/23  while (submit == 0 and i_var == 1) {  // pushbutton depressed on and mode cfg
      while (submit == 0 and i_var == 0) {  // pushbutton depressed on and mode cfg  //1/29/23
        i_var = digitalRead(IOPin[38]);     // CFG/RUN MODE
        //5/25/23   submit = digitalRead(IOPin[37]);
        //5/25/23   for (p = 0; p < 10; p++) // switch debounce
        //5/25/23    PAUSE;
        //5/25/23   submit = digitalRead(IOPin[37]);

        presubmit = digitalRead(IOPin[37]);  //submit pushbutton for octet data  //5/23/23
        // Filter out any noise by setting a time bufer
        if ((millis() - lastDebounceTime) > debounceDelay) {
          if (presubmit == 0) {
            submit = 0;
            lastDebounceTime = millis();
          } else if (presubmit == 1) {
            submit = 1;
            lastDebounceTime = millis();
          }
        }  //if ((millis() - lastDebounceTime) > debounceDelay);



      }  // while (submit == 0 and i_var == 1)


    }  // for (i=0; i < 4; i++)

    i_var = digitalRead(IOPin[38]);  // CFG/RUN MODE
    //1/29/23  if (i_var == 0)
    if (i_var == 1)                  //1/29/23
      break;

    // } // if (i_var == 1)

    //1/29/23  } //while (i_var == 1);
  }  //while (i_var == 0);  //1/29/23

  digitalWrite(IOPin[39], LOW);
  digitalWrite(IOPin[40], LOW);

  /**************************************** 11/18/22 commented out above  *****************************************/


  // for (int i = 0; i < 4; i++) {
  //   EEPROM.write(i, ip[i]);  //4/7/22
  // EEPROM.write(i+4, subnet[i]);  //4/10/22
  // EEPROM.write(i+8, gateway[i]); //4/10/22
  // }

  SerialUSB.println("Past   ");  ///4/11/22

  //  for (i = 0; i < 4; i++)
  //   SerialUSB.println(ip[i], DEC);
  /////////////////////////////////////////////////////////////////////////////////////////



  Ethernet.begin(mac, ip, subnet);  //2/9/22  Assign MAC, IP and subnet mask

  // start the TCP server
  EthServer.begin();        //2/9/22  start listening to clients
  modbusTCPServer.begin();  //2/10/22 start listening to clients


  // configure Holding registers at address 0x00
  HoldingResult = modbusTCPServer.configureHoldingRegisters(0x0000, 10);  // 2/10/22

  // configure input registers at address 0x00
  InputResult = modbusTCPServer.configureInputRegisters(0x0010, 16);  //7/20/23

  digitalWriteFast(IOPin[16], LOW);   // CLK Init Low           //2/19/22
  digitalWriteFast(IOPin[17], HIGH);  // CSH Init Holding Registers High disabled //2/19/22
  digitalWriteFast(IOPin[18], HIGH);  // CSI Init Read Input Registers High disabled //2/19/22
  digitalWriteFast(IOPin[19], HIGH);  // CSI Init R/W to Read mode //2/19/22
  digitalWriteFast(IOPin[20], HIGH);  // CSH_B Init Holding Registers High disabled //2/25/22
  digitalWriteFast(IOPin[21], HIGH);  // CSI_8 Init Read Input Registers x0-x7 High disabled //2/25/22
  digitalWriteFast(IOPin[22], HIGH);  // CSI_18 Init Read Input Registers x10-x17 High disabled //2/25/22
}

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

void loop() {
  // listen for incoming clients
  EthernetClient client = EthServer.available();  //2/9/22
  if (client) { //10/03/23
    Serial.println("new client"); //10/03/23
    modbusTCPServer.accept(client);
    while (client.connected()) {  ///9/14/23
      if ( modbusTCPServer.poll() > 0) { //10/03/23

        UpdateWriteHoldingRegs(); //10/03/23
        UpdateReadInputRegs(); //10/03/2

        //  PAUSE;
        //  PAUSE;
        PAUSE;
        PAUSE;
        // digitalWriteFast(IOPin[26], HIGH);  //3/3/22  CSY to end SOM communication wih FPGA
        /****************************************************************************************/
      } //if( modbusTCPServer.poll() > 0) 10/03/23
    }  //while (client.connected()) ///9/14/23
    Serial.println("client disconnected"); //10/03/23
  } // 10/03/23 If (client)

}  //void loop()





void UpdateWriteHoldingRegs() {

  /******************** Write Holding Register data to Digital Outputs *************************/
  for (bitpin_idx = 0; bitpin_idx < 16; bitpin_idx++)  //2/21/22
    pinMode(IOPin[bitpin_idx], OUTPUT);                //  Data Pin init for Write Holding registers //2/21/22
  PAUSE;

  //////////////  Write Holding Registers 0x0000 to 0x0009  ////////////////
  //digitalWriteFast(IOPin[26], LOW);  //3/3/22  CYN Active Low to begin SOM communication with FPGA
  for (reg_idx = 0; reg_idx < 10; reg_idx++) {
    hr_var = modbusTCPServer.holdingRegisterRead(reg_idx);



    ////// Output address //////////////////////////////////////////////

    digitalWriteFast(IOPin[23], 0);
    digitalWriteFast(IOPin[24], 0);
    digitalWriteFast(IOPin[25], 0);

    if (reg_idx == 0) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 0);
    }
    if (reg_idx == 1) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 0);
    }
    if (reg_idx == 2) {
      digitalWriteFast(IOPin[26], HIGH); //10/03/23  Flag to detect Poll for Write to Holding Registers 2 - 9
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 0);
    }
    if (reg_idx == 3) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 0);
    }
    if (reg_idx == 4) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 1);
    }
    if (reg_idx == 5) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 1);
    }
    if (reg_idx == 6) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 1);
    }
    if (reg_idx == 7) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 1);
    }
    if (reg_idx == 8) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 1);
    }
    if (reg_idx == 9) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 1);
    }

    //////////////////////////////////////////////////////////////////


    for (bitpin_idx = 0; bitpin_idx < 16; bitpin_idx++) {
      //2/19/22 alternate way:
      digitalWriteFast(IOPin[bitpin_idx], bitRead(hr_var, bitpin_idx));
    }                                  // for(bitpin_idx = 0; bitpin_idx < 16; bitpin_idx++)
    digitalWriteFast(IOPin[17], LOW);  //CSH Active Low
    PAUSE;
    digitalWriteFast(IOPin[19], LOW);  // R/W Low for Write
    PAUSE;
    digitalWriteFast(IOPin[16], HIGH);  // CLK High rising Edge
    PAUSE;
    digitalWriteFast(IOPin[16], LOW);  // CLK Low  falling edge
    PAUSE;
    digitalWriteFast(IOPin[19], HIGH);  // R/W High for Read
    PAUSE;
    digitalWriteFast(IOPin[17], HIGH);  //CSH Disabled High

    digitalWriteFast(IOPin[26], LOW); //10/03/23 Flag to detect end of Write Holding Registers 2 - 9

  }                                     //for (reg_idx = 0; reg_idx < 10; regidx++)

} //Void UpdateWriteHoldingRegs() {




void UpdateReadInputRegs() {
  /*************** Read Input Registers data from Digital Inputs ********************************/
  for (bitpin_idx = 0; bitpin_idx < 16; bitpin_idx++)  //2/21/22
    pinMode(IOPin[bitpin_idx], INPUT);                 // Data Pin init Read Input registers //2/21/22
  PAUSE;

  //////////////  Read Input Registers 0x0000 to 0x0007  ///////////////
  for (reg_idx = 16; reg_idx < 32; reg_idx++) {
    i_var = 0x0000;
    /**********************************************/


    digitalWriteFast(IOPin[24], 1);  //7/17/23  //Addr bit 4
    digitalWriteFast(IOPin[25], 0);  //Addr bit 5

    if (reg_idx == 16)
    { digitalWriteFast(IOPin[26], HIGH);  //10/03/23  Flag to detect Read Input Registers 16-31
      digitalWriteFast(IOPin[20], 0); //Addr bit 0
      digitalWriteFast(IOPin[21], 0); //Addr bit 1
      digitalWriteFast(IOPin[22], 0); //Addr bit 2
      digitalWriteFast(IOPin[23], 0); //Addr bit 3

    }
    if (reg_idx == 17) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 0); //Addr bit 3

    }
    if (reg_idx == 18) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 0); //Addr bit 3

    }
    if (reg_idx == 19) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 0); //Addr bit 3

    }
    if (reg_idx == 20) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 0); //Addr bit 3

    }
    if (reg_idx == 21) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 0); //Addr bit 3
    }

    if (reg_idx == 22) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 0); //Addr bit 3
    }

    if (reg_idx == 23) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 0); //Addr bit 3
    }


    if (reg_idx == 24) {
      digitalWriteFast(IOPin[20], 0); //Addr bit 0
      digitalWriteFast(IOPin[21], 0); //Addr bit 1
      digitalWriteFast(IOPin[22], 0); //Addr bit 2
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }

    if (reg_idx == 25) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }

    if (reg_idx == 26) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }

    if (reg_idx == 27) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }

    if (reg_idx == 28) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }

    if (reg_idx == 29) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }

    if (reg_idx == 30) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }

    if (reg_idx == 31) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }


    digitalWriteFast(IOPin[18], LOW);  //CSI Active Low

    digitalWriteFast(IOPin[26], LOW);  //10/03/23  Flag to detect end of Read Input Registers 16-31

    PAUSE; //7/18/23
    PAUSE; //7/18/23
    PAUSE; //7/18/23
    digitalWriteFast(IOPin[16], HIGH);  // CLK High rising Edge
    PAUSE;
    PAUSE;
    for (bitpin_idx = 0; bitpin_idx < 16; bitpin_idx++) {
      i_var = i_var + (digitalRead(IOPin[bitpin_idx]) << bitpin_idx);
      PAUSE;
      PAUSE;//7/18/23
      PAUSE;//7/18/23
      PAUSE;//7/18/23
    }  // for(bitpin_idx = 0; bitpin_idx < 16; bitpin_idx++)


    modbusTCPServer.inputRegisterWrite(reg_idx, i_var);

    /***** Debug ********************/
    SerialUSB.println("reg_idx:");
    SerialUSB.println(reg_idx, HEX);
    SerialUSB.println("i_var:");
    SerialUSB.println(i_var, HEX);
    /********************************/
    PAUSE; //7/18/23
    PAUSE; //7/18/23
    PAUSE; //7/18/23
    PAUSE; //7/28/23

    digitalWriteFast(IOPin[16], LOW);   // CLK Low  falling edge
    PAUSE; //7/18/23
    PAUSE; //7/18/23
    // PAUSE; //7/18/23
    // PAUSE; //7/28/23
    digitalWriteFast(IOPin[18], HIGH);  //CSI Disabled High
    PAUSE; //7/18/23
    PAUSE; //7/18/23
    // PAUSE; //7/18/23
    // PAUSE; //7/28/23
    // PAUSE; //7/18/23
    //PAUSE; //7/28/23
  }
} // void UpdateInputReadRegs() {

Please, post your code in the proper format using code tags. If you're unsure, you'll find help here:

Although you don't appear to be a newbie, I think it's time for a refresher.

Oh, and please, stop SHOUTING. You'll wake the moderators, and then you'll have no end of headaches.

I edited the post by selecting the code portion, right clicking for the codetags feature for what I selected. I could have also added to it my functions that are called from the top level be it would cause the post to be much longer. I can still do so if you feel it is warranted in the near term.

BTW, I still was unable to see my post in the forum.Arduino.cc until I clicked on the highlighted in blue YourPost in the emailed message,

So you're not working on the forum webpage, but via email. I have no advice to give, except that your post appears ill-formatted on the forum.
The bold and all-caps are a well-known internet short form for strong statements and shouting, and is perceived that way by most.

I'd suggest you use the forum web interface and give up with the email, I lasted with that about 2-3 messages back when I started.
Good luck.

I did not use bold all-caps upon my submission so I did not know what you were referring to
except that it appeared that way after the fact. There was never any anger or aggressiveness ever intended from me I can assure you. So I will go back to it and see if I can reedit it for small caps in a normal font.
As one of the methods proposed I selected the code portion and right clicked on it for code tags.
Did that work ? If not , can I instead insert before the start and after the end of the code?
Would that do?

I tried to get it out of Bold thru reediting but it wants to stay bold.
I used the B icon in the editor upon selecting the text but it does not help.
Maybe I should use a different editor and just paste it in there?
Can I just type in before the start and after the end of the code section?
Would that work?

At this point, I wouldn't sweat it, you've tried to fix it. If someone with the access rights can fix, they might, or might not.

But, the code posting is trickier. The reason we want it that way is so we can reliably copy and paste it into our own IDE, where we can adjust it and add to it for you. So,

In the Arduino IDE, do the following. Press ctrl-T. Press ctrl-shft-C. That has formatted and copied your entire ino content for forum use.
Now, in a new message box (below this message), press ctrl-V. The ino content should be correctly inserted and formatted in your message. That is the most reliable, and easiest, way to post code.

Hope that helps, if not, I'll be watching for a reply.

There was never any anger or aggressiveness ever intended from me I can assure you

Was quite sure there wasn't, but that's how it comes across when formatted that way. It's instinctive.

That, I suspect, is just a latency of the email-processing vs being live-on-forum. It's unfortunate, and another reason to abandon the email approach - you'll be forever out-of-sync with answers on-forum.

Below you will see my the entirety of the code with code formatting.
I hope this adds clarity to my questions.

//void setup() {
//  // put your setup code here, to run once:
//
//}
//
//void loop() {
//  // put your main code here, to run repeatedly:
//
//}


/*
    Industruino Modbus TCP example master(client)addr_sel

  hardware:
  >Industruino D21G (INDIO or PROTO)
  >Ethernet module

  ArduinoModbus TCP Client Toggle = MASTER
  needs IP address of server = slave to send requests

  notes:
  >using new Ethernet library did not work
  >modbus.connected() is slow to fail

  read 8 input registers of slave
  show on LCD
  post the results to a dummy server

  Tom Tobback - March 2020
*/


//////and the SLAVE sketch:

/*
  Industruino Modbus TCP example slave(server)
  hardware:
  >Industruino D21G (INDIO or PROTO)
  >Ethernet module

  ArduinoModbus TCP Server LED = SLAVE
  responds to client = master

  set 8 input registers from 0x00
  changing every second

  Tom Tobback - March 2020
*/
////////////////// RTC MAC ////////////////////////////////////////
#include <Wire.h>  // for RTC MAC
byte mac[6];       // read from RTC

////////////////// ETHERNET //////////////////////////////////////



#include <NativeEthernet.h>  //2/8/22               // use Teensy 4.1   2/8/22
#include <ArduinoModbus.h>
#include <EEPROM.h>  //4/7/22

unsigned long TimeAct, TimePrev, InputResult, HoldingResult, StartingAddr;  //2/10/22

//2/9/22 EthernetServer server(502);
EthernetServer EthServer(502);  //2/9/22
ModbusTCPServer modbusTCPServer;

#define D35_PIN 35  //2/2/22
#define D34_PIN 34  //2/2/22
#define D33_PIN 33  //2/2/22
#define D32_PIN 32  //2/2/22
#define D31_PIN 31  //2/2/22
#define D30_PIN 30  //2/2/22
#define D29_PIN 29  //2/2/22
#define D28_PIN 28  //2/2/22

#define D26_PIN_26  //3/7/22
#define D25_PIN 25  //2/2/22
#define D24_PIN 24  //2/2/22
#define D23_PIN 23  //2/2/22
#define D22_PIN 22  //2/2/22
#define D21_PIN 21  //2/2/22
#define D20_PIN 20  //2/2/22
#define D15_PIN 15  //2/2/22
#define D14_PIN 14  //2/2/22
#define D9_PIN 9    //2/2/22
#define D8_PIN 8    //2/2/22
#define D7_PIN 7    //2/2/22
#define D6_PIN 6    //2/2/22
#define D5_PIN 5    //2/2/22
#define D4_PIN 4    //2/2/22
#define D3_PIN 3    //2/2/22
#define D2_PIN 2    //2/2/22

//// D0 - D7 for IP Octet prog ////
#define D0_PIN 0    //4/7/22
#define D1_PIN 1    //4/7/22
#define D10_PIN 10  //4/7/22
#define D11_PIN 11  //4/7/22
#define D12_PIN 12  //4/7/22
#define D16_PIN 16  //4/7/22
#define D17_PIN 17  //4/7/22
#define D18_PIN 18  //4/7/22

//// CFG for IP/Subnet/Gateway ////
#define D19_PIN_19  //3/7/22
#define D26_PIN 26  //2/2/22
#define D13_PIN_13  //5/24/23

//// Submit button to prog Octet //
#define D27_PIN 27  //2/2/22

//// Mode button for CFG/RUN //////
#define D39_PIN 39  //2/2/22

//// LED Outputs //////////////////
#define D40_PIN 40  //2/2/22
#define D41_PIN 41  //2/2/22


byte ETH_COM = 0;  //12/13/22
byte c = 0;
byte p;
byte addr_sel = 0;   //4/8/22
byte addr_sel_bit2;  //5/24/23
//byte ip_addr[3]; //4/8/22
byte submit = 1;                     //4/8/22 pushbutton off as default
byte presubmit = 1;                  //5/23/23
byte submit_first;                   //5/23/23
byte octet_data[] = { 0, 0, 0, 0 };  //4/8/22
int i;                               //4/7/22
int d;
int reg_idx;
unsigned long last_change1 = millis();
unsigned long last_change2 = millis();
unsigned long modbus_timeout = 1000;
unsigned long last_modbus_connection;
int hr_var, hr_var1;  //2/21/22
int i_var = 1;        //1/29/23
int i_var_old = 0;    //4/11/22
int i_var_break = 0;  //4/11/22
uint16_t vals[] = { 54, 62, 83, 76, 22, 11, 99, 40 };
//uint16_t  IOPin[] = {2, 3, 4, 5, 6, 7, 8, 9, 14, 15, 20, 21, 22, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 0, 1, 10, 11, 12, 16, 17, 18, 19, 26, 27, 39, 40, 41};  //4/7/22  Total: 41 pins
uint16_t IOPin[] = { 2, 3, 4, 5, 6, 7, 8, 9, 14, 15, 20, 21, 22, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 0, 1, 10, 11, 12, 16, 17, 18, 19, 26, 27, 39, 40, 41, 13 };  //5/24/23 Added pin 13 for BITPIN_IDX = 41  Total: 42 pins
uint16_t bitpin_idx;                                                                                                                                                                  //2/19/22

long debounceDelay = 150;   //5/23/23
long lastDebounceTime = 0;  //5/23/23

/**********************Delays  2/17/22 ****************************************/
#define NOP3 "nop\n\t" \
  "nop\n\t" \
  "nop\n\t"
#define NOP4 "nop\n\t" \
  "nop\n\t" \
  "nop\n\t" \
  "nop\n\t"
#define NOP6 "nop\n\t" \
  "nop\n\t" \
  "nop\n\t" \
  "nop\n\t" \
  "nop\n\t" \
  "nop\n\t"

// P1-5 are 100-500 ns pauses, tested with an oscilloscope (2 second
// display persistence) and a Teensy 3.2 compiling with
// Teensyduino/Arduino 1.8.1, "faster" setting
//#if F_CPU == 96000000
#define P1 __asm__(NOP4 NOP4)
#define P2 __asm__(NOP6 NOP6 NOP6)
#define P3 __asm__(NOP6 NOP6 NOP6 NOP6 NOP3)
#define P4 __asm__(NOP6 NOP6 NOP6 NOP6 NOP6 NOP4 NOP4)
#define P5 __asm__(NOP6 NOP6 NOP6 NOP6 NOP6 NOP6 NOP6 NOP4 NOP3)

#define PAUSE P5  //2/17/22
/************************Delays  2/17/22 *******************************/


////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

void setup() {

  for (bitpin_idx = 0; bitpin_idx < 16; bitpin_idx++)  //2/21/22
    pinMode(IOPin[bitpin_idx], INPUT);

  for (bitpin_idx = 16; bitpin_idx < 27; bitpin_idx++)  //3/7/22  11 pins
    pinMode(IOPin[bitpin_idx], OUTPUT);                 // Init all Digital I/O as Outputs//2/21/22

  for (bitpin_idx = 27; bitpin_idx < 39; bitpin_idx++)  //4/8/22   14 pins
    pinMode(IOPin[bitpin_idx], INPUT);                  // Init all Digital I/O as Inputs//4/8/22

  for (bitpin_idx = 39; bitpin_idx < 41; bitpin_idx++)  //4/8/22   2 pins for LED0, LED1
    pinMode(IOPin[bitpin_idx], OUTPUT);                 // Init all Digital I/O as Outputs//4/8/22

  pinMode(IOPin[41], INPUT);  //5/24/23  Added for SOM_CFG_ADDR2 which was missing in code! It's bitpin_idx = 41.


  digitalWriteFast(IOPin[26], LOW); //initialize POLL Flag to 0 10/03/23

  //Initialize serial and wait for port to open:
  SerialUSB.begin(115200);  ///4/7/22
  delay(2000);              ///4/7/22

  SerialUSB.println("Modbus TCP Server = SLAVE");  ///4/7/22

  //2/9/22 readMACfromRTC();             // MAC stored in RTC eeprom
  byte mac[] = { 0x04, 0xe9, 0xe5, 0x0f, 0xb9, 0xcd };  //2/9/22 Define MAC address
  byte ip[] = { 192, 168, 1, 10 };                      // this is server=slave  //// 2/9/22
  byte subnet[] = { 255, 255, 255, 0 };                 //2/9/22  Define subnet mask
  byte gateway[] = { 192, 168, 1, 1 };                  //4/10/22

  //gateway[3] = 1;    //4/10/22

  //  Ethernet.begin(mac, industruino_ip, subnet); //2/9/22


  /********************************  11/18/22 commented out below ******************************************/
  //
  //  /********** 4/12/22 ***************************/
  for (i = 0; i < 4; i++) {
    //12/13/22 SerialUSB.println(ip[i], DEC);
    //12/13/22  if (i < 4)
    SerialUSB.println(i, DEC);
    delay(1);
    ip[i] = EEPROM.read(i);  //4/12/22
    delay(1);
    SerialUSB.println(ip[i], DEC);
    delay(1);
    subnet[i] = EEPROM.read(i + 4);  //4/12/22
    delay(1);
    SerialUSB.println(subnet[i], DEC);
    delay(1);
    gateway[i] = EEPROM.read(i + 8);  //4/12/22
    delay(1);
    SerialUSB.println(gateway[i], DEC);
    delay(1);
  }

  //  /*********************************************/


  //
  //  /******** Exercise in using Serial Monitor ****************************************************/
  //  //  for (i = 0; i < 4; i++)
  //  //    SerialUSB.println(ip[i], DEC);  ///4/7/22
  //  //
  //  //  SerialUSB.println("Write IP Address to EEPROM"); ///4/7/22
  //  //  for (int i = 0; i < 4; i++)
  //  //    EEPROM.write(i, ip[i] + 1);  //4/7/22
  //  //
  //  //  SerialUSB.println("Read IP Address from EEPROM"); ///4/7/22
  //  //  for (int i = 0; i < 4; i++)
  //  //    ip[i] = EEPROM.read(i);  //4/7/22
  //  //
  //  //  for (i = 0; i < 4; i++)
  //  //    SerialUSB.println(ip[i], DEC);  ///4/7/22
  //  /*******************************************************************************************/
  //
  //
  //
  ////// 4/8/22  Read Switches to set up IP/Subnet/Gateway Addr ///////////////////////////
  digitalWrite(IOPin[39], LOW);  // init LED 0 = 0
  digitalWrite(IOPin[40], LOW);  // init LED 1 = 0


  i_var = digitalRead(IOPin[38]);  // CFG/RUN MODE

  //1/29/23 while (i_var == 1) //4/12/22
  while (i_var == 0)  //1/29/23
  {
    i_var = digitalRead(IOPin[38]);  // CFG/RUN MODE

    //     SerialUSB.println("i_var:"); ///4/11/22
    //     SerialUSB.println(i_var, DEC); //debug 4/11/22
    //     SerialUSB.println("i_var_old:"); ///4/11/22
    //     SerialUSB.println(i_var_old, DEC); //debug 4/11/22

    // if (i_var == 1)
    // {




    //5/25/23  addr_sel_bit2 = digitalRead(IOPin[41]); // 5/24/23  Allows for decoding of SOM1 vs SOM0 with addr_sel bit 2.

    //SerialUSB.println(addr_sel, DEC); //4/11/22 Debug

    for (i = 0; i < 4; i++) {


      addr_sel = 0;  //5/24/23
      for (bitpin_idx = 35; bitpin_idx < 37; bitpin_idx++)
        addr_sel = addr_sel + (digitalRead(IOPin[bitpin_idx]) << bitpin_idx - 35);  /// added ( ) around bitpin-35    11/17/22



      do {
        presubmit = digitalRead(IOPin[37]);  //submit pushbutton for octet data  //5/23/23
        // Filter out any noise by setting a time bufer
        if ((millis() - lastDebounceTime) > debounceDelay) {
          if (presubmit == 0) {
            submit = 0;
            lastDebounceTime = millis();
          } else if (presubmit == 1) {
            submit = 1;
            lastDebounceTime = millis();
          }
        }  //if ((millis() - lastDebounceTime) > debounceDelay);


        delay(1);  // switch bounce

        /****** 4/12/22 ************************************************************************/
        if (submit == 1) {    // pushbutton off
          octet_data[i] = 0;  // init octet_data
          for (bitpin_idx = 27; bitpin_idx < 35; bitpin_idx++) {
            octet_data[i] = octet_data[i] + (digitalRead(IOPin[bitpin_idx]) << (bitpin_idx - 27));
            delay(1);
          }  // for (bitpin_idx = 27; bitpin_idx < 35;bitpin_idx++)
        }    // (submit == 1)
        /*************************************************************************************/



        if (submit == 0)  //pushbutton depressed on for SOM0
        {
          addr_sel = 0;  //5/25/23
          for (bitpin_idx = 35; bitpin_idx < 37; bitpin_idx++)
            addr_sel = addr_sel + (digitalRead(IOPin[bitpin_idx]) << bitpin_idx - 35);  /// added ( ) around bitpin-35    11/17/22


          if (addr_sel == 0) {
            SerialUSB.println("addr_sel:");
            SerialUSB.println(addr_sel, DEC);  //12/14/22
            ip[i] = octet_data[i];
            SerialUSB.println("IPAddr: ");  //12/14/22
            SerialUSB.println(ip[i], DEC);  //12/14/22
            EEPROM.write(i, ip[i]);
          } else if (addr_sel == 1) {
            SerialUSB.println("addr_sel:");
            SerialUSB.println(addr_sel, DEC);  //12/14/22
            subnet[i] = octet_data[i];
            SerialUSB.println("Subnet: ");      //12/14/22
            SerialUSB.println(subnet[i], DEC);  //12/14/22
            EEPROM.write(i + 4, subnet[i]);
          } else if (addr_sel == 2) {
            SerialUSB.println("addr_sel:");
            SerialUSB.println(addr_sel, DEC);  //12/14/22
            gateway[i] = octet_data[i];
            SerialUSB.println("Gateway: ");      //12/14/22
            SerialUSB.println(gateway[i], DEC);  //12/14/22
            EEPROM.write(i + 8, gateway[i]);
          }


          //            SerialUSB.println(submit, DEC); //debug 4/11/22
          //            delay(1);
          //            SerialUSB.println(addr_sel, DEC); //debug 4/11/22
          //            delay(1);
          //            SerialUSB.println(i, DEC); //debug 4/11/22
          //            delay(1);
          //            SerialUSB.println(octet_data[i], DEC); //debug 4/11/22
          //            delay(1);
          //            SerialUSB.println(ip[i], DEC); //debug 4/11/22
          //            delay(1);
          //            SerialUSB.println("   "); //debug 4/11/22

          if (i == 0) {                     // was i == 1
            digitalWrite(IOPin[39], HIGH);  //was HIGH   5/24/23
            digitalWrite(IOPin[40], LOW);   // was LOW
          } else if (i == 1) {              // was i == 2
            digitalWrite(IOPin[39], LOW);   // was LOW   5/24/23
            digitalWrite(IOPin[40], HIGH);  // was HIGH
          } else if (i == 2) {              //  was i == 3
            digitalWrite(IOPin[39], HIGH);  // was HIGH  5/24/23
            digitalWrite(IOPin[40], HIGH);  // was HIGH
          }

          else {
            digitalWrite(IOPin[39], LOW);  // was LOW   5/24/23
            digitalWrite(IOPin[40], LOW);  // was LOW
          }                                // else

        }  // if(submit == 0)

        i_var = digitalRead(IOPin[38]);  // CFG/RUN MODE

        //1/29/23 } while (submit == 1 and i_var == 1); // pushbutton off and mode cfg
      } while (submit == 1 and i_var == 0);  // pushbutton off and mode cfg  //1/29/23

      //1/29/23  while (submit == 0 and i_var == 1) {  // pushbutton depressed on and mode cfg
      while (submit == 0 and i_var == 0) {  // pushbutton depressed on and mode cfg  //1/29/23
        i_var = digitalRead(IOPin[38]);     // CFG/RUN MODE
        //5/25/23   submit = digitalRead(IOPin[37]);
        //5/25/23   for (p = 0; p < 10; p++) // switch debounce
        //5/25/23    PAUSE;
        //5/25/23   submit = digitalRead(IOPin[37]);

        presubmit = digitalRead(IOPin[37]);  //submit pushbutton for octet data  //5/23/23
        // Filter out any noise by setting a time bufer
        if ((millis() - lastDebounceTime) > debounceDelay) {
          if (presubmit == 0) {
            submit = 0;
            lastDebounceTime = millis();
          } else if (presubmit == 1) {
            submit = 1;
            lastDebounceTime = millis();
          }
        }  //if ((millis() - lastDebounceTime) > debounceDelay);



      }  // while (submit == 0 and i_var == 1)


    }  // for (i=0; i < 4; i++)

    i_var = digitalRead(IOPin[38]);  // CFG/RUN MODE
    //1/29/23  if (i_var == 0)
    if (i_var == 1)                  //1/29/23
      break;

    // } // if (i_var == 1)

    //1/29/23  } //while (i_var == 1);
  }  //while (i_var == 0);  //1/29/23

  digitalWrite(IOPin[39], LOW);
  digitalWrite(IOPin[40], LOW);

  /**************************************** 11/18/22 commented out above  *****************************************/


  // for (int i = 0; i < 4; i++) {
  //   EEPROM.write(i, ip[i]);  //4/7/22
  // EEPROM.write(i+4, subnet[i]);  //4/10/22
  // EEPROM.write(i+8, gateway[i]); //4/10/22
  // }

  SerialUSB.println("Past   ");  ///4/11/22

  //  for (i = 0; i < 4; i++)
  //   SerialUSB.println(ip[i], DEC);
  /////////////////////////////////////////////////////////////////////////////////////////



  Ethernet.begin(mac, ip, subnet);  //2/9/22  Assign MAC, IP and subnet mask

  // start the TCP server
  EthServer.begin();        //2/9/22  start listening to clients
  modbusTCPServer.begin();  //2/10/22 start listening to clients


  // configure Holding registers at address 0x00
  HoldingResult = modbusTCPServer.configureHoldingRegisters(0x0000, 10);  // 2/10/22

  // configure input registers at address 0x00
  InputResult = modbusTCPServer.configureInputRegisters(0x0010, 16);  //7/20/23

  digitalWriteFast(IOPin[16], LOW);   // CLK Init Low           //2/19/22
  digitalWriteFast(IOPin[17], HIGH);  // CSH Init Holding Registers High disabled //2/19/22
  digitalWriteFast(IOPin[18], HIGH);  // CSI Init Read Input Registers High disabled //2/19/22
  digitalWriteFast(IOPin[19], HIGH);  // CSI Init R/W to Read mode //2/19/22
  digitalWriteFast(IOPin[20], HIGH);  // CSH_B Init Holding Registers High disabled //2/25/22
  digitalWriteFast(IOPin[21], HIGH);  // CSI_8 Init Read Input Registers x0-x7 High disabled //2/25/22
  digitalWriteFast(IOPin[22], HIGH);  // CSI_18 Init Read Input Registers x10-x17 High disabled //2/25/22
}

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

void loop() {
  // listen for incoming clients
  EthernetClient client = EthServer.available();  //2/9/22
  if (client) { //10/03/23
    Serial.println("new client"); //10/03/23
    modbusTCPServer.accept(client);
    while (client.connected()) {  ///9/14/23
      if ( modbusTCPServer.poll() > 0) { //10/03/23

        UpdateWriteHoldingRegs(); //10/03/23
        UpdateReadInputRegs(); //10/03/2

        //  PAUSE;
        //  PAUSE;
        PAUSE;
        PAUSE;
        // digitalWriteFast(IOPin[26], HIGH);  //3/3/22  CSY to end SOM communication wih FPGA
        /****************************************************************************************/
      } //if( modbusTCPServer.poll() > 0) 10/03/23
    }  //while (client.connected()) ///9/14/23
    Serial.println("client disconnected"); //10/03/23
  } // 10/03/23 If (client)

}  //void loop()





void UpdateWriteHoldingRegs() {

  /******************** Write Holding Register data to Digital Outputs *************************/
  for (bitpin_idx = 0; bitpin_idx < 16; bitpin_idx++)  //2/21/22
    pinMode(IOPin[bitpin_idx], OUTPUT);                //  Data Pin init for Write Holding registers //2/21/22
  PAUSE;

  //////////////  Write Holding Registers 0x0000 to 0x0009  ////////////////
  //digitalWriteFast(IOPin[26], LOW);  //3/3/22  CYN Active Low to begin SOM communication with FPGA
  for (reg_idx = 0; reg_idx < 10; reg_idx++) {
    hr_var = modbusTCPServer.holdingRegisterRead(reg_idx);



    ////// Output address //////////////////////////////////////////////

    digitalWriteFast(IOPin[23], 0);
    digitalWriteFast(IOPin[24], 0);
    digitalWriteFast(IOPin[25], 0);

    if (reg_idx == 0) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 0);
    }
    if (reg_idx == 1) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 0);
    }
    if (reg_idx == 2) {
      digitalWriteFast(IOPin[26], HIGH); //10/03/23  Flag to detect Poll for Write to Holding Registers 2 - 9
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 0);
    }
    if (reg_idx == 3) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 0);
    }
    if (reg_idx == 4) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 1);
    }
    if (reg_idx == 5) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 1);
    }
    if (reg_idx == 6) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 1);
    }
    if (reg_idx == 7) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 1);
    }
    if (reg_idx == 8) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 1);
    }
    if (reg_idx == 9) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 1);
    }

    //////////////////////////////////////////////////////////////////


    for (bitpin_idx = 0; bitpin_idx < 16; bitpin_idx++) {
      //2/19/22 alternate way:
      digitalWriteFast(IOPin[bitpin_idx], bitRead(hr_var, bitpin_idx));
    }                                  // for(bitpin_idx = 0; bitpin_idx < 16; bitpin_idx++)
    digitalWriteFast(IOPin[17], LOW);  //CSH Active Low
    PAUSE;
    digitalWriteFast(IOPin[19], LOW);  // R/W Low for Write
    PAUSE;
    digitalWriteFast(IOPin[16], HIGH);  // CLK High rising Edge
    PAUSE;
    digitalWriteFast(IOPin[16], LOW);  // CLK Low  falling edge
    PAUSE;
    digitalWriteFast(IOPin[19], HIGH);  // R/W High for Read
    PAUSE;
    digitalWriteFast(IOPin[17], HIGH);  //CSH Disabled High

    digitalWriteFast(IOPin[26], LOW); //10/03/23 Flag to detect end of Write Holding Registers 2 - 9

  }                                     //for (reg_idx = 0; reg_idx < 10; regidx++)

} //Void UpdateWriteHoldingRegs() {




void UpdateReadInputRegs() {
  /*************** Read Input Registers data from Digital Inputs ********************************/
  for (bitpin_idx = 0; bitpin_idx < 16; bitpin_idx++)  //2/21/22
    pinMode(IOPin[bitpin_idx], INPUT);                 // Data Pin init Read Input registers //2/21/22
  PAUSE;

  //////////////  Read Input Registers 0x0000 to 0x0007  ///////////////
  for (reg_idx = 16; reg_idx < 32; reg_idx++) {
    i_var = 0x0000;
    /**********************************************/


    digitalWriteFast(IOPin[24], 1);  //7/17/23  //Addr bit 4
    digitalWriteFast(IOPin[25], 0);  //Addr bit 5

    if (reg_idx == 16)
    { digitalWriteFast(IOPin[26], HIGH);  //10/03/23  Flag to detect Read Input Registers 16-31
      digitalWriteFast(IOPin[20], 0); //Addr bit 0
      digitalWriteFast(IOPin[21], 0); //Addr bit 1
      digitalWriteFast(IOPin[22], 0); //Addr bit 2
      digitalWriteFast(IOPin[23], 0); //Addr bit 3

    }
    if (reg_idx == 17) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 0); //Addr bit 3

    }
    if (reg_idx == 18) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 0); //Addr bit 3

    }
    if (reg_idx == 19) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 0); //Addr bit 3

    }
    if (reg_idx == 20) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 0); //Addr bit 3

    }
    if (reg_idx == 21) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 0); //Addr bit 3
    }

    if (reg_idx == 22) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 0); //Addr bit 3
    }

    if (reg_idx == 23) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 0); //Addr bit 3
    }


    if (reg_idx == 24) {
      digitalWriteFast(IOPin[20], 0); //Addr bit 0
      digitalWriteFast(IOPin[21], 0); //Addr bit 1
      digitalWriteFast(IOPin[22], 0); //Addr bit 2
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }

    if (reg_idx == 25) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }

    if (reg_idx == 26) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }

    if (reg_idx == 27) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 0);
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }

    if (reg_idx == 28) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }

    if (reg_idx == 29) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 0);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }

    if (reg_idx == 30) {
      digitalWriteFast(IOPin[20], 0);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }

    if (reg_idx == 31) {
      digitalWriteFast(IOPin[20], 1);
      digitalWriteFast(IOPin[21], 1);
      digitalWriteFast(IOPin[22], 1);
      digitalWriteFast(IOPin[23], 1); //Addr bit 3
    }


    digitalWriteFast(IOPin[18], LOW);  //CSI Active Low

    digitalWriteFast(IOPin[26], LOW);  //10/03/23  Flag to detect end of Read Input Registers 16-31

    PAUSE; //7/18/23
    PAUSE; //7/18/23
    PAUSE; //7/18/23
    digitalWriteFast(IOPin[16], HIGH);  // CLK High rising Edge
    PAUSE;
    PAUSE;
    for (bitpin_idx = 0; bitpin_idx < 16; bitpin_idx++) {
      i_var = i_var + (digitalRead(IOPin[bitpin_idx]) << bitpin_idx);
      PAUSE;
      PAUSE;//7/18/23
      PAUSE;//7/18/23
      PAUSE;//7/18/23
    }  // for(bitpin_idx = 0; bitpin_idx < 16; bitpin_idx++)


    modbusTCPServer.inputRegisterWrite(reg_idx, i_var);

    /***** Debug ********************/
    SerialUSB.println("reg_idx:");
    SerialUSB.println(reg_idx, HEX);
    SerialUSB.println("i_var:");
    SerialUSB.println(i_var, HEX);
    /********************************/
    PAUSE; //7/18/23
    PAUSE; //7/18/23
    PAUSE; //7/18/23
    PAUSE; //7/28/23

    digitalWriteFast(IOPin[16], LOW);   // CLK Low  falling edge
    PAUSE; //7/18/23
    PAUSE; //7/18/23
    // PAUSE; //7/18/23
    // PAUSE; //7/28/23
    digitalWriteFast(IOPin[18], HIGH);  //CSI Disabled High
    PAUSE; //7/18/23
    PAUSE; //7/18/23
    // PAUSE; //7/18/23
    // PAUSE; //7/28/23
    // PAUSE; //7/18/23
    //PAUSE; //7/28/23
  }
} // void UpdateInputReadRegs() {

I used ctrl--T, ctl-shift-c to copy my full formatted code from the Arduino IDE.
Then I edited my post by pasting the full code in with ctl-v.
It appears to have worked.

However I no longer see my post.
Not in Latest, New, Unread or Top.
How can I see it?

Also everytime I Log in to the Forum I am forced to use a Recovery code,
How do I change this so just my user name and password are needed and can be saved.
?

BTW, I once again tried to remove the Bold font from the 2nd para of text but no matter what I do it wants to stay bold. I even copied the text into word and removed the Bold font and changed it to 12 from 18 and pasted it back into the post but your editor keeps it bold!!!
Can you try to put it into a normal font and tell me exactly how you did it?

The text in bold is that way because you seem to have inadvertently created a link(usually, a link to a web page), and pasted your text into it. Somehow.
I'm hoping one of our moderators who is familiar with login issues can chip in here, because I haven't the faintest idea what's going on for you.

You might have to create a new topic describing the problem, in this forum section:

Hi @leadhead613,

Did you able to decode ModbusTCPserver.poll(). Kindly let us know.

I ended up making use of the PHY LED output signal of the Teensy 4.1 to monitor
Modbus TCP activity on the link used instead of relying on the ModbusTCPServer.poll()
function for this. I was originally trying to acertain activity of the link and to decipher whether
it be from a write to holding registers or a read from input registerswithin the loop.
I gave up on this approach since no one on the forum responded.
Ultamately I was doing this for a dual link redundancy algorithm. I am now relying on the Phy LED signal for this as said.

Hi leadhead613,

Thanks for the reply.
I am also stucked to decode the ModbusTCPServer.Poll() to get the Function Code to know whether it is the read Operation or write operation. I am using the Hardware: Arduino Mega board + Ethernet Shield 2 Board:
Code Used:

#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoModbus.h>

bool _1s;
unsigned long HoldingResult, InputResult, HeartBeat;
long Cmd;

EthernetServer EthServer(502);
ModbusTCPServer modbusTCPServer;

void setup() {
  // Ethernet Settings
  byte mac[] = { 0x4E, 0xA0, 0xBE, 0x3F, 0xFE, 0x0F };  // Define MAc address
  byte ip[] = { 192, 168, 1, 23 };                      // Define IP address
  byte subnet[] = { 255, 255, 255, 0 };                 // Define SubNEt mask

  // initialize the ethernet device
  Ethernet.begin(mac, ip, subnet);                      // Assign MAC, IP, and subnet mask
  Serial.begin(9600);
  EthServer.begin();          // start listening for clients
  modbusTCPServer.begin();    // start listening for clients

  // Define Holding register:
  HoldingResult = modbusTCPServer.configureHoldingRegisters(0, 100);
  InputResult = modbusTCPServer.configureInputRegisters(0, 100);

  Serial.print("Holding Reg init result =");
  Serial.print(HoldingResult);
  Serial.print("\n");

  Serial.print("Input Reg init result =");
  Serial.print(InputResult);
  Serial.print("\n");

  Serial.print("Modbus server address=");
  Serial.println(Ethernet.localIP());
  Serial.print("\n");
}

void loop() {
  // Modbus server accept incoming connections
  EthernetClient client = EthServer.available();

  if (client.connected()) {
    modbusTCPServer.accept(client);
    // poll for Modbus TCP requests, while client connected
    modbusTCPServer.poll();
    // Serial.print("poll");
  }

  //---------------------------------------------------------------------------------
  // Modbus server  :

  // holding resgiter 40001: heartbeat (FC3)
  modbusTCPServer.holdingRegisterWrite(0x00, HeartBeat);

  // holding resgiter 40500: Command Word (FC6)
  Cmd = modbusTCPServer.holdingRegisterRead(90);
}

I have to identify whether the Frame received from the Master Client is Function code -->read(03) or Multiplewrite(10).

If the Frame received is read --> I have to read the holding register.
If the Frame received is MultipleWrite --> I have to write into that holding register and also to the EEPROM location.

I have to differentiate the Function code received is read or multiple write function.

Is there anyother way to differentiate the Read and write function for me? Kindly let us know.

Like I said previously I indeed was unable to differentiate the modbusTCPServer.poll() function
for a read input register or write holding registers function.
Otherwise I have found that following a .poll() execution both read input and rite holding reisters
are performed one after the other in the Loop().
This complicates what I was tying to acheive at least timewise on the backend to my application.
Surpringly, see very little to no documentation for the .poll function whether it be in the Arduino
ModbusTCP reference or anywhere else. Certainly that documentation is incomplete.

Ok. Thanks for the reply. I have raised the Query in the 'Issue board' of the Github Arduino Modbus library:Not able to decode ModbusTCPServer.Poll(); function to get the Function code for read or write Operation. · Issue #129 · arduino-libraries/ArduinoModbus · GitHub. Lets hope for the best.

Thanks. I really think they should document in detail the operation of all
modbustTCPServer functions including modbusTCPServer.poll(), UpdateWriteHoldingRegs(), UpdateReadInputRegs() and how .poll relates to those read and write functions as I suspect it does.
There does exist examples showing either reading or writing within the loop using ,poll
but I have not found any examples for both reading or writing depending upon the client
polling the server. If there was It could be quite helpful.

1 Like

Hi Prakash,
Have you heard back from anyone on this issue of mutual interest?
les