Skip to content

Commit d584400

Browse files
committed
data types
1 parent b44bcee commit d584400

File tree

6 files changed

+143
-143
lines changed

6 files changed

+143
-143
lines changed

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ void startSerial() {
5555
}
5656

5757
// number of bits per character (11 in default Modbus RTU settings)
58-
uint8_t bitsPerChar() {
59-
uint8_t bits =
58+
byte bitsPerChar() {
59+
byte bits =
6060
1 + // start bit
6161
(((localConfig.serialConfig & 0x06) >> 1) + 5) + // data bits
6262
(((localConfig.serialConfig & 0x08) >> 3) + 1); // stop bits
@@ -117,7 +117,7 @@ void (*resetFunc)(void) = 0; //declare reset function at address 0
117117
#ifdef ENABLE_DHCP
118118
void maintainDhcp() {
119119
if (localConfig.enableDhcp && dhcpSuccess == true) { // only call maintain if initial DHCP request by startEthernet was successfull
120-
uint8_t maintainResult = Ethernet.maintain();
120+
byte maintainResult = Ethernet.maintain();
121121
if (maintainResult == 1 || maintainResult == 3) { // renew failed or rebind failed
122122
dhcpSuccess = false;
123123
startEthernet(); // another DHCP request, fallback to static IP
@@ -145,7 +145,7 @@ void maintainUptime() {
145145
bool rollover() {
146146
// synchronize roll-over of run time, data counters and modbus stats to zero, at 0xFFFFFF00
147147
const uint32_t ROLLOVER = 0xFFFFFF00;
148-
for (uint8_t i = 0; i < ERROR_LAST; i++) {
148+
for (byte i = 0; i < ERROR_LAST; i++) {
149149
if (errorCount[i] > ROLLOVER) {
150150
return true;
151151
}
@@ -154,7 +154,7 @@ bool rollover() {
154154
if (seconds > ROLLOVER) {
155155
return true;
156156
}
157-
for (uint8_t i = 0; i < DATA_LAST; i++) {
157+
for (byte i = 0; i < DATA_LAST; i++) {
158158
if (rtuCount[i] > ROLLOVER || ethCount[i] > ROLLOVER) {
159159
return true;
160160
}
@@ -179,7 +179,7 @@ void generateMac() {
179179
seed2 = 18000L * (seed2 & 65535L) + (seed2 >> 16);
180180
uint32_t randomBuffer = (seed1 << 16) + seed2; /* 32-bit random */
181181
memcpy(mac, MAC_START, 3); // set first 3 bytes
182-
for (uint8_t i = 0; i < 3; i++) {
182+
for (byte i = 0; i < 3; i++) {
183183
mac[i + 3] = randomBuffer & 0xFF; // random last 3 bytes
184184
randomBuffer >>= 8;
185185
}
@@ -209,24 +209,24 @@ void updateEeprom() {
209209

210210
#if MAX_SOCK_NUM == 8
211211
uint32_t lastSocketUse[MAX_SOCK_NUM] = { 0, 0, 0, 0, 0, 0, 0, 0 };
212-
uint8_t socketInQueue[MAX_SOCK_NUM] = { 0, 0, 0, 0, 0, 0, 0, 0 };
212+
byte socketInQueue[MAX_SOCK_NUM] = { 0, 0, 0, 0, 0, 0, 0, 0 };
213213
#elif MAX_SOCK_NUM == 4
214214
uint32_t lastSocketUse[MAX_SOCK_NUM] = { 0, 0, 0, 0 };
215-
uint8_t socketInQueue[MAX_SOCK_NUM] = { 0, 0, 0, 0 };
215+
byte socketInQueue[MAX_SOCK_NUM] = { 0, 0, 0, 0 };
216216
#endif
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
221-
uint8_t oldest = MAX_SOCK_NUM; // the socket number of the 'oldest' disconnectable socket
222-
uint8_t modbusListening = MAX_SOCK_NUM;
223-
uint8_t webListening = MAX_SOCK_NUM;
224-
uint8_t dataAvailable = MAX_SOCK_NUM;
225-
uint8_t socketsAvailable = 0;
220+
uint32_t maxAge = 0; // the 'age' of the socket in a 'disconnectable' state that was last used the longest time ago
221+
byte oldest = MAX_SOCK_NUM; // the socket number of the 'oldest' disconnectable socket
222+
byte modbusListening = MAX_SOCK_NUM;
223+
byte webListening = MAX_SOCK_NUM;
224+
byte dataAvailable = MAX_SOCK_NUM;
225+
byte 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 (byte s = 0; s < maxSockNum; s++) { // for each hardware socket ...
229+
byte 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();
@@ -313,7 +313,7 @@ void manageSockets() {
313313
// we do not need SPI.beginTransaction(SPI_ETHERNET_SETTINGS) or SPI.endTransaction()
314314
}
315315

316-
void disconSocket(uint8_t s) {
316+
void disconSocket(byte s) {
317317
if (W5100.readSnSR(s) == SnSR::ESTABLISHED) {
318318
W5100.execCmdSn(s, Sock_DISCON); // Sock_DISCON does not close LISTEN sockets
319319
lastSocketUse[s] = millis(); // record time at which it was sent...

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
2929
***************************************************************** */
3030

31-
uint8_t masks[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
31+
byte masks[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
3232

3333
// Stored in "header.requestType"
3434
#define PRIORITY_REQUEST B10000000 // Request to slave which is not "nonresponding"
@@ -44,23 +44,23 @@ void recvUdp() {
4444
if (msgLength) {
4545
#ifdef ENABLE_EXTRA_DIAG
4646
ethCount[DATA_RX] += msgLength;
47-
#endif /* ENABLE_EXTRA_DIAG */
48-
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
47+
#endif /* ENABLE_EXTRA_DIAG */
48+
byte 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
5151
Udp.read(inBuffer, sizeof(inBuffer));
5252
while (Udp.available()) Udp.read();
53-
uint8_t errorCode = checkRequest(inBuffer, msgLength, (uint32_t)Udp.remoteIP(), Udp.remotePort(), UDP_REQUEST);
53+
byte errorCode = checkRequest(inBuffer, msgLength, (uint32_t)Udp.remoteIP(), Udp.remotePort(), UDP_REQUEST);
5454
if (errorCode) {
5555
// send back message with error code
5656
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
5757
if (!localConfig.enableRtuOverTcp) {
5858
Udp.write(inBuffer, 5);
5959
Udp.write(0x03);
6060
}
61-
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
61+
byte 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
6464
Udp.write(errorCode);
6565
if (localConfig.enableRtuOverTcp) {
6666
crc = 0xFFFF;
@@ -83,25 +83,25 @@ 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 */
87-
uint8_t inBuffer[MODBUS_SIZE + 4]; // Modbus TCP frame is 4 bytes longer than Modbus RTU frame
86+
#endif /* ENABLE_EXTRA_DIAG */
87+
byte 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.....
9090
client.read(inBuffer, sizeof(inBuffer));
9191
while (client.available()) client.read();
92-
uint8_t errorCode = checkRequest(inBuffer, msgLength, {}, client.remotePort(), TCP_REQUEST | client.getSocketNumber());
92+
byte errorCode = checkRequest(inBuffer, msgLength, {}, client.remotePort(), TCP_REQUEST | client.getSocketNumber());
9393
if (errorCode) {
9494
// send back message with error code
95-
uint8_t i = 0;
96-
uint8_t outBuffer[9];
95+
byte i = 0;
96+
byte outBuffer[9];
9797
if (!localConfig.enableRtuOverTcp) {
9898
memcpy(outBuffer, inBuffer, 5);
9999
outBuffer[5] = 0x03;
100100
i = 6;
101101
}
102-
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
102+
byte 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
105105
outBuffer[i++] = errorCode;
106106
if (localConfig.enableRtuOverTcp) {
107107
crc = 0xFFFF;
@@ -122,7 +122,7 @@ void recvTcp(EthernetClient &client) {
122122

123123
void scanRequest() {
124124
// Insert scan request into queue, allow only one scan request in a queue
125-
static uint8_t scanCommand[] = { SCAN_FUNCTION_FIRST, 0x00, SCAN_DATA_ADDRESS, 0x00, 0x01 };
125+
static byte scanCommand[] = { SCAN_FUNCTION_FIRST, 0x00, SCAN_DATA_ADDRESS, 0x00, 0x01 };
126126
if (scanCounter != 0 && queueHeaders.available() > 1 && queueData.available() > sizeof(scanCommand) + 1 && scanReqInQueue == false) {
127127
scanReqInQueue = true;
128128
// Store scan request in request queue
@@ -135,7 +135,7 @@ void scanRequest() {
135135
0, // atts
136136
});
137137
queueData.push(scanCounter); // address of the scanned slave
138-
for (uint8_t i = 0; i < sizeof(scanCommand); i++) {
138+
for (byte i = 0; i < sizeof(scanCommand); i++) {
139139
queueData.push(scanCommand[i]);
140140
}
141141
if (scanCommand[0] == SCAN_FUNCTION_FIRST) {
@@ -148,9 +148,9 @@ void scanRequest() {
148148
}
149149
}
150150

151-
uint8_t checkRequest(uint8_t inBuffer[], uint16_t msgLength, const uint32_t remoteIP, const uint16_t remotePort, uint8_t requestType) {
152-
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
151+
byte checkRequest(byte inBuffer[], uint16_t msgLength, const uint32_t remoteIP, const uint16_t remotePort, byte requestType) {
152+
byte 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
154154
if (checkCRC(inBuffer, msgLength) == false) {
155155
errorCount[ERROR_TCP]++;
156156
return 0; // drop request and do not return any error code
@@ -194,7 +194,7 @@ uint8_t checkRequest(uint8_t inBuffer[], uint16_t msgLength, const uint32_t remo
194194
byte(requestType), // requestType
195195
0, // atts
196196
});
197-
for (uint8_t i = 0; i < msgLength; i++) {
197+
for (byte i = 0; i < msgLength; i++) {
198198
queueData.push(inBuffer[i + addressPos]);
199199
}
200200
if (queueData.size() > queueDataSize) queueDataSize = queueData.size();
@@ -208,7 +208,7 @@ void deleteRequest() // delete request from queue
208208
if (myHeader.requestType & SCAN_REQUEST) scanReqInQueue = false;
209209
if (myHeader.requestType & TCP_REQUEST) socketInQueue[myHeader.requestType & TCP_REQUEST_MASK]--;
210210
if (myHeader.requestType & PRIORITY_REQUEST) priorityReqInQueue--;
211-
for (uint8_t i = 0; i < myHeader.msgLen; i++) {
211+
for (byte i = 0; i < myHeader.msgLen; i++) {
212212
queueData.shift();
213213
}
214214
queueHeaders.shift();
@@ -224,17 +224,17 @@ void clearQueue() {
224224
sendMicroTimer.sleep(0);
225225
}
226226

227-
bool getSlaveStatus(const uint8_t slave, const uint8_t status) {
227+
bool getSlaveStatus(const byte slave, const byte status) {
228228
if (slave >= MAX_SLAVES) return false; // error
229229
return (slaveStatus[status][slave / 8] & masks[slave & 7]) > 0;
230230
}
231231

232-
void setSlaveStatus(const uint8_t slave, uint8_t status, const bool value, const bool isScan) {
232+
void setSlaveStatus(const byte slave, byte status, const bool value, const bool isScan) {
233233
if (slave >= MAX_SLAVES || status > SLAVE_ERROR_0B_QUEUE) return; // error
234234
if (value == 0) {
235235
slaveStatus[status][slave / 8] &= ~masks[slave & 7];
236236
} else {
237-
for (uint8_t i = 0; i <= SLAVE_ERROR_0B_QUEUE; i++) {
237+
for (byte i = 0; i <= SLAVE_ERROR_0B_QUEUE; i++) {
238238
slaveStatus[i][slave / 8] &= ~masks[slave & 7]; // set all other flags to false, SLAVE_ERROR_0B_QUEUE is the last slave status
239239
}
240240
slaveStatus[status][slave / 8] |= masks[slave & 7];

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void sendSerial() {
3030
case 0: // IDLE: Optimize queue (prioritize requests from responding slaves) and trigger sending via serial
3131
while (priorityReqInQueue && (queueHeaders.first().requestType & PRIORITY_REQUEST) == false) {
3232
// move requests to non responding slaves to the tail of the queue
33-
for (uint8_t i = 0; i < queueHeaders.first().msgLen; i++) {
33+
for (byte i = 0; i < queueHeaders.first().msgLen; i++) {
3434
queueData.push(queueData.shift());
3535
}
3636
queueHeaders.push(queueHeaders.shift());
@@ -93,17 +93,17 @@ void sendSerial() {
9393
} else if (myHeader.atts >= localConfig.serialAttempts) {
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);
96-
uint8_t MBAP[] = { myHeader.tid[0],
97-
myHeader.tid[1],
98-
0x00,
99-
0x00,
100-
0x00,
101-
0x03 };
102-
uint8_t PDU[5] = { queueData[0],
103-
byte(queueData[1] + 0x80),
104-
0x0B };
96+
byte MBAP[] = { myHeader.tid[0],
97+
myHeader.tid[1],
98+
0x00,
99+
0x00,
100+
0x00,
101+
0x03 };
102+
byte PDU[5] = { queueData[0],
103+
byte(queueData[1] + 0x80),
104+
0x0B };
105105
crc = 0xFFFF;
106-
for (uint8_t i = 0; i < 3; i++) {
106+
for (byte i = 0; i < 3; i++) {
107107
calculateCRC(PDU[i]);
108108
}
109109
PDU[3] = lowByte(crc); // send CRC, low byte first
@@ -124,9 +124,9 @@ void sendSerial() {
124124

125125
void recvSerial() {
126126
static uint16_t rxNdx = 0;
127-
static uint8_t serialIn[MODBUS_SIZE];
127+
static byte serialIn[MODBUS_SIZE];
128128
while (mySerial.available() > 0) {
129-
uint8_t b = mySerial.read();
129+
byte b = mySerial.read();
130130
if (rxNdx < MODBUS_SIZE) {
131131
serialIn[rxNdx] = b;
132132
rxNdx++;
@@ -144,7 +144,7 @@ void recvSerial() {
144144
} else {
145145
setSlaveStatus(serialIn[0], SLAVE_OK, true, myHeader.requestType & SCAN_REQUEST);
146146
}
147-
uint8_t MBAP[] = {
147+
byte MBAP[] = {
148148
myHeader.tid[0],
149149
myHeader.tid[1],
150150
0x00,
@@ -164,7 +164,7 @@ void recvSerial() {
164164
}
165165
}
166166

167-
void sendResponse(const uint8_t MBAP[], const uint8_t PDU[], const uint16_t pduLength) {
167+
void sendResponse(const byte MBAP[], const byte PDU[], const uint16_t pduLength) {
168168
header myHeader = queueHeaders.first();
169169
responseLen = 0;
170170
while (responseLen < pduLength) { // include CRC
@@ -186,7 +186,7 @@ void sendResponse(const uint8_t MBAP[], const uint8_t PDU[], const uint16_t pduL
186186
if (!localConfig.enableRtuOverTcp) ethCount[DATA_TX] += 4;
187187
#endif /* ENABLE_EXTRA_DIAG */
188188
} else if (myHeader.requestType & TCP_REQUEST) {
189-
uint8_t sock = myHeader.requestType & TCP_REQUEST_MASK;
189+
byte sock = myHeader.requestType & TCP_REQUEST_MASK;
190190
EthernetClient client = EthernetClient(sock);
191191
if (W5100.readSnSR(sock) == SnSR::ESTABLISHED && W5100.readSnDPORT(sock) == myHeader.remPort) { // Check remote port should be enough or check also rem IP?
192192
if (localConfig.enableRtuOverTcp) client.write(PDU, pduLength);
@@ -203,9 +203,9 @@ void sendResponse(const uint8_t MBAP[], const uint8_t PDU[], const uint16_t pduL
203203
deleteRequest();
204204
}
205205

206-
bool checkCRC(uint8_t buf[], int16_t len) {
206+
bool checkCRC(byte buf[], int16_t len) {
207207
crc = 0xFFFF;
208-
for (uint8_t i = 0; i < len - 2; i++) {
208+
for (byte i = 0; i < len - 2; i++) {
209209
calculateCRC(buf[i]);
210210
}
211211
if (highByte(crc) == buf[len - 1] && lowByte(crc) == buf[len - 2]) {
@@ -215,11 +215,11 @@ bool checkCRC(uint8_t buf[], int16_t len) {
215215
}
216216
}
217217

218-
void calculateCRC(uint8_t b) {
219-
crc ^= (uint16_t)b; // XOR byte into least sig. byte of crc
220-
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
218+
void calculateCRC(byte b) {
219+
crc ^= (uint16_t)b; // XOR byte into least sig. byte of crc
220+
for (byte 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
223223
crc ^= 0xA001;
224224
} else // Else LSB is not set
225225
crc >>= 1; // Just shift right

0 commit comments

Comments
 (0)