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