ESP32 Modbus RTU Using Arduino IDE

We have executed ESP32 with MODBUS RTU RS485 Protocol Using Arduino IDE ..

Can some body help me to go further on reading the data over Modbus RTU Hodling addresses to read data from HMI or PLC?

Help will be much appreciated!

1 Like

you linked a tutorial which says

"In this user guide for ESP32 board with Arduino IDE, we have demonstrated how we can read the data from Modbus RTU device on ESP32 "

so please explain what you have done not according to the tutorial and what are the issues you are currently dealing with.

Furthermore, please link to the datasheet of the HMI you want to read from.

Thanks for your reply, we have tried modbus rtu communication over microcontroller and read the data over serial read by creating a buffer and then extracted the data to print.
I want to read the data from HMI over modbus RTU holding addresses like 400001---400010

For HMI i am not specific as most of the HMI having spport on Modbus RTU over RS485/RS232 protocol.

Get and read following documents:

"Modbus over Serial Line" Specification and Implementation Guide
and
"Modbus Application Protocol Specification"

its very unlikely that a device with holding register 400001---400010 exists as addresses are only 2 bytes HEX and even if you read about "Registers" ... there is no such range for FC3/FC6/FC16.

Which register is readed by the example on the page you linked?

Yes, it is unlike only will have holding address from 400001---400010.. One of the HMI with me is having this range of holding addresses.

But I questions not limited to this.. I jusr wanted to ready the data from any HMI in holding regster .. it can be 40001... 40010.. etc.

Thanks!

I repeat my question:

did you try the code on the linked page?
If you understand what is described on that page and if you read the two specifications I proposed yesterday, you should easily find out how to read the registers 40001- 400010 also ...

Refer the below code..
we have read the data being transmitted serially using SoftwareSerial library in the buffer. ..
How it can be handled by holding registers?

If you suggest with some example.. that will be much appreaciated.

#include <SoftwareSerial.h>
// RS485 setup with ESp32
#define RE 32  // Connect RE terminal with 32 of ESP
#define DE 33    // Connect DE terminal with 33 of ESP            
const byte ModReadBuffer[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
byte BufferValue[8];
SoftwareSerial mod(26, 27); // RX=26 , TX =27

void setup() {
  Serial.begin(115200);
  mod.begin(9600);// modbus configuration
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);
}


void loop() {
  byte val1;
  while (mod.write(ModReadBuffer, sizeof(ModReadBuffer)) == 8) {
    val1 = ModbusData();
    delay(5000);
  }
}
byte ModbusData(){
  byte i;
  digitalWrite(DE,HIGH);
  digitalWrite(RE,HIGH);
  delay(10);
  if(mod.write(ModReadBuffer,sizeof(ModReadBuffer))==8){
    digitalWrite(DE,LOW);
    digitalWrite(RE,LOW);
    for(i=0;i<4;i++){
    //Serial.print(mod.read(),HEX);
    BufferValue[i] = mod.read();
    }
    if(BufferValue[1]!=255 && BufferValue[2]!=255 && BufferValue[2]!=255){
    Serial.println("Serial Data received On:"); 
    Serial.print("Modbus Buffer[1]="); 
    Serial.println(BufferValue[1],DEC);
    Serial.print("Modbus Buffer[2]="); 
    Serial.println(BufferValue[2],DEC);
    Serial.print("Modbus Buffer[3]="); 
    Serial.println(BufferValue[3],DEC);
     Serial.println("");}
   // }
  }
  return BufferValue[8];
}

my 3rd try to ask you a question:

None.. I read data on any holding register.. I just read the data over serial port using software serial which is used for to communicate with microcontroller over RS485 using MAX485 TTL To RS485 conveter. Hope this clarified with you.

ESP32 has it's own SoftwareSerial has esp in the lib name and probably not needed..
Should be able to use HardwareSerial on 26,27..
Check this thread for more info..
Could give you some trouble..

~q

No,, I dont find any trouble with softwareserial library while commuicating with ESP32..

Why use software serial at all? ESP32 has multiple hardware UART ports.

1 Like

That example doesn't look so good to me.. :frowning:
Inside ModbusData, they got you reading from mod Serial with out first checking if anything is yet available, not good..
Down at the bottom the link to the espsoftwareserial lib they use..
I followed it, went to the GitHub and checked the examples, the constructs look much different, so not sure about that..

~q

Thanks! I will try this and let you know.
Do you have any reference article for how to use hardware uart port?

Noted! I was trying to figure out the same .. how it can be handled like normal modbus RTU protocol ..

Maybe look into using a lib for the RTU..
quick and dirty..

while (mod.available()<4){}

sit and wait for 4 bytes, then read them..

~q

Post #3 on the link i posted #10 has a good example of using HardwareSerial..

~q

Will workout and update you on this..

Thanks for sharing lot of inputs..

thats not correct. You should re-read the tutorial you have linked.

{0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
 xxxx                                               server ID
       xxxx                                         Function Code 
	         xxxx  xxxx                             start address
                         xxxx  xxxx				    quantity of holding registers
                                     xxxx  xxxx     crc

you try to read from server id 1
you try request 1 address starting from address 30.
or in other words you read register 40031

1 Like