I have set up modbus communication between my solar inverter SOFAR HYD 3000-ES and an ESP32 board
The ESP32 Serial1 (modbus master node) is connected to a RS485 board RX/TX
The D+/D- of RS485 board is connected to inverter (modbus slave node connector 485s) (30m length cable)
The following code is working, I removed the screen display and ethernet communication
The library is https://docs.arduino.cc/libraries/modbusmaster/
#include <ModbusMaster.h>
// Parametres RS485
const int RS485_RX = 5; // Serial1 RX WT32 ETH01 => connect to RS485 board
const int RS485_TX = 17; // Serial1 TX WT32 ETH01 => connect to RS485 board
const int BAUDRATE = 9600;
const byte UNIT_ID = 1;
const int REG_start = 0x020C; // Hexa Adress to start register read (readHoldingRegisters)
// SOFAR Solar HYD 3000-ES Software Master Version:G410 DSP Version:V410 Vice DSP Version:V410
ModbusMaster node; // init ModbusMaster
const char* noms[] = { "PV", "Grid", "Load", "Inv", "Batt","SOC", "Purch", "Conso", "Export", "gen"};
uint16_t data[16] = { 4998,0,486,0,18,15,65510,26,0,0,13,0,20,0,681,683}; //default values for debug
uint8_t j, result;
int y;
void setup() {
Serial.begin(115200);
Serial1.begin(9600, SERIAL_8N1, RS485_RX, RS485_TX);
node.begin(1, Serial1); // remplace node.begin(RS485_RX, RS485_TX, BAUDRATE);
}
void loop()
{
result = node.readHoldingRegisters(REG_start, 16); // slave: read 16-bit registers starting at register REG_start to RX buffer
if (result == node.ku8MBSuccess) { // do something with data if read is successful
for (j = 0; j < 16; j++) {
data[j] = node.getResponseBuffer(j);
Serial.print(j); Serial.print(":"); Serial.println(data[j]);
}
} else {
Serial.println("Erreur de lecture");
}
/* with the units conversions :
data[4] //SOC / Unit: 1%
data[14]*10 //Today import / Unit: 0.01KWH
data[15]*10
data[13]*10
data[12]*10
if (data[9]<32768) { 10*data[9] } // PV power positif
else { -10*(65535-data[9]) } // PV power negatif
if (data[6]<32768) { 10*data[6] } // Grid power positif
else { -10*(65535-data[6]) } // Grid power negatif
if (data[7]<32768) { 10*data[7] } // Load power positif
else { -10*(65535-data[7]) } // Load power negatif
if (data[8]<32768) { 10*data[8] } // Inv power positif
else { -10*(65535-data[8]) } // Inv power negatif
if (data[1]<32768) { 10*data[1] } // BATT power positif
else { -10*(65535-data[1]) } // BATT power negatif
*/
delay(1000);
}