4-20ma/ModbusMaster

No value recieved - ModbusRTU_result = 226 - Modbus communication between ESP32 and a Frequency Inverter for a Motor

Closed this issue · 0 comments

ModbusMaster version

ModbusMaster V 2.0.1

Arduino IDE version

PlatformIO

Arduino Hardware

ESP32

Platform Details

Steps to Reproduce:

CODE
/********************************************************
// LIBARIES
*********************************************************/
//----- MAIN
#include <Arduino.h>
#include <Wire.h>
#include <math.h>
#include <SPI.h>

//----- IMPORTED
#include <Adafruit_ADS1X15.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ModbusMaster.h>
#include <PubSubClient.h>

//---------------------------

/*********************************************************
RT-485 DETAILS
*********************************************************/

#define BAUDRATE 9600 // Baud rate for RT-485
#define MotorController_ID 1 // ID of the frequency Inverter in the RTU Modbus System

//-------------------------------------------------------

//--- RS-485 communication wiring
#define RXD 25
#define TXD 22
#define FlowControl 15 //RE & DE = FlowControl are connected with the GPIO 15

//--- RTU Modbus Constants
uint8_t NumOfReq_Registers = 1;
uint8_t NumOfResp_Registers = 1;
uint16_t AddressFirstHoldingRegister;
uint16_t ResponseData[1];
uint8_t ModbusRTU_result, j;

//--- object node for class ModbusMaster
ModbusMaster node;

//--- Function for setting the state of Flow control pin of RS-485
void preTransmission()
{
digitalWrite(FlowControl, 1);
}
void postTransmission()
{
digitalWrite(FlowControl, 0);
}

