Hello, everyone
I am trying to read data from 4 slaves with M5Core via Modbus RTU. Unfortunately, only communication with 2 slaves works. As soon as a third slave comes along, the connection breaks down with errorcode "E2" (Timeout).
Here the code:
#include <M5Stack.h>
#include <ModbusMaster.h>
ModbusMaster node1,node2,node3,node4;
uint8_t result1,result2,result3,result4;
TFT_eSprite img = TFT_eSprite(&M5.Lcd);
int i;
void setup() {
M5.begin(true, false, true, false); // Init M5Core.
M5.Power.begin(); // Init Power module.
img.setColorDepth(8); // Set color depth.
img.setTextSize(2);
img.createSprite(320, 240); //Create a 320x240 canvas.
Serial.begin(115200);
Serial1.begin(9600,SERIAL_8N1,16,17);
node1.begin(1, Serial1);
node2.begin(2 , Serial1);
node3.begin(3, Serial1);
node4.begin(4, Serial1);
}
void loop() {
Check1();
delay(1000);
Check2();
delay(1000);
Check3();
delay(1000);
Check4();
delay(1000);
}
void Check1(){
i=1;
// Read 21 register starting at 0
while ((i>0)&(i<5)) {
result1 = node1.readInputRegisters(0,21);
Serial.println(result1, HEX);
if (result1 == node1.ku8MBSuccess) {
Serial.print("Id1 ");
Serial.print(node1.getResponseBuffer(0)/100);
Serial.print(node1.getResponseBuffer(1)/100);
Serial.println(node1.getResponseBuffer(2)/100);
i=0;
}
else {
Serial.print("Error ID1\n");
delay(250);
i++;
}
}
}
void Check2(){
i=1;
// Read 21 register starting at 0
while ((i>0)&(i<5)) {
result2 = node2.readInputRegisters(0,21);
Serial.println(result2, HEX);
if (result2 == node2.ku8MBSuccess) {
Serial.print("Id2 ");
Serial.print(node2.getResponseBuffer(0)/100);
Serial.print(node2.getResponseBuffer(1)/100);
Serial.println(node2.getResponseBuffer(2)/100);
i=0;
}
else {
Serial.print("Error ID2\n");
delay(250);
i++;
}
}
}
void Check3(){
// Read 21 register starting at 0
result3 = node3.readInputRegisters(0,21);
Serial.println(result3, HEX);
if (result3 == node3.ku8MBSuccess) {
Serial.print("Id3 ");
Serial.print(node3.getResponseBuffer(0)/100);
Serial.print(node3.getResponseBuffer(1)/100);
Serial.println(node3.getResponseBuffer(2)/100);
}
else {
Serial.print("Error ID3\n");
}
}
void Check4(){
// Read 21 register starting at 0
result4 = node4.readInputRegisters(0,21);
Serial.println(result4, HEX);
if (result4 == node4.ku8MBSuccess) {
Serial.print("Id4 ");
Serial.print(node4.getResponseBuffer(0)/100);
Serial.print(node4.getResponseBuffer(1)/100);
Serial.println(node4.getResponseBuffer(2)/100);
}
else {
Serial.print("Error ID4\n");
}
}