Can't read inputRegister : Modbus RTU rs485

Hello;
I want to read electrical data from Kepler KP-835 Power meter through RS485 and use protocol Modbus RTU
I use TTL to RS485: https://core-electronics.com.au/ttl-uart-to-rs485-converter-module.html
Slave id is 8; none priority bit; stop bit 1
From registers list of this power meter Its Address is 30001 length 4 bytes
but it can't read data I want . How should I do
This is code i am using btw this code is work fine when i read data from Schneider power meter(read from holding register).
///////////////////////////////////////////
#include <ModbusMaster.h>
#include<SoftwareSerial.h>
#define TxEnable 2
// instantiate ModbusMaster object
ModbusMaster node;
SoftwareSerial mySerial(6,7); //Pin6 is RX,Pin7 is TX
void preTransmission()
{
digitalWrite(TxEnable, 1);
}

void postTransmission()
{
digitalWrite(TxEnable, 0);

}
float HexTofloat(uint32_t x){
return ((float)&x);
}
float ReadModbus(uint8_t SlaveID,uint16_t address,uint16_t qty){
uint16_t result;
uint16_t data[2];
int32_t value = 0;
float i =0;
uint8_t Test;
Test = node.available();
node.begin(SlaveID, mySerial);
result = node.readInputRegisters(address, qty);
Serial.println(result);
delay(50);
0
for (uint8_t j = 0; j < 2; j++)
{
data[j] = node.getResponseBuffer(j);

}
value = data[0]; 
value = value <<16;
value = value + data[1];
i = HexTofloat(value);    
return i;

}
else{
Serial.println("Modbus failed");
delay(500);
return 0;
}
}
void setup()
{
// use Serial (port 0); initialize Modbus communication baud rate
Serial.begin(19200);
mySerial.begin(9600);
pinMode(TxEnable,OUTPUT);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
void loop()
{

float modbusData = ReadModbus(8,1,2); //SlaveID; Address register; data size
Serial.print("Meter Value: ");
Serial.println(modbusData);
delay(500);

}
///////////////////////////////////////////////////

First: the posted code isn't complete, it doesn't compile.

Check the result code you get and post the value you get!

As you failed to post a link to that device's manual the following is wild guessing: Is the register address 30001 correct? Have you tried to use address 1? As many PLCs distinguish the register type only by the register address they use ranges for the types.

It's work!!!! Thank you for your help Appreciate. Library have to use 16bit address to indicate register

#include <ModbusMaster.h>
#include<SoftwareSerial.h>
#define TxEnable 2
// instantiate ModbusMaster object
ModbusMaster node;
SoftwareSerial mySerial(6,7); //Pin6 is RX,Pin7 is TX
void preTransmission()
{
  digitalWrite(TxEnable, 1);
}

void postTransmission()
{
  digitalWrite(TxEnable, 0);
  
}
float HexTofloat(uint32_t x){
  return (*(float*)&x);
  }
float ReadModbus(uint8_t SlaveID,uint16_t address,uint16_t qty){
  uint16_t result;
  uint16_t data[2];
  int32_t value = 0;
  float i =0;
  uint8_t  Test;
  Test = node.available();
  node.begin(SlaveID, mySerial);
  result = node.readInputRegisters(address, qty);
  //Serial.println(result);
  delay(50);
 if (result == node.ku8MBSuccess){
    for (uint8_t j = 0; j < 2; j++)
    {
      data[j] = node.getResponseBuffer(j);
   
    }
    value = data[0]; 
    value = value <<16;
    value = value + data[1];
    i = HexTofloat(value);    
    return i;
  } 
  else{
    Serial.println("Modbus failed");
    delay(500);
    return 0;
    }
}
void setup()
{
  // use Serial (port 0); initialize Modbus communication baud rate
  Serial.begin(19200);
  mySerial.begin(9600);
  pinMode(TxEnable,OUTPUT); 
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}
void loop()
{
    
    float modbusData = ReadModbus(8,0x08,2); //SlaveID; Address register; data size
    Serial.print("Meter Value1: ");
    Serial.println(modbusData);
    delay(500);
    float modbusData2 = ReadModbus(1,0x08,2); //SlaveID; Address register; data size
    Serial.print("Meter Value2: ");
    Serial.println(modbusData2);
    delay(500);
}

This is my code i use. I hope this forum will be useful for someone who have trouble about modbus RTU . Thank you Pylon

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.