Hi,
I'm trying to connect a modbus-device to my ESP32 10 io relay module, but cannot get it to work.
I have connected an EM340 energy meeter, and have verified the modbus communication with one of my other arduino PLCS (controllino)
I think I have tried everyting, including
* Used external power
* Swapped A/B lines
The behaviour is that I simply get no answer. My code attached below. What have I missed? There arent any jumpers that needs to be configured for rs485 right? Any suggestions?
Output from skecth below is:sending
sending
sending
sending
sending
sending
sending
sending
etc....
#include <ModbusRTUMaster.h>
#include <RS485.h>
ModbusRTUMaster master(RS485);
uint32_t lastSentTime = 0UL;
const uint32_t baudrate = 9600UL;
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600UL);
// Start the serial port
RS485.begin(baudrate, HALFDUPLEX, SERIAL_8N1);
RS485.setTimeout(1000);
master.begin(baudrate);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
// Check available responses often
if (master.isWaitingResponse()) {
ModbusResponse response = master.available();
if (response) {
if (response.hasError()) {
Serial.print("Error ");
Serial.println(response.getErrorCode());
} else {
Serial.print("Holding Register values: ");
for (int i = 0; i < 1; ++i) {
Serial.print(response.getRegister(i));
Serial.print(',');
Serial.println();
}
}
}
}
else {
// Send a request every 1000ms
if (millis() - lastSentTime > 1000) {
Serial.println("sending");
if (!master.readHoldingRegisters(1, 0, 1)) {
Serial.println("Could not send");
}
lastSentTime = millis();
}
}
}