CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum
Deutsche Version English version russian version 
It is currently Sun Nov 19, 2017 12:07 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Tue Nov 22, 2016 10:21 am 
Offline

Joined: Thu Nov 17, 2016 8:21 am
Posts: 9
Hello all,

I have an array of x bytes. I want to process this data somewhere else in my program so I want to use pointers. I want to be able to increment the pointer address in an easy way. I know have the following code:
______________________________________________________________________
receivedData : ARRAY [0..MAX_RECEIVE_TCP_SERVER] OF BYTE;
______________________________________________________________________
processCommand( ADR(receivedData[6]) );
______________________________________________________________________
FUNCTION processCommand : BOOL
VAR_INPUT
dataPtr : POINTER TO BYTE;
END_VAR
VAR
channel: BYTE;
value: UINT;
state: BYTE;
END_VAR
--------------------------------------------------------------------------------------------------
channel := dataPtr^;
dataPtr := dataPtr + 1;
value := dataPtr^;
dataPtr := dataPtr + 1;
value := value + (dataPtr^ * 256);
dataPtr := dataPtr + 1;
state := dataPtr^;

dummyFunction(channel, value, state);
______________________________________________________________________

Is there an easier way to increment the pointer address? I would prefer to have something like this, but I don't know if this is possible:
dummyFunction(dataPtr[0]^, dataPtr[1]^ + dataPtr[2]^ *256 , dataPtr[3]^);


Top
   
PostPosted: Tue Nov 22, 2016 12:44 pm 
Offline
Frequent User
Frequent User

Joined: Fri Sep 02, 2011 8:02 pm
Posts: 257
Instead of defining a pointer to byte, define your pointer as:
Code:
dataPtr := POINTER TO ARRAY [0..MAX_RECEIVE_TCP_SERVER] OF BYTE;


Then you can access your data with array indexing like you wish.

_________________
Scott Cunningham
KEB America, Inc.
www.kebblog.com
www.kebamerica.com


Top
   
PostPosted: Thu Nov 24, 2016 8:08 am 
Offline

Joined: Thu Nov 17, 2016 8:21 am
Posts: 9
Thanks for your reply. Unfortunately, this is not possible (error 4110, '[<index>]' needs array variable).

Other solutions, or is my implementation wrong: dummyFunction(dataPtr[0]^, dataPtr[1]^ + dataPtr[2]^ *256 , dataPtr[3]^);


Top
   
PostPosted: Sat Nov 26, 2016 8:06 pm 
Offline
Frequent User
Frequent User

Joined: Fri Sep 02, 2011 8:02 pm
Posts: 257
I am not by my computer, but I believe you need to dereference before index: dataPtr^[1].

_________________
Scott Cunningham
KEB America, Inc.
www.kebblog.com
www.kebamerica.com


Top
   
PostPosted: Wed Nov 30, 2016 3:40 pm 
Offline

Joined: Thu Nov 17, 2016 8:21 am
Posts: 9
scott_cunningham wrote:
I am not by my computer, but I believe you need to dereference before index: dataPtr^[1].


Works! Unbelievable I didn't try that... Thanks very much!


Top
   
PostPosted: Wed Nov 30, 2016 7:45 pm 
Offline
Frequent User
Frequent User

Joined: Fri Sep 02, 2011 8:02 pm
Posts: 257
If you get tired of dereferencing your pointer, you can use REFERENCE TO instead of POINTER TO. Check the help for more info.

_________________
Scott Cunningham
KEB America, Inc.
www.kebblog.com
www.kebamerica.com


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 6 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited