Skip to content

Commit 6d01f9c

Browse files
committed
Coding style adjustments
1 parent 0482fb2 commit 6d01f9c

File tree

6 files changed

+78
-91
lines changed

6 files changed

+78
-91
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ void startSerial() {
5757
}
5858

5959
void startEthernet() {
60-
if (ethResetPin != 0) {
61-
pinMode(ethResetPin, OUTPUT);
62-
digitalWrite(ethResetPin, LOW);
60+
if (ETH_RESET_PIN != 0) {
61+
pinMode(ETH_RESET_PIN, OUTPUT);
62+
digitalWrite(ETH_RESET_PIN, LOW);
6363
delay(25);
64-
digitalWrite(ethResetPin, HIGH);
64+
digitalWrite(ETH_RESET_PIN, HIGH);
6565
delay(500);
6666
}
6767
byte mac[6];
@@ -121,9 +121,8 @@ void maintainUptime() {
121121
#ifdef ENABLE_EXTRA_DIAG
122122
void maintainCounters() {
123123
// synchronize roll-over of data counters to zero, at 0xFFFFFF00 or 0xFF00 respectively
124-
const unsigned long rollover = 0xFFFFFF00;
125-
const unsigned int rollover = 0xFF00;
126-
if (serialTxCount > rollover || serialRxCount > rollover || ethTxCount > rollover || ethRxCount > rollover) {
124+
const unsigned long ROLLOVER = 0xFFFFFF00;
125+
if (serialTxCount > ROLLOVER || serialRxCount > ROLLOVER || ethTxCount > ROLLOVER || ethRxCount > ROLLOVER) {
127126
ethRxCount = 0;
128127
ethTxCount = 0;
129128
serialRxCount = 0;

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

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
2727
***************************************************************** */
2828

29-
// Stored in "header.requestType"
29+
// Stored in "header.requestType"
3030
#define PRIORITY_REQUEST B10000000 // Request to slave which is not "nonresponding"
31-
#define SCAN_REQUEST B01000000 // Request triggered by slave scanner
32-
#define UDP_REQUEST B00100000 // UDP request
33-
#define TCP_REQUEST B00001111 // TCP request, also stores TCP client number
31+
#define SCAN_REQUEST B01000000 // Request triggered by slave scanner
32+
#define UDP_REQUEST B00100000 // UDP request
33+
#define TCP_REQUEST B00001111 // TCP request, also stores TCP client number
3434

3535
enum status : byte {
3636
STAT_OK, // Slave Responded
@@ -42,7 +42,7 @@ enum status : byte {
4242
};
4343

4444
// bool arrays for storing Modbus RTU status of individual slaves
45-
uint8_t stat[STAT_NUM][(maxSlaves + 1 + 7) / 8];
45+
uint8_t stat[STAT_NUM][(MAX_SLAVES + 1 + 7) / 8];
4646

4747
// Scan request is in the queue
4848
bool scanReqInQueue = false;
@@ -63,21 +63,21 @@ typedef struct {
6363
byte msgLen; // lenght of Modbus message stored in queueData
6464
IPAddress remIP; // remote IP for UDP client (UDP response is sent back to remote IP)
6565
unsigned int remPort; // remote port for UDP client (UDP response is sent back to remote port)
66-
byte requestType; // TCP client who sent the request
66+
byte requestType; // TCP client who sent the request
6767
byte atts; // attempts counter
6868
} header;
6969

7070
// each request is stored in 3 queues (all queues are written to, read and deleted in sync)
71-
CircularBuffer<header, maxQueueRequests> queueHeaders; // queue of requests' headers and metadata (MBAP transaction ID, MBAP unit ID, PDU length, remIP, remPort, TCP client)
72-
CircularBuffer<byte, maxQueueData> queueData; // queue of PDU data (function code, data)
71+
CircularBuffer<header, MAX_QUEUE_REQUESTS> queueHeaders; // queue of requests' headers and metadata
72+
CircularBuffer<byte, MAX_QUEUE_DATA> queueData; // queue of PDU data
7373

7474
void recvUdp() {
7575
unsigned int msgLength = Udp.parsePacket();
7676
if (msgLength) {
7777
#ifdef ENABLE_EXTRA_DIAG
7878
ethRxCount += msgLength;
7979
#endif /* ENABLE_EXTRA_DIAG */
80-
byte inBuffer[modbusSize + 4]; // Modbus TCP frame is 4 bytes longer than Modbus RTU frame
80+
byte inBuffer[MODBUS_SIZE + 4]; // Modbus TCP frame is 4 bytes longer than Modbus RTU frame
8181
// Modbus TCP/UDP frame: [0][1] transaction ID, [2][3] protocol ID, [4][5] length and [6] unit ID (address)..... no CRC
8282
// Modbus RTU frame: [0] address.....[n-1][n] CRC
8383
Udp.read(inBuffer, sizeof(inBuffer));
@@ -118,7 +118,7 @@ void recvTcp() {
118118
#ifdef ENABLE_EXTRA_DIAG
119119
ethRxCount += msgLength;
120120
#endif /* ENABLE_EXTRA_DIAG */
121-
byte inBuffer[modbusSize + 4]; // Modbus TCP frame is 4 bytes longer than Modbus RTU frame
121+
byte inBuffer[MODBUS_SIZE + 4]; // Modbus TCP frame is 4 bytes longer than Modbus RTU frame
122122
// Modbus TCP/UDP frame: [0][1] transaction ID, [2][3] protocol ID, [4][5] length and [6] unit ID (address).....
123123
// Modbus RTU frame: [0] address.....
124124
client.read(inBuffer, sizeof(inBuffer));
@@ -152,23 +152,23 @@ void recvTcp() {
152152

153153
void processRequests() {
154154
// Insert scan request into queue, allow only one scan request in a queue
155-
if (scanCounter != 0 && queueHeaders.available() > 1 && queueData.available() > sizeof(scanCommand) + 1 && scanReqInQueue == false) {
155+
if (scanCounter != 0 && queueHeaders.available() > 1 && queueData.available() > sizeof(SCAN_COMMAND) + 1 && scanReqInQueue == false) {
156156
scanReqInQueue = true;
157157
// Store scan request in request queue
158158
queueHeaders.push(header{
159-
{ 0x00, 0x00 }, // tid[2]
160-
sizeof(scanCommand) + 1, // msgLen
161-
{}, // remIP
162-
0, // remPort
163-
SCAN_REQUEST, // requestType
164-
0, // atts
159+
{ 0x00, 0x00 }, // tid[2]
160+
sizeof(SCAN_COMMAND) + 1, // msgLen
161+
{}, // remIP
162+
0, // remPort
163+
SCAN_REQUEST, // requestType
164+
0, // atts
165165
});
166166
queueData.push(scanCounter); // address of the scanned slave
167-
for (byte i = 0; i < sizeof(scanCommand); i++) {
168-
queueData.push(scanCommand[i]);
167+
for (byte i = 0; i < sizeof(SCAN_COMMAND); i++) {
168+
queueData.push(SCAN_COMMAND[i]);
169169
}
170170
scanCounter++;
171-
if (scanCounter == maxSlaves + 1) scanCounter = 0;
171+
if (scanCounter == MAX_SLAVES + 1) scanCounter = 0;
172172
}
173173
// Optimize queue (prioritize requests from responding slaves) and trigger sending via serial
174174
if (serialState == IDLE) { // send new data over serial only if we are not waiting for response
@@ -206,25 +206,24 @@ byte checkRequest(const byte inBuffer[], unsigned int msgLength, const IPAddress
206206
}
207207
// allow only one request to non responding slaves
208208
if (getSlaveStatus(inBuffer[addressPos], STAT_ERROR_0B_QUEUE)) {
209-
setSlaveStatus(inBuffer[addressPos], STAT_ERROR_0B, true);
209+
errorCount[STAT_ERROR_0B]++;
210210
return 0x0B; // return modbus error 11 (Gateway Target Device Failed to Respond) - usually means that target device (address) is not present
211211
} else if (getSlaveStatus(inBuffer[addressPos], STAT_ERROR_0B)) {
212212
setSlaveStatus(inBuffer[addressPos], STAT_ERROR_0B_QUEUE, true);
213-
}
214-
215-
// all checkes passed OK, we can store the incoming data in request queue
216-
// Add PRIORITY_REQUEST flag to requests for responding slaves
217-
if (getSlaveStatus(inBuffer[addressPos], STAT_ERROR_0B_QUEUE) == false) {
213+
} else {
214+
// Add PRIORITY_REQUEST flag to requests for responding slaves
218215
requestType = requestType | PRIORITY_REQUEST;
219216
priorityReqInQueue++;
220217
}
218+
219+
// all checkes passed OK, we can store the incoming data in request queue
221220
// Store in request queue
222221
queueHeaders.push(header{
223222
{ inBuffer[0], inBuffer[1] }, // tid[2] (ignored in Modbus RTU over TCP/UDP)
224223
(byte)msgLength, // msgLen
225224
(IPAddress)remoteIP, // remIP
226225
(unsigned int)remotePort, // remPort
227-
(byte)(requestType), // requestType
226+
(byte)(requestType), // requestType
228227
0, // atts
229228
});
230229
for (byte i = 0; i < msgLength; i++) {
@@ -247,12 +246,12 @@ void deleteRequest() // delete request from queue
247246
}
248247

249248
bool getSlaveStatus(const uint8_t slave, const byte status) {
250-
if (slave >= maxSlaves) return false; // error
249+
if (slave >= MAX_SLAVES) return false; // error
251250
return (stat[status][slave / 8] & masks[slave & 7]) > 0;
252251
}
253252

254253
void setSlaveStatus(const uint8_t slave, byte status, const bool value) {
255-
if (slave >= maxSlaves) return; // error
254+
if (slave >= MAX_SLAVES) return; // error
256255
if (value == 0) {
257256
stat[status][slave / 8] &= ~masks[slave & 7];
258257
} else {

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void sendSerial() {
6060
#ifdef RS485_CONTROL_PIN
6161
digitalWrite(RS485_CONTROL_PIN, RS485_RECEIVE); // Disable RS485 Transmit
6262
#endif /* RS485_CONTROL_PIN */
63-
if (queueData[0] == 0x00) { // Modbus broadcast - we do not count attempts and delete immediatelly
63+
if (queueData[0] == 0x00) { // Modbus broadcast - we do not count attempts and delete immediatelly
6464
serialState = IDLE;
6565
deleteRequest();
6666
} else {
@@ -74,12 +74,12 @@ void sendSerial() {
7474
}
7575

7676
void recvSerial() {
77-
static byte serialIn[modbusSize];
77+
static byte serialIn[MODBUS_SIZE];
7878
while (mySerial.available() > 0) {
7979
if (rxTimeout.isOver() && rxNdx != 0) {
8080
rxErr = true; // character timeout
8181
}
82-
if (rxNdx < modbusSize) {
82+
if (rxNdx < MODBUS_SIZE) {
8383
serialIn[rxNdx] = mySerial.read();
8484
rxNdx++;
8585
} else {
@@ -93,13 +93,12 @@ void recvSerial() {
9393
// Process Serial data
9494
// Checks: 1) RTU frame is without errors; 2) CRC; 3) address of incoming packet against first request in queue; 4) only expected responses are forwarded to TCP/UDP
9595
header myHeader = queueHeaders.first();
96-
if (!rxErr && checkCRC(serialIn, rxNdx) == true) {
97-
if (serialIn[1] > 0x80 && (myHeader.requestType & SCAN_REQUEST) == false) {
98-
setSlaveStatus(serialIn[0], STAT_ERROR_0X, true);
99-
} else {
100-
setSlaveStatus(serialIn[0], STAT_OK, true);
101-
}
102-
if (serialIn[0] == queueData[0] && serialState == WAITING) {
96+
if (!rxErr && checkCRC(serialIn, rxNdx) == true && serialIn[0] == queueData[0] && serialState == WAITING) {
97+
if (serialIn[1] > 0x80 && (myHeader.requestType & SCAN_REQUEST) == false) {
98+
setSlaveStatus(serialIn[0], STAT_ERROR_0X, true);
99+
} else {
100+
setSlaveStatus(serialIn[0], STAT_OK, true);
101+
}
103102
byte MBAP[] = { myHeader.tid[0], myHeader.tid[1], 0x00, 0x00, highByte(rxNdx - 2), lowByte(rxNdx - 2) };
104103
if (myHeader.requestType & UDP_REQUEST) {
105104
Udp.beginPacket(myHeader.remIP, myHeader.remPort);
@@ -130,7 +129,6 @@ void recvSerial() {
130129
deleteRequest();
131130
serialState = IDLE;
132131
}
133-
}
134132
#ifdef ENABLE_EXTRA_DIAG
135133
serialRxCount += rxNdx;
136134
#endif /* ENABLE_EXTRA_DIAG */

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
1717
***************************************************************** */
1818

19-
const byte webInBufferSize = 128; // size of web server read buffer (reads a complete line), 128 bytes necessary for POST data
20-
const byte smallbuffersize = 32; // a smaller buffer for uri
19+
const byte WEB_IN_BUFFER_SIZE = 128; // size of web server read buffer (reads a complete line), 128 bytes necessary for POST data
20+
const byte SMALL_BUFFER_SIZE = 32; // a smaller buffer for uri
2121

2222
// Actions that need to be taken after saving configuration.
2323
// These actions are also used by buttons on the Tools page.
@@ -84,11 +84,11 @@ enum post_key : byte {
8484
void recvWeb() {
8585
EthernetClient client = webServer.available();
8686
if (client) {
87-
char uri[smallbuffersize]; // the requested page
88-
// char requestParameter[smallbuffersize]; // parameter appended to the URI after a ?
89-
// char postParameter[smallbuffersize] {'\0'}; // parameter transmitted in the body / by POST
87+
char uri[SMALL_BUFFER_SIZE]; // the requested page
88+
// char requestParameter[SMALL_BUFFER_SIZE]; // parameter appended to the URI after a ?
89+
// char postParameter[SMALL_BUFFER_SIZE] {'\0'}; // parameter transmitted in the body / by POST
9090
if (client.available()) {
91-
char webInBuffer[webInBufferSize]{ '\0' }; // buffer for incoming data
91+
char webInBuffer[WEB_IN_BUFFER_SIZE]{ '\0' }; // buffer for incoming data
9292
unsigned int i = 0; // index / current read position
9393
enum status_type : byte {
9494
REQUEST,
@@ -116,7 +116,7 @@ void recvWeb() {
116116
i = 0;
117117
memset(webInBuffer, 0, sizeof(webInBuffer));
118118
} else {
119-
if (i < (webInBufferSize - 1)) // <==== BUG FOUND
119+
if (i < (WEB_IN_BUFFER_SIZE - 1))
120120
{
121121
webInBuffer[i] = c;
122122
i++;
@@ -306,7 +306,7 @@ void processPost(char postParameter[]) {
306306
{
307307
byte tempMac[3];
308308
memcpy(tempMac, localConfig.macEnd, 3); // keep current MAC
309-
localConfig = defaultConfig;
309+
localConfig = DEFAULT_CONFIG;
310310
memcpy(localConfig.macEnd, tempMac, 3);
311311
break;
312312
}
@@ -329,7 +329,7 @@ void processPost(char postParameter[]) {
329329
break;
330330
}
331331
// new parameter values received, save them to EEPROM
332-
EEPROM.put(configStart + 1, localConfig); // it is safe to call, only changed values are updated
332+
EEPROM.put(CONFIG_START + 1, localConfig); // it is safe to call, only changed values are updated
333333
if (action == SERIAL_SOFT) { // can do it without "please wait" page
334334
Serial.flush();
335335
Serial.end();

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
2525
***************************************************************** */
2626

27-
const byte webOutBufferSize = 128; // size of web server write buffer (used by StreamLib)
27+
const byte WEB_OUT_BUFFER_SIZE = 128; // size of web server write buffer (used by StreamLib)
2828

2929
void sendPage(EthernetClient &client, byte reqPage) {
30-
char webOutBuffer[webOutBufferSize];
30+
char webOutBuffer[WEB_OUT_BUFFER_SIZE];
3131
ChunkedPrint chunked(client, webOutBuffer, sizeof(webOutBuffer)); // the StreamLib object to replace client print
3232
chunked.print(F("HTTP/1.1 200 OK\r\n"
3333
"Connection: close\r\n"
@@ -156,9 +156,9 @@ void menuItem(ChunkedPrint &chunked, byte item) {
156156
// Current Status
157157
void contentStatus(ChunkedPrint &chunked) {
158158
chunked.print(F("<tr><td>SW Version:<td>"));
159-
chunked.print(version[0], HEX);
159+
chunked.print(VERSION[0], HEX);
160160
chunked.print(F("."));
161-
chunked.print(version[1], HEX);
161+
chunked.print(VERSION[1], HEX);
162162
chunked.print(F("<tr><td>Microcontroller:<td>"));
163163
chunked.print(BOARD);
164164
// chunked.print(freeMemory());
@@ -327,12 +327,12 @@ void contentStatus(ChunkedPrint &chunked) {
327327
"<tr><td>Requests Queue:<td>"));
328328
chunked.print(queueDataSize);
329329
chunked.print(F(" / "));
330-
chunked.print(maxQueueData);
330+
chunked.print(MAX_QUEUE_DATA);
331331
chunked.print(F(" bytes"
332332
"<tr><td><td>"));
333333
chunked.print(queueHeadersSize);
334334
chunked.print(F(" / "));
335-
chunked.print(maxQueueRequests);
335+
chunked.print(MAX_QUEUE_REQUESTS);
336336
chunked.print(F(" requests"));
337337
queueDataSize = 0;
338338
queueHeadersSize = 0;
@@ -368,7 +368,7 @@ void contentStatus(ChunkedPrint &chunked) {
368368
chunked.print(SCAN);
369369
chunked.print(F(">Scan & Reset Stats</button>"));
370370
byte countSlaves = 0;
371-
for (int k = 1; k < maxSlaves; k++) {
371+
for (int k = 1; k < MAX_SLAVES; k++) {
372372
for (int s = 0; s < STAT_NUM; s++) {
373373
if (getSlaveStatus(k, s) == true || k == scanCounter) {
374374
countSlaves++;
@@ -583,7 +583,7 @@ void contentTools(ChunkedPrint &chunked) {
583583
chunked.print(F(" value="));
584584
chunked.print(FACTORY);
585585
chunked.print(F(">Restore</button> (Static IP: "));
586-
chunked.print((IPAddress)defaultConfig.ip);
586+
chunked.print((IPAddress)DEFAULT_CONFIG.ip);
587587
chunked.print(F(")"
588588
"<tr><td>MAC Address: <td><button name="));
589589
chunked.print(POST_ACTION);

0 commit comments

Comments
 (0)