Hello. I need some advice on how to get Modbus working between 2 or more devices.
I have 2 ESP32. One is programmed as master and the other one as slave. All I want to do is to send some message from master to slave. For now, all I am trying to do is to write number "5" to Holding register and then receive that number "5" on my slave device.
My rs485 schematic:
RS48_RXD is connected to GPIO16
RS485_TXD is connected to GPIO17
RS485_DIR is connected to GPIO15
For modbus, I use this library:
https://github.com/emelianov/modbus-esp8266
I have implemented slave, master sketches for my devices and changed them slightly accordingly to my needs.
Slave code:
As you can see, I have defined REGN as 1 and SLAVE_ID as 1
I then call function to addHreg:
I also write direction to LOW since thats what I read. In order to read RS485 data RE,DE needs to be pulled low. Am I right?
My master code:
So my master as you can see above, is writing to the Holding register with the address offset 1 (same as I created in my slave):
QUESTIONS:
1. In the slave sketch, after initialzing holding register, Hreg function is used. Can someone help me understand the purpose of that?:
2. What is the best way to control DE/RE pins of RS485 IC? Is it okay to pull the pins HIGH on master device and pins LOW on slave devices since at for the moment master will be only sending data and slaves only receiving.
Later I will need to be able to write/read back data on my master device so I am not entirely sure how I am going to control DE and RE pins since I am not going to know when I need to receive? Would wiring DE/RE pins to USART2 RTS pin is better?
3. I can seem to understand how this library works even though I have gone through the source code and docs a few times. I dont understand what is callback function required for?. All I want to do is just send a number from master device to slave device I do not need any callbacks or etc.
If anyone have used this library and could help me understand how to implement the most simple MODBUS transmit and receive I would be very happy! Thanks in advance.
I have 2 ESP32. One is programmed as master and the other one as slave. All I want to do is to send some message from master to slave. For now, all I am trying to do is to write number "5" to Holding register and then receive that number "5" on my slave device.
My rs485 schematic:
RS48_RXD is connected to GPIO16
RS485_TXD is connected to GPIO17
RS485_DIR is connected to GPIO15
For modbus, I use this library:
https://github.com/emelianov/modbus-esp8266
I have implemented slave, master sketches for my devices and changed them slightly accordingly to my needs.
Slave code:
Code:
/*
ModbusRTU ESP8266/ESP32
Simple slave example
(c)2019 Alexander Emelianov (a.m.emelianov@gmail.com)
https://github.com/emelianov/modbus-esp8266
modified 13 May 2020
by brainelectronics
This code is licensed under the BSD New License. See LICENSE.txt for more info.
*/
#include <ModbusRTU.h>
#define RXD2 16
#define TXD2 17
#define DIR 15
#define REGN 1
#define SLAVE_ID 1
ModbusRTU mb;
void setup() {
Serial.begin(9600, SERIAL_8N1);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
mb.begin(&Serial2);
mb.setBaudrate(9600);
mb.slave(SLAVE_ID);//Initializng modbus slave device with ID 1
mb.addHreg(REGN); // add the register with address 1
mb.Hreg(REGN, 100); // DONT UNDERSTAND WHAT THIS FUNCTION FOR?????
pinMode(DIR, OUTPUT); // sets the digital pin 13 as output
digitalWrite(13, LOW); // sets the digital pin 13 on
}
uint16_t storing_data = 0; // place holder to store data
void loop() {
mb.readHreg(1, 1, &storing_data, 1, cbWrite);
mb.task();
yield();
}
I then call function to addHreg:
Code:
mb.addHreg(REGN); // add the register with address 1
mb.Hreg(REGN, 100); // DONT UNDERSTAND WHAT THIS FUNCTION FOR?????
My master code:
Code:
/*
ModbusRTU ESP8266/ESP32
Read multiple coils from slave device example
(c)2019 Alexander Emelianov (a.m.emelianov@gmail.com)
https://github.com/emelianov/modbus-esp8266
modified 13 May 2020
by brainelectronics
This code is licensed under the BSD New License. See LICENSE.txt for more info.
*/
#include <ModbusRTU.h>
#define RXD2 16
#define TXD2 17
#define DIR 15
ModbusRTU mb;
bool cbWrite(Modbus::ResultCode event, uint16_t transactionId, void* data) {
Serial.printf_P("Request result: 0x%02X, Mem: %d\n", event, ESP.getFreeHeap());
return true;
}
void setup() {
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
mb.begin(&Serial2);
mb.master();
pinMode(DIR, OUTPUT); // sets the digital pin 13 as output
digitalWrite(13, HIGH); // sets the digital pin 13 on
}
uint16_t send_data = 5;
void loop() {
if (!mb.slave()) {
mb.writeHreg(1, 1, &send_data, 1, cbWrite);
}
mb.task();
yield();
delay(5000);
}
Code:
mb.writeHreg(1, 1, &send_data, 1, cbWrite);
QUESTIONS:
1. In the slave sketch, after initialzing holding register, Hreg function is used. Can someone help me understand the purpose of that?:
Code:
mb.Hreg(REGN, 100); // DONT UNDERSTAND WHAT THIS FUNCTION FOR?????
2. What is the best way to control DE/RE pins of RS485 IC? Is it okay to pull the pins HIGH on master device and pins LOW on slave devices since at for the moment master will be only sending data and slaves only receiving.
Later I will need to be able to write/read back data on my master device so I am not entirely sure how I am going to control DE and RE pins since I am not going to know when I need to receive? Would wiring DE/RE pins to USART2 RTS pin is better?
3. I can seem to understand how this library works even though I have gone through the source code and docs a few times. I dont understand what is callback function required for?. All I want to do is just send a number from master device to slave device I do not need any callbacks or etc.
If anyone have used this library and could help me understand how to implement the most simple MODBUS transmit and receive I would be very happy! Thanks in advance.