Structured Text - Saving memory address of a variable as string

Join Date
Sep 2019
Location
Here
Posts
10
Hi,


For strictly development purposes I would like to save the memory address of a variable as a string (e.g. "16#FFFFFFFF"). Now, specifically it is the address of an interface that I want to get the address of, and I have tried the following:


Code:
MemoryString := ADR(THIS^.StatusInterface);
Code:
MemoryString := DWORD_TO_STRING(ADR(THIS^.StatusInterface));

But neither lets me compile, due to the error "cannot convert type 'POINTER TO I_StatusInterface' to type 'STRING' / 'DWORD''

Using TwinCAT, if that matters.
 
Last edited:
I think the problem is with the DWORD_TO conversion as the data type for a pointer is technically a dword but isn't explicitly defined as one. Try the TO conversion for instances where the input data type isn't explicitly given. I've never tried this but it might do the trick.

Cheers



Operator ‘TO_<xxx>’¶

TO_<data type>

The function depends on what is described for each typed conversion.


Examples

ST implementation language:


VAR
iVar : INT;
bVar : BOOL;
sVar : STRING;
rVar : REAL;
END_VAR

iVar := TO_INT(4.22); (* Result: 4 *)
bVar := TO_BOOL(1); (* Result: TRUE *)
sVar := TO_STRING(342); (* Result: '342' *)
rVar := TO_WORD('123'); (* Result: 123 *)
 
Use an intermediate dword to get over the type conversions:


Code:
dwAddress:=ADR(pTable);
myString:=DWORD_TO_STRING(dwAddress);
 
Use an intermediate dword to get over the type conversions:


Code:
dwAddress:=ADR(pTable);
myString:=DWORD_TO_STRING(dwAddress);
This does not work in TwinCAT, and fails at the line "dwAddress:=ADR(pTable)". VisualStudio reports upon attempting to build:


Code:
Error Cannot convert type 'POINTER TO I_Status' to type 'DWORD'



Kvogel's solution seems to work though, with the following code:



Code:
dwAddress := TO_DWORD(ADR(StatusInterface));
sAddress := DWORD_TO_STRING(dwAddress);


Thank you both.
 
The target system is crucial for pointers etc, I used TwinCat3 for an x86 target for the previously posted code which compiled ok - is your target x64?, if so the intermediate variable needs to LWORD and the string conversion LWORD_TO_STRING


Code:
lwAddress:=ADR(pTable);
myString:=LWORD_TO_STRING(lwAddress);
 
The target system is crucial for pointers etc, I used TwinCat3 for an x86 target for the previously posted code which compiled ok - is your target x64?, if so the intermediate variable needs to LWORD and the string conversion LWORD_TO_STRING


Code:
lwAddress:=ADR(pTable);
myString:=LWORD_TO_STRING(lwAddress);
You are completely right, I should have included the fact that the target system was a 64 bit architecture in the first post. Your code compiles and works without problem, and LWORD is of course the correct datatype in my case.



... Kind of scary that it ran fine with the DWORD (32 bit) casting though, there would be a lot of information loss in such a conversion.
 

Similar Topics

I have an expression in a structured text routine of a Logix controller that looks more or less like the following: ResultInteger := Integer1 *...
Replies
13
Views
359
Good evening. I display the step number of a SFC on a display. Sometimes, on a trip, it goes quickly through many steps and I need to prove to...
Replies
1
Views
105
I am trying to set up a piece of equipment with a Horner HE-X4R. I'd like to use structured text and so far I'm just trying to get a basic On/off...
Replies
0
Views
59
Good morning. I'm doing a rehab and I need to recycle some part of the old code that won't change and that I need. This is a calculation that...
Replies
22
Views
1,332
I'm writing some structured text that's handling a data structure that comes from a PC. The PC structure is in the "new" LREAL 64-bit floating...
Replies
3
Views
478
Back
Top Bottom