You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+97-62Lines changed: 97 additions & 62 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,102 +9,146 @@ Change settings of your Arduino-based Modbus RTU to Modbus TCP/UDP gateway via w
9
9
10
10
## What are the technical specifications?
11
11
12
-
* slaves are connected via RS485 interface
13
-
* master(s) are connected via ethernet interface
14
-
*up to 247 Modbus RTU slaves
15
-
*up to 7 Modbus TCP masters
16
-
* unlimited number of Modbus UDP masters
12
+
* slaves are connected via RS485 interface:
13
+
- up to 247 Modbus RTU slaves
14
+
*master(s) are connected via ethernet interface:
15
+
-up to 6 Modbus TCP masters (depending on hardware)
16
+
- unlimited number of Modbus UDP masters
17
17
* RS485 interface protocols:
18
18
- Modbus RTU
19
-
*Ethernet interface protocols:
19
+
*ethernet interface protocols:
20
20
- Modbus TCP
21
21
- Modbus UDP
22
22
- Modbus RTU over TCP
23
23
- Modbus RTU over UDP
24
-
* supports broadcast (slave address 0x00) and error codes
24
+
* supports broadcast (slave address 0x00)
25
25
* supports all Modbus function codes
26
-
* diagnostics and Modbus RTU scan via web interface
27
-
* optimized queue for Modbus requests (queue will accept only one requests to non-responding slaves)
28
-
* settings can be changed via web interface, they are stored in EEPROM
29
-
* user settings are retained during firmware upgrade (only in case of major VERSION change, Arduino loads factory defaults).
26
+
* supports error codes:
27
+
- codes 1~8 are forwarded from slaves
28
+
- codes 10, 11 are generated by the gateway
29
+
* diagnostics via web interface:
30
+
- send Modbus request and recieve Modbus response
31
+
- scan Modbus slaves on RS485 interface
32
+
- queue (buffer) status
33
+
- error counts
34
+
- content of the Modbus Status page is updated in the background (fetch API), javascript alert is shown if connection is lost
35
+
* optimized TCP socket management (web interface and Modbus TCP):
36
+
- gateway always listens for new web and Modbus TCP connections
37
+
- existing connections are kept alive (persistent), unless the gateway runs out of available sockets
38
+
- if there are no available sockets, oldest connections are closed after they are idle for a certain time, Modbus TCP idle timeout is configured in user settings (web UI), webserver idle timeout is configured in advanced settings (sketch)
39
+
* optimized queue (buffer) for Modbus requests:
40
+
- queue will accept only one requests to a non-responding slave
41
+
- requests to responding slaves are prioritized over requests to non-responding slaves
42
+
- queue size configured in advanced settings (sketch)
43
+
* user settings:
44
+
- can be changed via web interface (see screenshots bellow)
45
+
- stored in EEPROM
46
+
- retained during firmware upgrade (only in case of major version change, Arduino loads factory defaults)
47
+
- all web interface inputs have proper validation
48
+
- settings marked \* are only available if ENABLE_DHCP is defined in the sketch
49
+
- settings marked \*\* are only available if ENABLE_EXTRA_DIAG is defined in the sketch
50
+
* advanced settings:
51
+
- can be changed in sketch (see the initial section of arduino-modbus-rtu-tcp-gateway.ino)
**Modbus RTU Request**. Send a Modbus RTU request directly from web UI. First byte (slave address) and second byte (function code) are mandatory, no need to calculate CRC. Gateway remembers last request for your convenience.
44
65
45
-
**Run Time**.
66
+
**Modbus RTU Response**. Shows response to the last Modbus request. Response is shown in Modbus RTU format (incl. CRC). Due to memory limitations, only few initial bytes are shown.
46
67
47
-
**Modbus Statistics**.
68
+
**Run Time**.\*\* Days, hours, minutes and seconds since boot or stat reset.
48
69
49
-
**Requests Queue**.
70
+
**RTU Data**.\*\* Counter for bytes sent and received via RS485.
50
71
51
-
**Modbus TCP/UDP Masters**.
72
+
**Ethernet Data**.\*\* Counter for bytes sent and received via Modbus TCP/UDP.
52
73
53
-
**Modbus Slaves**.
74
+
**Requests Queue**. Monitors internal request queue (buffer). The limits for bytes and for the number of requests stored in the queue can be configured in advanced settings.
54
75
55
-
**Scan Slaves**.
76
+
**Modbus Statistics**. Counters for various errors. Insigned longs are used, rollover of counters is synchronized:
77
+
***Slave Responded**. Slave responded with a valid Modbus RTU response within response timeout.
78
+
***Slave Responded with Error (Codes 1~8)**. Slave responded, but with an error. For the list of error codes see https://en.wikipedia.org/wiki/Modbus#Exception_responses.
79
+
***Gateway Overloaded (Code 10)**. Request queue is full (either the number of bytes stored or the number of requests stored). Request was dropped and the gateway responded with an error code 10.
80
+
***Slave Failed to Respond (Code 11)**. Slave is not responding. Response timeouts have passed, all attempts have failed. The gateway responded with an error code 11.
81
+
***Invalid TCP/UDP Request**. Invalid request was received via TCP or UDP. Request was dropped, no response was sent by the gateway. Validation criteria depends on the Modbus mode:
82
+
- Modbus TCP/UDP: MBAP header (protocol identifier is 0x0000, length is < 255 and corresponds to the number of bytes in the remainder of the Modbus request)
83
+
- Modbus RTU over TCP/UDP: CRC ckeck
84
+
***Invalid RTU Response**. Invalid data were recieved via RS485. Could be caused by wrong Modbus RTU settings, short response timeout (any response arriving after timeout is invalid) or Arduino delays in processing the response. Validation criteria:
85
+
- silence between individual bytes is shorter than char timeout specified in Modbus RTU standards
86
+
- CRC check
87
+
- slave address in the Modbus RTU response corresponds to the slave address in the request
88
+
- response arrived before response timeout
89
+
***Response Timeout**. Slave failed to respond within the specified response timeout. New attempt follows (or error code 11 if all attempts were spent).
**Modbus Masters**. Shows IP addresses for Modbus TCP or UDP masters:
92
+
***UDP**. Only the last Modbus UDP master is shown, because all UDP masters connect to the same socket.
93
+
***TCP**. All connected Modbus TCP masters are shown. Each Modbus TCP connection occupies one socket.
58
94
59
-
### IP Settings:
95
+
**Modbus Slaves**. Shows the slave address (in hex) and the last status (error) for all slaves who responded to a slave scan or who were recipients of a Modbus request.
60
96
61
-
**Auto IP**. Only if ENABLE_DHCP. Once enabled, Arduino will receive IP, gateway, subnet and DNS from the DHCP server.
97
+
**Scan Slaves**. An attempt is made to find Modbus RTU slaves connected to the RS485 interface:
98
+
- scan is launched automaticaly after boot or manualy
99
+
- scans all slave addresses 1 - 247
100
+
- dummy requests are sent to each slave address for two different Modbus functions (configured in advanced settings)
101
+
- fixed response timeout (very short, configured in advanced settings), only one attempt
102
+
- gateway marks the slave as "Slave Responded" if any response is sent by the slave (even error)
**Modbus UDP Port**. Can be the same as Modbus TCP Port.
78
122
79
-
**Web Port**.
123
+
**Web Port**. Gateway automatically redirect the web interface to the new Web UI port.
80
124
81
-
**Modbus Mode**. Modbus TCP/UDP or Modbus RTU over TCP/UDP.
125
+
**Modbus Mode**. Modbus TCP/UDP or Modbus RTU over TCP/UDP. Be aware that "Modbus RTU over TCP/UDP" is not a standard Modbus protocol. In this mode, the gateway expects to recieve Modbus RTU request (incl. CRC) via TCP or UDP. Responses from RS485 line are forwarded as they are in Modbus RTU format (incl. CRC).
126
+
127
+
**Modbus TCP Idle Timeout**. Amount of time that a connection is always held alive (open) with no incoming traffic from a Modbus TCP master. This timeout should be longer than polling period (scan rate) set on your Modbus TCP master device.
**Baud Rate**. Choose baud rate from a pre-aranged list. The list can be adjusted in advanced settings.
86
132
87
-
**Baud Rate**.
133
+
**Data Bits**. Data bits available on arduino HW serial line: 5, 6, 7, 8.
88
134
89
-
**Data Bits**.
135
+
**Parity**. Parity options available on arduino HW serial line: None, Even, Odd.
90
136
91
-
**Parity**.
137
+
**Stop Bits**. 1 or 2 stop bits.
92
138
93
-
**Stop Bits**.
139
+
**Inter-frame Delay**. Delay (ms) between the end of reading Modbus RTU frame and writing new frame. Higher Frame Delay is needed for RS485 modules with automatic flow control. Increase Frame Delay if you have very short polling period and Response Timeouts in stats. The minimum Inter-frame Delay is calculated from baud rate according to Modbus standards.
94
140
95
-
**Frame Delay**. Delay (ms) between the end of reading Modbus RTU frame and writing new frame. Higher Frame Delay is needed for RS485 modules with automatic flow control. Increase Frame Delay if you see Response Timeouts in stats.
141
+
**Response Timeout**. Timeout for Modbus RTU response. Increase Response Timeout if you see Response Timeouts in Modbus statistics.
96
142
97
-
**Response Timeout**.
143
+
**Attempts**. Number of attempts before error (Code 11) is sent back to the Modbus TCP/UDP master.
98
144
99
-
**Attempts**.
100
-
101
145
## How can I build it myself?
102
146
Get the hardware (cheap clones from China are sufficient) and connect together:
103
147
104
-
* Arduino Nano, Uno or Mega (and possibly other). On Mega you have to configure Serial in ADVANCED SETTINGS in the sketch.
105
-
* W5100, W5200 or W5500 based Ethernet shield (for Nano, I recommend W5500 Ethernet Shield from RobotDyn)
106
-
* TTL to RS485 module:
107
-
- with hardware automatic flow control (recommended)<br>
148
+
***Arduino Nano, Uno or Mega** (and possibly other). On Mega you have to configure Serial in advanced settings in the sketch.
149
+
***W5100, W5200 or W5500 based Ethernet shield**. The ubiquitous W5100 shield for Uno/Mega is sufficient. If available, I recommend W5500 Ethernet Shield. !!! ENC28J60 will not work !!!
150
+
***TTL to RS485 module**:
151
+
- with hardware automatic flow control (recommended, available on Aliexpress)<br>
108
152
Arduino <-> Module<br>
109
153
Tx1 <-> Tx<br>
110
154
Rx0 <-> Rx
@@ -113,13 +157,12 @@ Get the hardware (cheap clones from China are sufficient) and connect together:
113
157
Tx1 <-> DI<br>
114
158
Rx0 <-> RO<br>
115
159
Pin 6 <-> DE,RE
116
-
117
160
118
161
Here is my setup:
119
-
Terminal shield + Arduino Nano + W5500 eth shield (RobotDyn) + TTL to RS485 module (HW automatic flow control)
Download this repository (all *.ino files) and open arduino-modbus-rtu-tcp-gateway.ino in Arduino IDE. Download all required libraries (both are available in "library manager"). If you want, you can check the default factory settings (can be later changed via web interface) and advanced settings (can only be changed in sketch). Compile and upload your program to Arduino. Connect your Arduino to ethernet, connect your Modbus RTU slaves to MAX485 module. Use your web browser to access the web interface on default IP http://192.168.1.254 Enjoy :-)
165
+
Download this repository (all *.ino files) and open arduino-modbus-rtu-tcp-gateway.ino in Arduino IDE. Download all required libraries (they are available in "library manager"). If you want, you can check the default factory settings (can be later changed via web interface) and advanced settings (can only be changed in the sketch). Compile and upload your program to Arduino. Connect your Arduino to ethernet, connect your Modbus RTU slaves to MAX485 module. Use your web browser to access the web interface on default IP http://192.168.1.254 Enjoy :-)
123
166
124
167
## Where can I learn more about Modbus protocols?
125
168
@@ -136,7 +179,7 @@ The key to success is:
136
179
137
180
* use StreamLib https://github.com/jandrassy/StreamLib
138
181
* use F macros for your HTML code
139
-
* use for() loop for repetitive code
182
+
* use for() loop or dedicated functions for repetitive code
140
183
* use POST method (rather than GET) for your webforms, see this tutorial https://werner.rothschopf.net/202003_arduino_webserver_post_en.htm
141
184
142
185
Big thanks to the authors of these libraries and tutorials!
@@ -147,27 +190,19 @@ Big thanks to the authors of these libraries and tutorials!
147
190
148
191
The code was tested on Arduino Nano, Uno and Mega, ethernet chips W5100 and W5500. It may work on other platforms, but:
149
192
150
-
* The pseudorandom generator (for random MAC) is seeded through watch dog timer interrupt - this will work only on Arduino (credits to https://sites.google.com/site/astudyofentropy/project-definition/timer-jitter-entropy-sources/entropy-library/arduino-random-seed)
193
+
* The random number generator (for random MAC) is seeded through watch dog timer interrupt - this will work only on Arduino (credits to https://sites.google.com/site/astudyofentropy/project-definition/timer-jitter-entropy-sources/entropy-library/arduino-random-seed)
151
194
* The restart function will also work only on Arduino.
152
195
153
-
#### Ethernet socket
196
+
#### Ethernet sockets
154
197
155
-
The default Ethernet.h library determines MAX_SOCK_NUM by microcontroller RAM (not by Ethernet chip type). So if you use W5500 (which has 8 sockets available) on Arduino Nano, only 4 sockets will be used. If you want to force the library to use 8 sockets, redefine MAX_SOCK_NUM in advanced settings in the sketch.
198
+
The number of used sockets is determined (by the Ethernet.h library) based on microcontroller RAM. Therefore, even if you use W5500 (which has 8 sockets available) on Arduino Nano, only 4 sockets will be used due to limited RAM on Nano.
156
199
157
200
#### Memory
158
201
159
-
Not everything could fit into the limited flash memory of Arduino Nano / Uno. If you have a microcontroller with more memory (such as Mega), you can enable extra features in the main sketch by uncommenting:
160
-
161
-
* #define ENABLE_DHCP will allow you to set "Auto IP" via DHCP in the IP settings web interface. Leased IP is automatically renewed.
Not everything could fit into the limited flash memory of Arduino Nano / Uno. If you have a microcontroller with more memory (such as Mega), you can enable extra settings in the main sketch by defining ENABLE_DHCP and/or ENABLE_EXTRA_DIAG in the sketch.
0 commit comments