Use Modbus with Arduino Zero ( Using Arduino MKR 485 Shield)

Hi, i'm trying to use a Modbus device with an arduino zero. I'm using the ArduinoModbus library to do it. I'm trying to use the functions: holdingRegisterWrite and holdingRegisterRead, and they both give me error when using (Connection timed out). The code is:

#include <ArduinoModbus.h>
#include <ArduinoRS485.h> 

const int BUTTON_PIN = 7;

int lastState = HIGH; 
int currentState;
int buttonState = digitalRead(BUTTON_PIN);

void setup() {
  
  Serial.begin(9600, SERIAL_8E1);
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  while(!Serial){

    if (!ModbusRTUClient.begin(9600, SERIAL_8E1)) {
      Serial.println("Failed to start Modbus RTU Client!");
      while (1);
    }
    
  }

}

void loop() {

  currentState = digitalRead(BUTTON_PIN);

  if(lastState == LOW && currentState == HIGH){

      ModbusRTUClient.holdingRegisterRead(0x03, 0x00);
      Serial.println(ModbusRTUClient.lastError());
      
      ModbusRTUClient.holdingRegisterWrite(0x00, 0x30 , 0x1234);
      Serial.println(ModbusRTUClient.lastError());
      
      ModbusRTUClient.holdingRegisterWrite(0x25, 0x00, 1);
      Serial.println(ModbusRTUClient.lastError());
      
      ModbusRTUClient.coilWrite(0x21, 3, 1);
      Serial.println(ModbusRTUClient.lastError());
      
  }

  lastState = currentState;
  delay(100);
}

Thank you!

I'm not that familiar with the library you are using, but are you sure the above is correct? A quick look at the ArduinoModbus library says:

long ModbusClient::holdingRegisterRead(int id, int address)

If i've got the right function, you're trying to talk to a device with address 0.

Hi, thx for the reply, the thing is i'm learning this. I'm using the LumiPlus Modbus and in the PDF says the adress it's 0x03 and the initial value it's 0, those are registers, and i want to read them. i'm not really familiar with adresses or initial values or id's so I don't really know if that's right :confused:

Can you post a link to a PDF manual of the modbus device you are trying to communicate with and let us know what registers you are trying to read.

EDIT: Is this the manual: https://gestor-doc-s3.s3.eu-west-1.amazonaws.com/documents/category/57434e201_02-157319.pdf

Hey thank you ! I solved the problem, i was doing the Color update wit hthe holding registers, and i needed to refresh the 3rd bit (0x08) and i was refreshing the 1rst. Thx tho !

What did you have to change in the code? What is the color update?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.