Skip to content

Commit 1437919

Browse files
committed
Formating
1 parent 1cb3899 commit 1437919

File tree

5 files changed

+45
-45
lines changed

5 files changed

+45
-45
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,16 @@ uint8_t socketInQueue[MAX_SOCK_NUM] = { 0, 0, 0, 0 };
217217

218218
// from https://github.com/SapientHetero/Ethernet/blob/master/src/socket.cpp
219219
void manageSockets() {
220-
uint32_t maxAge = 0; // the 'age' of the socket in a 'disconnectable' state that was last used the longest time ago
220+
uint32_t maxAge = 0; // the 'age' of the socket in a 'disconnectable' state that was last used the longest time ago
221221
uint8_t oldest = MAX_SOCK_NUM; // the socket number of the 'oldest' disconnectable socket
222222
uint8_t modbusListening = MAX_SOCK_NUM;
223223
uint8_t webListening = MAX_SOCK_NUM;
224224
uint8_t dataAvailable = MAX_SOCK_NUM;
225225
uint8_t socketsAvailable = 0;
226226
// SPI.beginTransaction(SPI_ETHERNET_SETTINGS); // begin SPI transaction
227227
// look at all the hardware sockets, record and take action based on current states
228-
for (uint8_t s = 0; s < maxSockNum; s++) { // for each hardware socket ...
229-
uint8_t status = W5100.readSnSR(s); // get socket status...
228+
for (uint8_t s = 0; s < maxSockNum; s++) { // for each hardware socket ...
229+
uint8_t status = W5100.readSnSR(s); // get socket status...
230230
uint32_t sockAge = millis() - lastSocketUse[s]; // age of the current socket
231231
if (socketInQueue[s] > 0) {
232232
lastSocketUse[s] = millis();

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ void recvUdp() {
4444
if (msgLength) {
4545
#ifdef ENABLE_EXTRA_DIAG
4646
ethCount[DATA_RX] += msgLength;
47-
#endif /* ENABLE_EXTRA_DIAG */
47+
#endif /* ENABLE_EXTRA_DIAG */
4848
uint8_t inBuffer[MODBUS_SIZE + 4]; // Modbus TCP frame is 4 bytes longer than Modbus RTU frame
49-
// Modbus TCP/UDP frame: [0][1] transaction ID, [2][3] protocol ID, [4][5] length and [6] unit ID (address)..... no CRC
50-
// Modbus RTU frame: [0] address.....[n-1][n] CRC
49+
// Modbus TCP/UDP frame: [0][1] transaction ID, [2][3] protocol ID, [4][5] length and [6] unit ID (address)..... no CRC
50+
// Modbus RTU frame: [0] address.....[n-1][n] CRC
5151
Udp.read(inBuffer, sizeof(inBuffer));
5252
while (Udp.available()) Udp.read();
5353
uint8_t errorCode = checkRequest(inBuffer, msgLength, (uint32_t)Udp.remoteIP(), Udp.remotePort(), UDP_REQUEST);
@@ -59,8 +59,8 @@ void recvUdp() {
5959
Udp.write(0x03);
6060
}
6161
uint8_t addressPos = 6 * !localConfig.enableRtuOverTcp; // position of slave address in the incoming TCP/UDP message (0 for Modbus RTU over TCP/UDP and 6 for Modbus RTU over TCP/UDP)
62-
Udp.write(inBuffer[addressPos]); // address
63-
Udp.write(inBuffer[addressPos + 1] + 0x80); // function + 0x80
62+
Udp.write(inBuffer[addressPos]); // address
63+
Udp.write(inBuffer[addressPos + 1] + 0x80); // function + 0x80
6464
Udp.write(errorCode);
6565
if (localConfig.enableRtuOverTcp) {
6666
crc = 0xFFFF;
@@ -83,7 +83,7 @@ void recvTcp(EthernetClient &client) {
8383
uint16_t msgLength = client.available();
8484
#ifdef ENABLE_EXTRA_DIAG
8585
ethCount[DATA_RX] += msgLength;
86-
#endif /* ENABLE_EXTRA_DIAG */
86+
#endif /* ENABLE_EXTRA_DIAG */
8787
uint8_t inBuffer[MODBUS_SIZE + 4]; // Modbus TCP frame is 4 bytes longer than Modbus RTU frame
8888
// Modbus TCP/UDP frame: [0][1] transaction ID, [2][3] protocol ID, [4][5] length and [6] unit ID (address).....
8989
// Modbus RTU frame: [0] address.....
@@ -100,8 +100,8 @@ void recvTcp(EthernetClient &client) {
100100
i = 6;
101101
}
102102
uint8_t addressPos = 6 * !localConfig.enableRtuOverTcp; // position of slave address in the incoming TCP/UDP message (0 for Modbus RTU over TCP/UDP and 6 for Modbus RTU over TCP/UDP)
103-
outBuffer[i++] = inBuffer[addressPos]; // address
104-
outBuffer[i++] = inBuffer[addressPos + 1] + 0x80; // function + 0x80
103+
outBuffer[i++] = inBuffer[addressPos]; // address
104+
outBuffer[i++] = inBuffer[addressPos + 1] + 0x80; // function + 0x80
105105
outBuffer[i++] = errorCode;
106106
if (localConfig.enableRtuOverTcp) {
107107
crc = 0xFFFF;
@@ -150,7 +150,7 @@ void scanRequest() {
150150

151151
uint8_t checkRequest(uint8_t inBuffer[], uint16_t msgLength, const uint32_t remoteIP, const uint16_t remotePort, uint8_t requestType) {
152152
uint8_t addressPos = 6 * !localConfig.enableRtuOverTcp; // position of slave address in the incoming TCP/UDP message (0 for Modbus RTU over TCP/UDP and 6 for Modbus RTU over TCP/UDP)
153-
if (localConfig.enableRtuOverTcp) { // check CRC for Modbus RTU over TCP/UDP
153+
if (localConfig.enableRtuOverTcp) { // check CRC for Modbus RTU over TCP/UDP
154154
if (checkCRC(inBuffer, msgLength) == false) {
155155
errorCount[ERROR_TCP]++;
156156
return 0; // drop request and do not return any error code

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ void sendSerial() {
9494
// send modbus error 0x0B (Gateway Target Device Failed to Respond) - usually means that target device (address) is not present
9595
setSlaveStatus(queueData[0], SLAVE_ERROR_0B, true, false);
9696
uint8_t MBAP[] = { myHeader.tid[0],
97-
myHeader.tid[1],
98-
0x00,
99-
0x00,
100-
0x00,
101-
0x03 };
97+
myHeader.tid[1],
98+
0x00,
99+
0x00,
100+
0x00,
101+
0x03 };
102102
uint8_t PDU[5] = { queueData[0],
103-
byte(queueData[1] + 0x80),
104-
0x0B };
103+
byte(queueData[1] + 0x80),
104+
0x0B };
105105
crc = 0xFFFF;
106106
for (uint8_t i = 0; i < 3; i++) {
107107
calculateCRC(PDU[i]);
@@ -216,10 +216,10 @@ bool checkCRC(uint8_t buf[], int16_t len) {
216216
}
217217

218218
void calculateCRC(uint8_t b) {
219-
crc ^= (uint16_t)b; // XOR byte into least sig. byte of crc
219+
crc ^= (uint16_t)b; // XOR byte into least sig. byte of crc
220220
for (uint8_t i = 8; i != 0; i--) { // Loop over each bit
221-
if ((crc & 0x0001) != 0) { // If the LSB is set
222-
crc >>= 1; // Shift right and XOR 0xA001
221+
if ((crc & 0x0001) != 0) { // If the LSB is set
222+
crc >>= 1; // Shift right and XOR 0xA001
223223
crc ^= 0xA001;
224224
} else // Else LSB is not set
225225
crc >>= 1; // Just shift right

arduino-modbus-rtu-tcp-gateway/advanced_settings.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@
66
#define mySerial Serial // define serial port for RS485 interface, for Arduino Mega choose from Serial1, Serial2 or Serial3
77
// List of baud rates (divided by 100) available in WebUI. Feel free to add your custom baud rate (anything between 3 and 2500)
88
const uint16_t 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
9+
#define RS485_CONTROL_PIN 6 // Arduino Pin for RS485 Direction control, disable if you have module with hardware flow control
1010
const uint8_t MAX_QUEUE_REQUESTS = 10; // max number of TCP or UDP requests stored in a queue
11-
const uint16_t MAX_QUEUE_DATA = 256; // total length of TCP or UDP requests stored in a queue (in bytes)
12-
const uint16_t MAX_SLAVES = 247; // max number of Modbus slaves (Modbus supports up to 247 slaves, the rest is for reserved addresses)
13-
const uint16_t MODBUS_SIZE = 256; // size of a MODBUS RTU frame (determines size of various buffers)
11+
const uint16_t MAX_QUEUE_DATA = 256; // total length of TCP or UDP requests stored in a queue (in bytes)
12+
const uint16_t MAX_SLAVES = 247; // max number of Modbus slaves (Modbus supports up to 247 slaves, the rest is for reserved addresses)
13+
const uint16_t MODBUS_SIZE = 256; // size of a MODBUS RTU frame (determines size of various buffers)
1414
const uint8_t MAX_RESPONSE_LEN = 16; // Max length (bytes) of the Modbus response shown in WebUI
1515
const uint8_t SCAN_FUNCTION_FIRST = 0x03; // Function code sent during Modbus RTU Scan request (first attempt)
1616
const uint8_t SCAN_FUNCTION_SECOND = 0x04; // Function code sent during Modbus RTU Scan request (second attempt)
1717
const uint8_t SCAN_DATA_ADDRESS = 0x01; // Data address sent during Modbus RTU Scan request (both attempts)
18-
const uint16_t SCAN_TIMEOUT = 200; // Timeout (ms) for Modbus scan requests
18+
const uint16_t SCAN_TIMEOUT = 200; // Timeout (ms) for Modbus scan requests
1919

20-
const uint8_t MAC_START[3] = { 0x90, 0xA2, 0xDA }; // MAC range for Gheo SA
21-
const uint8_t ETH_RESET_PIN = 7; // Ethernet shield reset pin (deals with power on reset issue on low quality ethernet shields)
22-
const uint16_t ETH_RESET_DELAY = 500; // Delay (ms) during Ethernet start, wait for Ethernet shield to start (reset issue on low quality ethernet shields)
23-
const uint16_t WEB_IDLE_TIMEOUT = 400; // Time (ms) from last client data after which webserver TCP socket could be disconnected, non-blocking.
24-
const uint16_t 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 uint16_t TCP_RETRANSMISSION_TIMEOUT = 50; // Ethernet controller’s timeout (ms), blocking (see https://www.arduino.cc/reference/en/libraries/ethernet/ethernet.setretransmissiontimeout/)
26-
const uint8_t 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 uint16_t FETCH_INTERVAL = 2000; // Fetch API interval (ms) for the Modbus Status webpage to renew data from JSON served by Arduino
20+
const uint8_t MAC_START[3] = { 0x90, 0xA2, 0xDA }; // MAC range for Gheo SA
21+
const uint8_t ETH_RESET_PIN = 7; // Ethernet shield reset pin (deals with power on reset issue on low quality ethernet shields)
22+
const uint16_t ETH_RESET_DELAY = 500; // Delay (ms) during Ethernet start, wait for Ethernet shield to start (reset issue on low quality ethernet shields)
23+
const uint16_t WEB_IDLE_TIMEOUT = 400; // Time (ms) from last client data after which webserver TCP socket could be disconnected, non-blocking.
24+
const uint16_t 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 uint16_t TCP_RETRANSMISSION_TIMEOUT = 50; // Ethernet controller’s timeout (ms), blocking (see https://www.arduino.cc/reference/en/libraries/ethernet/ethernet.setretransmissiontimeout/)
26+
const uint8_t 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 uint16_t FETCH_INTERVAL = 2000; // Fetch API interval (ms) for the Modbus Status webpage to renew data from JSON served by Arduino
2828

29-
const uint8_t CONFIG_START = 96; // Start address where config and counters are saved in EEPROM
29+
const uint8_t CONFIG_START = 96; // Start address where config and counters are saved in EEPROM
3030
const uint8_t EEPROM_INTERVAL = 6; // Interval (hours) for saving Modbus statistics to EEPROM (in order to minimize writes to EEPROM)
3131

3232
/****** EXTRA FUNCTIONS ******/

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ typedef struct {
4242
uint8_t ip[4];
4343
uint8_t subnet[4];
4444
uint8_t gateway[4];
45-
uint8_t dns[4]; // only used if ENABLE_DHCP
45+
uint8_t dns[4]; // only used if ENABLE_DHCP
4646
bool enableDhcp; // only used if ENABLE_DHCP
4747
uint16_t tcpPort;
4848
uint16_t udpPort;
@@ -60,19 +60,19 @@ typedef struct {
6060
config_type localConfig;
6161

6262
typedef struct {
63-
uint8_t tid[2]; // MBAP Transaction ID
64-
uint8_t msgLen; // lenght of Modbus message stored in queueData
65-
IPAddress remIP; // remote IP for UDP client (UDP response is sent back to remote IP)
66-
uint16_t remPort; // remote port for UDP client (UDP response is sent back to remote port)
67-
uint8_t requestType; // TCP client who sent the request
68-
uint8_t atts; // attempts counter
63+
uint8_t tid[2]; // MBAP Transaction ID
64+
uint8_t msgLen; // lenght of Modbus message stored in queueData
65+
IPAddress remIP; // remote IP for UDP client (UDP response is sent back to remote IP)
66+
uint16_t remPort; // remote port for UDP client (UDP response is sent back to remote port)
67+
uint8_t requestType; // TCP client who sent the request
68+
uint8_t atts; // attempts counter
6969
} header;
7070

7171
#include "advanced_settings.h"
7272

7373
// each request is stored in 3 queues (all queues are written to, read and deleted in sync)
7474
CircularBuffer<header, MAX_QUEUE_REQUESTS> queueHeaders; // queue of requests' headers and metadata
75-
CircularBuffer<uint8_t, MAX_QUEUE_DATA> queueData; // queue of PDU data
75+
CircularBuffer<uint8_t, MAX_QUEUE_DATA> queueData; // queue of PDU data
7676

7777

7878
/****** ETHERNET AND SERIAL ******/
@@ -175,7 +175,7 @@ uint32_t errorCount[ERROR_LAST]; // there is no counter for SLAVE_ERROR_0B_QUEU
175175
uint32_t eepromWrites; // Number of EEPROM write cycles
176176

177177
bool scanReqInQueue = false; // Scan request is in the queue
178-
uint8_t priorityReqInQueue; // Counter for priority requests in the queue
178+
uint8_t priorityReqInQueue; // Counter for priority requests in the queue
179179

180180
uint8_t response[MAX_RESPONSE_LEN]; // buffer to store the last Modbus response
181181
uint8_t responseLen; // stores actual length of the response shown in WebUI

0 commit comments

Comments
 (0)