Help needed for arduino rs485 communicate

Hello everyone,

I am trying to get data from my battery unit. I follow as :

http://downloads.arduino.cc/libraries/github.com/4-20ma/ModbusMaster-2.0.1.zip

Here is my code:

void setup()
{
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  // Init in receive mode
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  // Modbus communication runs at 9600 baud, default config 8bit data, parity None, 1 bit stop
  Serial.begin(9600, SERIAL_8N1);

  // Modbus slave ID 1
  node.begin(1, Serial);
  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

void loop()
{
  uint8_t result;
  uint16_t data[6];
  
  result = node.readHoldingRegisters(0x3, 2);
  if (result == node.ku8MBSuccess)
  {
    Serial.print("Phase A voltage: ");
    Serial.println(node.getResponseBuffer(0x00)/100.0f);
  }
  delay(1000);
}

But i always get result of 226 and some unreadable text. Can you pls take a look and give me some advices?

Thank a lot.

modbus diagram rs485.pdf (196 KB)

Modbus Communication protocol.pdf (449 KB)

If some module is connected to serial pins, you can't use Serial for debug printing

Hi

readHoldingRegisters = code function0x03

result = node.readHoldingRegisters(0x3, 2) is going to read modbus register 0x03 lenght 2

In your case you are reading Battery Voltage and should return 4 word

Is 226V is correct ??

Juraj:
If some module is connected to serial pins, you can't use Serial for debug printing

Thanks, I will try to use other Serial for debug printing.

wilga:
Hi

readHoldingRegisters = code function0x03

result = node.readHoldingRegisters(0x3, 2) is going to read modbus register 0x03 lenght 2

In your case you are reading Battery Voltage and should return 4 word

Is 226V is correct ??

hi Wilga, it isn't correct, the voltage should be in range of 47-52 (VDC).

I use Modbus TCP. Modbus works with 2 byte registers. if the value is in two registers you calculate it like reg1 * 65535L + reg2

Hi;
Have you got 120R terminating resistor fitted?

https://arduino-info.wikispaces.com/RS485-Modules

https://arduino-info.wikispaces.com/SoftwareSerialRS485Example

Have you googled arduino RS485

Tom... :slight_smile:

Normally with an RS485 driver you common RE and #DE so you only need to drive a signal line
to switch direction. The only time you want to disable both read and write is for low power mode
if battery operated.

You’ll noticed the RE and #DE pins are neighbours on the typical driver chips to make layout
easy for this commoning.

Thanks you all for your times. I had followed as the video DIY RS485 wifi ESP 8266 Adaptor Part 1 - Flashing and software - YouTube and it worked (I used NodeMCU 1.0 and XY-017), then i can get information via virtual COM. Thus i think the circuit i have is fine. But when i change to use Modbus serial, i get an error again.

Any idea, please help!.