Hi, I'm trying to write a method for transferring a floating point number into 2 holding registers. I convert the float into a long (while scaling up by 400K) then I used This thread to convert the long into two ints which go into holding registers.
float Aval = 3.5634;
long y = lround(Aval*scale);
int16_t high = y >> 16;
int16_t low = (int16_t)y;
holdingRegs[Aa] = low;
holdingRegs[Ab] = high;
It works great - but I need to do it for many different values in my code, and I can't get the general method below to work - could someone please point out what I'm missing? Many thanks,
void floatconv (float input, int16_t Aa, int16_t Ab){
long lconv = lround(input*scale);
Aa = lconv >> 16;
Ab = (int16_t)lconv;
}
float Bval = 3.5634;
floatconv(Bval, holdingRegs[Ba], holdingRegs[Bb]);
The fuller version of the above code is here:
#include <ModbusRtu.h>
#define ID 1
#define TXEN 2
long scale = 400000000;
enum
{
Aa, //CHANNEL 0
Ab, //CHANNEL 1
Ba, //CHANNEL 2
Bb, //CHANNEL 3
HOLDING_REGS_SIZE
};
unsigned int holdingRegs[HOLDING_REGS_SIZE];
//*/
//Modbus slave(1, 0, TXEN); // this is slave @1 and RS-485
void setup() {
//////////////////////////////////////////////////////////////////////////////////////////////
// start modbus communication
//slave.begin( 115200 );
Serial.begin(115200);
}
float Aval = 3.5634;
float Bval = 3.5634;
void floatconv (float input, int16_t hAa, int16_t hAb){
long lconv = lround(input*scale);
hAa = lconv >> 16;
hAb = (int16_t)lconv;
}
void loop() {
long y = lround(Aval*scale);
int16_t high = y >> 16;
int16_t low = (int16_t)y;
holdingRegs[Aa] = low;
holdingRegs[Ab] = high;
floatconv(Bval, holdingRegs[Ba], holdingRegs[Bb]);
//modbus communication
//slave.poll( holdingRegs, HOLDING_REGS_SIZE );
Serial.print("Aa: "), Serial.println(holdingRegs[Aa]);
Serial.print("Ab: "), Serial.println(holdingRegs[Ab]);
Serial.print("Ba: "), Serial.println(holdingRegs[Ba]);
Serial.print("Bb: "), Serial.println(holdingRegs[Bb]);
Serial.println("");
delay(50000);
}