Skip to content

Commit 44f146d

Browse files
committed
Set MX_SOCK_NUM to 8
Set maximum sockets to 8 as default. Set 4 sockets for W5200.
1 parent 56af9c9 commit 44f146d

File tree

4 files changed

+28
-31
lines changed

4 files changed

+28
-31
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ Screenshots of the web interface:
3636
* supports all Modbus function codes
3737
* settings can be changed via web interface, stored in EEPROM
3838
* diagnostics and Modbus RTU scan via web interface
39-
* optimized queue for Modbus requests
40-
- prioritization of requests to responding slaves
41-
- queue will accept only one requests to non-responding slaves
39+
* optimized queue for Modbus requests (queue will accept only one requests to non-responding slaves)
4240

4341
## How can I build it myself?
4442
Get the hardware (cheap clones from China are sufficient) and connect together:

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,8 @@ void startEthernet() {
8383
Udp.begin(localConfig.udpPort);
8484
modbusServer.begin();
8585
webServer.begin();
86-
// Ethernet library determines MAX_SOCK_NUM by Microcontroller RAM (not by Ethernet chip type).
87-
// Therefore, for Arduino Mega + W5100, MAX_SOCK_NUM is wrongly set to 8
8886
if (Ethernet.hardwareStatus() == EthernetW5100) {
89-
maxSockNum = 4;
87+
maxSockNum = 4; // W5100 chip never supports more than 4 sockets
9088
}
9189
dbg(F("[arduino] Server available at http://"));
9290
dbgln(Ethernet.localIP());
@@ -122,14 +120,15 @@ void maintainCounters() {
122120
// synchronize roll-over of data counters to zero, at 0xFFFFFF00 or 0xFF00 respectively
123121
#ifdef ENABLE_EXTRA_DIAG
124122
const unsigned long rollover = 0xFFFFFF00;
123+
if (serialTxCount > rollover || serialRxCount > rollover || ethTxCount > rollover || ethRxCount > rollover) {
124+
ethRxCount = 0;
125+
ethTxCount = 0;
125126
#else
126127
const unsigned int rollover = 0xFF00;
128+
if (serialTxCount > rollover || serialRxCount > rollover) {
127129
#endif /* ENABLE_EXTRA_DIAG */
128-
if (serialTxCount > rollover || serialRxCount > rollover || ethTxCount > rollover || ethRxCount > rollover) {
129130
serialRxCount = 0;
130131
serialTxCount = 0;
131-
ethRxCount = 0;
132-
ethTxCount = 0;
133132
}
134133
}
135134

@@ -139,29 +138,25 @@ void generateMac() {
139138
seed1 = 36969L * (seed1 & 65535L) + (seed1 >> 16);
140139
seed2 = 18000L * (seed2 & 65535L) + (seed2 >> 16);
141140
uint32_t randomBuffer = (seed1 << 16) + seed2; /* 32-bit random */
142-
143141
for (byte i = 0; i < 3; i++) {
144142
localConfig.macEnd[i] = randomBuffer & 0xFF;
145143
randomBuffer >>= 8;
146144
}
147145
}
148146

147+
// https://sites.google.com/site/astudyofentropy/project-definition/timer-jitter-entropy-sources/entropy-library/arduino-random-seed
149148
void CreateTrulyRandomSeed() {
150149
seed1 = 0;
151-
nrot = 32; // Must be at least 4, but more increased the uniformity of the produced
152-
// seeds entropy.
153-
150+
nrot = 32; // Must be at least 4, but more increased the uniformity of the produced seeds entropy.
154151
// The following five lines of code turn on the watch dog timer interrupt to create
155152
// the seed value
156153
cli();
157154
MCUSR = 0;
158155
_WD_CONTROL_REG |= (1 << _WD_CHANGE_BIT) | (1 << WDE);
159156
_WD_CONTROL_REG = (1 << WDIE);
160157
sei();
161-
162158
while (nrot > 0)
163159
; // wait here until seed is created
164-
165160
// The following five lines turn off the watch dog timer interrupt
166161
cli();
167162
MCUSR = 0;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ CircularBuffer<byte, reqQueueCount> queueRetries; // queue of retry counters
4848
void recvUdp() {
4949
unsigned int packetSize = Udp.parsePacket();
5050
if (packetSize) {
51+
#ifdef ENABLE_EXTRA_DIAG
5152
ethRxCount += packetSize;
53+
#endif /* ENABLE_EXTRA_DIAG */
5254
byte udpInBuffer[modbusSize + 4]; // Modbus TCP frame is 4 bytes longer than Modbus RTU frame
5355
// Modbus TCP/UDP frame: [0][1] transaction ID, [2][3] protocol ID, [4][5] length and [6] unit ID (address).....
5456
// Modbus RTU frame: [0] address.....
@@ -98,7 +100,9 @@ void recvTcp() {
98100
EthernetClient client = modbusServer.available();
99101
if (client) {
100102
unsigned int packetSize = client.available();
103+
#ifdef ENABLE_EXTRA_DIAG
101104
ethRxCount += packetSize;
105+
#endif /* ENABLE_EXTRA_DIAG */
102106
byte tcpInBuffer[modbusSize + 4]; // Modbus TCP frame is 4 bytes longer than Modbus RTU frame
103107
// Modbus TCP/UDP frame: [0][1] transaction ID, [2][3] protocol ID, [4][5] length and [6] unit ID (address).....
104108
// Modbus RTU frame: [0] address.....

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,14 @@ const int modbusSize = 256; // size of a MODBUS RTU frame (determines s
6565
#define mySerial Serial // define serial port for RS485 interface, for Arduino Mega choose from Serial1, Serial2 or Serial3
6666
#define RS485_CONTROL_PIN 6 // Arduino Pin for RS485 Direction control, disable if you have module with hardware flow control
6767
const byte ethResetPin = 7; // Ethernet shield reset pin (deals with power on reset issue of the ethernet shield)
68-
const byte scanCommand[] = {0x03, 0x00, 0x00, 0x00, 0x01}; // Command sent during Modbus RTU Scan. Slave is detected if any response (even error) is received.
68+
const byte scanCommand[] = { 0x03, 0x00, 0x00, 0x00, 0x01 }; // Command sent during Modbus RTU Scan. Slave is detected if any response (even error) is received.
6969

7070
// #define DEBUG // Main Serial (USB) is used for printing some debug info, not for Modbus RTU. At the moment, only web server related debug messages are printed.
7171
#define debugSerial Serial
7272

73-
#ifdef MAX_SOCK_NUM //if the macro MAX_SOCK_NUM is defined
74-
// #undef MAX_SOCK_NUM //un-define it
75-
// #define MAX_SOCK_NUM 8 //redefine it with the new value
76-
#endif
77-
7873
/****** EXTRA FUNCTIONS ******/
7974

80-
// these do not fit into the limited flash memory of Arduino Uno/Nano, uncomment if you have board with more memory
75+
// these do not fit into the limited flash memory of Arduino Uno/Nano, uncomment if you have a board with more memory
8176
// #define ENABLE_DHCP // Enable DHCP (Auto IP settings)
8277
// #define ENABLE_EXTRA_DIAG // Enable per socket diagnostics, run time counter
8378

@@ -137,6 +132,11 @@ const byte configStart = 128;
137132
#define UDP_TX_PACKET_MAX_SIZE modbusSize //redefine it with the new value
138133
#endif
139134

135+
#ifdef MAX_SOCK_NUM // Ethernet.h library determines MAX_SOCK_NUM by Microcontroller RAM (not by Ethernet chip type).
136+
#undef MAX_SOCK_NUM // Ignore the RAM-based limitation on the number of sockets.
137+
#define MAX_SOCK_NUM 8 // Use all sockets supported by the ethernet chip.
138+
#endif
139+
140140
byte maxSockNum = MAX_SOCK_NUM;
141141

142142
bool dhcpSuccess = false;
@@ -199,9 +199,11 @@ uint16_t crc;
199199
#define RS485_TRANSMIT HIGH
200200
#define RS485_RECEIVE LOW
201201
byte scanCounter = 0;
202-
enum state : byte
203-
{
204-
IDLE, SENDING, DELAY, WAITING
202+
enum state : byte {
203+
IDLE,
204+
SENDING,
205+
DELAY,
206+
WAITING
205207
};
206208
enum state serialState;
207209
unsigned int charTimeout;
@@ -228,14 +230,13 @@ unsigned long ethRxCount = 0;
228230
#else
229231
unsigned int serialTxCount = 0;
230232
unsigned int serialRxCount = 0;
231-
unsigned int ethTxCount = 0;
232-
unsigned int ethRxCount = 0;
233+
// unsigned int ethTxCount = 0;
234+
// unsigned int ethRxCount = 0;
233235
#endif /* ENABLE_EXTRA_DIAG */
234236

235237
/****** SETUP: RUNS ONCE ******/
236238

237-
void setup()
238-
{
239+
void setup() {
239240
CreateTrulyRandomSeed();
240241

241242
// is config already stored in EEPROM?
@@ -262,8 +263,7 @@ void setup()
262263

263264
/****** LOOP ******/
264265

265-
void loop()
266-
{
266+
void loop() {
267267
recvUdp();
268268
recvTcp();
269269
processRequests();

0 commit comments

Comments
 (0)