Welcome!

This community is for professionals and enthusiasts of our products and services. Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.

1

Modbus RS485 - good data back but the code is not picking it up

By
brandon wiewel
on 5/1/19, 12:26 PM 164 views

I have sniffed the data to and from the M21+ and i have valid data going out and coming back but the M21+ is not recognizing it.  I have attached what is basically the example code. Can anyone see anything wrong with the Rx portion?

I am on Z and Y and have half duplex switch set to on.  again validated i have a good modbus string leaving and coming to the M21+. 

sniffed data


SENT - 01 03 00 05 00 03 15 CA

Returned - 01 03 06 00 00 00 44 00 00 61 60



At this point this if statment is not firing:   " if (modbus.isWaitingResponse())  "


----------------------------------------------------------------------------code


#include <ModbusRTUMaster.h>


// Define the ModbusRTUMaster object, using the RS-485 or RS-232 port (depending on availability)

#include <RS485.h>

ModbusRTUMaster modbus(RS485);



uint32_t lastSentTime = 0UL;

const uint32_t baudrate = 9600;

bool Dig1State = 0;


////////////////////////////////////////////////////////////////////////////////////////////////////

void setup() {

  Serial.begin(9600UL);

  Serial.println("Modbus starting...");

  


  // Start the serial port


  RS485.begin(baudrate, HALFDUPLEX, SERIAL_8N1);



  // Start the modbus master object.

  // It is possible to define the port rate (default: 38400)

  modbus.begin(9600);


  pinMode(Q0_1, OUTPUT);

  delay(2000);

  

  Serial.println("Loop start...");

}


////////////////////////////////////////////////////////////////////////////////////////////////////

void loop() {

  // Send a request every 1000ms

  if (millis() - lastSentTime > 2000) {

    

    Dig1State = !Dig1State;

    digitalWrite(Q0_1, Dig1State);

    

    Serial.println("Modbus poll...");

    // Send a Read Coils request to the slave with address 1

    // It requests for 23 coils starting at address 10

    // IMPORTANT: all read and write functions start a Modbus transmission, but they are not

    // blocking, so you can continue the program while the Modbus functions work. To check for

    // available responses, call master.available() function often.

    modbus.readHoldingRegisters( 1 , 5, 3);



    lastSentTime = millis();

  }


  // Check available responses often

  if (modbus.isWaitingResponse()) {

    ModbusResponse response = modbus.available();

    if (response) {

      Serial.println("Modbus responce...");

      if (response.hasError()) {

        // Response failure treatment. You can use response.getErrorCode()

        Serial.print("Modbus Comm Error");

      } 

      else {

        // Get the coil value from the response

        for (int i = 0; i < 3; ++i) {

          Serial.print(i);

          Serial.print("- ");

          Serial.println(response.getRegister(i));

      }

   }

  }

 }

} //loop



0
Industrial Shields Forum user

Dani Salvans Quesada

--Dani Salvans Quesada--

1000000005
| 0 0 0
Sant Fruitós de Bages, España
--Dani Salvans Quesada--
Dani Salvans Quesada
On 8/25/20, 8:28 AM

Hello friend, 


Maybe the !modbus.isWaitingResponse() is not used in the right situation, you can check it and compare it with this example: https://www.industrialshields.com/blog/arduino-industrial-1/post/communicating-by-modbus-rtu-through-a-rs485-serial-interface-seneca-z-d-in-module-93. Or you can adapt this example and try it directly.


Thank you, 

 


Keep Informed

About This Community

This community is for professionals and enthusiasts of our products and services. Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together. Read Guidelines

Question tools

5 follower(s)

Stats

Asked: 5/1/19, 12:26 PM
Seen: 164 times
Last updated: 8/25/20, 8:28 AM