Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[General] Type casting operation integer to 32 bit floating point

Status
Not open for further replies.

nayakajit87

Member level 5
Member level 5
Joined
Aug 13, 2018
Messages
84
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
1,065
I am using arduino Nodemcu and Modbus device to communicate. I have connected the nodemcu tx and rx line to Modbus device TX and rx line . Here i am sending modbus request in hex and could able to read data .

I have attached image for reference. i expected to display the value as per modbus display. The value is is displaying matching with unsigned decimal in modscan32. How could i convert to 32bit float to display actual reading. Serial_out_Ard.jpgModbusout-Actual out.jpg

method1:
Code:
#include <ModbusMaster232.h>
#include <SoftwareSerial.h>
float  data[100];
ModbusMaster232 node(1);

// Define one address for reading
#define address 1
// Define the number of bits to read
#define bitQty 70

void setup()
{
  Serial.begin(9600);
  // Initialize Modbus communication baud rate
  node.begin(9600);


}

void loop()
{

  int result =  node.readHoldingRegisters(address, bitQty);
  data[0] = (float) node.getResponseBuffer(0);
  data[1] = (float)node.getResponseBuffer(1);
  data[2] = (float)node.getResponseBuffer(2);
  data[3] = (float) node.getResponseBuffer(3);
  data[4] = (float) node.getResponseBuffer(4);
  data[5] = (float)node.getResponseBuffer(5);
  for (int i = 0; i < 100; i++)
  {
    //data[i] = node.getResponseBuffer(i);
    Serial.println(data[i]);
  }
  Serial.println("............");



}

type casting tried
method2:
Code:
void loop()
{

  int result =  node.readHoldingRegisters(address, bitQty);
  data[0] =float( node.getResponseBuffer(0));
 // long val =(data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
//  Serial.print("val:");
//  Serial.println(val);
  data[1] =float(node.getResponseBuffer(1));
  data[2] = float(node.getResponseBuffer(2));
  data[3] = float( node.getResponseBuffer(3));
  data[4] =float( node.getResponseBuffer(4));
  data[5] = float(node.getResponseBuffer(5));
}

method 3: it almost matched the reading of modscan32 but not accurate enough to match software reading
Code:
void loop()
{

  int result =  node.readHoldingRegisters(address, bitQty);
  data[0] =float( node.getResponseBuffer(0));
 long val =(data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
 Serial.print("val:");
 Serial.println(val);
}


Only one thing is controller is 8 bit controller and modbus device uses 32 bit precision. I m suspecting the result may not match because of it.
kindly requested to give some suggestion to display the data
 

The MODBUS protocol specification doesn't have a definition how to represent larger data entities than 16 bit. 32 bit integer and float data are however used by custom implementations, but there's no generally agreed format you can rely on. You first need to find out which float representation is used by the MODBUS server you are communicating with.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top