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.

Pointer Variables @X


ryan42 Apr 22, 2015 06:32 AM

Hi,

I was wondering if anyone has been able to get the pointer vairbales to return the name of the variable at that memory location.

Currently I'm trying

Public battV
Public name As String
Public ptr As Long

BeginProg
Scan (1,Sec,0,0)
Battery (battV)
ptr = @batt_volt
name = (@)ptr
NextScan
EndProg

I'm reading this from the section of the help that says:
Additionally, the name of the variable stored in the memory location can be returned using (@); e.g.,
Name=(@)X

Any help would be great


Sam Apr 23, 2015 06:22 PM

Ryan42,

I offer my apologies. It appears that I didn't proof read our help.

The notation you are looking for is actually

VarNameAsString = !(Pointer)

@ is the get-address-of operator
! is the deference operator

So your program should be


Public battV
Public name As String
Public ptr As Long
BeginProg
Scan (1,Sec,0,0)
Battery (battV)
ptr = @battv
name = !(ptr)
NextScan
EndProg


Related syntax also includes,

'Get-address-of variable in memory
Pointer = @Variable

'Get-address-of variable in memory, referencing variable by name specified as a string
Pointer = @("VariableNameAsString")
,where VariableNameAsString is specified by a string variable, such as
Public Str as String * 32
Str = "VariableName"
Pointer = @(Str)

'Assign value to variable referenced by pointer
!Pointer = value

'Assign value of variable referenced by pointer to another variable
AnotherVariable = !Pointer

'Get the name of the variable as a string
VariableNameAsString = !(Pointer)


'Using pointer to variable as an argument for Sub or Function parameter

Public MyCounter

Sub AddOne(Pointer as Long)
!Pointer = !Pointer + 1
EndSub

BeginProg
Scan(1,Sec,3,0)
Call AddOne(@MyCounter)
NextScan
EndProg

* Last updated by: Sam on 4/26/2015 @ 11:27 PM *


ryan42 Apr 27, 2015 12:43 AM

Hey Sam,

Thanks so much for the reply. I have been able to get most of these to work but I'm having trouble getting a variable pointer from variable name as string?

'Get-address-of variable in memory, referencing variable by name specified as a string
ptr = @("batteryVolts")

I get a compile error saying:
Invalid symbol in expression: ptr = @("batteryVolts")
Am I doing something wrong here?

Cheers
Ryan


ryan42 Apr 27, 2015 01:23 AM

Well I got it to work, I had to store the string name into a variable first before trying to get the value, this makes sense anyway as it will be how I'll use it

Dim stringName As String = "batteryVolts"
public batteryVolts
Dim ptr
Public Value

Value = @(stringName)


Sam Apr 27, 2015 05:24 AM

Yeah. Seems like if someone was going to hard code

Value=@("VariableNameAsStringLiteral")

they might as well have coded

Value=@VariableName


Sam Apr 27, 2015 05:32 AM

Following is a program I wrote as a demonstration for a customer who wanted to query variable data from the command line.

Public PTemp, batt_volt

DataTable (Test,1,1000)
DataInterval (0,15,Sec,10)
Minimum (1,batt_volt,FP2,0,False)
Sample (1,PTemp,FP2)
EndTable

BeginProg
Scan (1,Sec,0,0)
PanelTemp (PTemp,250)
Battery (batt_volt)
CallTable Test
NextScan


'======================================================
'======================================================
'======================================================
'Instructions:
'Copy and Paste the following block of code into your program just
'before EndProg.
'Edit the SerialOpen and Call ServiceCmdLine parameters to
'reflect the serial port setup desired
'Do not edit the ServiceCmdLine sub routine unless for the explicit
'purpose of changing its behavior / capabilities
'
SlowSequence
'open port for serial, commands and PakBus
SerialOpen (ComRS232,115200,0,0,50)
Do
Delay (1,10,mSec)
Call ServiceCmdLine(ComRS232)
Loop
EndSequence

Sub ServiceCmdLine(ComPort As Long)
Dim SerIn As String * 16, Bytes As Long
Dim SerInSplit(3) As String * 16
Alias SerInSplit(1) = cmd,name,arg1
Dim VarPtr As Long, TableRec As String * 1000

'pull in data if present, expecting command to start with "+"
'and end with carriage return
SerialInRecord (ComPort,SerIn,43,0,13,Bytes,01)
If Bytes > 0 Then
'split up CSV input on "=" and comma
SplitStr (SerInSplit,SerIn,"=,",3,7)

'check input for command
Select Case UpperCase(cmd)
Case "T"
'get table name : +T=TEST,RecNum
If UpperCase(name) <> "PUBLIC" AND UpperCase(name) <> "DATATABLEINFO" Then
TableRec = ""
GetRecord (TableRec,name,arg1) 'get record from table
SerialOut (ComPort,CHR(13) & CHR(10) & TableRec,"",0,1)
EndIf

Case "V"
'get variable name : +V=BATT_VOLT
VarPtr = @(name) 'get pointer to variable
SerialOut (ComPort,CHR(13) & CHR(10) & !VarPtr & CHR(13) & CHR(10),"",0,100)
EndSelect

EndIf

EndSub

'======================================================
'======================================================
'======================================================


EndProg

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