I'm trying to connect a modbus meter to a nodeMcu LoLin v3
I'm using a MAX3485 module to transfer the TTL to a RS485 connection. The modbus server has unitId 1, baud rate 9600 and no Parity
This is the scheme. A e B from the module go to the modbus server device
And this is my simple script
#define DIRECTION D3
#include <ModbusRTU.h>
#include <SoftwareSerial.h>
SoftwareSerial S(D2, D1);
ModbusRTU mb;
bool cb(Modbus::ResultCode event, uint16_t transactionId, void* data) { // Callback to monitor errors
if (event != Modbus::EX_SUCCESS) {
Serial.print("Error result: 0x");
Serial.println(event, HEX);
} else {
Serial.print("No Error");
}
return true;
}
void setup() {
Serial.begin(115200);
pinMode(DIRECTION, OUTPUT);
digitalWrite(DIRECTION, 0);
S.begin(9600, SWSERIAL_8N1);
mb.begin(&S);
mb.client();
}
void loop() {
uint16_t res[2];
if (!mb.slave()) { // Check if no transaction in progress
mb.readHreg(1, 5, res, 1, cb); // Send Read Hreg from Modbus Server
while (mb.slave()) { // Check if transaction is active
mb.task();
delay(10);
}
Serial.println("Res");
Serial.println(res[0]);
Serial.println(res[1]);
}
delay(1000);
}
I created a Software Serial on D1 and D2 and i'm trying to read from it but no matter what i do, i keep getting error 4 from the response and 2 values in my reg array i do not where they come from...
Error result: 0xE4
Res
59044
16382
Am I missing something? The modbus server works because i connected it to another client and everything is ok