//--- Motor Reading
void MotorReading()
{
//--- frequency Parameter (Hz) P716: read (1) 16-bit registers starting at Parameter P716 (register 0xB300) to RX buffer
AddressFirstHoldingRegister = 0xB300; //(4 und +1)
ModbusRTU_result = node.readHoldingRegisters(AddressFirstHoldingRegister, NumOfReq_Registers);
// do something with data if read is success
if (ModbusRTU_result == node.ku8MBSuccess)
{
for (j = 0; j < NumOfResp_Registers; j++)
{
Serial.println("Freq read success");
ResponseData[j] = node.getResponseBuffer(j);
float ResponseData_int = (ResponseData[j]) / 100.0f;
Serial.println("Freq getResponseBuffer = " + String(ResponseData_int));
}
node.clearResponseBuffer();
}
Frequency = ResponseData[0];
float Frequency_f = Frequency / 100.0f;
Serial.println("");

Serial.println("Motor Frequency = " + String(Frequency_f));

//==============================================

//--- Speed of Revolution (rpm) Parameter P717: read (1) 16-bit registers starting at Parameter P717 (register 0xB340) to RX buffer
AddressFirstHoldingRegister = 0x3264;
ModbusRTU_result = node.readHoldingRegisters(AddressFirstHoldingRegister, NumOfReq_Registers);

// do something with data if read is successful
if (ModbusRTU_result == node.ku8MBSuccess)
{
for (j = 0; j < NumOfResp_Registers; j++)
{
Serial.println("Speed read success");
ResponseData[j] = node.getResponseBuffer(j);
float ResponseData_int = (ResponseData[j]) / 100.0f;
Serial.println("Freq getResponseBuffer = " + String(ResponseData_int));
}
node.clearResponseBuffer();
}
RevolutionSpeed = ResponseData[1];
float RevolutionSpeed_f = RevolutionSpeed / 100.0f;
Serial.println("Motor RevolutionSpeed = " + String(RevolutionSpeed_f)); // rpm
delay(10);

//==============================================

//--- Current (A) P719: read (1) 16-bit registers starting at Parameter P719 (register 0xB3C0) to RX buffer
AddressFirstHoldingRegister = 0x3265;
ModbusRTU_result = node.readHoldingRegisters(AddressFirstHoldingRegister, NumOfReq_Registers);

// do something with data if read is successful
if (ModbusRTU_result == node.ku8MBSuccess)
{
for (j = 0; j < NumOfResp_Registers; j++)
{
Serial.println("Current read success");
ResponseData[j] = node.getResponseBuffer(j);
}
node.clearResponseBuffer();
}
Current = ResponseData[2];
Serial.println("Motor Current = " + String(Current)); // A

delay(10);

//==============================================

//--- Voltage (V) P722: read (1) 16-bit registers starting at Parameter P722 (register 0xB480) to RX buffer
AddressFirstHoldingRegister = 0xB480;
ModbusRTU_result = node.readHoldingRegisters(AddressFirstHoldingRegister, NumOfReq_Registers);

// do something with data if read is successful
if (ModbusRTU_result == node.ku8MBSuccess)
{
for (j = 0; j < NumOfResp_Registers; j++)
{
Serial.println("Volt read success");
ResponseData[j] = node.getResponseBuffer(j);
}
node.clearResponseBuffer();
}
Voltage = ResponseData[3];
Serial.println("Motor Voltage = " + String(Voltage)); // V

delay(10);

//==============================================

//--- Power (kW) P727 : read (1) 16-bit registers starting at Parameter P727 (register 0x‭B5C0‬‬) to RX buffer
AddressFirstHoldingRegister = 0xB5C0;
ModbusRTU_result = node.readHoldingRegisters(AddressFirstHoldingRegister, NumOfReq_Registers);

// do something with data if read is successful
if (ModbusRTU_result == node.ku8MBSuccess)
{
for (j = 0; j < NumOfResp_Registers; j++)
{
Serial.println("Power read success");
ResponseData[j] = node.getResponseBuffer(j);
}
node.clearResponseBuffer();
}
Power = ResponseData[4];
Serial.println("Motor Power = " + String(Power));

delay(10);

//--- Cos Phi P725: read (1) 16-bit registers starting at Parameter P725 (register 0x‭B540‬) to RX buffer
AddressFirstHoldingRegister = 0xB540;
ModbusRTU_result = node.readHoldingRegisters(AddressFirstHoldingRegister, NumOfReq_Registers);

// do something with data if read is successful
if (ModbusRTU_result == node.ku8MBSuccess)
{
for (j = 0; j < NumOfResp_Registers; j++)
{
Serial.println("CosPHI read success");
ResponseData[j] = node.getResponseBuffer(j);
}
node.clearResponseBuffer();
}
CosPHI = ResponseData[5];
Serial.println("Motor CosPHI = " + String(CosPHI));

delay(10);

//--- Drehmoment (% of the Maximal) P729: read (1) 16-bit registers starting at Parameter P729 (register 0x‭B640‬) to RX buffer
AddressFirstHoldingRegister = 0xB640;
ModbusRTU_result = node.readHoldingRegisters(AddressFirstHoldingRegister, NumOfReq_Registers);

// do something with data if read is successful
if (ModbusRTU_result == node.ku8MBSuccess)
{
for (j = 0; j < NumOfResp_Registers; j++)
{
Serial.println("DrehMoment read success");
ResponseData[j] = node.getResponseBuffer(j);
}
node.clearResponseBuffer();
}
Torque = ResponseData[4];
Serial.println("Motor Moment = " + String(Torque));

delay(10);

//--- Operating time (h) P714: read (1) 16-bit registers starting at Parameter P714 (register 0x‭‭B280‬‬) to RX buffer
AddressFirstHoldingRegister = 0xB280;
ModbusRTU_result = node.readHoldingRegisters(AddressFirstHoldingRegister, NumOfReq_Registers);

// do something with data if read is successful
if (ModbusRTU_result == node.ku8MBSuccess)
{
for (j = 0; j < NumOfResp_Registers; j++)
{
Serial.println("Op.time read success");
ResponseData[j] = node.getResponseBuffer(j);
}
node.clearResponseBuffer();
}
OperatingTime = ResponseData[3];
Serial.println("Operating time = " + String(OperatingTime));

delay(10);

//==============================================
}

void setup()
{
//--- define the Slave ID
pinMode(FlowControl, OUTPUT);
digitalWrite(FlowControl, 0);

//--- Baud Rate
Serial.begin(BAUDRATE);
Wire.begin(SDA, SCL);

delay(10);

//--- Initialize Slave ID as 1
node.begin(MotorController_ID, Serial);
node.preTransmission(preTransmission); //Callback for configuring RS-485 Transreceiver correctly
node.postTransmission(postTransmission);
}
//-------------------------------------------------------

void loop()
{

read_timeNow = millis();
int readTimeCount = read_timeNow - read_timeOld;

if (abs(readTimeCount) >= readTIME_interval) // Data is collected every 5sec
{
Serial.println("Entered Motor reading Function...");

// Get the State of the Motor
MotorReading();

}

}

//-------------------------------------------------------

Expected Result:

Real values from the Motor (Greater as 0)

Actual Result:

RESPONSE
Entered Motor reading Function...
␁␃��␀␁s]
Motor Frequency = 0.00
␁␃2d␀␁�mMotor RevolutionSpeed = 0.00
␁␃2e␀␁��Motor Current = 0
␁␃��␀␁�␒Motor Voltage = 0
␁␃��␀␁�:Motor Power = 0
␁␃�@␀␁��Motor CosPHI = 0
␁␃�@␀␁��Motor Moment = 0
␁␃��␀␁��Operating time = 0
Entered reading display Function...