26
26
27
27
***************************************************************** */
28
28
29
- // Stored in "header.requestType"
29
+ // Stored in "header.requestType"
30
30
#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
34
34
35
35
enum status : byte {
36
36
STAT_OK, // Slave Responded
@@ -42,7 +42,7 @@ enum status : byte {
42
42
};
43
43
44
44
// 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 ];
46
46
47
47
// Scan request is in the queue
48
48
bool scanReqInQueue = false ;
@@ -63,21 +63,21 @@ typedef struct {
63
63
byte msgLen; // lenght of Modbus message stored in queueData
64
64
IPAddress remIP; // remote IP for UDP client (UDP response is sent back to remote IP)
65
65
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
67
67
byte atts; // attempts counter
68
68
} header;
69
69
70
70
// 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
73
73
74
74
void recvUdp () {
75
75
unsigned int msgLength = Udp.parsePacket ();
76
76
if (msgLength) {
77
77
#ifdef ENABLE_EXTRA_DIAG
78
78
ethRxCount += msgLength;
79
79
#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
81
81
// Modbus TCP/UDP frame: [0][1] transaction ID, [2][3] protocol ID, [4][5] length and [6] unit ID (address)..... no CRC
82
82
// Modbus RTU frame: [0] address.....[n-1][n] CRC
83
83
Udp.read (inBuffer, sizeof (inBuffer));
@@ -118,7 +118,7 @@ void recvTcp() {
118
118
#ifdef ENABLE_EXTRA_DIAG
119
119
ethRxCount += msgLength;
120
120
#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
122
122
// Modbus TCP/UDP frame: [0][1] transaction ID, [2][3] protocol ID, [4][5] length and [6] unit ID (address).....
123
123
// Modbus RTU frame: [0] address.....
124
124
client.read (inBuffer, sizeof (inBuffer));
@@ -152,23 +152,23 @@ void recvTcp() {
152
152
153
153
void processRequests () {
154
154
// 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 ) {
156
156
scanReqInQueue = true ;
157
157
// Store scan request in request queue
158
158
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
165
165
});
166
166
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]);
169
169
}
170
170
scanCounter++;
171
- if (scanCounter == maxSlaves + 1 ) scanCounter = 0 ;
171
+ if (scanCounter == MAX_SLAVES + 1 ) scanCounter = 0 ;
172
172
}
173
173
// Optimize queue (prioritize requests from responding slaves) and trigger sending via serial
174
174
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
206
206
}
207
207
// allow only one request to non responding slaves
208
208
if (getSlaveStatus (inBuffer[addressPos], STAT_ERROR_0B_QUEUE)) {
209
- setSlaveStatus (inBuffer[addressPos], STAT_ERROR_0B, true ) ;
209
+ errorCount[ STAT_ERROR_0B]++ ;
210
210
return 0x0B ; // return modbus error 11 (Gateway Target Device Failed to Respond) - usually means that target device (address) is not present
211
211
} else if (getSlaveStatus (inBuffer[addressPos], STAT_ERROR_0B)) {
212
212
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
218
215
requestType = requestType | PRIORITY_REQUEST;
219
216
priorityReqInQueue++;
220
217
}
218
+
219
+ // all checkes passed OK, we can store the incoming data in request queue
221
220
// Store in request queue
222
221
queueHeaders.push (header{
223
222
{ inBuffer[0 ], inBuffer[1 ] }, // tid[2] (ignored in Modbus RTU over TCP/UDP)
224
223
(byte)msgLength, // msgLen
225
224
(IPAddress)remoteIP, // remIP
226
225
(unsigned int )remotePort, // remPort
227
- (byte)(requestType), // requestType
226
+ (byte)(requestType), // requestType
228
227
0 , // atts
229
228
});
230
229
for (byte i = 0 ; i < msgLength; i++) {
@@ -247,12 +246,12 @@ void deleteRequest() // delete request from queue
247
246
}
248
247
249
248
bool getSlaveStatus (const uint8_t slave, const byte status) {
250
- if (slave >= maxSlaves ) return false ; // error
249
+ if (slave >= MAX_SLAVES ) return false ; // error
251
250
return (stat[status][slave / 8 ] & masks[slave & 7 ]) > 0 ;
252
251
}
253
252
254
253
void setSlaveStatus (const uint8_t slave, byte status, const bool value) {
255
- if (slave >= maxSlaves ) return ; // error
254
+ if (slave >= MAX_SLAVES ) return ; // error
256
255
if (value == 0 ) {
257
256
stat[status][slave / 8 ] &= ~masks[slave & 7 ];
258
257
} else {
0 commit comments