Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Converting values from scientific notation (in a string) to float


dontunderstand Aug 24, 2023 11:16 AM

Hello,

My end goal is to store some calibration coefficients into the non-volatile memory of a CR300 data logger using the CalFile function. I'm receiving the values from the serial port in a NMEA-like format, for example:

$UPL,1E4,-2E2,3E7,-2E-8,1E1,2E4,5E7,1.6E-6,-2.3E9*0B

where UPL is the command directive and 0B is the checksum.

I can then pick the individual values with SplitStr into an array of Strings, which results into this: 

CaliabrationValues(1) = "1E4"
CaliabrationValues(2) = "-2E3"
CaliabrationValues(3) = "3E7"
CaliabrationValues(4) = "-2E8"
CaliabrationValues(5) = "1E1"
CaliabrationValues(6) = "2E4"
CaliabrationValues(7) = "5E7"
CaliabrationValues(8) = "1.6E-6"
CaliabrationValues(9) = "-2.3E9"

Ok so far. However, CalFile requires the values to be of type float, and this is where I'm having issues. The compiler seems to be quite good at casting the values correctly if I just assign each individual value, unless the power value (after the E) is negative. So if I do this: 

For Index = 1 to 9 Step 1
  CalibrationValuesFloat(Index) = CalibrationValues(Index)
Next Index

 CalibrationValuesFloat ends up like this: 

CaliabrationValuesFloat(1) = 10,000.00
CaliabrationValuesFloat(2) = -200.00
CaliabrationValuesFloat(3) = 3E07
CaliabrationValuesFloat(4) = 0.00
CaliabrationValuesFloat(5) = 10.00
CaliabrationValuesFloat(6) = 20,000.00
CaliabrationValuesFloat(7) = 5E07
CaliabrationValuesFloat(8) = 0.00
CaliabrationValuesFloat(9) = -2.3E09

As you can see, the values with a negative power are ignored. I haven't yet found a way to resolve this. What could be a good approach?

Kind regards and thanks in advance


JDavis Aug 24, 2023 03:43 PM

 This code is working correctly on my CR1000X. It would be helpful to know which data logger model and OS version you are seeing issue with.

Public FloatResult(9) As Float
Dim i As Long
Dim CaliabrationValues(9) As String

'Main Program
BeginProg
  CaliabrationValues(1) = "1E4"
  CaliabrationValues(2) = "-2E3"
  CaliabrationValues(3) = "3E7"
  CaliabrationValues(4) = "-2E8"
  CaliabrationValues(5) = "1E1"
  CaliabrationValues(6) = "2E4"
  CaliabrationValues(7) = "5E7"
  CaliabrationValues(8) = "1.6E-6"
  CaliabrationValues(9) = "-2.3E9"

  Scan (1,Sec,0,0)
    For i = 1 To 9
      FloatResult(i) = CaliabrationValues(i)
    Next
  NextScan
EndProg

 


dontunderstand Aug 25, 2023 08:45 AM

Hi JDavis,

Thank you for your reply. I copied your exact code and I'm still having the same issue. It results into this: 

FloatResult(1) = 10,000.00
FloatResult(2) = -2,000.00
FloatResult(3) = 3E07
FloatResult(4) = -2E08
FloatResult(5) = 10.00
FloatResult(6) = 20,000.00
FloatResult(7) = 5E07
FloatResult(8) = 0.00
FloatResult(9) = -2.3E09

I'm using a CR300 data logger running OS Version 10.07 (latest).

Thank you


JDavis Aug 25, 2023 06:31 PM

I just ran the code on a CR300 with OS 10.07. Looking at values with the Table Monitor in LoggerNet and the Table Monitor in DevConfig I see 1.6E-06. The value is correctly converted in the data logger.

Whatever screen you are using to view the data must be converting that value from scientific notation, and not showing enough decimal points.


dontunderstand Aug 29, 2023 07:22 AM

Sincere apologies, I hadn't selected a resolution big enough to display values with more than 2 decimal points. Thank you for your help.

Log in or register to post/reply in the forum.