nayakajit87
Member level 5
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.
method1:
type casting tried
method2:
method 3: it almost matched the reading of modscan32 but not accurate enough to match software reading
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
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.
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