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
+96-62Lines changed: 96 additions & 62 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,102 +9,145 @@ 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
+
- requests queue 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 web UI)
39
+
* optimized queue 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
+
* user settings:
43
+
- can be changed via web interface (see bellow)
44
+
- stored in EEPROM
45
+
- retained during firmware upgrade (only in case of major version change, Arduino loads factory defaults)
46
+
- all web interface inputs have proper validation
47
+
- settings marked \* are only available if ENABLE_DHCP is defined in the sketch
48
+
- settings marked \*\* are only available if ENABLE_EXTRA_DIAG is defined in the sketch
49
+
* advanced settings:
50
+
- can be changed in sketch (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
64
45
-
**Run Time**.
65
+
**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
66
47
-
**Modbus Statistics**.
67
+
**Run Time**.\*\* Days, hours, minutes and seconds since boot or stat reset.
48
68
49
-
**Requests Queue**.
69
+
**RTU Data**.\*\* Counter for bytes sent and received via RS485.
50
70
51
-
**Modbus TCP/UDP Masters**.
71
+
**Ethernet Data**.\*\* Counter for bytes sent and received via Modbus TCP/UDP.
52
72
53
-
**Modbus Slaves**.
73
+
**Requests Queue**. Monitors the 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
74
55
-
**Scan Slaves**.
75
+
**Modbus Statistics**. Counters for various errors. Insigned longs are used, rollover of counters is synchronized:
76
+
***Slave Responded**. Slave responded with a valid Modbus RTU response within response timeout.
77
+
***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.
78
+
***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.
79
+
***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.
80
+
***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:
81
+
- 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)
82
+
- Modbus RTU over TCP/UDP: CRC ckeck
83
+
***Invalid RTU Response**. Invalid data were recieved via RS485. Usually caused by wrong Modbus RTU settings or too short response timeout (any response arriving after timeout is invalid). Validation criteria:
84
+
- silence between individual bytes is shorter than char timeout specified in Modbus RTU standards
85
+
- CRC check
86
+
- slave address in the Modbus RTU response corresponds to the slave address in the request
87
+
- response arrived before response timeout
88
+
***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:
91
+
***UDP**. Only the last Modbus UDP master is shown, because all UDP masters connect to the same socket.
92
+
***TCP**. All connected Modbus TCP masters are shown. Each Modbus TCP connection occupies one socket.
58
93
59
-
### IP Settings:
94
+
**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
95
61
-
**Auto IP**. Only if ENABLE_DHCP. Once enabled, Arduino will receive IP, gateway, subnet and DNS from the DHCP server.
96
+
**Scan Slaves**. An attempt is made to find Modbus RTU slaves connected to the RS485 interface:
97
+
- scan is launched automaticaly after boot or manualy later
98
+
- scans all slave addresses 1 - 247
99
+
- dummy requests are sent to each slave address for two different Modbus functions (configured in advanced settings)
100
+
- fixed response timeout (very short, configured in advanced settings), only one attempt
101
+
- gateway marks the slave as "Slave Responded" if any response is received (even error)
**Modbus UDP Port**. Can be the same as Modbus TCP Port.
78
121
79
-
**Web Port**.
122
+
**Web Port**. Gateway automatically redirect the web interface to the new Web UI port.
80
123
81
-
**Modbus Mode**. Modbus TCP/UDP or Modbus RTU over TCP/UDP.
124
+
**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).
125
+
126
+
**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
131
87
-
**Baud Rate**.
132
+
**Data Bits**. Data bits available on arduino HW serial line: 5, 6, 7, 8.
88
133
89
-
**Data Bits**.
134
+
**Parity**. Parity options available on arduino HW serial line: None, Even, Odd.
90
135
91
-
**Parity**.
136
+
**Stop Bits**. 1 or 2 stop bits.
92
137
93
-
**Stop Bits**.
138
+
**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
139
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.
140
+
**Response Timeout**. Timeout for Modbus RTU response. Increase Response Timeout if you see Response Timeouts in Modbus statistics.
96
141
97
-
**Response Timeout**.
142
+
**Attempts**. Number of attempts before error (Code 11) is sent back to the Modbus TCP/UDP master.
98
143
99
-
**Attempts**.
100
-
101
144
## How can I build it myself?
102
145
Get the hardware (cheap clones from China are sufficient) and connect together:
103
146
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>
147
+
***Arduino Nano, Uno or Mega** (and possibly other). On Mega you have to configure Serial in advanced settings in the sketch.
148
+
***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 !!!
149
+
***TTL to RS485 module**:
150
+
- with hardware automatic flow control (recommended, available on Aliexpress)<br>
108
151
Arduino <-> Module<br>
109
152
Tx1 <-> Tx<br>
110
153
Rx0 <-> Rx
@@ -113,13 +156,12 @@ Get the hardware (cheap clones from China are sufficient) and connect together:
113
156
Tx1 <-> DI<br>
114
157
Rx0 <-> RO<br>
115
158
Pin 6 <-> DE,RE
116
-
117
159
118
160
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 :-)
164
+
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
165
124
166
## Where can I learn more about Modbus protocols?
125
167
@@ -136,7 +178,7 @@ The key to success is:
136
178
137
179
* use StreamLib https://github.com/jandrassy/StreamLib
138
180
* use F macros for your HTML code
139
-
* use for() loop for repetitive code
181
+
* use for() loop or dedicated functions for repetitive code
140
182
* use POST method (rather than GET) for your webforms, see this tutorial https://werner.rothschopf.net/202003_arduino_webserver_post_en.htm
141
183
142
184
Big thanks to the authors of these libraries and tutorials!
@@ -147,27 +189,19 @@ Big thanks to the authors of these libraries and tutorials!
147
189
148
190
The code was tested on Arduino Nano, Uno and Mega, ethernet chips W5100 and W5500. It may work on other platforms, but:
149
191
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)
192
+
* 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
193
* The restart function will also work only on Arduino.
152
194
153
-
#### Ethernet socket
195
+
#### Ethernet sockets
154
196
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.
197
+
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
198
157
199
#### Memory
158
200
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