emelianov / modbus-esp8266

Most complete Modbus library for Arduino. A library that allows your Arduino board to communicate via Modbus protocol, acting as a master, slave or both. Supports network transport (Modbus TCP) and Serial line/RS-485 (Modbus RTU). Supports Modbus TCP Security for ESP8266/ESP32.
Other
519 stars 186 forks source link

Reading Holding Registers Succeeds but keeps returning 0 values #283

Closed AlHasanSameh closed 11 months ago

AlHasanSameh commented 1 year ago

Hi, thanks for your effort on this library I am trying to take readings from this smart meter here ( datasheet attached ) ES32LS en.pdf

I am using NodeMCU and this RS485 converter MAX485 TTL To RS485 Converter Module

This smart meter gives me readings using NodeRed successfully, I tried reading its holding registers using this library without luck, what happens is the read requests succeeds as it returns 0x00, but all the values read in my ESP are zeroes, despite the fact a load is attached to the smart meter and it's displaying readings on its screen

Code's about to be attached, I need help solving this problem and knowing how to read holding registers properly, my feedback for the library is : it's great, but ZERO documentation on how things should be done, examples are useless, I had to figure out that a function called readHreg exists on my own.

Here's the code : `

include

include

/ PIN Definitions /

define RX_PIN 4

define TX_PIN 5

define DE_RE_PIN 13

/ Modbus Settings /

define MODBUS_BAUD 9600

define MODBUS_CONFIG SWSERIAL_8N1 //8-data bits , No Parity , 1-stop bit

define SLAVE_ID 6

/ Smart Meter Definitions /

define MODBUS_ADDRESS_REG 1

define BAUDRATE_REG 2

define PARITY_REG 3

define INDEX_H_REG 4

define INDEX_L_REG 5

define VOLTAGE_REG 6

define CURRENT_REG 7

define FREQUENCY_REG 8

define POWER_FACTOR_REG 9

define ACTIVE_POWER_REG 10

define REACTIVE_POWER_REG 11

define APPARENT_POWER_REG 12

SoftwareSerial rs_485( RX_PIN , TX_PIN ); ModbusRTU Smart_Meter;

void setup() { Serial.begin( 9600 ); delay( 100 ); Serial.printf( "\nSmart Energy Meter Started\n" );

rs_485.begin( MODBUS_BAUD , MODBUS_CONFIG ); delay( 100 ); Smart_Meter.begin( &rs_485 , DE_RE_PIN ); Smart_Meter.setBaudrate( MODBUS_BAUD ); Smart_Meter.master(); }

void loop() { if(!Smart_Meter.slave()) { uint8_t registerCount = 9; uint16_t Registers[ registerCount ] = { 0 };

Read_Smart_Meter_Registers( Registers , INDEX_H_REG , registerCount );

Serial.printf( "\nSmart Meter Readings : \n" );

Serial.print( "Power High Register = " );
Serial.println( (unsigned int)Registers[ 0 ] );
Serial.print( "Power Low  Register = " );
Serial.println( (unsigned int)Registers[ 1 ] );
Serial.print( "Voltage             = "  );
Serial.println( (unsigned int)Registers[ 2 ] );
Serial.print( "Current             = "  );
Serial.println( (unsigned int)Registers[ 3 ] );
Serial.print( "Frequency           = "  );
Serial.println( (unsigned int)Registers[ 4 ] );
Serial.print( "Power Factor        = "  );
Serial.println( (unsigned int)Registers[ 5 ] );
Serial.print( "Active Power        = "  );
Serial.println( (unsigned int)Registers[ 6 ] );
Serial.print( "Reactive Power      = "  );
Serial.println( (unsigned int)Registers[ 7 ] );
Serial.print( "Apparent Power      = "  );
Serial.println( (unsigned int)Registers[ 8 ] );

Serial.println();

}

Smart_Meter.task(); yield();

delay( 2000 ); }

/ Read Input Registers From Smart Meter / void Read_Smart_Meter_Registers( uint16_t* buffers_arr , uint8_t Start_Address , uint8_t registerCount ) { Smart_Meter.readHreg( SLAVE_ID , Start_Address , buffers_arr , registerCount , cbWrite ); }

/ Modbus Callback / bool cbWrite( Modbus::ResultCode event , uint16_t transactionId , void* data ) { Serial.printf( "Request result: 0x%02X, Mem: %d\n" , event , ESP.getFreeHeap() ); return true; } `

jpily commented 1 year ago

It is wrong to use the function