Hi!
I am trying to comunicate an VFD with an ESP32 via Modbus but it did not work.
Conecting the VFD with an "Modbus-TTL converter" it is working perfectly. Also I made a code using arduino and also it is working well, but when I try to use the same code to ESP32, it does not work.
I tried a lot of examples and libraries without any results.
I attach the code that work in Arduino:
#include <SimpleModbusMaster.h>
#define baud 9600
#define timeout 1000
#define polling 200
#define retry_count 10
#define TxEnablePin 15
enum
{
PACKET1,
TOTAL_NO_OF_PACKETS
};
Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet1 = &packets[PACKET1];
unsigned int volt[2];
long time_past=0;
void setup()
{
pinMode(7,INPUT);
Serial.begin(9600);
Serial2.begin(9600, SERIAL_8N1, 27, 26);
modbus_construct(packet1, 2, READ_HOLDING_REGISTERS, 3846, 1, volt);
modbus_configure(&Serial2, baud, SERIAL_8N1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);
time_past = millis();
}
void loop()
{
modbus_update();
if((millis()-time_past) >= 500)
{
Serial.print("Voltage: "); Serial.print(volt[0] / 10.0f); Serial.println(" V");
Serial.println();
time_past = millis();
}
}
I have also tried another library (ModbusRTU.h) and with Arduino I can read some data but again, when I upload the same code to the ESP32 nothing work.
The code is this one:
#include <ModbusRTU.h>
#include <SoftwareSerial.h>
float InttoFloat(uint16_t Data0,uint16_t Data1) {
float x;
unsigned long *p;
p = (unsigned long*)&x;
*p = (unsigned long)Data0 << 16 | Data1; //Big-endian
return(x);
}
SoftwareSerial S(27, 26);
ModbusRTU mb;
bool cb(Modbus::ResultCode event, uint16_t transactionId, void* data) { // Callback to monitor errors
if (event != Modbus::EX_SUCCESS) {
Serial.print("Request result: 0x");
Serial.println(event, HEX);
}
return true;
}
void setup() {
Serial.begin(9600);
S.begin(9600);
mb.begin(&S,15); // RE/DE connected to D0 of ESP8266
mb.master();
}
uint16_t val[2];
void loop() {
if (!mb.slave()) {
mb.readHreg(2, 2057, val, 2, cb); // Slave id is 1 and register address is 2057 and
//we are reading 2 bytes from the register and saving in val
while(mb.slave()) { // Check if transaction is active
mb.task();
delay(100);
}
Serial.println("Register Values ");
Serial.println(val[0]);
Serial.println(val[1]);
float voltage= InttoFloat(val[1],val[0]);
Serial.print("Voltage= ");
Serial.print(voltage);
Serial.println("V");
}
delay(1000);
}
The wiring is correct on ESP32 because I checked if the number of pin that I write in code are the same on board.
Please if someone know what is happen I will be grateful for the help. I was trying different codes for one month but no one worked.
Thanks in advance.