I have to read and store data in a series of unsigned short variables in Shared Memory.
The data is scaled and stored, but entered and displayed in engineering units.
Now these variables are scaled and I need to convert them into their proper units. I display them as either float or integers.
For example for one variable:-
Max Val = 0.999969
Min Val = -1.000000
Least Sig Bit = 0.000030517578 = scaleFactor1.
So what I do is:-
// Get current data item for Shared Memory
unsigned short currentField = *(unsigned short *) fieldPtr;
float scaleFactor1 = (float)GetScalingFactor(scaleFactorStr);
float rescaledDataEng = currentField * scaleFactor1;
This works OK (rescaledDataEng ) for positive numbers, but for negative numbers it fails, e.g. I get for -0.44 the incorrect value displayed as (rescaledDataEng = 1.560028).
Also I dont think that I store the data (negative value corectly) when writing to the Shared Memory:-
*(unsigned short *) fieldPtr = (unsigned short)(fEngValue/m_fScalingFactor);
For example for another variable:-
Max Val = 179.999
Min Val = -180.000
Least Sig Bit = 0.005493 = m_fScalingFactor.
I enter -20 and 159.01 is displayed, but if I enter 20, then 20.000 is displayed OK.
Any suggestions please as the more I look at the problem the more confused I get.
In summary I take the entered value and scale it (divide by m_fScalingFactor) and store it.
Then I get the value and re-scale for display (multiple by m_fScalingFactor). All works fine apart from negative numbers.
The data is scaled and stored, but entered and displayed in engineering units.
Now these variables are scaled and I need to convert them into their proper units. I display them as either float or integers.
For example for one variable:-
Max Val = 0.999969
Min Val = -1.000000
Least Sig Bit = 0.000030517578 = scaleFactor1.
So what I do is:-
// Get current data item for Shared Memory
unsigned short currentField = *(unsigned short *) fieldPtr;
float scaleFactor1 = (float)GetScalingFactor(scaleFactorStr);
float rescaledDataEng = currentField * scaleFactor1;
This works OK (rescaledDataEng ) for positive numbers, but for negative numbers it fails, e.g. I get for -0.44 the incorrect value displayed as (rescaledDataEng = 1.560028).
Also I dont think that I store the data (negative value corectly) when writing to the Shared Memory:-
*(unsigned short *) fieldPtr = (unsigned short)(fEngValue/m_fScalingFactor);
For example for another variable:-
Max Val = 179.999
Min Val = -180.000
Least Sig Bit = 0.005493 = m_fScalingFactor.
I enter -20 and 159.01 is displayed, but if I enter 20, then 20.000 is displayed OK.
Any suggestions please as the more I look at the problem the more confused I get.
In summary I take the entered value and scale it (divide by m_fScalingFactor) and store it.
Then I get the value and re-scale for display (multiple by m_fScalingFactor). All works fine apart from negative numbers.