04-15-2012 08:53 PM - edited 04-15-2012 08:54 PM
Hello im having an issue with a multimeter being used to measure the power of a laser beam. I found some sample code online that included a prebuilt function for taking a single sample reading from the device. The function sotres the power reading as a ViReal64 data type and prints it to the screen. I am trying to store that data as a float however so that i may write it to an external txt file for subsequent analysis.
Here is the function block that I found in the sample code.
/*---------------------------------------------------------------------------
Measure Power
---------------------------------------------------------------------------*/
ViStatus get_power(ViSession ihdl)
{
ViStatus err = VI_SUCCESS;
ViReal64 power;
ViInt16 power_unit;
char *unit;
err = PM100D_getPowerUnit(ihdl, &power_unit);
switch(power_unit)
{
case PM100D_POWER_UNIT_DBM: unit = "dBm"; break;
default: unit = "W"; break;
}
err = PM100D_measPower(ihdl, &power);
if(!err) printf("Power reading: %15.9f %s\n\n", power, unit);
return (err);
}
When this function is run, the the power value in Watts prints to the screen perfectl. I need to somehow take that value and store it into an array for later use. I need to modify this code to suit my needs. I want it to return the variable named power and store that returned value into a anoth variable within my int main loop. The end goal being, having the get_power function run inside a loop during the operation of my device storing all values into a file.
So i made a couple of changes to the sample code.
First I changed the return line in the function block to read this
The original sample code called the function inside of a switch statement, within the int main loop. here is the statement that concerns me.
case 'p':
if((err = get_power(instrHdl))) error_exit(instrHdl, err);
break;
In an attempt to change this code to suit my needs i wrote this inside of my int main loop
float p;
p=get_power(instrHdl);
No matter what data type i set 'p' as, be it float, double, etc the value is alwasy stored as 0.000000
I know this is not the case though because the function prints the correct value to the screen when i call it.
I assume there must be an intermediary step in order to convert from the ViReal64 data type to a float. Has anyone had this issue before? Am i missing something in my code?
Any advice or help with this matter would be much appreciated, this device needs to be up and running within the next week... I never thought this would be an issue since the function printed the data to the screen so nicely.
thank you for your time
PS: I have enclosed the complete sample code as an attachment
04-16-2012 07:25 AM
#PROBLEM SOLVED
turns out it was an issue with my function definition.
instead of ViStatus get_power
i changed it to
float get_power
and the output now matches perfectly.
Just thought i would post this in case any one else ends up having a similar issue