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.

Getting variable names during runtime


MortenS Jun 30, 2011 01:34 PM

Hi

Is it posible on a cr1000 and CRBasic to retrieve a list of varibles and there names during runtime ?

Regards

Morten Skov


Nico Jul 4, 2011 09:21 PM

If you got CRbasic you should have Loggernet, no?
This has got a live-view of the 'public' variables and their values at run-time.

Besides that you can always download the program from the logger and read the variable names in the .cr1 program?

I probably don't get the question..


MortenS Jul 5, 2011 08:57 AM

Hi Nico

You are right. You didn't got the question. But I don't blame you for it :-)

What I need is a way to get a varibles name into a string and not the contents of the varible. It should work a bit like CalFile, but that will not write to file in ascii. Only binary which is unreadable to those who is maintaining our test facilities.


Imagine we got a command called VarNameToString !

Public VarName(10) as String * 35
Public BottleNo

'Main Program
BeginProg
Scan (5,Sec,1,0)
VarName(1) = VarNameToString(BottleNo)
NextScan

EndProg

Regards

Morten Skov


mjb Jul 6, 2011 09:41 PM

I don't believe CRBasic has this functionality, but I imagine it would be possible to write a function to do so, with clever use of constants and FileRead.

I am imagining a call to your function as so:

VarName(1) = VarNameToString(BottleNo, "BOTTLENOVAR")

and the function itself would be some like this, in pseudo-code:

Function VarNameToString(var, marker) As String
  Open Program filename on CPU: (Status.ProgName)
  Use FileRead or similar to read the contents
  Search for the term <marker> passed as arg 2
  Process that line, getting the bit between "(" and ","
  Return the result
EndFunction


So, obviously, your program needs to have a unique "marker" for every call to the function you make.

There may be a better and/or easier way to do this, but if not I imagine this would work ok.

* Last updated by: mjb on 7/6/2011 @ 3:45 PM *


MortenS Jul 7, 2011 08:27 AM

Hi mjb

That is excactly how I solved it. But now I would like to go the other way around.
Parsing contents from the file to the varible.

Using only 1 or 2 varibles it's easier to just do it manually, but in my case I got 20 varibles.

A solution could be using an array to pass to, but the types are mixed strings, long and floats. Then I need 3 arrays and that is not easy to overlook.

I think I leave it here and think it over again

thanks anyway :-)

morten


aps Jul 7, 2011 09:50 AM

Sorry but I do not see what you are saving in terms of typing or code space if you had this function.

In your example:

VarName(1) = VarNameToString(BottleNo)

You might have well as typed:

Varname(1)="BottleNo"

IF the variable is an array, all the names will be the same so you can just assign the names to the string away in a loop in the same way.

Am I missing something here?


ryan42 Dec 18, 2014 06:04 AM

Hi guys,

I am looking to do the same thing here and was wondering if there was an easier way than parsing the file. I think aps you might have miss understood.

What we are actually looking for is a function or something to return the name of a variable. So if I have...

Public data(3)
Alias data(1) = batteryVolts
Alias data(2) = rainfall
Alias data(3) = temperature

Public dataString as String * 100
dim i

for i = 1 to 3
dataString += "The variable name is "+varName(data(i))+" and the value is "+data(i)
next i

Thus the output of dataString would be

The variable name is batteryVolts and the value is 13.1
The variable name is rainfall and the value is 0.6
The variable name is temperature and the value is 17.3

Does that make sense? I'm actually using this with HTTPPost to send data to a web server. The name is how it stores the data and I don't want to have to write it all out as it varies and is ~20 variables per site...


Carli Apr 27, 2017 01:05 PM

Hey,

this is a very old thread with the problem still not solved.

Is there anything to achieve a varName like behavior ?

Would be very practical to have it...


ryan42 Apr 28, 2017 08:20 PM

Hi Carli,

This is now possible. See the 'Pointer Variable' section of the CRBasic help docs. 

From memory it's as follows. 

Public x, varName As String, ptr As Long, value

x=3
varName="x"
ptr=@(varName)
value=!ptr 'This should be the value of x

Or as a short function

Function getValByStr(name As String) As Float
  Dim ptr As Long
  ptr=@(name)
  Return !ptr
EndFunction

'Then just use...
val=getValByStr("nameOfVarible")

I haven't tested this but I'm pretty sure that's how I'm using it. 

Cheers, Ryan


Carli May 8, 2017 08:34 AM

Thank you for your answer Ryan! 
Learned again about a new aspect of CR programming.

But I was looking for the other way aroung:

Not to get a value based on the value name but to get the Alias name of an array.

Dim Values(4)
Dim i
Dim Output As String
Alias Values(1) = Temperatur
Alias Values(2) = Speed
Alias Values(3) = Humidity
Alias Values(4) = WindDirection

Output=""

For i=1 To 4
   Output &= "The Value for " & GetNameByVal(Values(i)) & " is " & Values(i) & ";"
Next i

So unfortunately this doesn't solve this problem.


ryan42 Jun 15, 2017 02:38 AM

Hi again,

If you still looking for a solution try searching in the help docs for 'Pointer Variable'

Try using 

ptr=@value
name=(!)ptr

so it would be 

Dim ptr As Long
For i=1 To 4
   ptr = @Values(i)
   Output &= "The Value for " & (!)ptr & " is " & Values(i) & ";"
Next i

Again i haven't tested this but believe this is how it operates.

Cheers
Ryan


Carli Jun 15, 2017 06:52 AM

Hi Ryan,

I already thought of this solution.

(see also: https://www.campbellsci.com/forum?forum=1&l=thread&tid=3357)

It seems aliases and arrays are pointing to different parts of the memory.

So a pointer to an array name will return the array name, no matter if an alias is specified. Where as an pointer to an alias will return the alias name.

Cheers,

Carli*

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