Skip to content

Commit f1c9cd0

Browse files
committed
Separate file for advanced settings
1 parent bc016fb commit f1c9cd0

File tree

3 files changed

+69
-67
lines changed

3 files changed

+69
-67
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ Change settings of your Arduino-based Modbus RTU to Modbus TCP/UDP gateway via w
4646
- stored in EEPROM
4747
- retained during firmware upgrade (only in case of major version change, Arduino loads factory defaults)
4848
- all web interface inputs have proper validation
49+
- factory defaults for user settings can be specified in advanced_settings.h
4950
- settings marked \* are only available if ENABLE_DHCP is defined in the sketch
5051
- settings marked \*\* are only available if ENABLE_EXTRA_DIAG is defined in the sketch
5152
* advanced settings:
52-
- can be changed in sketch (see the initial section of arduino-modbus-rtu-tcp-gateway.ino)
53+
- can be changed in sketch (advanced_settings.h)
5354
- stored in flash memory
5455

5556
<img src="/pics/modbus1.png" alt="01" style="zoom:100%;" />
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Advanced settings, extra functions and default config for Modbus RTU ⇒ Modbus TCP/UDP Gateway
2+
*/
3+
4+
/****** ADVANCED SETTINGS ******/
5+
6+
#define mySerial Serial // define serial port for RS485 interface, for Arduino Mega choose from Serial1, Serial2 or Serial3
7+
// List of baud rates (divided by 100) available in WebUI. Feel free to add your custom baud rate (anything between 3 and 2500)
8+
const unsigned int BAUD_RATES[] = { 3, 6, 9, 12, 24, 48, 96, 192, 384, 576, 1152 };
9+
#define RS485_CONTROL_PIN 6 // Arduino Pin for RS485 Direction control, disable if you have module with hardware flow control
10+
const byte MAX_QUEUE_REQUESTS = 10; // max number of TCP or UDP requests stored in a queue
11+
const int MAX_QUEUE_DATA = 256; // total length of TCP or UDP requests stored in a queue (in bytes)
12+
const byte MAX_SLAVES = 247; // max number of Modbus slaves (Modbus supports up to 247 slaves, the rest is for reserved addresses)
13+
const int MODBUS_SIZE = 256; // size of a MODBUS RTU frame (determines size of various buffers)
14+
const byte MAX_RESPONSE_LEN = 16; // Max length (bytes) of the Modbus response shown in WebUI
15+
const byte SCAN_FUNCTION_FIRST = 0x03; // Function code sent during Modbus RTU Scan request (first attempt)
16+
const byte SCAN_FUNCTION_SECOND = 0x04; // Function code sent during Modbus RTU Scan request (second attempt)
17+
const byte SCAN_DATA_ADDRESS = 0x01; // Data address sent during Modbus RTU Scan request (both attempts)
18+
const unsigned int SCAN_TIMEOUT = 200; // Timeout (ms) for Modbus scan requests
19+
20+
const byte MAC_START[3] = { 0x90, 0xA2, 0xDA }; // MAC range for Gheo SA
21+
const byte ETH_RESET_PIN = 7; // Ethernet shield reset pin (deals with power on reset issue on low quality ethernet shields)
22+
const unsigned int ETH_RESET_DELAY = 500; // Delay (ms) during Ethernet start, wait for Ethernet shield to start (reset issue on low quality ethernet shields)
23+
const unsigned int WEB_IDLE_TIMEOUT = 400; // Time (ms) from last client data after which webserver TCP socket could be disconnected, non-blocking.
24+
const unsigned int TCP_DISCON_TIMEOUT = 500; // Timeout (ms) for client DISCON socket command, non-blocking alternative to https://www.arduino.cc/reference/en/libraries/ethernet/client.setconnectiontimeout/
25+
const unsigned int TCP_RETRANSMISSION_TIMEOUT = 50; // Ethernet controller’s timeout (ms), blocking (see https://www.arduino.cc/reference/en/libraries/ethernet/ethernet.setretransmissiontimeout/)
26+
const byte TCP_RETRANSMISSION_COUNT = 3; // Number of transmission attempts the Ethernet controller will make before giving up (see https://www.arduino.cc/reference/en/libraries/ethernet/ethernet.setretransmissioncount/)
27+
const int FETCH_INTERVAL = 2000; // Fetch API interval (ms) for the Modbus Status webpage to renew data from JSON served by Arduino
28+
29+
const int CONFIG_START = 96; // Start address where config and counters are saved in EEPROM
30+
const byte EEPROM_INTERVAL = 6; // Interval (hours) for saving Modbus statistics to EEPROM (in order to minimize writes to EEPROM)
31+
32+
/****** EXTRA FUNCTIONS ******/
33+
34+
// these do not fit into the limited flash memory of Arduino Uno/Nano, uncomment if you have a board with more memory
35+
// #define ENABLE_DHCP // Enable DHCP (Auto IP settings)
36+
// #define ENABLE_EXTRA_DIAG // Enable Ethernet and Serial byte counter.
37+
// #define TEST_SOCKS // shows 1) port, 2) status and 3) age for all sockets in "Modbus Status" page. IP settings are not available.
38+
39+
/****** DEFAULT FACTORY SETTINGS ******/
40+
41+
/*
42+
Please note that after boot, Arduino loads user settings stored in EEPROM, even if you flash new program to it!
43+
Arduino loads factory defaults if:
44+
1) User clicks "Load default settings" in WebUI (factory reset configuration, keeps MAC)
45+
2) VERSION_MAJOR changes (factory reset configuration AND generates new MAC)
46+
*/
47+
const config_type DEFAULT_CONFIG = {
48+
{}, // MAC Address (last 3 bytes)
49+
{ 192, 168, 1, 254 }, // Static IP
50+
{ 255, 255, 255, 0 }, // Submask
51+
{ 192, 168, 1, 1 }, // Gateway
52+
{ 192, 168, 1, 1 }, // Dns (only used if ENABLE_DHCP)
53+
false, // enableDhcp (only used if ENABLE_DHCP)
54+
502, // Modbus TCP Port
55+
502, // Modbus UDP Port
56+
80, // WebUI Port
57+
false, // Modbus Mode (enableRTU over TCP)
58+
600, // Modbus TCP Idle Timeout
59+
96, // Baud Rate / 100
60+
SERIAL_8E1, // Serial Config (Data Bits, Parity, Stop bits), Modbus RTU default is 8E1, another frequently used option is 8N2
61+
150, // Inter-frame Delay (byte)
62+
500, // Response Timeout
63+
3 // Attempts (byte)
64+
};

