Error in Modbus RTU communication using Arduino Uno

I am trying to read a energy meter data using Arduino uno. The energy meter communicates through MODBUS. I have connected the Meter with Arduino through RS-485 to TTL but I am unable to get the data. The aim is to read the register data from ENergy meter and print on serial monitor. Please help me finding whats's wrong with the code and how can I proceed.

Datasheet of Energy Meter

 #include<ModbusMaster.h>
 #include <SoftwareSerial.h>
 #define MAX485_DE 3
 #define MAX485_RE 2
 int RX = 10;
 int TX = 11;
 SoftwareSerial s(RX,TX);
 
  ModbusMaster node;

  void preTransmission() {
      
      digitalWrite(MAX485_DE,HIGH);
      digitalWrite(MAX485_RE,HIGH);
  }

  void postTransmission()
  {
      
      digitalWrite(MAX485_DE, LOW);
      digitalWrite(MAX485_RE, LOW);
  }

  void  setup() {
      
      pinMode(MAX485_DE, OUTPUT);
      pinMode(MAX485_RE, OUTPUT);
 
      digitalWrite(MAX485_RE, LOW);  
      digitalWrite(MAX485_DE, LOW);  
  
      Serial.begin(9600);
      s.begin(9600);   
      node.begin(1, s);   

      Serial.println("Starting Modbus Transaction:");  
      node.preTransmission(preTransmission);  
      node.postTransmission(postTransmission);  
  }
  

void loop() {
uint16_t result;

int data[26];


result = node.readInputRegisters(30001,1);

 




if (result == node.ku8MBSuccess) {
  
  Serial.print("Total Active Energy: ");
  Serial.println(node.getResponseBuffer(0));
  delay(2000);
     
  }

else {
  Serial.print("Failed, Response Code: ");
  Serial.print(result, DEC);
  Serial.println(" ");
  delay(2000); 
}
delay(1000);
}

By runnig this code I'm getting
image

Sometimes I get 226 also in different registers.

This is my wiring diagram

Hi,
it would make our attempt to help a lot easier if you let us know which meter model, datasheet, link or image, which RS485 module you are using and which link you used to download the "ModbusMaster.h" library.
(I recommend that when using libraries that are not the IDE's default ones, comment out its origin link in the include line of the library).

The one I downloaded,
GitHub - syvic/ModbusMaster: Arduino class library for communicating with Modbus slaves over RS232/485 ,
is giving compilation error with your code.

1 Like

His datasheet link is in the first post.

1 Like

Thanks for your reply @ruilviana
I have used the ModbusMaster library from here

I am using MAX485 chip based module

Print the error code in hex and check what the library tells you. 0xE0 (224) is "id not matching". Imho your register 30001 is far off. Try numbering start with 0 instead.

@noiasca I don't know why it's showing these error code. I checked on modbus software using the same slave ID but there it's showing data

have you tried to read

result = node.readInputRegisters(0,1);

as a first step? What are the hex error codes then?

Yeah but same issue

Seems overly simple, but are you certain that the Rx and Tx pins of your Arduino module are/are-not swapped? My RS-485 transceiver board has LEDs for both Tx and Rx pins which makes basic wiring troubleshooting a bit easier (at least you know that you are getting Tx and Rx "bit" transmissions both TO and FROM your microcontroller to the RS-485 transceiver...

Also, there are a few MAX485 serial transceiver variants, so a specific board/model; number would be required.

I am using one from eBay/Amazon that does automatic ""transmission driver activation" and does not need any of the "pre" and "post" transmission bit toggling to both send and receive half-duplex RS-485 serial messages. It works fine for me communicating to a PZEM-014.

These modules have only 4-pins on the microcontroller serial side:
+5V, GND, Tx, Rx (note no DE or RE

Also, if you are stuck on using "manual" transceiver RE/DE for asserting the RS-485 transmitter, you can just as easily wire DE/RE to the same pin and save a digital output as DE is active HIGH and RE is active LOW.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.