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() {