arduino-modbus-rtu-tcp-gateway/arduino-modbus-rtu-tcp-gateway.ino

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
v4.1 2023-01-14 Fetch API, bugfix MAX485
1919
v5.0 2023-02-19 Send Modbus Request from WebUI, optimized POST parameter processing (less RAM consumption), select baud rate in WebUI,
2020
improved TCP socket management, Modbus TCP Idle Timeout settings
21-
v6.0 2023-XX-XX Save error counters to EEPROM, code optimization
22-
21+
v6.0 2023-XX-XX Save error counters to EEPROM, code optimization, separate file for advanced settings
2322
*/
2423

2524
const byte VERSION[] = { 6, 0 };
@@ -37,40 +36,6 @@ const byte VERSION[] = { 6, 0 };
3736
#include <avr/wdt.h>
3837
#include <util/atomic.h>
3938

40-
41-
/****** ADVANCED SETTINGS ******/
42-
43-
const byte MAX_QUEUE_REQUESTS = 10; // max number of TCP or UDP requests stored in a queue
44-
const int MAX_QUEUE_DATA = 256; // total length of TCP or UDP requests stored in a queue (in bytes)
45-
const byte MAX_SLAVES = 247; // max number of Modbus slaves (Modbus supports up to 247 slaves, the rest is for reserved addresses)
46-
const int MODBUS_SIZE = 256; // size of a MODBUS RTU frame (determines size of various buffers)
47-
#define mySerial Serial // define serial port for RS485 interface, for Arduino Mega choose from Serial1, Serial2 or Serial3
48-
#define RS485_CONTROL_PIN 6 // Arduino Pin for RS485 Direction control, disable if you have module with hardware flow control
49-
const byte ETH_RESET_PIN = 7; // Ethernet shield reset pin (deals with power on reset issue on low quality ethernet shields)
50-
const unsigned int ETH_RESET_DELAY = 500; // Delay (ms) during Ethernet start, wait for Ethernet shield to start (reset issue on low quality ethernet shields)
51-
const unsigned int WEB_IDLE_TIMEOUT = 400; // Time (ms) from last client data after which webserver TCP socket could be disconnected, non-blocking.
52-
const unsigned int TCP_DISCON_TIMEOUT = 500; // Timeout (ms) for client DISCON socket command, non-blocking alternative to https://www.arduino.cc/reference/en/libraries/ethernet/client.setconnectiontimeout/
53-
const unsigned int TCP_RETRANSMISSION_TIMEOUT = 50; // Ethernet controller’s timeout (ms), blocking (see https://www.arduino.cc/reference/en/libraries/ethernet/ethernet.setretransmissiontimeout/)
54-
const byte TCP_RETRANSMISSION_COUNT = 3; // Number of transmission attempts the Ethernet controller will make before giving up (see https://www.arduino.cc/reference/en/libraries/ethernet/ethernet.setretransmissioncount/)
55-
const unsigned int SCAN_TIMEOUT = 200; // Timeout (ms) for Modbus scan requests
56-
const byte SCAN_FUNCTION_FIRST = 0x03; // Function code sent during Modbus RTU Scan request (first attempt)
57-
const byte SCAN_FUNCTION_SECOND = 0x04; // Function code sent during Modbus RTU Scan request (second attempt)
58-
const byte SCAN_DATA_ADDRESS = 0x01; // Data address sent during Modbus RTU Scan request (both attempts)
59-
const int FETCH_INTERVAL = 2000; // Fetch API interval (ms) for the Modbus Status webpage to renew data from JSON served by Arduino
60-
const byte EEPROM_INTERVAL = 6; // Interval (hours) for saving Modbus statistics to EEPROM (in order to minimize writes to EEPROM)
61-
const byte MAX_RESPONSE_LEN = 16; // Max length (bytes) of the Modbus response shown in WebUI
62-
// List of baud rates (divided by 100) available in WebUI. Feel free to add your custom baud rate (anything between 3 and 2500)
63-
const unsigned int BAUD_RATES[] = { 3, 6, 9, 12, 24, 48, 96, 192, 384, 576, 1152 };
64-
65-
/****** EXTRA FUNCTIONS ******/
66-
67-
// these do not fit into the limited flash memory of Arduino Uno/Nano, uncomment if you have a board with more memory
68-
// #define ENABLE_DHCP // Enable DHCP (Auto IP settings)
69-
// #define ENABLE_EXTRA_DIAG // Enable Ethernet and Serial byte counter.
70-
// #define TEST_SOCKS // shows 1) port, 2) status and 3) age for all sockets in "Modbus Status" page. IP settings are not available.
71-
72-
/****** DEFAULT FACTORY SETTINGS ******/
73-
7439
typedef struct {
7540
byte macEnd[3];
7641
byte ip[4];
@@ -90,36 +55,8 @@ typedef struct {
9055
byte serialAttempts;
9156
} config_type;
9257

93-
/*
94-
Please note that after boot, Arduino loads settings stored in EEPROM, even if you flash new program to it!
95-
96-
Arduino loads factory defaults specified bellow in case:
97-
1) User clicks "Restore" defaults in WebUI (factory reset configuration, keeps MAC)
98-
2) VERSION_MAJOR changes (factory reset configuration AND generates new MAC)
99-
*/
100-
101-
const config_type DEFAULT_CONFIG = {
102-
{}, // macEnd (last 3 bytes)
103-
{ 192, 168, 1, 254 }, // ip
104-
{ 255, 255, 255, 0 }, // subnet
105-
{ 192, 168, 1, 1 }, // gateway
106-
{ 192, 168, 1, 1 }, // dns
107-
false, // enableDhcp
108-
502, // tcpPort
109-
502, // udpPort
110-
80, // webPort
111-
false, // enableRtuOverTcp
112-
600, // tcpTimeout
113-
96, // baud / 100
114-
SERIAL_8E1, // serialConfig (Modbus RTU default is 8E1, another frequently used option is 8N2)
115-
150, // frameDelay
116-
500, // serialTimeout
117-
3 // serialAttempts
118-
};
11958
// local configuration values (stored in RAM)
12059
config_type localConfig;
121-
// Start address where config is saved in EEPROM
122-
const int CONFIG_START = 96;
12360

12461
typedef struct {
12562
byte tid[2]; // MBAP Transaction ID
@@ -130,6 +67,8 @@ typedef struct {
13067
byte atts; // attempts counter
13168
} header;
13269

70+
#include "advanced_settings.h"
71+
13372
// each request is stored in 3 queues (all queues are written to, read and deleted in sync)
13473
CircularBuffer<header, MAX_QUEUE_REQUESTS> queueHeaders; // queue of requests' headers and metadata
13574
CircularBuffer<byte, MAX_QUEUE_DATA> queueData; // queue of PDU data
@@ -148,8 +87,6 @@ byte maxSockNum = MAX_SOCK_NUM;
14887
bool dhcpSuccess = false;
14988
#endif /* ENABLE_DHCP */
15089

151-
const byte MAC_START[3] = { 0x90, 0xA2, 0xDA };
152-
15390
EthernetUDP Udp;
15491
EthernetServer modbusServer(DEFAULT_CONFIG.tcpPort);
15592
EthernetServer webServer(DEFAULT_CONFIG.webPort);

0 commit comments

Comments
 (0)