Arduino and Weintek HMI connected with modbus

Hi,

I try to connect the Arduino Uno with a Weintek HMI using Modbus. I connected the RS485 shield like on the picture (without servo and LCD)

On the HMI I configured a toggle switch and a Bit-Led on adress 0x1

The communication doesn´t work. In the serial monitor I receive following message

slave ID = 0Failed to write coil! Invalid CRC

Any idea?

/*1200000
  Modbus RTU Client Toggle

  This sketch toggles the coil of a Modbus RTU server connected via RS485
  on and off every second.

  Circuit:
   - MKR board
   - MKR 485 shield
     - ISO GND connected to GND of the Modbus RTU server
     - Y connected to A/Y of the Modbus RTU server
     - Z connected to B/Z of the Modbus RTU server
     - Jumper positions
       - FULL set to OFF
       - Z \/\/ Y set to ON

  created 16 July 2018
  by Sandeep Mistry
*/

#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println("Modbus RTU Client Toggle");

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

}

void loop() {
  if (!ModbusRTUClient.holdingRegisterWrite(0, 1, 1)) {
    Serial.print("slave ID = 0");
    Serial.print("Failed to write coil! ");
    Serial.println(ModbusRTUClient.lastError());
  }
  // for (slave) id 0: write the value of 1, to the coil at address 1
  if (!ModbusRTUClient.coilWrite(0, 1, 1)) {
    Serial.print("slave ID = 0");
    Serial.print("Failed to write coil! ");
    Serial.println(ModbusRTUClient.lastError());
  }
int coilWrite(0, 1, 1);
  // wait for 1 second
  delay(1000);

  // for (slave) id 0: write the value of 0, to the coil at address 1
  if (!ModbusRTUClient.coilWrite(0, 1, 0)) {
    Serial.print("slave ID = 0");
    Serial.print("Failed to write coil! ");
    Serial.println(ModbusRTUClient.lastError());
  }

  // wait for 1 second
  delay(1000);
}

@agentriggs
Posted in the wrong section, moved here. Please read the description you posted in a section that said Not for your project.

You shouldn't use the ArduinoModbus library on AVR Arduinos, it wastes too much RAM, more than these Arduinos have.

If you insist in using it (accepting that you won't get a reliable result), you should correctly configure the DE/RE pin.

A

RS485.setPins(1, 4, -1);

at the start of setup() should do the job.

You must not use the Serial object for debugging as it's used for the Modbus communication!

Thanks for your reply. Which library do you recomend?

That depends on your needs and your predilection. I guess ModbusMaster should match your needs. Other people like the SimpleModbusMaster more (both available in the Library Manager).