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
#defineADDRESS_POS (6 * !localConfig.enableRtuOverTcp) // position of slave address in the TCP/UDP message (0 for Modbus RTU over TCP/UDP and 6 for Modbus RTU over TCP/UDP)
30
+
29
31
// bool arrays for storing Modbus RTU status (responging or not responding). Array index corresponds to slave address.
30
32
uint8_t responding[(maxSlaves + 1 + 7) / 8];
31
33
uint8_t error[(maxSlaves + 1 + 7) / 8];
@@ -44,38 +46,33 @@ typedef struct {
44
46
CircularBuffer<header, reqQueueCount> queueHeaders; // queue of requests' headers and metadata (MBAP transaction ID, MBAP unit ID, PDU length, remIP, remPort, TCP client)
45
47
CircularBuffer<byte, reqQueueSize> queuePDUs; // queue of PDU data (function code, data)
46
48
CircularBuffer<byte, reqQueueCount> queueRetries; // queue of retry counters
47
-
byte pduStart; // first byte of Protocol Data Unit (i.e. Function code)
48
-
49
49
50
50
voidrecvUdp() {
51
-
unsignedintpacketSize = Udp.parsePacket();
52
-
if (packetSize) {
51
+
unsignedintmsgLength = Udp.parsePacket();
52
+
if (msgLength) {
53
53
#ifdef ENABLE_EXTRA_DIAG
54
-
ethRxCount += packetSize;
55
-
#endif/* ENABLE_EXTRA_DIAG */
54
+
ethRxCount += msgLength;
55
+
#endif/* ENABLE_EXTRA_DIAG */
56
56
byte inBuffer[modbusSize + 4]; // Modbus TCP frame is 4 bytes longer than Modbus RTU frame
57
57
// Modbus TCP/UDP frame: [0][1] transaction ID, [2][3] protocol ID, [4][5] length and [6] unit ID (address)..... no CRC
58
58
// Modbus RTU frame: [0] address.....[n-1][n] CRC
59
59
Udp.read(inBuffer, sizeof(inBuffer));
60
60
Udp.flush();
61
-
if (localConfig.enableRtuOverTcp) pduStart = 1; // In Modbus RTU, Function code is second byte (after address)
62
-
else pduStart = 7; // In Modbus TCP/UDP, Function code is 8th byte (after address)
if (queueHeaders.available() < 1 || queuePDUs.available() < msgLength) {
178
173
return0x06; // return modbus error 6 (Slave Device Busy) - try again later
179
174
}
180
-
//al checkes passed OK, we can store the incoming data in request queue
181
-
// Store in request queue: 2 bytes MBAP Transaction ID (ignored in Modbus RTU over TCP); MBAP Unit ID (address); PDUlen (func + data);remote IP; remote port; TCP client Number (socket) - 0xFF for UDP
//all checkes passed OK, we can store the incoming data in request queue
176
+
// Store in request queue: 2 bytes MBAP Transaction ID (ignored in Modbus RTU over TCP); MBAP Unit ID (address); message length; remote IP; remote port; TCP client Number (socket) - 0xFF for UDP
if (myHeader.uid == 0x00) { // Modbus broadcast - we do not count attempts and delete immediatelly
61
+
if (queuePDUs[0] == 0x00) { // Modbus broadcast - we do not count attempts and delete immediatelly
67
62
serialState = IDLE;
68
63
deleteRequest();
69
64
} else {
@@ -95,7 +90,7 @@ void recvSerial() {
95
90
// Process Serial data
96
91
// 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
0 commit comments