Hello guys,
I bought this module relay which uses RS485 modbus.
Initially, I purchased a converter USB-RS485 and I had the manufacturer send me the modbus poll program and found no anomalies, that is: I was able to control my relay module.
So, I can say that my relay module is perfectly functional.
Unfortunately though when I run this code:
/*
This sketch demonstrates controlling a 16 coil relay board that receives commands using
Modbus RTU connected via RS485
Turns on each relay in turn.
Turns off each relay
Toggles relays on and off at random
*/
//#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
byte gSlaveID = 0x01;
void setup() {
Serial.begin(9600);
//while (!Serial);
Serial.println("Modbus RTU demo controlling 16 relay board");
// start the Modbus RTU client
if (!ModbusRTUClient.begin(9600)) {
Serial.println("Failed to start Modbus RTU Client!");
//while (1);
}
}
void loop() {
Serial.println("Turning on all relays");
for(int i =1;i<=16;i++){
if (!ModbusRTUClient.holdingRegisterWrite(gSlaveID, 0x00, 0xFF00)) {// 0x0100 relay on
Serial.print("Failed to turn on coil ");
Serial.println(i);
Serial.println(ModbusRTUClient.lastError());
}else{
Serial.print("Turned on coil ");
Serial.println(i);
}
delay(500);
}
delay(1000);// wait for 1 second
Serial.println("Turning all relays off");
for(int i =1;i<=16;i++){
if (!ModbusRTUClient.holdingRegisterWrite(gSlaveID, i, 0x0200)) {// 0x0100 relay off
Serial.print("Failed to turn off coil ");
Serial.println(i);
Serial.println(ModbusRTUClient.lastError());
}else{
Serial.print("Turned off coil ");
Serial.println(i);
}
delay(500);
}
delay(1000);// wait for 1 second
Serial.println("Toggling relays on or off randomly");
for(int i =1;i<=16;i++){
int randRelay = random(1, 17);
if (!ModbusRTUClient.holdingRegisterWrite(gSlaveID, randRelay, 0x0300)) {// 0x0300 toggle relay
Serial.print("Failed to toggle coil ");
Serial.println(randRelay);
Serial.println(ModbusRTUClient.lastError());
}else{
Serial.print("Toggled coil ");
Serial.println(randRelay);
}
delay(500);
}
delay(1000);
}
in serial it always prints the same problem:
"Connection timed out"
I got the code from this article.
I tried to reverse the wires that appear in the relay module A+ e B- e nell'opta A- e B+ but nothing to do..I think it's a software communication problem, also because with the USB-RS485 converter I didn't use a 120 ohm resistor.
I've been trying to solve this for days, but it's buggy from all sides... I can't figure it out anymore and I need to solve it for a personal project of mine... it's really sad that I spent hundreds of euros and I'm stuck here!
I really hope you can help me... please!