🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Core2 with W5500 and RS485 (ModbusMaster) with more then 2 slaves

    Scheduled Pinned Locked Moved General
    1 Posts 1 Posters 1.9k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      Highlandy
      last edited by

      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");
      }
      }

      1 Reply Last reply Reply Quote 0

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      • First post
        Last post